main.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. unit main;
  2. interface
  3. uses
  4. System.SysUtils,
  5. System.Types,
  6. System.UITypes,
  7. System.Classes,
  8. System.Variants,
  9. System.Generics.Collections,
  10. FMX.Types,
  11. FMX.Controls,
  12. FMX.Forms,
  13. FMX.Graphics,
  14. FMX.Dialogs,
  15. FMX.Controls.Presentation,
  16. FMX.ScrollBox,
  17. FMX.Memo,
  18. FMX.StdCtrls,
  19. Quick.JsonRecord,
  20. Quick.Base64,
  21. Quick.Json.Serializer;
  22. type
  23. TID = Int64;
  24. TContactType = (ctInternal, ctExternal);
  25. TMessageState = (msPending, msSent, msNotSent);
  26. TRecipientArray = array of TID;
  27. TRecipient = record
  28. ID : TID;
  29. RType : TContactType;
  30. Confirm : TMessageState;
  31. end;
  32. TGenre = (gnMale, gnFemale);
  33. TGroupType = (gtInternal, gtExternal);
  34. TDayOfWeek = (wdSunday, wdMonday, wdThuesday, wdWednesday, wdThursday, wdFriday, wdSaturday);
  35. TUserStatus = (usAtOffice, usAtHome, usOnVacation);
  36. TDays = set of TDayOfWeek;
  37. const
  38. DEF_WORKDAYS : TDays = [wdMonday, wdThuesday, wdWednesday, wdThursday, wdFriday];
  39. DEF_WEEKEND : TDays = [wdSaturday, wdSunday];
  40. type
  41. TDepartment = record
  42. Id : TID;
  43. Name : string;
  44. end;
  45. TContactIdArray = array of TID;
  46. TGroup = class
  47. private
  48. fId : TID;
  49. fGType : TGroupType;
  50. published
  51. property Id : TID read fId write fId;
  52. property GType : TGroupType read fGType write fGType;
  53. end;
  54. TOptions = class
  55. private
  56. fOption1 : Integer;
  57. fOption2 : string;
  58. fAllowGroups : TGroupType;
  59. published
  60. property Option1 : Integer read fOption1 write fOption1;
  61. property Option2 : string read fOption2 write fOption2;
  62. property AllowGroups : TGroupType read fAllowGroups write fAllowGroups;
  63. end;
  64. TConnectionInfo = record
  65. IP : string;
  66. ConnectionDate : TDateTime;
  67. end;
  68. TConnectionArray = array of TConnectionInfo;
  69. TGroupList = TObjectList<TGroup>;
  70. TWorkingTime = class
  71. private
  72. fName : string;
  73. fWorkDays : TDays;
  74. fFreeDays : TDays;
  75. published
  76. property Name : string read fName write fName;
  77. property WorkDays : TDays read fWorkDays write fWorkDays;
  78. property FreeDays : TDays read fFreeDays write fFreeDays;
  79. end;
  80. TLevelPrivilege = array of TID;
  81. TUser = class(TJsonRecord)
  82. private
  83. fId : TID;
  84. fName : string;
  85. fSurname : string;
  86. fAge : Integer;
  87. fAddress : string;
  88. fPath : string;
  89. fOptions : TOptions;
  90. fLastConnections : TConnectionArray;
  91. fMarried : Boolean;
  92. fWorkingTime : TWorkingTime;
  93. fGenre : TGenre;
  94. fDepartment : TDepartment;
  95. fBalance : Double;
  96. fHireDate : TDateTime;
  97. fLevelPrivilege : TLevelPrivilege;
  98. fObservations : string;
  99. fStatus : TUserStatus;
  100. fGroups : TGroupList;
  101. public
  102. constructor Create;
  103. destructor Destroy; override;
  104. published
  105. [TCommentProperty('Is user Id')]
  106. property Id : TID read fId write fId;
  107. property Name : string read fName write fName;
  108. property Surname : string read fSurname write fSurname;
  109. property Age : Integer read fAge write fAge;
  110. [TCommentProperty('gnFemale or gnMale')]
  111. property Genre : TGenre read fGenre write fGenre;
  112. property Department : TDepartment read fDepartment write fDepartment;
  113. property Address : string read fAddress write fAddress;
  114. property Path : string read fPath write fPath;
  115. property Balance : Double read fBalance write fBalance;
  116. [TCustomNameProperty('IsMarried')]
  117. property Married : Boolean read fMarried write fMarried;
  118. property WorkingTime : TWorkingTime read fWorkingTime write fWorkingTime;
  119. property HireDate : TDateTime read fHireDate write fHireDate;
  120. [TCommentProperty('Possible values = usAtOffice, usAtHome or usOnVacation')]
  121. property Status : TUserStatus read fStatus write fStatus;
  122. property LastConnections : TConnectionArray read fLastConnections write fLastConnections;
  123. property Observations : string read fObservations write fObservations;
  124. property LevelPrivilege : TLevelPrivilege read fLevelPrivilege write fLevelPrivilege;
  125. property Options : TOptions read fOptions write fOptions;
  126. property Groups : TGroupList read fGroups write fGroups;
  127. end;
  128. TUserList = TObjectList<TUser>;
  129. TForm1 = class(TForm)
  130. Memo1: TMemo;
  131. btnToJson: TButton;
  132. btnFromJson: TButton;
  133. procedure FormCreate(Sender: TObject);
  134. procedure btnToJsonClick(Sender: TObject);
  135. procedure btnFromJsonClick(Sender: TObject);
  136. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  137. private
  138. { Private declarations }
  139. public
  140. { Public declarations }
  141. end;
  142. var
  143. Form1: TForm1;
  144. Serializer : TJsonSerializer;
  145. User : TUser;
  146. User2 : TUser;
  147. UserList : TUserList;
  148. implementation
  149. {$R *.fmx}
  150. procedure TForm1.btnFromJsonClick(Sender: TObject);
  151. var
  152. s : string;
  153. begin
  154. if User2 <> nil then User2.Free;
  155. User2 := TUser.Create;
  156. User2.FromJson(Memo1.Text);
  157. //User2 := TUser.CreateFromJson(Memo1.Text);
  158. //User2.CreateFromJson(Memo1.Text);
  159. Memo1.Lines.Add('User2 as json:');
  160. Memo1.Lines.Add(User2.ToJson(True));
  161. Memo1.Lines.Add(Format('Groups.OwnedObjects=%s',[BoolToStr(User2.Groups.OwnsObjects,True)]));
  162. Memo1.Lines.Add(Format('Groups.Count=%d',[User2.Groups.Count]));
  163. Memo1.Lines.Add(Format('Groups.Capacity=%d',[User2.Groups.Capacity]));
  164. ShowMessage(Format('%s %s from %s',[User2.Name,User2.Surname,User2.Address]));
  165. end;
  166. procedure TForm1.btnToJsonClick(Sender: TObject);
  167. begin
  168. Memo1.Text := User.ToJson(True);
  169. end;
  170. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  171. begin
  172. if Assigned(User) then User.Free;
  173. if Assigned(User2) then User2.Free;
  174. Serializer.Free;
  175. end;
  176. procedure TForm1.FormCreate(Sender: TObject);
  177. var
  178. lastcon : TConnectionInfo;
  179. group : TGroup;
  180. department : TDepartment;
  181. begin
  182. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  183. user := TUser.Create;
  184. user.Id := 77;
  185. user.Name := 'Joe';
  186. user.Surname := 'Smith Valdés';
  187. user.Age := 30;
  188. user.Married := True;
  189. user.Address := 'Sunset st. 2 \b';
  190. User.Path := 'C:\documents\files';
  191. user.Options.Option1 := 1;
  192. user.Options.Option2 := 'good';
  193. user.Options.AllowGroups := gtExternal;
  194. user.Balance := 99.9;
  195. user.HireDate := Now();
  196. user.LevelPrivilege := [1,2,3,4];
  197. user.WorkingTime.Name:= 'WeekConfig';
  198. user.WorkingTime.WorkDays := DEF_WORKDAYS;
  199. user.WorkingTime.FreeDays := DEF_WEEKEND;
  200. user.Observations := 'Good aptitude';
  201. department.Id := 10;
  202. department.Name := 'IT';
  203. user.Department := department;
  204. user.Status := TUserStatus.usOnVacation;
  205. lastcon.IP := '127.0.0.1';
  206. lastcon.ConnectionDate := Now();
  207. User.LastConnections := [lastcon];
  208. lastcon.IP := '192.0.0.1';
  209. lastcon.ConnectionDate := Now();
  210. User.LastConnections := User.LastConnections + [lastcon];
  211. group := TGroup.Create;
  212. group.Id := 1;
  213. group.GType := gtInternal;
  214. user.Groups.Add(group);
  215. group := TGroup.Create;
  216. group.Id := 2;
  217. group.GType := gtExternal;
  218. user.Groups.Add(group);
  219. end;
  220. { TUser }
  221. constructor TUser.Create;
  222. begin
  223. fOptions := TOptions.Create;
  224. fWorkingTime := TWorkingTime.Create;
  225. fGroups := TGroupList.Create(True);
  226. end;
  227. destructor TUser.Destroy;
  228. begin
  229. fOptions.Free;
  230. fWorkingTime.Free;
  231. fGroups.Free;
  232. inherited;
  233. end;
  234. end.