Changeset 33 for trunk/UAcronym.pas


Ignore:
Timestamp:
Jul 13, 2016, 2:51:53 PM (8 years ago)
Author:
chronos
Message:
  • Added: Now import sources have categories which are merged to new imported acronym meanings.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UAcronym.pas

    r31 r33  
    5050
    5151  TAcronymMeaning = class
     52    Id: Integer;
    5253    Name: string;
    5354    Description: string;
     
    6566
    6667  TAcronymMeanings = class(TObjectList)
     68  public
    6769    Acronym: TAcronym;
     70    procedure UpdateIds;
    6871    procedure SaveToNode(Node: TDOMNode);
    6972    procedure LoadFromNode(Node: TDOMNode);
     
    8790
    8891  TAcronymCategories = class(TObjectList)
     92    Db: TAcronymDb;
    8993    procedure UpdateIds;
    9094    procedure SaveToNode(Node: TDOMNode);
    9195    procedure LoadFromNode(Node: TDOMNode);
     96    procedure SaveRefToNode(Node: TDOMNode);
     97    procedure LoadRefFromNode(Node: TDOMNode);
    9298    function SearchByName(Name: string): TAcronymCategory;
    9399    function SearchById(Id: Integer): TAcronymCategory;
     
    173179    Sources: TImportSources;
    174180    ItemCount: Integer;
     181    Categories: TAcronymCategories;
    175182    function DownloadHTTP(URL: string; Stream: TStream): Boolean;
    176183    procedure Process;
     
    660667                  AddedAcronym := Sources.AcronymDb.AddAcronym(NewAcronym.Name, NewAcronym.Meaning);
    661668                  AddedAcronym.Description := NewAcronym.Description;
     669                  AddedAcronym.Categories.Assign(Categories, laOr);
    662670                  Inc(ItemCount);
    663671                end;
     
    692700  LastTime := Source.LastTime;
    693701  ItemCount := Source.ItemCount;
     702  Categories.Assign(Source.Categories);
    694703end;
    695704
    696705procedure TImportSource.SaveToNode(Node: TDOMNode);
     706var
     707  NewNode: TDOMNode;
    697708begin
    698709  WriteString(Node, 'Name', Name);
     
    701712    else WriteInteger(Node, 'ImportFormat', -1);
    702713  WriteBoolean(Node, 'Enabled', Enabled);
     714  WriteInteger(Node, 'ItemCount', ItemCount);
     715
     716  NewNode := Node.OwnerDocument.CreateElement('Categories');
     717  Node.AppendChild(NewNode);
     718  Categories.SaveRefToNode(NewNode);
    703719end;
    704720
    705721procedure TImportSource.LoadFromNode(Node: TDOMNode);
     722var
     723  Node2: TDOMNode;
    706724begin
    707725  Name := ReadString(Node, 'Name', '');
     
    709727  Format := Sources.AcronymDb.ImportFormats.SearchById(ReadInteger(Node, 'ImportFormat', -1));
    710728  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);
    711735end;
    712736
     
    715739  Format := nil;
    716740  Enabled := True;
     741  Categories := TAcronymCategories.Create;
     742  Categories.OwnsObjects := False;
    717743end;
    718744
    719745destructor TImportSource.Destroy;
    720 begin
    721   inherited Destroy;
    722 end;
    723 
    724 { TAcronymEntry }
    725 
    726 constructor TAcronymEntry.Create;
    727 begin
    728   Categories := TStringList.Create;
    729 end;
    730 
    731 destructor TAcronymEntry.Destroy;
    732746begin
    733747  FreeAndNil(Categories);
     
    735749end;
    736750
     751{ TAcronymEntry }
     752
     753constructor TAcronymEntry.Create;
     754begin
     755  Categories := TStringList.Create;
     756end;
     757
     758destructor TAcronymEntry.Destroy;
     759begin
     760  FreeAndNil(Categories);
     761  inherited Destroy;
     762end;
     763
    737764{ TAcronymMeanings }
    738765
     766procedure TAcronymMeanings.UpdateIds;
     767var
     768  LastId: Integer;
     769  I: Integer;
     770begin
     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;
     783end;
     784
    739785procedure TAcronymMeanings.SaveToNode(Node: TDOMNode);
    740786var
     
    742788  NewNode2: TDOMNode;
    743789begin
     790  UpdateIds;
    744791  for I := 0 to Count - 1 do
    745792  with TAcronymMeaning(Items[I]) do begin
     
    764811    Node2 := Node2.NextSibling;
    765812  end;
     813  UpdateIds;
    766814end;
    767815
     
    788836var
    789837  NewNode: TDOMNode;
    790   NewNode2: TDOMNode;
    791   I: Integer;
    792838begin
    793839  WriteString(Node, 'Name', Name);
     
    797843  NewNode := Node.OwnerDocument.CreateElement('Categories');
    798844  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);
    804846end;
    805847
     
    807849var
    808850  Node2: TDOMNode;
    809   Node3: TDOMNode;
    810   Category: TAcronymCategory;
    811   Id: Integer;
     851  I: Integer;
    812852begin
    813853  Name := ReadString(Node, 'Name', '');
     
    815855  Language := ReadString(Node, 'Language', '');
    816856
    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);
    829865  end;
    830866end;
     
    839875  I: Integer;
    840876begin
     877  if Assigned(Acronym) then
     878    Acronym.Meanings.Remove(Self);
    841879  for I := 0 to Categories.Count - 1 do
    842880    TAcronymCategory(Categories[I]).AcronymMeanings.Remove(Self);
     
    942980  end;
    943981  UpdateIds;
     982end;
     983
     984procedure TAcronymCategories.SaveRefToNode(Node: TDOMNode);
     985var
     986  I: Integer;
     987  NewNode: TDOMNode;
     988begin
     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;
     994end;
     995
     996procedure TAcronymCategories.LoadRefFromNode(Node: TDOMNode);
     997var
     998  Node2: TDOMNode;
     999  Id: Integer;
     1000  Category: TAcronymCategory;
     1001begin
     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;
    9441011end;
    9451012
Note: See TracChangeset for help on using the changeset viewer.