Statistiques
| Révision:

root / InraAfz / UnitMain.pas @ 1

Historique | Voir | Annoter | Télécharger (10,246 ko)

1 1 avalancogn
unit UnitMain;
2
3
interface
4
5
uses
6
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7
  Dialogs, MidasLib, DB, DBClient, Grids, DBGrids, XPMan, LbClass, LbCipher,
8
  Buttons, ExtCtrls, StdCtrls, DBCtrls, JvCsvData, DBTables, xmldom, XMLIntf,
9
  msxmldom, XMLDoc, JvSimpleXml;
10
11
type
12
  TFormMain = class(TForm)
13
    DataSourceInraAfz: TDataSource;
14
    ClientDataSetClasses: TClientDataSet;
15
    ClientDataSetClassesId: TIntegerField;
16
    ClientDataSetClassesName: TWideStringField;
17
    DBGridInraAfz: TDBGrid;
18
    XPManifestStyle: TXPManifest;
19
    PanelBoutons: TPanel;
20
    SpeedButtonExit: TSpeedButton;
21
    SpeedButtonSave: TSpeedButton;
22
    ListBoxClasses: TListBox;
23
    ClientDataSetInraAfz: TClientDataSet;
24
    ClientDataSetInraAfzId: TAutoIncField;
25
    ClientDataSetInraAfzName: TStringField;
26
    ClientDataSetInraAfzClass: TIntegerField;
27
    ClientDataSetInraAfzMS: TFloatField;
28
    ClientDataSetInraAfzMAT: TFloatField;
29
    ClientDataSetInraAfzCB: TFloatField;
30
    ClientDataSetInraAfzCoefCB: TFloatField;
31
    ClientDataSetInraAfzMG: TFloatField;
32
    ClientDataSetInraAfzMM: TFloatField;
33
    ClientDataSetInraAfzAmidon: TFloatField;
34
    ClientDataSetInraAfzSucres: TFloatField;
35
    ClientDataSetInraAfzNDF: TFloatField;
36
    ClientDataSetInraAfzCoefNDF: TFloatField;
37
    ClientDataSetInraAfzADF: TFloatField;
38
    ClientDataSetInraAfzCoefADF: TFloatField;
39
    ClientDataSetInraAfzADL: TFloatField;
40
    ClientDataSetInraAfzEB: TFloatField;
41
    ClientDataSetInraAfzdEc: TFloatField;
42
    ClientDataSetInraAfzEMc_EDc: TFloatField;
43
    ClientDataSetInraAfzENc_EMc: TFloatField;
44
    ClientDataSetInraAfzEMt_EDt: TFloatField;
45
    ClientDataSetInraAfzENt_EMt: TFloatField;
46
    ClientDataSetInraAfzEDt_EDc: TFloatField;
47
    ClientDataSetInraAfzLys: TFloatField;
48
    ClientDataSetInraAfzThr: TFloatField;
49
    ClientDataSetInraAfzMet: TFloatField;
50
    ClientDataSetInraAfzCys: TFloatField;
51
    ClientDataSetInraAfzMetCys: TFloatField;
52
    ClientDataSetInraAfzTrp: TFloatField;
53
    ClientDataSetInraAfzIle: TFloatField;
54
    ClientDataSetInraAfzVal: TFloatField;
55
    ClientDataSetInraAfzLeu: TFloatField;
56
    ClientDataSetInraAfzPhe: TFloatField;
57
    ClientDataSetInraAfzTyr: TFloatField;
58
    ClientDataSetInraAfzPheTyr: TFloatField;
59
    ClientDataSetInraAfzHis: TFloatField;
60
    ClientDataSetInraAfzArg: TFloatField;
61
    ClientDataSetInraAfzAla: TFloatField;
62
    ClientDataSetInraAfzAsp: TFloatField;
63
    ClientDataSetInraAfzGlu: TFloatField;
64
    ClientDataSetInraAfzGly: TFloatField;
65
    ClientDataSetInraAfzSer: TFloatField;
66
    ClientDataSetInraAfzPro: TFloatField;
67
    ClientDataSetInraAfzdLys: TFloatField;
68
    ClientDataSetInraAfzdThr: TFloatField;
69
    ClientDataSetInraAfzdMet: TFloatField;
70
    ClientDataSetInraAfzdCys: TFloatField;
71
    ClientDataSetInraAfzdMetCys: TFloatField;
72
    ClientDataSetInraAfzdTrp: TFloatField;
73
    ClientDataSetInraAfzdIle: TFloatField;
