| 1 | unit FormImport;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
|---|
| 7 | Acronym, FormEx;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 10 |
|
|---|
| 11 | { TFormImport }
|
|---|
| 12 |
|
|---|
| 13 | TFormImport = class(TFormEx)
|
|---|
| 14 | ButtonLoadFromFile: TButton;
|
|---|
| 15 | ButtonProcess: TButton;
|
|---|
| 16 | ComboBoxDataFormat: TComboBox;
|
|---|
| 17 | Label1: TLabel;
|
|---|
| 18 | Memo1: TMemo;
|
|---|
| 19 | OpenDialog1: TOpenDialog;
|
|---|
| 20 | procedure ButtonProcessClick(Sender: TObject);
|
|---|
| 21 | procedure ButtonLoadFromFileClick(Sender: TObject);
|
|---|
| 22 | private
|
|---|
| 23 | procedure ImportMediaWiki;
|
|---|
| 24 | procedure ImportCSV;
|
|---|
| 25 | public
|
|---|
| 26 | AcronymDb: TAcronymDb;
|
|---|
| 27 | end;
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | implementation
|
|---|
| 31 |
|
|---|
| 32 | {$R *.lfm}
|
|---|
| 33 |
|
|---|
| 34 | resourcestring
|
|---|
| 35 | SImportedNewAcronyms = 'Imported %d new acronyms.';
|
|---|
| 36 |
|
|---|
| 37 | { TFormImport }
|
|---|
| 38 |
|
|---|
| 39 | procedure TFormImport.ButtonProcessClick(Sender: TObject);
|
|---|
| 40 | begin
|
|---|
| 41 | if ComboBoxDataFormat.ItemIndex = 0 then ImportCSV;
|
|---|
| 42 | if ComboBoxDataFormat.ItemIndex = 1 then ImportMediaWiki;
|
|---|
| 43 | end;
|
|---|
| 44 |
|
|---|
| 45 | procedure TFormImport.ButtonLoadFromFileClick(Sender: TObject);
|
|---|
| 46 | begin
|
|---|
| 47 | if ComboBoxDataFormat.ItemIndex = 0 then OpenDialog1.DefaultExt := '.csv';
|
|---|
| 48 | if ComboBoxDataFormat.ItemIndex = 1 then OpenDialog1.DefaultExt := '.txt';
|
|---|
| 49 | if OpenDialog1.Execute then
|
|---|
| 50 | Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
|
|---|
| 51 | end;
|
|---|
| 52 |
|
|---|
| 53 | procedure TFormImport.ImportMediaWiki;
|
|---|
| 54 | var
|
|---|
| 55 | I: Integer;
|
|---|
| 56 | Line: string;
|
|---|
| 57 | AcronymName: string;
|
|---|
| 58 | AcronymMeaning: string;
|
|---|
| 59 | AddedCount: Integer;
|
|---|
| 60 | Acronym: TAcronym;
|
|---|
| 61 | Meaning: TAcronymMeaning;
|
|---|
| 62 | begin
|
|---|
| 63 | AcronymName := '';
|
|---|
| 64 | AddedCount := 0;
|
|---|
| 65 | for I := 0 to Memo1.Lines.Count - 1 do begin
|
|---|
| 66 | Line := Trim(Memo1.Lines[I]);
|
|---|
| 67 | if Copy(Line, 1, 1) = ';' then begin
|
|---|
| 68 | if Pos(':', Line) > 0 then begin
|
|---|
| 69 | AcronymName := Trim(Copy(Line, 2, Pos(':', Line) - 2));
|
|---|
| 70 | Line := Copy(Line, Pos(':', Line), Length(Line));
|
|---|
| 71 | end else AcronymName := Trim(Copy(Line, 2, Length(Line)));
|
|---|
| 72 | end;
|
|---|
| 73 | if Copy(Line, 1, 1) = ':' then begin
|
|---|
| 74 | AcronymMeaning := Trim(Copy(Line, 2, Length(Line)));
|
|---|
| 75 | if (AcronymName <> '') and (AcronymMeaning <> '') then begin
|
|---|
| 76 | Acronym := AcronymDb.Acronyms.SearchByName(AcronymName);
|
|---|
| 77 | if not Assigned(Acronym) then begin
|
|---|
| 78 | Acronym := TAcronym.Create;
|
|---|
| 79 | Acronym.Name := AcronymName;
|
|---|
| 80 | AcronymDb.Acronyms.Add(Acronym);
|
|---|
| 81 | end;
|
|---|
| 82 | Meaning := Acronym.Meanings.SearchByName(AcronymMeaning);
|
|---|
| 83 | if not Assigned(Meaning) then begin
|
|---|
| 84 | Meaning := TAcronymMeaning.Create;
|
|---|
| 85 | Meaning.Name := AcronymMeaning;
|
|---|
| 86 | Meaning.Acronym := Acronym;
|
|---|
| 87 | Acronym.Meanings.Add(Meaning);
|
|---|
| 88 | Inc(AddedCount)
|
|---|
| 89 | end;
|
|---|
| 90 | end;
|
|---|
| 91 | end;
|
|---|
| 92 | end;
|
|---|
| 93 | if AddedCount > 0 then AcronymDb.Modified := True;
|
|---|
| 94 | ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
|
|---|
| 95 | end;
|
|---|
| 96 |
|
|---|
| 97 | procedure TFormImport.ImportCSV;
|
|---|
| 98 | var
|
|---|
| 99 | I: Integer;
|
|---|
| 100 | Line: string;
|
|---|
| 101 | Columns: TStringList;
|
|---|
| 102 | Acronym: TAcronym;
|
|---|
| 103 | Meaning: TAcronymMeaning;
|
|---|
| 104 | AcronymName: string;
|
|---|
| 105 | AcronymMeaning: string;
|
|---|
| 106 | AcronymDescription: string;
|
|---|
| 107 | AddedCount: Integer;
|
|---|
| 108 | begin
|
|---|
| 109 | AddedCount := 0;
|
|---|
| 110 | Columns := TStringList.Create;
|
|---|
| 111 | Columns.StrictDelimiter := True;
|
|---|
| 112 | for I := 0 to Memo1.Lines.Count - 1 do begin
|
|---|
| 113 | Line := Trim(Memo1.Lines[I]);
|
|---|
| 114 | Columns.DelimitedText := Line;
|
|---|
| 115 | if Columns.Count > 0 then AcronymName := Trim(Columns[0])
|
|---|
| 116 | else AcronymName := '';
|
|---|
| 117 | if Columns.Count > 1 then AcronymMeaning := Trim(Columns[1])
|
|---|
| 118 | else AcronymMeaning := '';
|
|---|
| 119 | if Columns.Count > 2 then AcronymDescription := Trim(Columns[2])
|
|---|
| 120 | else AcronymDescription := '';
|
|---|
| 121 | if (AcronymName <> '') and (AcronymDescription <> '') then begin
|
|---|
| 122 | Acronym := AcronymDb.Acronyms.SearchByName(AcronymName);
|
|---|
| 123 | if not Assigned(Acronym) then begin
|
|---|
| 124 | Acronym := TAcronym.Create;
|
|---|
| 125 | Acronym.Name := AcronymName;
|
|---|
| 126 | AcronymDb.Acronyms.Add(Acronym);
|
|---|
| 127 | end;
|
|---|
| 128 | Meaning := Acronym.Meanings.SearchByName(AcronymDescription);
|
|---|
| 129 | if not Assigned(Meaning) then begin
|
|---|
| 130 | Meaning := TAcronymMeaning.Create;
|
|---|
| 131 | Meaning.Name := AcronymMeaning;
|
|---|
| 132 | Meaning.Description := AcronymDescription;
|
|---|
| 133 | Meaning.Acronym := Acronym;
|
|---|
| 134 | Acronym.Meanings.Add(Meaning);
|
|---|
| 135 | Inc(AddedCount)
|
|---|
| 136 | end;
|
|---|
| 137 | end;
|
|---|
| 138 | end;
|
|---|
| 139 | Columns.Free;
|
|---|
| 140 | if AddedCount > 0 then AcronymDb.Modified := True;
|
|---|
| 141 | ShowMessage(Format(SImportedNewAcronyms, [AddedCount]));
|
|---|
| 142 | end;
|
|---|
| 143 |
|
|---|
| 144 | end.
|
|---|
| 145 |
|
|---|