Changeset 89 for trunk/UAcronym.pas


Ignore:
Timestamp:
Aug 9, 2016, 10:53:36 PM (8 years ago)
Author:
chronos
Message:
  • Added: Show from which imports acronym meanings comes from.

This would easy correcting wrong acronyms directly in source.

  • Modified: References to categories stored more efficiently in XML project file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UAcronym.pas

    r88 r89  
    66
    77uses
    8   Classes, SysUtils, Contnrs, XMLConf, XMLRead, XMLWrite, DOM, UXMLUtils,
     8  Classes, SysUtils, Contnrs, XMLRead, XMLWrite, DOM, UXMLUtils,
    99  SpecializedList, fphttpclient, Dialogs, odbcconn, sqldb;
    1010
     
    1515  TImportSources = class;
    1616  TImportFormats = class;
    17 
    18   TAcronymSource = class
    19     Name: string;
    20     URL: string;
    21   end;
    22 
    23   TAcronymSources = class(TObjectList)
    24 
    25   end;
    2617
    2718  { TAcronym }
     
    5647    Acronym: TAcronym;
    5748    Categories: TAcronymCategories;
    58     Source: TAcronymSource;
     49    Sources: TImportSources;
     50    procedure MergeCategories(MergedCategories: TAcronymCategories);
    5951    procedure SaveToNode(Node: TDOMNode);
    6052    procedure LoadFromNode(Node: TDOMNode);
     
    111103    Description: string;
    112104    Categories: TStringList;
     105    Sources: TStringList;
    113106    constructor Create;
    114107    destructor Destroy; override;
     
    174167    procedure TextParse(S: string);
    175168  public
     169    Id: Integer;
    176170    Enabled: Boolean;
    177171    Name: string;
     
    200194  TImportSources = class(TObjectList)
    201195    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);
    202201    function SearchByName(Name: string): TImportSource;
    203202    procedure SaveToNode(Node: TDOMNode);
     
    210209  TAcronymDb = class
    211210    FileName: string;
    212     Sources: TAcronymSources;
    213211    Acronyms: TAcronyms;
    214212    Categories: TAcronymCategories;
     
    298296  SQLQuery1: TSQLQuery;
    299297  NewAcronym: TAcronymEntry;
    300   J: Integer;
    301298  AddedAcronym: TAcronymMeaning;
    302299begin
     
    329326      if (NewAcronym.Name <> '') and (NewAcronym.Meaning <> '') then begin
    330327        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);
    338331      end;
    339332      SQLQuery1.Next;
     
    435428  Q: Integer;
    436429  I: Integer;
    437   J: Integer;
    438430  T: string;
    439431  LastLength: Integer;
     
    508500              AddedAcronym := Sources.AcronymDb.AddAcronym(NewAcronym.Name, NewAcronym.Meaning);
    509501              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
    517506              Inc(ItemCount);
    518507            end;
     
    643632{ TImportSources }
    644633
     634procedure TImportSources.UpdateIds;
     635var
     636  LastId: Integer;
     637  I: Integer;
     638begin
     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;
     651end;
     652
     653function TImportSources.SearchById(Id: Integer): TImportSource;
     654var
     655  I: Integer;
     656begin
     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;
     661end;
     662
     663procedure TImportSources.SaveRefToNode(Node: TDOMNode);
     664var
     665  I: Integer;
     666  NewNode: TDOMNode;
     667begin
     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;
     673end;
     674
     675procedure TImportSources.LoadRefFromNode(Node: TDOMNode);
     676var
     677  Node2: TDOMNode;
     678  Id: Integer;
     679  Source: TImportSource;
     680begin
     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;
     691end;
     692
     693procedure TImportSources.AssignToStrings(Strings: TStrings);
     694var
     695  I: Integer;
     696begin
     697  Strings.Clear;
     698  for I := 0 to Count - 1 do
     699    Strings.AddObject(TImportSource(Items[I]).Name, Items[I]);
     700end;
     701
    645702function TImportSources.SearchByName(Name: string): TImportSource;
    646703var
     
    658715  NewNode2: TDOMNode;
    659716begin
     717  UpdateIds;
    660718  for I := 0 to Count - 1 do
    661719  with TImportSource(Items[I]) do begin
     
    680738    Node2 := Node2.NextSibling;
    681739  end;
     740  UpdateIds;
    682741end;
    683742
     
    854913begin
    855914  Categories := TStringList.Create;
     915  Sources := TStringList.Create;
    856916  Name := '';
    857917  Meaning := '';
     
    862922begin
    863923  FreeAndNil(Categories);
     924  FreeAndNil(Sources);
    864925  inherited Destroy;
    865926end;
     
    936997{ TAcronymMeaning }
    937998
     999procedure TAcronymMeaning.MergeCategories(MergedCategories: TAcronymCategories);
     1000var
     1001  I: Integer;
     1002begin
     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;
     1008end;
     1009
    9381010procedure TAcronymMeaning.SaveToNode(Node: TDOMNode);
    9391011var
     
    9471019  Node.AppendChild(NewNode);
    9481020  Categories.SaveRefToNode(NewNode);
     1021
     1022  NewNode := Node.OwnerDocument.CreateElement('Sources');
     1023  Node.AppendChild(NewNode);
     1024  Sources.SaveRefToNode(NewNode);
    9491025end;
    9501026
     
    9671043      TAcronymCategory(Categories[I]).AcronymMeanings.Add(Self);
    9681044  end;
     1045
     1046  Sources.AcronymDb := Acronym.Db;
     1047  Node2 := Node.FindNode('Sources');
     1048  if Assigned(Node2) then begin
     1049    Sources.LoadRefFromNode(Node2);
     1050  end;
    9691051end;
    9701052
     
    9721054begin
    9731055  Categories := TAcronymCategories.Create(False);
     1056  Sources := TImportSources.Create(False);
    9741057end;
    9751058
     
    9811064    TAcronymCategory(Categories[I]).AcronymMeanings.Remove(Self);
    9821065  FreeAndNil(Categories);
     1066  FreeAndNil(Sources);
    9831067  inherited Destroy;
    9841068end;
     
    10891173begin
    10901174  for I := 0 to Count - 1 do begin
    1091     NewNode := Node.OwnerDocument.CreateElement('Category');
     1175    NewNode := Node.OwnerDocument.CreateElement('Ref');
    10921176    Node.AppendChild(NewNode);
    1093     WriteInteger(NewNode, 'Id', TAcronymCategory(Items[I]).Id);
     1177    NewNode.TextContent := IntToStr(TAcronymCategory(Items[I]).Id);
    10941178  end;
    10951179end;
     
    11011185  Category: TAcronymCategory;
    11021186begin
     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
    11031199  Node2 := Node.FirstChild;
    11041200  while Assigned(Node2) and (Node2.NodeName = 'Category') do begin
     
    12381334constructor TAcronymDb.Create;
    12391335begin
    1240   Sources := TAcronymSources.Create;
    12411336  Acronyms := TAcronyms.Create;
    12421337  Acronyms.Db := Self;
     
    12511346  FreeAndNil(ImportFormats);
    12521347  FreeAndNil(ImportSources);
    1253   FreeAndNil(Sources);
    12541348  FreeAndNil(Acronyms);
    12551349  FreeAndNil(Categories);
Note: See TracChangeset for help on using the changeset viewer.