74
    ClientDataSetInraAfzdVal: TFloatField;
75
    ClientDataSetInraAfzdLeu: TFloatField;
76
    ClientDataSetInraAfzdPhe: TFloatField;
77
    ClientDataSetInraAfzdTyr: TFloatField;
78
    ClientDataSetInraAfzdPheTyr: TFloatField;
79
    ClientDataSetInraAfzdHis: TFloatField;
80
    ClientDataSetInraAfzdArg: TFloatField;
81
    ClientDataSetInraAfzdAla: TFloatField;
82
    ClientDataSetInraAfzdAsp: TFloatField;
83
    ClientDataSetInraAfzdGlu: TFloatField;
84
    ClientDataSetInraAfzdGly: TFloatField;
85
    ClientDataSetInraAfzdSer: TFloatField;
86
    ClientDataSetInraAfzdPro: TFloatField;
87
    ClientDataSetInraAfzClassName: TStringField;
88
    SpeedButtonLoad: TSpeedButton;
89
    ClientDataSetInraAfzCa: TFloatField;
90
    ClientDataSetInraAfzP: TFloatField;
91
    ClientDataSetInraAfzdP: TFloatField;
92
    ClientDataSetInraAfzdPphy: TFloatField;
93
    ClientDataSetInraAfza: TFloatField;
94
    ClientDataSetInraAfzb: TFloatField;
95
    ClientDataSetInraAfzdNc: TFloatField;
96
    ClientDataSetInraAfzdNt: TFloatField;
97
    ClientDataSetInraAfzSource: TStringField;
98
    ClientDataSetInraAfzComment: TStringField;
99
    OpenDialogCSV: TOpenDialog;
100
    JvCsvDataSetInput: TJvCsvDataSet;
101
    procedure FormShow(Sender: TObject);
102
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
103
    procedure SpeedButtonExitClick(Sender: TObject);
104
    procedure SpeedButtonSaveClick(Sender: TObject);
105
    procedure DBGridInraAfzTitleClick(Column: TColumn);
