2
0

main.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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, FMX.Memo.Types;
  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. fGlobalID: TGUID;
  49. fId : TID;
  50. fGType : TGroupType;
  51. published
  52. property GlobalID: TGUID read fGlobalID write fGlobalID;
  53. property Id : TID read fId write fId;
  54. property GType : TGroupType read fGType write fGType;
  55. end;
  56. TOptions = class
  57. private
  58. fOption1 : Integer;
  59. fOption2 : string;
  60. fAllowGroups : TGroupType;
  61. published
  62. property Option1 : Integer read fOption1 write fOption1;
  63. property Option2 : string read fOption2 write fOption2;
  64. property AllowGroups : TGroupType read fAllowGroups write fAllowGroups;
  65. end;
  66. TConnectionInfo = record
  67. IP : string;
  68. ConnectionDate : TDateTime;
  69. end;
  70. TConnectionArray = array of TConnectionInfo;
  71. TGroupList = TObjectList<TGroup>;
  72. TWorkingTime = class
  73. private
  74. fName : string;
  75. fWorkDays : TDays;
  76. fFreeDays : TDays;
  77. published
  78. property Name : string read fName write fName;
  79. property WorkDays : TDays read fWorkDays write fWorkDays;
  80. property FreeDays : TDays read fFreeDays write fFreeDays;
  81. end;
  82. TLevelPrivilege = array of TID;
  83. TUser = class(TJsonRecord)
  84. private
  85. fId : TID;
  86. fName : string;
  87. fSurname : string;
  88. fAge : Integer;
  89. fAddress : string;
  90. fPath : string;
  91. fOptions : TOptions;
  92. fLastConnections : TConnectionArray;
  93. fMarried : Boolean;
  94. fWorkingTime : TWorkingTime;
  95. fGenre : TGenre;
  96. fDepartment : TDepartment;
  97. fBalance : Double;
  98. fHireDate : TDateTime;
  99. fLevelPrivilege : TLevelPrivilege;
  100. fObservations : string;
  101. fStatus : TUserStatus;
  102. fGroups : TGroupList;
  103. public
  104. constructor Create;
  105. destructor Destroy; override;
  106. published
  107. [TCommentProperty('Is user Id')]
  108. property Id : TID read fId write fId;
  109. property Name : string read fName write fName;
  110. property Surname : string read fSurname write fSurname;
  111. property Age : Integer read fAge write fAge;
  112. [TCommentProperty('gnFemale or gnMale')]
  113. property Genre : TGenre read fGenre write fGenre;
  114. property Department : TDepartment read fDepartment write fDepartment;
  115. property Address : string read fAddress write fAddress;
  116. property Path : string read fPath write fPath;
  117. property Balance : Double read fBalance write fBalance;
  118. [TCustomNameProperty('IsMarried')]
  119. property Married : Boolean read fMarried write fMarried;
  120. property WorkingTime : TWorkingTime read fWorkingTime write fWorkingTime;
  121. property HireDate : TDateTime read fHireDate write fHireDate;
  122. [TCommentProperty('Possible values = usAtOffice, usAtHome or usOnVacation')]
  123. property Status : TUserStatus read fStatus write fStatus;
  124. property LastConnections : TConnectionArray read fLastConnections write fLastConnections;
  125. property Observations : string read fObservations write fObservations;
  126. property LevelPrivilege : TLevelPrivilege read fLevelPrivilege write fLevelPrivilege;
  127. property Options : TOptions read fOptions write fOptions;
  128. property Groups : TGroupList read fGroups write fGroups;
  129. end;
  130. TUserList = TObjectList<TUser>;
  131. TForm1 = class(TForm)
  132. Memo1: TMemo;
  133. btnToJson: TButton;
  134. btnFromJson: TButton;
  135. procedure FormCreate(Sender: TObject);
  136. procedure btnToJsonClick(Sender: TObject);
  137. procedure btnFromJsonClick(Sender: TObject);
  138. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  139. private
  140. { Private declarations }
  141. public
  142. { Public declarations }
  143. end;
  144. var
  145. Form1: TForm1;
  146. Serializer : TJsonSerializer;
  147. User : TUser;
  148. User2 : TUser;
  149. UserList : TUserList;
  150. implementation
  151. {$R *.fmx}
  152. procedure TForm1.btnFromJsonClick(Sender: TObject);
  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. guid: TGUID;
  182. begin
  183. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  184. user := TUser.Create;
  185. user.Id := 77;
  186. user.Name := 'Joe';
  187. user.Surname := 'Smith Valdés';
  188. user.Age := 30;
  189. user.Married := True;
  190. user.Address := 'Sunset st. 2 \b';
  191. User.Path := 'C:\documents\files';
  192. user.Options.Option1 := 1;
  193. user.Options.Option2 := 'good';
  194. user.Options.AllowGroups := gtExternal;
  195. user.Balance := 99.9;
  196. user.HireDate := Now();
  197. user.LevelPrivilege := [1,2,3,4];
  198. user.WorkingTime.Name:= 'WeekConfig';
  199. user.WorkingTime.WorkDays := DEF_WORKDAYS;
  200. user.WorkingTime.FreeDays := DEF_WEEKEND;
  201. user.Observations := 'Good aptitude';
  202. department.Id := 10;
  203. department.Name := 'IT';
  204. user.Department := department;
  205. user.Status := TUserStatus.usOnVacation;
  206. lastcon.IP := '127.0.0.1';
  207. lastcon.ConnectionDate := Now();
  208. User.LastConnections := [lastcon];
  209. lastcon.IP := '192.0.0.1';
  210. lastcon.ConnectionDate := Now();
  211. User.LastConnections := User.LastConnections + [lastcon];
  212. group := TGroup.Create;
  213. group.Id := 1;
  214. group.GType := gtInternal;
  215. CreateGUID(guid);
  216. group.GlobalID:=guid;
  217. user.Groups.Add(group);
  218. group := TGroup.Create;
  219. group.Id := 2;
  220. group.GType := gtExternal;
  221. CreateGUID(guid);
  222. group.GlobalID:=guid;
  223. user.Groups.Add(group);
  224. end;
  225. { TUser }
  226. constructor TUser.Create;
  227. begin
  228. fOptions := TOptions.Create;
  229. fWorkingTime := TWorkingTime.Create;
  230. fGroups := TGroupList.Create(True);
  231. end;
  232. destructor TUser.Destroy;
  233. begin
  234. fOptions.Free;
  235. fWorkingTime.Free;
  236. fGroups.Free;
  237. inherited;
  238. end;
  239. end.