root / InraAfz / UnitMain.pas @ 3
Historique | Voir | Annoter | Télécharger (12,649 ko)
1 |
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 |
const
|
12 |
CALORIE = 4.184; // Conversion J -> cal |
13 |
|
14 |
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 |
ClientDataSetInraAfzNa: TFloatField; |
105 |
ClientDataSetInraAfzK: TFloatField; |
106 |
ClientDataSetInraAfzCl: TFloatField; |
107 |
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 |
if (MessageDlg('DataSet has been modified' + sLineBreak |
202 |
+ 'Do you want to exit without saving?', mtConfirmation, [mbYes, mbNo], 0) = mrYes) |
203 |
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 |
(*
|
221 |
// Ignorer la seconde ligne de titre (unit?s de mesure)
|
222 |
JvCsvDataSetInput.Next;
|
223 |
*)
|
224 |
while not JvCsvDataSetInput.Eof do |
225 |
begin
|
226 |
ClientDataSetInraAfz.Append; |
227 |
for i := 0 to ClientDataSetInraAfz.Fields.Count - 1 do |
228 |
(*
|
229 |
case i of
|
230 |
0: // Id (AutoInc)
|
231 |
;
|
232 |
1..3: // Name, Comment, Source (String)
|
233 |
if (Length(JvCsvDataSetInput.Fields[i].AsString) > 0) then
|
234 |
ClientDataSetInraAfz.Fields[i].AsString := JvCsvDataSetInput.Fields[i].AsString;
|
235 |
4: // ClassId
|
236 |
ClientDataSetInraAfz.Fields[i].AsInteger := JvCsvDataSetInput.Fields[i].AsInteger;
|
237 |
5: ; // ClassName (fkLookup)
|
238 |
6..10, 12, 14, 16..18: // %
|
239 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100;
|
240 |
11, 13, 15: // Coef
|
241 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat;
|
242 |
// Colonne 19 du CSV ignor?e (EB kCal) => d?callage des indices
|
243 |
19: // EB MJ
|
244 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat;
|
245 |
20..25: // %
|
246 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
|
247 |
26, 27: // Coef
|
248 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat;
|
249 |
28..29: // %
|
250 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
|
251 |
30..49: // AA (g/kg MS) -> fraction de MAT
|
252 |
if JvCsvDataSetInput.Fields[i+1].AsFloat = 0
|
253 |
then
|
254 |
ClientDataSetInraAfz.Fields[i].AsFloat := 0
|
255 |
else
|
256 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / ClientDataSetInraAfzMAT.Value / 1000;
|
257 |
50..69: // dAA (%)
|
258 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
|
259 |
70..71: // Min (g/kg MS)
|
260 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 1000;
|
261 |
72..73: // dP (%)
|
262 |
ClientDataSetInraAfz.Fields[i].AsFloat := JvCsvDataSetInput.Fields[i+1].AsFloat / 100;
|
263 |
end;
|
264 |
*)
|
265 |
case i of |
266 |
0: // Nom (ENG) |
267 |
if (Length(JvCsvDataSetInput.Fields[i].AsString) > 0) then |
268 |
ClientDataSetInraAfz.Fields[i+1].AsString := JvCsvDataSetInput.Fields[i].AsString;
|
269 |
1: ; // Nom (FRA) ignor? |
270 |
2: // Origine |
271 |
if (Length(JvCsvDataSetInput.Fields[i].AsString) > 0) then |
272 |
ClientDataSetInraAfz.Fields[i+1].AsString := JvCsvDataSetInput.Fields[i].AsString;
|
273 |
3: ; // Classe (fkLookup) |
274 |
4: // ClassId |
275 |
ClientDataSetInraAfz.Fields[i].AsInteger := JvCsvDataSetInput.Fields[i].AsInteger; |
276 |
5..9, 11, 13, 15..17: // % |
277 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100; |
278 |
10, 12, 14: // Coef |
279 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat;
|
280 |
18: // EB kcal |
281 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat * CALORIE / 1000; |
282 |
19..24: // % |
283 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100; |
284 |
25, 26: // Coef |
285 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat;
|
286 |
27..28: // % |
287 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100; |
288 |
29..48: // AA (g/kg MS) -> fraction de MAT |
289 |
if JvCsvDataSetInput.Fields[i].AsFloat = 0 |
290 |
then
|
291 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := 0 |
292 |
else
|
293 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / ClientDataSetInraAfzMAT.Value / 1000; |
294 |
49..68: // dAA (%) |
295 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100; |
296 |
69..70: // Min (g/kg MS) |
297 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 1000; |
298 |
71..72: // dP (%) |
299 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 100; |
300 |
73..75: // Min (g/kg MS) |
301 |
ClientDataSetInraAfz.Fields[i+1].AsFloat := JvCsvDataSetInput.Fields[i].AsFloat / 1000; |
302 |
end;
|
303 |
JvCsvDataSetInput.Next; |
304 |
end;
|
305 |
ClientDataSetInraAfz.First; |
306 |
end;
|
307 |
end;
|
308 |
|
309 |
procedure TFormMain.SpeedButtonSaveClick(Sender: TObject);
|
310 |
begin
|
311 |
FluxClair := TMemoryStream.Create; |
312 |
FluxCrypte := TMemoryStream.Create; |
313 |
try
|
314 |
ClientDataSetInraAfz.MergeChangeLog; |
315 |
ClientDataSetInraAfz.SaveToStream(FluxClair, dfBinary); |
316 |
FluxClair.Position := 0;
|
317 |
Cryptage.EncryptStream(FluxClair, FluxCrypte); |
318 |
FluxCrypte.Position := 0;
|
319 |
FluxCrypte.SaveToFile(OutputFileName); |
320 |
finally
|
321 |
FluxClair.Free; |
322 |
FluxCrypte.Free; |
323 |
end;
|
324 |
end;
|
325 |
|
326 |
end.
|