Changeset 8 for trunk/UFormImport.pas


Ignore:
Timestamp:
Apr 21, 2016, 1:12:45 PM (9 years ago)
Author:
chronos
Message:
  • Added: Also allow to edit acronym detailed description.
  • Added: Import from CSV file.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UFormImport.pas

    r6 r8  
    1414
    1515  TFormImport = class(TForm)
     16    ButtonLoadFromFile: TButton;
    1617    ButtonImport: TButton;
     18    ComboBoxDataFormat: TComboBox;
     19    Label1: TLabel;
    1720    Memo1: TMemo;
     21    OpenDialog1: TOpenDialog;
    1822    procedure ButtonImportClick(Sender: TObject);
     23    procedure ButtonLoadFromFileClick(Sender: TObject);
    1924  private
    20     { private declarations }
     25    procedure ImportMediaWiki;
     26    procedure ImportCSV;
    2127  public
    2228    { public declarations }
     
    5561
    5662procedure TFormImport.ButtonImportClick(Sender: TObject);
     63begin
     64  if ComboBoxDataFormat.ItemIndex = 0 then ImportCSV;
     65  if ComboBoxDataFormat.ItemIndex = 1 then ImportMediaWiki;
     66end;
     67
     68procedure TFormImport.ButtonLoadFromFileClick(Sender: TObject);
     69begin
     70  if OpenDialog1.Execute then
     71    Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
     72end;
     73
     74procedure TFormImport.ImportMediaWiki;
    5775var
    5876  I: Integer;
    5977  Line: string;
    6078  AcronymName: string;
    61   AcronymDescription: string;
     79  AcronymMeaning: string;
    6280  AddedCount: Integer;
    6381  Acronym: TAcronym;
     
    7492    end;
    7593    if Copy(Line, 1, 1) = ':' then begin
    76       AcronymDescription := Trim(Copy(Line, 2, Length(Line)));
    77       if AcronymName <> '' then begin
     94      AcronymMeaning := Trim(Copy(Line, 2, Length(Line)));
     95      if (AcronymName <> '') and (AcronymMeaning <> '') then begin
    7896        Acronym := FormMain.AcronymDb.Acronyms.SearchByName(AcronymName);
    7997        if not Assigned(Acronym) then begin
     
    82100          FormMain.AcronymDb.Acronyms.Add(Acronym);
    83101        end;
    84         Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
     102        Meaning := Acronym.Meanings.SearchByName(AcronymMeaning);
    85103        if not Assigned(Meaning) then begin
    86104          Meaning := TAcronymMeaning.Create;
    87           Meaning.Name := AcronymDescription;
     105          Meaning.Name := AcronymMeaning;
    88106          Meaning.Acronym := Acronym;
    89107          Acronym.Meanings.Add(Meaning);
     
    97115end;
    98116
     117procedure TFormImport.ImportCSV;
     118var
     119  I: Integer;
     120  Line: string;
     121  Columns: TStringList;
     122  Acronym: TAcronym;
     123  Meaning: TAcronymMeaning;
     124  AcronymName: string;
     125  AcronymMeaning: string;
     126  AcronymDescription: string;
     127  AddedCount: Integer;
     128begin
     129  AddedCount := 0;
     130  Columns := TStringList.Create;
     131  Columns.StrictDelimiter := True;
     132  for I := 0 to Memo1.Lines.Count - 1 do begin
     133    Line := Trim(Memo1.Lines[I]);
     134    Columns.DelimitedText := Line;
     135    if Columns.Count > 0 then AcronymName := Trim(Columns[0])
     136      else AcronymName := '';
     137    if Columns.Count > 1 then AcronymMeaning := Trim(Columns[1])
     138      else AcronymMeaning := '';
     139    if Columns.Count > 2 then AcronymDescription := Trim(Columns[2])
     140      else AcronymDescription := '';
     141    if (AcronymName <> '') and (AcronymDescription <> '') then begin
     142      Acronym := FormMain.AcronymDb.Acronyms.SearchByName(AcronymName);
     143      if not Assigned(Acronym) then begin
     144        Acronym := TAcronym.Create;
     145        Acronym.Name := AcronymName;
     146        FormMain.AcronymDb.Acronyms.Add(Acronym);
     147      end;
     148      Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
     149      if not Assigned(Meaning) then begin
     150        Meaning := TAcronymMeaning.Create;
     151        Meaning.Name := AcronymMeaning;
     152        Meaning.Description := AcronymDescription;
     153        Meaning.Acronym := Acronym;
     154        Acronym.Meanings.Add(Meaning);
     155        Inc(AddedCount)
     156      end;
     157    end;
     158  end;
     159  Columns.Free;
     160  if AddedCount > 0 then FormMain.AcronymDb.Modified := True;
     161  ShowMessage(Format('Imported %d new acronyms.', [AddedCount]));
     162end;
     163
    99164end.
    100165
Note: See TracChangeset for help on using the changeset viewer.