106
    procedure DBGridInraAfzDrawColumnCell(Sender: TObject; const Rect: TRect;
107
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
108
    procedure ClientDataSetInraAfzBeforePost(DataSet: TDataSet);
109
    procedure SpeedButtonLoadClick(Sender: TObject);
110
  private
111
    { D?clarations priv?es }
112
    InputFileName, OutputFileName: WideString;
113
    FluxClair, FluxCrypte: TMemoryStream;
114
    Cryptage: TLbBlowfish;
115
  public
116
    { D?clarations publiques }
117
  end;
118
119
var
120
  FormMain: TFormMain;
121
122
implementation
123
124
{$R *.dfm}
125
126
const
127
//  INPUT = 'EvaPig2020.xml';
128
  INPUT = 'EvaPig2020.csv';
129
  OUTPUT = 'EvaPig2020.dat';
130
131
procedure TFormMain.ClientDataSetInraAfzBeforePost(DataSet: TDataSet);
132
begin
133
  if DataSet.FieldByName('Class').IsNull
134
  then // Remplacer Null par 0 (Non renseign?)
135
    DataSet.FieldByName('Class').AsInteger := 0;
136
end;
137
138
procedure TFormMain.DBGridInraAfzDrawColumnCell(Sender: TObject;
139
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
140
begin
141
  if Column.FieldName = ClientDataSetInraAfz.IndexFieldNames
142
  then
143
    DBGridInraAfz.Canvas.Font.Style := [fsBold];
144
  DBGridInraAfz.DefaultDrawColumnCell(Rect, DataCol, Column, State);
145
end;
146
147
procedure TFormMain.DBGridInraAfzTitleClick(Column: TColumn);
148
begin
149
  ClientDataSetInraAfz.IndexFieldNames := Column.FieldName;
150
end;
151
152
procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);
153
begin
154
  Cryptage.Free;
155
  ClientDataSetInraAfz.Active := False;
156
end;
157
158
procedure TFormMain.FormShow(Sender: TObject);
159
var
160
  i: Integer;
161
begin
162
  InputFileName := ExtractFilePath(Application.ExeName) + INPUT;
163
  OutputFileName := ExtractFilePath(Application.ExeName) + OUTPUT;
164
  // Classes
165
  ClientDataSetClasses.CreateDataSet;
166
  for i := 0 to ListBoxClasses.Items.Count - 1 do
167
    ClientDataSetClasses.AppendRecord([i+1, ListBoxClasses.Items[i]]);
168
  // Tables INRA-AFZ
169
  ClientDataSetInraAfz.CreateDataSet;
170
  Cryptage := TlbBlowfish.Create(nil);
171
  Cryptage.GenerateKey('Tables of Composition and Nutritional Value of Feed Materials');
172
  if FileExists(OutputFileName)
173
  then // Chargement de la table
174
  begin
175
    FluxCrypte := TMemoryStream.Create;
176
    FluxClair := TMemoryStream.Create;
177
    try
178
      FluxCrypte.LoadFromFile(OutputFileName);
179
      FluxCrypte.Position := 0;
180
      Cryptage.DecryptStream(FluxCrypte, FluxClair);
181
      FluxClair.Position := 0;
182
      ClientDataSetInraAfz.LoadFromStream(FluxClair);
183
      ClientDataSetInraAfz.MergeChangeLog;
184
    finally
185
      FluxCrypte.Free;
186
      FluxClair.Free;
187
    end;
188
  end;
189
end;
190
191
procedure TFormMain.SpeedButtonExitClick(Sender: TObject);
192
begin
193
  if ClientDataSetInraAfz.ChangeCount > 0
194
  then
195
    if (MessageDlg('DataSet has been modified' + sLineBreak
196
      + 'Do you want to exit without saving?', mtConfirmation, [mbYes, mbNo], 0) = mrYes)
197
    then
198
      ClientDataSetInraAfz.CancelUpdates
199
    else
200
      Exit;
201
  Close;
202
end;
203
204
procedure TFormMain.SpeedButtonLoadClick(Sender: TObject);
205
var
206
  i: Integer;
207
begin
208
  OpenDialogCSV.FileName := InputFileName;
209
  if OpenDialogCSV.Execute then
210
  begin
211
    JvCsvDataSetInput.FileName := OpenDialogCSV.FileName;
212
    JvCsvDataSetInput.Active := True;
213
    JvCsvDataSetInput.First;
214
    JvCsvDataSetInput.Next; // Ignorer la seconde ligne de titre (unit?s de mesure)
215
    while not JvCsvDataSetInput.Eof do
216
    begin
217
      ClientDataSetInraAfz.Append;
218
      for i := 0 to ClientDataSetInraAfz.Fields.Count - 1 do
219
        case i of
220
          0: // Id (AutoInc)
221
            ;
222
          1..3: // Name, Comment, Source (String)
223
            if (Length(JvCsvDataSetInput.Fields[i].AsString) > 0) then
224
              ClientDataSetInraAfz.Fields[i].AsString := JvCsvDataSetInput.Fields[i].AsString;
225
          4: // ClassId
226
            ClientDataSetInraAfz.Fields[i].AsInteger := JvCsvDataSetInput.Fields[i].AsInteger;
227
          5: ; // ClassName (fkLookup)
228
          6..10, 12, 14, 16..18: // %
229
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100;
230
          11, 13, 15: // Coef
231
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat;
232
          // Colonne 19 du CSV ignor?e (EB kCal) => d?callage des indices
233
          19: // EB MJ
234
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat;
235
          20..25: // %
236
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
237
          26, 27: // Coef
238
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat;
239
          28..29: // %
240
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
241
          30..49: // AA (g/kg MS) -> fraction de MAT
242
            if JvCsvDataSetInput.Fields[i+1].AsFloat = 0
243
            then
244
              ClientDataSetInraAfz.Fields[i].AsFloat := 0
245
            else
246
              ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / ClientDataSetInraAfzMAT.Value / 1000;
247
          50..69: // dAA (%)
248
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
249
          70..71: // Min (g/kg MS)
250
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 1000;
251
          72..73: // dP (%)
252
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
253
        end;
254
      JvCsvDataSetInput.Next;
255
    end;
256
    ClientDataSetInraAfz.First;
257
  end;
258
end;
259
260
procedure TFormMain.SpeedButtonSaveClick(Sender: TObject);
261
begin
262
  FluxClair := TMemoryStream.Create;
263
  FluxCrypte := TMemoryStream.Create;
264
  try
265
    ClientDataSetInraAfz.MergeChangeLog;
266
    ClientDataSetInraAfz.SaveToStream(FluxClair, dfBinary);
267
    FluxClair.Position := 0;
268
    Cryptage.EncryptStream(FluxClair, FluxCrypte);
269
    FluxCrypte.Position := 0;
270
    FluxCrypte.SaveToFile(OutputFileName);
271
  finally
272
    FluxClair.Free;
273
    FluxCrypte.Free;
274
  end;
275
end;
276
277
end.