Changeset 33 for trunk/UAcronym.pas
- Timestamp:
- Jul 13, 2016, 2:51:53 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UAcronym.pas
r31 r33 50 50 51 51 TAcronymMeaning = class 52 Id: Integer; 52 53 Name: string; 53 54 Description: string; … … 65 66 66 67 TAcronymMeanings = class(TObjectList) 68 public 67 69 Acronym: TAcronym; 70 procedure UpdateIds; 68 71 procedure SaveToNode(Node: TDOMNode); 69 72 procedure LoadFromNode(Node: TDOMNode); … … 87 90 88 91 TAcronymCategories = class(TObjectList) 92 Db: TAcronymDb; 89 93 procedure UpdateIds; 90 94 procedure SaveToNode(Node: TDOMNode); 91 95 procedure LoadFromNode(Node: TDOMNode); 96 procedure SaveRefToNode(Node: TDOMNode); 97 procedure LoadRefFromNode(Node: TDOMNode); 92 98 function SearchByName(Name: string): TAcronymCategory; 93 99 function SearchById(Id: Integer): TAcronymCategory; … … 173 179 Sources: TImportSources; 174 180 ItemCount: Integer; 181 Categories: TAcronymCategories; 175 182 function DownloadHTTP(URL: string; Stream: TStream): Boolean; 176 183 procedure Process; … … 660 667 AddedAcronym := Sources.AcronymDb.AddAcronym(NewAcronym.Name, NewAcronym.Meaning); 661 668 AddedAcronym.Description := NewAcronym.Description; 669 AddedAcronym.Categories.Assign(Categories, laOr); 662 670 Inc(ItemCount); 663 671 end; … … 692 700 LastTime := Source.LastTime; 693 701 ItemCount := Source.ItemCount; 702 Categories.Assign(Source.Categories); 694 703 end; 695 704 696 705 procedure TImportSource.SaveToNode(Node: TDOMNode); 706 var 707 NewNode: TDOMNode; 697 708 begin 698 709 WriteString(Node, 'Name', Name); … … 701 712 else WriteInteger(Node, 'ImportFormat', -1); 702 713 WriteBoolean(Node, 'Enabled', Enabled); 714 WriteInteger(Node, 'ItemCount', ItemCount); 715 716 NewNode := Node.OwnerDocument.CreateElement('Categories'); 717 Node.AppendChild(NewNode); 718 Categories.SaveRefToNode(NewNode); 703 719 end; 704 720 705 721 procedure TImportSource.LoadFromNode(Node: TDOMNode); 722 var 723 Node2: TDOMNode; 706 724 begin 707 725 Name := ReadString(Node, 'Name', ''); … … 709 727 Format := Sources.AcronymDb.ImportFormats.SearchById(ReadInteger(Node, 'ImportFormat', -1)); 710 728 Enabled := ReadBoolean(Node, 'Enabled', True); 729 ItemCount := ReadInteger(Node, 'ItemCount', 0); 730 731 Categories.Db := Sources.AcronymDb; 732 Node2 := Node.FindNode('Categories'); 733 if Assigned(Node2) then 734 Categories.LoadRefFromNode(Node2); 711 735 end; 712 736 … … 715 739 Format := nil; 716 740 Enabled := True; 741 Categories := TAcronymCategories.Create; 742 Categories.OwnsObjects := False; 717 743 end; 718 744 719 745 destructor TImportSource.Destroy; 720 begin721 inherited Destroy;722 end;723 724 { TAcronymEntry }725 726 constructor TAcronymEntry.Create;727 begin728 Categories := TStringList.Create;729 end;730 731 destructor TAcronymEntry.Destroy;732 746 begin 733 747 FreeAndNil(Categories); … … 735 749 end; 736 750 751 { TAcronymEntry } 752 753 constructor TAcronymEntry.Create; 754 begin 755 Categories := TStringList.Create; 756 end; 757 758 destructor TAcronymEntry.Destroy; 759 begin 760 FreeAndNil(Categories); 761 inherited Destroy; 762 end; 763 737 764 { TAcronymMeanings } 738 765 766 procedure TAcronymMeanings.UpdateIds; 767 var 768 LastId: Integer; 769 I: Integer; 770 begin 771 // Get highest used ID 772 LastId := 0; 773 for I := 0 to Count - 1 do begin 774 if TAcronymCategory(Items[I]).Id > LastId then LastId := TAcronymCategory(Items[I]).Id; 775 end; 776 // Add ID to new items without ID 777 for I := 0 to Count - 1 do begin 778 if TAcronymCategory(Items[I]).Id = 0 then begin 779 Inc(LastId); 780 TAcronymCategory(Items[I]).Id := LastId; 781 end; 782 end; 783 end; 784 739 785 procedure TAcronymMeanings.SaveToNode(Node: TDOMNode); 740 786 var … … 742 788 NewNode2: TDOMNode; 743 789 begin 790 UpdateIds; 744 791 for I := 0 to Count - 1 do 745 792 with TAcronymMeaning(Items[I]) do begin … … 764 811 Node2 := Node2.NextSibling; 765 812 end; 813 UpdateIds; 766 814 end; 767 815 … … 788 836 var 789 837 NewNode: TDOMNode; 790 NewNode2: TDOMNode;791 I: Integer;792 838 begin 793 839 WriteString(Node, 'Name', Name); … … 797 843 NewNode := Node.OwnerDocument.CreateElement('Categories'); 798 844 Node.AppendChild(NewNode); 799 for I := 0 to Categories.Count - 1 do begin 800 NewNode2 := NewNode.OwnerDocument.CreateElement('Category'); 801 NewNode.AppendChild(NewNode2); 802 WriteInteger(NewNode2, 'Id', TAcronymCategory(Categories[I]).Id); 803 end; 845 Categories.SaveRefToNode(NewNode); 804 846 end; 805 847 … … 807 849 var 808 850 Node2: TDOMNode; 809 Node3: TDOMNode; 810 Category: TAcronymCategory; 811 Id: Integer; 851 I: Integer; 812 852 begin 813 853 Name := ReadString(Node, 'Name', ''); … … 815 855 Language := ReadString(Node, 'Language', ''); 816 856 817 Node3 := Node.FindNode('Categories'); 818 if Assigned(Node3) then begin 819 Node2 := Node3.FirstChild; 820 while Assigned(Node2) and (Node2.NodeName = 'Category') do begin 821 Id := ReadInteger(Node2, 'Id', 0); 822 Category := Acronym.Db.Categories.SearchById(Id); 823 if Assigned(Category) then begin 824 Categories.Add(Category); 825 Category.AcronymMeanings.Add(Self); 826 end; 827 Node2 := Node2.NextSibling; 828 end; 857 Categories.Db := Acronym.Db; 858 Node2 := Node.FindNode('Categories'); 859 if Assigned(Node2) then begin 860 Categories.LoadRefFromNode(Node2); 861 862 // Add reverse references 863 for I := 0 to Categories.Count - 1 do 864 TAcronymCategory(Categories[I]).AcronymMeanings.Add(Self); 829 865 end; 830 866 end; … … 839 875 I: Integer; 840 876 begin 877 if Assigned(Acronym) then 878 Acronym.Meanings.Remove(Self); 841 879 for I := 0 to Categories.Count - 1 do 842 880 TAcronymCategory(Categories[I]).AcronymMeanings.Remove(Self); … … 942 980 end; 943 981 UpdateIds; 982 end; 983 984 procedure TAcronymCategories.SaveRefToNode(Node: TDOMNode); 985 var 986 I: Integer; 987 NewNode: TDOMNode; 988 begin 989 for I := 0 to Count - 1 do begin 990 NewNode := Node.OwnerDocument.CreateElement('Category'); 991 Node.AppendChild(NewNode); 992 WriteInteger(NewNode, 'Id', TAcronymCategory(Items[I]).Id); 993 end; 994 end; 995 996 procedure TAcronymCategories.LoadRefFromNode(Node: TDOMNode); 997 var 998 Node2: TDOMNode; 999 Id: Integer; 1000 Category: TAcronymCategory; 1001 begin 1002 Node2 := Node.FirstChild; 1003 while Assigned(Node2) and (Node2.NodeName = 'Category') do begin 1004 Id := ReadInteger(Node2, 'Id', 0); 1005 Category := Db.Categories.SearchById(Id); 1006 if Assigned(Category) then begin 1007 Add(Category); 1008 end; 1009 Node2 := Node2.NextSibling; 1010 end; 944 1011 end; 945 1012
Note:
See TracChangeset
for help on using the changeset viewer.