Statistiques
| Révision:

root / InraAfz / UnitMain.pas @ 10

Historique | Voir | Annoter | Télécharger (13,032 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 3 avalancogn
const
12
  CALORIE = 4.184; // Conversion J -> cal
13
14 1 avalancogn
type
15
  TFormMain = class(TForm)
16
    DataSourceInraAfz: TDataSource;
17
    ClientDataSetClasses: TClientDataSet;
18
    ClientDataSetClassesId: TIntegerField;
19
    ClientDataSetClassesName: TWideStringField;
20
    DBGridInraAfz: TDBGrid;
21
    XPManifestStyle: TXPManifest;
22
    PanelBoutons: TPanel;
23
    SpeedButtonExit: TSpeedButton;
24
    SpeedButtonSave: TSpeedButton;
25
    ListBoxClasses: TListBox;
26
    ClientDataSetInraAfz: TClientDataSet;
27
    ClientDataSetInraAfzId: TAutoIncField;
28
    ClientDataSetInraAfzName: TStringField;
29
    ClientDataSetInraAfzClass: TIntegerField;
30
    ClientDataSetInraAfzMS: TFloatField;
31
    ClientDataSetInraAfzMAT: TFloatField;
32
    ClientDataSetInraAfzCB: TFloatField;
33
    ClientDataSetInraAfzCoefCB: TFloatField;
34
    ClientDataSetInraAfzMG: TFloatField;
35
    ClientDataSetInraAfzMM: TFloatField;
36
    ClientDataSetInraAfzAmidon: TFloatField;
37
    ClientDataSetInraAfzSucres: TFloatField;
38
    ClientDataSetInraAfzNDF: TFloatField;
39
    ClientDataSetInraAfzCoefNDF: TFloatField;
40
    ClientDataSetInraAfzADF: TFloatField;
41
    ClientDataSetInraAfzCoefADF: TFloatField;
42
    ClientDataSetInraAfzADL: TFloatField;
43
    ClientDataSetInraAfzEB: TFloatField;
44
    ClientDataSetInraAfzdEc: TFloatField;
45
    ClientDataSetInraAfzEMc_EDc: TFloatField;
46
    ClientDataSetInraAfzENc_EMc: TFloatField;
47
    ClientDataSetInraAfzEMt_EDt: TFloatField;
48
    ClientDataSetInraAfzENt_EMt: TFloatField;
49
    ClientDataSetInraAfzEDt_EDc: TFloatField;
50
    ClientDataSetInraAfzLys: TFloatField;
51
    ClientDataSetInraAfzThr: TFloatField;
52
    ClientDataSetInraAfzMet: TFloatField;
53
    ClientDataSetInraAfzCys: TFloatField;
54
    ClientDataSetInraAfzMetCys: TFloatField;
55
    ClientDataSetInraAfzTrp: TFloatField;
56
    ClientDataSetInraAfzIle: TFloatField;
57
    ClientDataSetInraAfzVal: TFloatField;
58
    ClientDataSetInraAfzLeu: TFloatField;
59
    ClientDataSetInraAfzPhe: TFloatField;
60
    ClientDataSetInraAfzTyr: TFloatField;
61
    ClientDataSetInraAfzPheTyr: TFloatField;
62
    ClientDataSetInraAfzHis: TFloatField;
63
    ClientDataSetInraAfzArg: TFloatField;
64
    ClientDataSetInraAfzAla: TFloatField;
65
    ClientDataSetInraAfzAsp: TFloatField;
66
    ClientDataSetInraAfzGlu: TFloatField;
67
    ClientDataSetInraAfzGly: TFloatField;
68
    ClientDataSetInraAfzSer: TFloatField;
69
    ClientDataSetInraAfzPro: TFloatField;
70
    ClientDataSetInraAfzdLys: TFloatField;
71
    ClientDataSetInraAfzdThr: TFloatField;
72
    ClientDataSetInraAfzdMet: TFloatField;
73
    ClientDataSetInraAfzdCys: TFloatField;
74
    ClientDataSetInraAfzdMetCys: TFloatField;
75
    ClientDataSetInraAfzdTrp: TFloatField;
76
    ClientDataSetInraAfzdIle: TFloatField;
77
    ClientDataSetInraAfzdVal: TFloatField;
78
    ClientDataSetInraAfzdLeu: TFloatField;
79
    ClientDataSetInraAfzdPhe: TFloatField;
80
    ClientDataSetInraAfzdTyr: TFloatField;
81
    ClientDataSetInraAfzdPheTyr: TFloatField;
82
    ClientDataSetInraAfzdHis: TFloatField;
83
    ClientDataSetInraAfzdArg: TFloatField;
84
    ClientDataSetInraAfzdAla: TFloatField;
85
    ClientDataSetInraAfzdAsp: TFloatField;
86
    ClientDataSetInraAfzdGlu: TFloatField;
87
    ClientDataSetInraAfzdGly: TFloatField;
88
    ClientDataSetInraAfzdSer: TFloatField;
89
    ClientDataSetInraAfzdPro: TFloatField;
90
    ClientDataSetInraAfzClassName: TStringField;
91
    SpeedButtonLoad: TSpeedButton;
92
    ClientDataSetInraAfzCa: TFloatField;
93
    ClientDataSetInraAfzP: TFloatField;
94
    ClientDataSetInraAfzdP: TFloatField;
95
    ClientDataSetInraAfzdPphy: TFloatField;
96
    ClientDataSetInraAfza: TFloatField;
97
    ClientDataSetInraAfzb: TFloatField;
98
    ClientDataSetInraAfzdNc: TFloatField;
99
    ClientDataSetInraAfzdNt: TFloatField;
100
    ClientDataSetInraAfzSource: TStringField;
101
    ClientDataSetInraAfzComment: TStringField;
102
    OpenDialogCSV: TOpenDialog;
103
    JvCsvDataSetInput: TJvCsvDataSet;
104 3 avalancogn
    ClientDataSetInraAfzNa: TFloatField;
105
    ClientDataSetInraAfzK: TFloatField;
106
    ClientDataSetInraAfzCl: TFloatField;
107 1 avalancogn
    procedure FormShow(Sender: TObject);
108
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
109
    procedure SpeedButtonExitClick(Sender: TObject);
110
    procedure SpeedButtonSaveClick(Sender: TObject);
111
    procedure DBGridInraAfzTitleClick(Column: TColumn);
112
    procedure DBGridInraAfzDrawColumnCell(Sender: TObject; const Rect: TRect;
113
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
114
    procedure ClientDataSetInraAfzBeforePost(DataSet: TDataSet);
115
    procedure SpeedButtonLoadClick(Sender: TObject);
116
  private
117
    { D?clarations priv?es }
118
    InputFileName, OutputFileName: WideString;
119
    FluxClair, FluxCrypte: TMemoryStream;
120
    Cryptage: TLbBlowfish;
121
  public
122
    { D?clarations publiques }
123
  end;
124
125
var
126
  FormMain: TFormMain;
127
128
implementation
129
130
{$R *.dfm}
131
132
const
133
//  INPUT = 'EvaPig2020.xml';
134
  INPUT = 'EvaPig2020.csv';
135
  OUTPUT = 'EvaPig2020.dat';
136
137
procedure TFormMain.ClientDataSetInraAfzBeforePost(DataSet: TDataSet);
138
begin
139
  if DataSet.FieldByName('Class').IsNull
140
  then // Remplacer Null par 0 (Non renseign?)
141
    DataSet.FieldByName('Class').AsInteger := 0;
142
end;
143
144
procedure TFormMain.DBGridInraAfzDrawColumnCell(Sender: TObject;
145
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
146
begin
147
  if Column.FieldName = ClientDataSetInraAfz.IndexFieldNames
148
  then
149
    DBGridInraAfz.Canvas.Font.Style := [fsBold];
150
  DBGridInraAfz.DefaultDrawColumnCell(Rect, DataCol, Column, State);
151
end;
152
153
procedure TFormMain.DBGridInraAfzTitleClick(Column: TColumn);
154
begin
155
  ClientDataSetInraAfz.IndexFieldNames := Column.FieldName;
156
end;
157
158
procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);
159
begin
160
  Cryptage.Free;
161
  ClientDataSetInraAfz.Active := False;
162
end;
163
164
procedure TFormMain.FormShow(Sender: TObject);
165
var
166
  i: Integer;
167
begin
168
  InputFileName := ExtractFilePath(Application.ExeName) + INPUT;
169
  OutputFileName := ExtractFilePath(Application.ExeName) + OUTPUT;
170
  // Classes
171
  ClientDataSetClasses.CreateDataSet;
172
  for i := 0 to ListBoxClasses.Items.Count - 1 do
173
    ClientDataSetClasses.AppendRecord([i+1, ListBoxClasses.Items[i]]);
174
  // Tables INRA-AFZ
175
  ClientDataSetInraAfz.CreateDataSet;
176
  Cryptage := TlbBlowfish.Create(nil);
177
  Cryptage.GenerateKey('Tables of Composition and Nutritional Value of Feed Materials');
178
  if FileExists(OutputFileName)
179
  then // Chargement de la table
180
  begin
181
    FluxCrypte := TMemoryStream.Create;
182
    FluxClair := TMemoryStream.Create;
183
    try
184
      FluxCrypte.LoadFromFile(OutputFileName);
185
      FluxCrypte.Position := 0;
186
      Cryptage.DecryptStream(FluxCrypte, FluxClair);
187
      FluxClair.Position := 0;
188
      ClientDataSetInraAfz.LoadFromStream(FluxClair);
189
      ClientDataSetInraAfz.MergeChangeLog;
190
    finally
191
      FluxCrypte.Free;
192
      FluxClair.Free;
193
    end;
194
  end;
195
end;
196
197
procedure TFormMain.SpeedButtonExitClick(Sender: TObject);
198
begin
199
  if ClientDataSetInraAfz.ChangeCount > 0
200
  then
201 9 avalancogn
    if (MessageDlg('Les donn?es ont ?t? modifi?es...' + sLineBreak
202
      + 'Souhaitez-vous quitter sans sauvegarder ?', mtConfirmation, [mbYes, mbNo], 0) = mrYes)
203 1 avalancogn
    then
204
      ClientDataSetInraAfz.CancelUpdates
205
    else
206
      Exit;
207
  Close;
208
end;
209
210
procedure TFormMain.SpeedButtonLoadClick(Sender: TObject);
211
var
212
  i: Integer;
213
begin
214
  OpenDialogCSV.FileName := InputFileName;
215
  if OpenDialogCSV.Execute then
216
  begin
217
    JvCsvDataSetInput.FileName := OpenDialogCSV.FileName;
218
    JvCsvDataSetInput.Active := True;
219
    JvCsvDataSetInput.First;
220 3 avalancogn
(*
221
    // Ignorer la seconde ligne de titre (unit?s de mesure)
222
    JvCsvDataSetInput.Next;
223
*)
224 9 avalancogn
    if ClientDataSetInraAfz.RecordCount > 0 then
225
      if (MessageDlg('Faut-il ajouter (Yes) ou remplacer (No) ?', mtConfirmation, [mbYes, mbNo], 0) = mrNo)
226
      then
227
      begin
228
        ClientDataSetInraAfz.Close;
229
        ClientDataSetInraAfz.CreateDataSet;
230
      end;
231 1 avalancogn
    while not JvCsvDataSetInput.Eof do
232
    begin
233
      ClientDataSetInraAfz.Append;
234
      for i := 0 to ClientDataSetInraAfz.Fields.Count - 1 do
235 3 avalancogn
(*
236 1 avalancogn
        case i of
237
          0: // Id (AutoInc)
238
            ;
239
          1..3: // Name, Comment, Source (String)
240
            if (Length(JvCsvDataSetInput.Fields[i].AsString) > 0) then
241
              ClientDataSetInraAfz.Fields[i].AsString := JvCsvDataSetInput.Fields[i].AsString;
242
          4: // ClassId
243
            ClientDataSetInraAfz.Fields[i].AsInteger := JvCsvDataSetInput.Fields[i].AsInteger;
244
          5: ; // ClassName (fkLookup)
245
          6..10, 12, 14, 16..18: // %
246
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100;
247
          11, 13, 15: // Coef
248
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat;
249
          // Colonne 19 du CSV ignor?e (EB kCal) => d?callage des indices
250
          19: // EB MJ
251
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat;
252
          20..25: // %
253
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
254
          26, 27: // Coef
255
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat;
256
          28..29: // %
257
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
258
          30..49: // AA (g/kg MS) -> fraction de MAT
259
            if JvCsvDataSetInput.Fields[i+1].AsFloat = 0
260
            then
261
              ClientDataSetInraAfz.Fields[i].AsFloat := 0
262
            else
263
              ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / ClientDataSetInraAfzMAT.Value / 1000;
264
          50..69: // dAA (%)
265
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
266
          70..71: // Min (g/kg MS)
267
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 1000;
268
          72..73: // dP (%)
269
            ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
270
        end;
271 3 avalancogn
*)
272
        case i of
273
          0: // Nom (ENG)
274
            if (Length(JvCsvDataSetInput.Fields[i].AsString) > 0) then
275
              ClientDataSetInraAfz.Fields[i+1].AsString := JvCsvDataSetInput.Fields[i].AsString;
276
          1: ; // Nom (FRA) ignor?
277
          2: // Origine
278
            if (Length(JvCsvDataSetInput.Fields[i].AsString) > 0) then
279
              ClientDataSetInraAfz.Fields[i+1].AsString := JvCsvDataSetInput.Fields[i].AsString;
280
          3: ; // Classe (fkLookup)
281
          4: // ClassId
282
            ClientDataSetInraAfz.Fields[i].AsInteger := JvCsvDataSetInput.Fields[i].AsInteger;
283
          5..9, 11, 13, 15..17: // %
284
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100;
285
          10, 12, 14: // Coef
286
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat;
287
          18: // EB kcal
288
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat * CALORIE / 1000;
289
          19..24: // %
290
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100;
291
          25, 26: // Coef
292
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat;
293 9 avalancogn
          27..28: // dN (%)
294 3 avalancogn
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100;
295
          29..48: // AA (g/kg MS) -> fraction de MAT
296
            if JvCsvDataSetInput.Fields[i].AsFloat = 0
297
            then
298
              ClientDataSetInraAfz.Fields[i+1].AsFloat := 0
299
            else
300
              ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / ClientDataSetInraAfzMAT.Value / 1000;
301
          49..68: // dAA (%)
302
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100;
303
          69..70: // Min (g/kg MS)
304
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 1000;
305
          71..72: // dP (%)
306
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100;
307
          73..75: // Min (g/kg MS)
308
            ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 1000;
309
        end;
310 1 avalancogn
      JvCsvDataSetInput.Next;
311
    end;
312 9 avalancogn
    JvCsvDataSetInput.Close;
313 1 avalancogn
    ClientDataSetInraAfz.First;
314
  end;
315
end;
316
317
procedure TFormMain.SpeedButtonSaveClick(Sender: TObject);
318
begin
319
  FluxClair := TMemoryStream.Create;
320
  FluxCrypte := TMemoryStream.Create;
321
  try
322
    ClientDataSetInraAfz.MergeChangeLog;
323
    ClientDataSetInraAfz.SaveToStream(FluxClair, dfBinary);
324
    FluxClair.Position := 0;
325
    Cryptage.EncryptStream(FluxClair, FluxCrypte);
326
    FluxCrypte.Position := 0;
327
    FluxCrypte.SaveToFile(OutputFileName);
328
  finally
329
    FluxClair.Free;
330
    FluxCrypte.Free;
331
  end;
332 9 avalancogn
  MessageDlg('Sauvegarde effectu?e !', mtInformation, [mbOk], 0)
333 1 avalancogn
end;
334
335
end.