Changeset 89 for trunk/UAcronym.pas
- Timestamp:
- Aug 9, 2016, 10:53:36 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UAcronym.pas
r88 r89 6 6 7 7 uses 8 Classes, SysUtils, Contnrs, XML Conf, XMLRead, XMLWrite, DOM, UXMLUtils,8 Classes, SysUtils, Contnrs, XMLRead, XMLWrite, DOM, UXMLUtils, 9 9 SpecializedList, fphttpclient, Dialogs, odbcconn, sqldb; 10 10 … … 15 15 TImportSources = class; 16 16 TImportFormats = class; 17 18 TAcronymSource = class19 Name: string;20 URL: string;21 end;22 23 TAcronymSources = class(TObjectList)24 25 end;26 17 27 18 { TAcronym } … … 56 47 Acronym: TAcronym; 57 48 Categories: TAcronymCategories; 58 Source: TAcronymSource; 49 Sources: TImportSources; 50 procedure MergeCategories(MergedCategories: TAcronymCategories); 59 51 procedure SaveToNode(Node: TDOMNode); 60 52 procedure LoadFromNode(Node: TDOMNode); … … 111 103 Description: string; 112 104 Categories: TStringList; 105 Sources: TStringList; 113 106 constructor Create; 114 107 destructor Destroy; override; … … 174 167 procedure TextParse(S: string); 175 168 public 169 Id: Integer; 176 170 Enabled: Boolean; 177 171 Name: string; … … 200 194 TImportSources = class(TObjectList) 201 195 AcronymDb: TAcronymDb; 196 procedure UpdateIds; 197 function SearchById(Id: Integer): TImportSource; 198 procedure SaveRefToNode(Node: TDOMNode); 199 procedure LoadRefFromNode(Node: TDOMNode); 200 procedure AssignToStrings(Strings: TStrings); 202 201 function SearchByName(Name: string): TImportSource; 203 202 procedure SaveToNode(Node: TDOMNode); … … 210 209 TAcronymDb = class 211 210 FileName: string; 212 Sources: TAcronymSources;213 211 Acronyms: TAcronyms; 214 212 Categories: TAcronymCategories; … … 298 296 SQLQuery1: TSQLQuery; 299 297 NewAcronym: TAcronymEntry; 300 J: Integer;301 298 AddedAcronym: TAcronymMeaning; 302 299 begin … … 329 326 if (NewAcronym.Name <> '') and (NewAcronym.Meaning <> '') then begin 330 327 AddedAcronym := Sources.AcronymDb.AddAcronym(NewAcronym.Name, NewAcronym.Meaning); 331 332 // Merge categories 333 for J := 0 to Categories.Count - 1 do 334 if AddedAcronym.Categories.IndexOf(Categories[J]) = -1 then begin 335 AddedAcronym.Categories.Add(Categories[J]); 336 TAcronymCategory(Categories[J]).AcronymMeanings.Add(AddedAcronym); 337 end; 328 AddedAcronym.MergeCategories(Categories); 329 if AddedAcronym.Sources.IndexOf(Self) = -1 then 330 AddedAcronym.Sources.Add(Self); 338 331 end; 339 332 SQLQuery1.Next; … … 435 428 Q: Integer; 436 429 I: Integer; 437 J: Integer;438 430 T: string; 439 431 LastLength: Integer; … … 508 500 AddedAcronym := Sources.AcronymDb.AddAcronym(NewAcronym.Name, NewAcronym.Meaning); 509 501 AddedAcronym.Description := NewAcronym.Description; 510 511 // Merge categories 512 for J := 0 to Categories.Count - 1 do 513 if AddedAcronym.Categories.IndexOf(Categories[J]) = -1 then begin 514 AddedAcronym.Categories.Add(Categories[J]); 515 TAcronymCategory(Categories[J]).AcronymMeanings.Add(AddedAcronym); 516 end; 502 AddedAcronym.MergeCategories(Categories); 503 if AddedAcronym.Sources.IndexOf(Self) = -1 then 504 AddedAcronym.Sources.Add(Self); 505 517 506 Inc(ItemCount); 518 507 end; … … 643 632 { TImportSources } 644 633 634 procedure TImportSources.UpdateIds; 635 var 636 LastId: Integer; 637 I: Integer; 638 begin 639 // Get highest used ID 640 LastId := 0; 641 for I := 0 to Count - 1 do begin 642 if TImportSource(Items[I]).Id > LastId then LastId := TImportSource(Items[I]).Id; 643 end; 644 // Add ID to new items without ID 645 for I := 0 to Count - 1 do begin 646 if TImportSource(Items[I]).Id = 0 then begin 647 Inc(LastId); 648 TImportSource(Items[I]).Id := LastId; 649 end; 650 end; 651 end; 652 653 function TImportSources.SearchById(Id: Integer): TImportSource; 654 var 655 I: Integer; 656 begin 657 I := 0; 658 while (I < Count) and (TImportSource(Items[I]).Id <> Id) do Inc(I); 659 if I < Count then Result := TImportSource(Items[I]) 660 else Result := nil; 661 end; 662 663 procedure TImportSources.SaveRefToNode(Node: TDOMNode); 664 var 665 I: Integer; 666 NewNode: TDOMNode; 667 begin 668 for I := 0 to Count - 1 do begin 669 NewNode := Node.OwnerDocument.CreateElement('Ref'); 670 Node.AppendChild(NewNode); 671 NewNode.TextContent := IntToStr(TImportSource(Items[I]).Id); 672 end; 673 end; 674 675 procedure TImportSources.LoadRefFromNode(Node: TDOMNode); 676 var 677 Node2: TDOMNode; 678 Id: Integer; 679 Source: TImportSource; 680 begin 681 Node2 := Node.FirstChild; 682 while Assigned(Node2) and (Node2.NodeName = 'Ref') do begin 683 if TryStrToInt(Node2.TextContent, Id) then begin 684 Source := AcronymDb.ImportSources.SearchById(Id); 685 if Assigned(Source) then begin 686 Add(Source); 687 end; 688 end; 689 Node2 := Node2.NextSibling; 690 end; 691 end; 692 693 procedure TImportSources.AssignToStrings(Strings: TStrings); 694 var 695 I: Integer; 696 begin 697 Strings.Clear; 698 for I := 0 to Count - 1 do 699 Strings.AddObject(TImportSource(Items[I]).Name, Items[I]); 700 end; 701 645 702 function TImportSources.SearchByName(Name: string): TImportSource; 646 703 var … … 658 715 NewNode2: TDOMNode; 659 716 begin 717 UpdateIds; 660 718 for I := 0 to Count - 1 do 661 719 with TImportSource(Items[I]) do begin … … 680 738 Node2 := Node2.NextSibling; 681 739 end; 740 UpdateIds; 682 741 end; 683 742 … … 854 913 begin 855 914 Categories := TStringList.Create; 915 Sources := TStringList.Create; 856 916 Name := ''; 857 917 Meaning := ''; … … 862 922 begin 863 923 FreeAndNil(Categories); 924 FreeAndNil(Sources); 864 925 inherited Destroy; 865 926 end; … … 936 997 { TAcronymMeaning } 937 998 999 procedure TAcronymMeaning.MergeCategories(MergedCategories: TAcronymCategories); 1000 var 1001 I: Integer; 1002 begin 1003 for I := 0 to MergedCategories.Count - 1 do 1004 if Categories.IndexOf(MergedCategories[I]) = -1 then begin 1005 Categories.Add(MergedCategories[I]); 1006 TAcronymCategory(MergedCategories[I]).AcronymMeanings.Add(Self); 1007 end; 1008 end; 1009 938 1010 procedure TAcronymMeaning.SaveToNode(Node: TDOMNode); 939 1011 var … … 947 1019 Node.AppendChild(NewNode); 948 1020 Categories.SaveRefToNode(NewNode); 1021 1022 NewNode := Node.OwnerDocument.CreateElement('Sources'); 1023 Node.AppendChild(NewNode); 1024 Sources.SaveRefToNode(NewNode); 949 1025 end; 950 1026 … … 967 1043 TAcronymCategory(Categories[I]).AcronymMeanings.Add(Self); 968 1044 end; 1045 1046 Sources.AcronymDb := Acronym.Db; 1047 Node2 := Node.FindNode('Sources'); 1048 if Assigned(Node2) then begin 1049 Sources.LoadRefFromNode(Node2); 1050 end; 969 1051 end; 970 1052 … … 972 1054 begin 973 1055 Categories := TAcronymCategories.Create(False); 1056 Sources := TImportSources.Create(False); 974 1057 end; 975 1058 … … 981 1064 TAcronymCategory(Categories[I]).AcronymMeanings.Remove(Self); 982 1065 FreeAndNil(Categories); 1066 FreeAndNil(Sources); 983 1067 inherited Destroy; 984 1068 end; … … 1089 1173 begin 1090 1174 for I := 0 to Count - 1 do begin 1091 NewNode := Node.OwnerDocument.CreateElement(' Category');1175 NewNode := Node.OwnerDocument.CreateElement('Ref'); 1092 1176 Node.AppendChild(NewNode); 1093 WriteInteger(NewNode, 'Id',TAcronymCategory(Items[I]).Id);1177 NewNode.TextContent := IntToStr(TAcronymCategory(Items[I]).Id); 1094 1178 end; 1095 1179 end; … … 1101 1185 Category: TAcronymCategory; 1102 1186 begin 1187 Node2 := Node.FirstChild; 1188 while Assigned(Node2) and (Node2.NodeName = 'Ref') do begin 1189 if TryStrToInt(Node2.TextContent, Id) then begin 1190 Category := Db.Categories.SearchById(Id); 1191 if Assigned(Category) then begin 1192 Add(Category); 1193 end; 1194 end; 1195 Node2 := Node2.NextSibling; 1196 end; 1197 1198 // Old way to store ref Id, remove in future 1103 1199 Node2 := Node.FirstChild; 1104 1200 while Assigned(Node2) and (Node2.NodeName = 'Category') do begin … … 1238 1334 constructor TAcronymDb.Create; 1239 1335 begin 1240 Sources := TAcronymSources.Create;1241 1336 Acronyms := TAcronyms.Create; 1242 1337 Acronyms.Db := Self; … … 1251 1346 FreeAndNil(ImportFormats); 1252 1347 FreeAndNil(ImportSources); 1253 FreeAndNil(Sources);1254 1348 FreeAndNil(Acronyms); 1255 1349 FreeAndNil(Categories);
Note:
See TracChangeset
for help on using the changeset viewer.