main.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. fOptions : TOptions;
  89. fLastConnections : TConnectionArray;
  90. fMarried : Boolean;
  91. fWorkingTime : TWorkingTime;
  92. fGenre : TGenre;
  93. fDepartment : TDepartment;
  94. fBalance : Double;
  95. fHireDate : TDateTime;
  96. fLevelPrivilege : TLevelPrivilege;
  97. fObservations : string;
  98. fStatus : TUserStatus;
  99. fGroups : TGroupList;
  100. public
  101. constructor Create;
  102. destructor Destroy; override;
  103. published
  104. [TCommentProperty('Is user Id')]
  105. property Id : TID read fId write fId;
  106. property Name : string read fName write fName;
  107. property Surname : string read fSurname write fSurname;
  108. property Age : Integer read fAge write fAge;
  109. [TCommentProperty('gnFemale or gnMale')]
  110. property Genre : TGenre read fGenre write fGenre;
  111. property Department : TDepartment read fDepartment write fDepartment;
  112. property Address : string read fAddress write fAddress;
  113. property Balance : Double read fBalance write fBalance;
  114. [TCustomNameProperty('IsMarried')]
  115. property Married : Boolean read fMarried write fMarried;
  116. property WorkingTime : TWorkingTime read fWorkingTime write fWorkingTime;
  117. property HireDate : TDateTime read fHireDate write fHireDate;
  118. [TCommentProperty('Possible values = usAtOffice, usAtHome or usOnVacation')]
  119. property Status : TUserStatus read fStatus write fStatus;
  120. property LastConnections : TConnectionArray read fLastConnections write fLastConnections;
  121. property Observations : string read fObservations write fObservations;
  122. property LevelPrivilege : TLevelPrivilege read fLevelPrivilege write fLevelPrivilege;
  123. property Options : TOptions read fOptions write fOptions;
  124. property Groups : TGroupList read fGroups write fGroups;
  125. end;
  126. TUserList = TObjectList<TUser>;
  127. TForm1 = class(TForm)
  128. Memo1: TMemo;
  129. btnToJson: TButton;
  130. btnFromJson: TButton;
  131. procedure FormCreate(Sender: TObject);
  132. procedure btnToJsonClick(Sender: TObject);
  133. procedure btnFromJsonClick(Sender: TObject);
  134. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  135. private
  136. { Private declarations }
  137. public
  138. { Public declarations }
  139. end;
  140. var
  141. Form1: TForm1;
  142. Serializer : TJsonSerializer;
  143. User : TUser;
  144. User2 : TUser;
  145. UserList : TUserList;
  146. implementation
  147. {$R *.fmx}
  148. procedure TForm1.btnFromJsonClick(Sender: TObject);
  149. var
  150. s : string;
  151. begin
  152. if User2 <> nil then User2.Free;
  153. User2 := TUser.Create;
  154. User2.FromJson(Memo1.Text);
  155. //User2.CreateFromJson(Memo1.Text);
  156. Memo1.Lines.Add('User2 as json:');
  157. Memo1.Lines.Add(User2.ToJson);
  158. Memo1.Lines.Add(Format('Groups.OwnedObjects=%s',[BoolToStr(User2.Groups.OwnsObjects,True)]));
  159. Memo1.Lines.Add(Format('Groups.Count=%d',[User2.Groups.Count]));
  160. Memo1.Lines.Add(Format('Groups.Capacity=%d',[User2.Groups.Capacity]));
  161. ShowMessage(Format('%s %s from %s',[User2.Name,User2.Surname,User2.Address]));
  162. end;
  163. procedure TForm1.btnToJsonClick(Sender: TObject);
  164. begin
  165. Memo1.Text := User.ToJson;
  166. end;
  167. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  168. begin
  169. if Assigned(User) then User.Free;
  170. if Assigned(User2) then User2.Free;
  171. Serializer.Free;
  172. end;
  173. procedure TForm1.FormCreate(Sender: TObject);
  174. var
  175. lastcon : TConnectionInfo;
  176. group : TGroup;
  177. department : TDepartment;
  178. begin
  179. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  180. user := TUser.Create;
  181. user.Id := 77;
  182. user.Name := 'Joe';
  183. user.Surname := 'Smith Valdés';
  184. user.Age := 30;
  185. user.Married := True;
  186. user.Address := 'Sunset st. 2 \b';
  187. user.Options.Option1 := 1;
  188. user.Options.Option2 := 'good';
  189. user.Options.AllowGroups := gtExternal;
  190. user.Balance := 99.9;
  191. user.HireDate := Now();
  192. user.LevelPrivilege := [1,2,3,4];
  193. user.WorkingTime.Name:= 'WeekConfig';
  194. user.WorkingTime.WorkDays := DEF_WORKDAYS;
  195. user.WorkingTime.FreeDays := DEF_WEEKEND;
  196. user.Observations := 'Good aptitude';
  197. department.Id := 10;
  198. department.Name := 'IT';
  199. user.Department := department;
  200. //user.Status := TUserStatus.usOnVacation;
  201. //lastcon.IP := '127.0.0.1';
  202. //lastcon.ConnectionDate := Now();
  203. //User.LastConnections := [lastcon];
  204. //lastcon.IP := '192.0.0.1';
  205. //lastcon.ConnectionDate := Now();
  206. //User.LastConnections := User.LastConnections + [lastcon];
  207. group := TGroup.Create;
  208. group.Id := 1;
  209. group.GType := gtInternal;
  210. user.Groups.Add(group);
  211. group := TGroup.Create;
  212. group.Id := 2;
  213. group.GType := gtExternal;
  214. user.Groups.Add(group);
  215. end;
  216. { TUser }
  217. constructor TUser.Create;
  218. begin
  219. fOptions := TOptions.Create;
  220. fWorkingTime := TWorkingTime.Create;
  221. fGroups := TGroupList.Create(True);
  222. end;
  223. destructor TUser.Destroy;
  224. begin
  225. fOptions.Free;
  226. fWorkingTime.Free;
  227. fGroups.Free;
  228. inherited;
  229. end;
  230. end.