frmMain.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. unit frmMain;
  2. interface
  3. uses
  4. System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  5. FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  6. FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.StdCtrls, Quick.Json.Serializer,
  7. System.Generics.Collections;
  8. type
  9. TID = Int64;
  10. TGenre = (gnMale, gnFemale);
  11. TGroupType = (gtInternal, gtExternal);
  12. TDayOfWeek = (wdSunday, wdMonday, wdThuesday, wdWednesday, wdThursday, wdFriday, wdSaturday);
  13. TUserStatus = (usAtOffice, usAtHome, usOnVacation);
  14. TDays = set of TDayOfWeek;
  15. const
  16. DEF_WORKDAYS : TDays = [wdMonday, wdThuesday, wdWednesday, wdThursday, wdFriday];
  17. DEF_WEEKEND : TDays = [wdSaturday, wdSunday];
  18. type
  19. TDepartment = record
  20. Id : TID;
  21. Name : string;
  22. end;
  23. TContactIdArray = array of TID;
  24. TGroup = class
  25. private
  26. fId : TID;
  27. fGType : TGroupType;
  28. public
  29. property Id : TID read fId write fId;
  30. property GType : TGroupType read fGType write fGType;
  31. end;
  32. TOptions = class
  33. private
  34. fOption1 : Integer;
  35. fOption2 : string;
  36. fAllowGroups : TGroupType;
  37. public
  38. property Option1 : Integer read fOption1 write fOption1;
  39. property Option2 : string read fOption2 write fOption2;
  40. property AllowGroups : TGroupType read fAllowGroups write fAllowGroups;
  41. end;
  42. TConnectionInfo = record
  43. IP : string;
  44. ConnectionDate : TDateTime;
  45. Contacts : TContactIdArray;
  46. end;
  47. TConnectionArray = array of TConnectionInfo;
  48. TGroupList = TObjectList<TGroup>;
  49. //TGroupList = class(TObjectList<TGroup>);
  50. TWorkingTime = class
  51. private
  52. fWorkDays : TDays;
  53. fFreeDays : TDays;
  54. public
  55. property WorkDays : TDays read fWorkDays write fWorkDays;
  56. property FreeDays : TDays read fFreeDays write fFreeDays;
  57. end;
  58. TLevelPrivilege = array of TID;
  59. TUser = class
  60. private
  61. fId : TID;
  62. fName : string;
  63. fSurname : string;
  64. fAge : Integer;
  65. fAddress : string;
  66. fOptions : TOptions;
  67. fLastConnections : TConnectionArray;
  68. fMarried : Boolean;
  69. fWorkingTime : TWorkingTime;
  70. [TCommentProperty('gnFemale or gnMale')]
  71. fGenre : TGenre;
  72. fHireDate : TDateTime;
  73. fLevelPrivilege : TLevelPrivilege;
  74. fObservations : string;
  75. fStatus : TUserStatus;
  76. fGroups : TGroupList;
  77. public
  78. constructor Create;
  79. destructor Destroy; override;
  80. [TCommentProperty('Is user Id')]
  81. property Id : TID read fId write fId;
  82. property Name : string read fName write fName;
  83. property Surname : string read fSurname write fSurname;
  84. property Age : Integer read fAge write fAge;
  85. property Address : string read fAddress write fAddress;
  86. [TCustomNameProperty('IsMarried')]
  87. property Married : Boolean read fMarried write fMarried;
  88. property WorkingTime : TWorkingTime read fWorkingTime write fWorkingTime;
  89. property HireDate : TDateTime read fHireDate write fHireDate;
  90. [TCommentProperty('Possible values = usAtOffice, usAtHome or usOnVacation')]
  91. property Status : TUserStatus read fStatus write fStatus;
  92. property LastConnections : TConnectionArray read fLastConnections write fLastConnections;
  93. property Observations : string read fObservations write fObservations;
  94. property LevelPrivilege : TLevelPrivilege read fLevelPrivilege write fLevelPrivilege;
  95. property Options : TOptions read fOptions write fOptions;
  96. property Groups : TGroupList read fGroups write fGroups;
  97. end;
  98. TUserList = TObjectList<TUser>;
  99. TForm1 = class(TForm)
  100. Memo1: TMemo;
  101. btnToJson: TButton;
  102. btnFromJson: TButton;
  103. procedure FormCreate(Sender: TObject);
  104. procedure btnToJsonClick(Sender: TObject);
  105. procedure btnFromJsonClick(Sender: TObject);
  106. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  107. private
  108. { Private declarations }
  109. public
  110. { Public declarations }
  111. end;
  112. var
  113. Form1: TForm1;
  114. serializer : TJsonSerializer;
  115. User : TUser;
  116. UserList : TUserList;
  117. implementation
  118. {$R *.fmx}
  119. procedure TForm1.btnFromJsonClick(Sender: TObject);
  120. var
  121. newuser : TUser;
  122. begin
  123. newuser := TUser.Create;
  124. try
  125. newuser := serializer.JsonToObject(newuser,Memo1.Text) as TUser;
  126. //newuser.Groups := serializer.JsonToObject(newuser.Groups,Memo1.Text) as TGroupList;
  127. Memo1.Lines.Add('NewUser:');
  128. Memo1.Lines.Add(serializer.ObjectToJson(newuser));
  129. Memo1.Lines.Add('TGroup OwnsObjects: ' + BoolToStr(newuser.Groups.OwnsObjects,True));
  130. Memo1.Lines.Add('TGroup Capacity: ' + newuser.Groups.Capacity.ToString);
  131. Memo1.Lines.Add('TGroup Count: ' + newuser.Groups.Count.ToString);
  132. Memo1.Lines.Add(newuser.Groups[0].Id.ToString);
  133. //Memo1.Lines.Add(serializer.ObjectToJson(newuser.Groups));
  134. finally
  135. newuser.Free;
  136. end;
  137. end;
  138. procedure TForm1.btnToJsonClick(Sender: TObject);
  139. begin
  140. Memo1.Text := serializer.ObjectToJson(User);
  141. btnFromJson.Enabled := True;
  142. end;
  143. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  144. begin
  145. User.Free;
  146. serializer.Free;
  147. end;
  148. procedure TForm1.FormCreate(Sender: TObject);
  149. var
  150. lastcon : TConnectionInfo;
  151. group : TGroup;
  152. begin
  153. serializer := TJsonSerializer.Create(slPublicProperty);
  154. user := TUser.Create;
  155. user.Id := 77;
  156. user.Name := 'Joe';
  157. user.Surname := 'Smith';
  158. user.Age := 30;
  159. user.Married := True;
  160. user.Address := 'Sunset st. 2';
  161. user.Options.Option1 := 1;
  162. user.Options.Option2 := 'good';
  163. user.Options.AllowGroups := gtExternal;
  164. user.HireDate := Now();
  165. user.LevelPrivilege := [1,2,3,4];
  166. user.WorkingTime.WorkDays := DEF_WORKDAYS;
  167. user.WorkingTime.FreeDays := DEF_WEEKEND;
  168. user.Observations := 'Good aptitude';
  169. user.Status := TUserStatus.usOnVacation;
  170. lastcon.IP := '127.0.0.1';
  171. lastcon.ConnectionDate := Now();
  172. lastcon.Contacts := [1,2,3,4,5];
  173. User.LastConnections := [lastcon];
  174. lastcon.IP := '192.0.0.1';
  175. lastcon.ConnectionDate := Now();
  176. User.LastConnections := User.LastConnections + [lastcon];
  177. group := TGroup.Create;
  178. group.Id := 1;
  179. group.GType := gtInternal;
  180. user.Groups.Add(group);
  181. group := TGroup.Create;
  182. group.Id := 2;
  183. group.GType := gtExternal;
  184. user.Groups.Add(group);
  185. end;
  186. { TUser }
  187. constructor TUser.Create;
  188. begin
  189. fOptions := TOptions.Create;
  190. fWorkingTime := TWorkingTime.Create;
  191. fGroups := TGroupList.Create(True);
  192. end;
  193. destructor TUser.Destroy;
  194. begin
  195. fOptions.Free;
  196. fWorkingTime.Free;
  197. fGroups.Free;
  198. inherited;
  199. end;
  200. end.