/
DataInterface.cs
895 lines (845 loc) · 29.2 KB
/
DataInterface.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Windows;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;
namespace EM
{
/// <summary>
/// Summary description for DataInterface.
/// </summary>
///
public class SortByBundleNum : System.Collections.IComparer
{
public virtual int Compare(object a, object b)
{
EMDataSet.ContBundleTblRow rowA = (EMDataSet.ContBundleTblRow)a;
EMDataSet.ContBundleTblRow rowB = (EMDataSet.ContBundleTblRow)b;
if (rowA.BundleSeqNumber < rowB.BundleSeqNumber)
return -1;
if (rowA.BundleSeqNumber == rowB.BundleSeqNumber)
return 0;
return 1;
}
}
public struct TaggedItem
{
public int key;
public string title;
public TaggedItem(int keyIn,string titleIn)
{
key = keyIn;
title = titleIn;
}
public override string ToString()
{
return title;
}
}
public struct TaggedItemStr
{
public string key;
public string title;
public TaggedItemStr(string keyIn,string titleIn)
{
key = keyIn;
title = titleIn;
}
public override string ToString()
{
return title;
}
}
public class DataInterface
{
public static bool IsContainerItemDone(EMDataSet.ContBundleTblRow row)
{
string reason;
return IsContainerItemDone(row, out reason);
}
public static bool IsContainerItemDone(EMDataSet.ContBundleTblRow row,out string reason)
{
reason = "Could not close container because bundle " + row.BundleSeqNumber +
" does not have a terminal or bill of lading set";
if (!row.IsProofOfDeliveryNull() && row.ProofOfDelivery != "")
return true;
if (row.IsPickupTerminalNull())
return false;
if (row.PickupTerminal == "")
return false;
return true;
}
public class POCompletedNodeTag
{
public int poid;
public int contID;
public POCompletedNodeTag(int poidIn,int contIDIn)
{
contID = contIDIn;
poid = poidIn;
}
}
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int StrCmpLogicalW(string strA, string strB);
class TreeNodeSorter : IComparer
{
public int Compare(object x, object y)
{
TreeNode left = (TreeNode)x;
TreeNode right = (TreeNode)y;
return StrCmpLogicalW(left.Text, right.Text);
}
}
public static void SortOnString(TreeNode node)
{
ArrayList listOfChildNodes = new ArrayList();
foreach (TreeNode child in node.Nodes)
{
SortOnString(child);
listOfChildNodes.Add(child);
}
listOfChildNodes.Sort(new TreeNodeSorter());
node.Nodes.Clear();
foreach (TreeNode child in listOfChildNodes)
{
node.Nodes.Add(child);
}
}
public delegate bool IsContainerBundleCompleted(EMDataSet.ContBundleTblRow bundleRow);
public static bool IsBundlePickedUp(EMDataSet.ContBundleTblRow bundleRow)
{
EMDataSet.ContainerTblRow containerRow = bundleRow.ContainerTblRow;
if (containerRow.ApplyClosingToEntireContainer != 0)
{
return DataInterface.IsCompleted(containerRow);
}
return DataInterface.IsContainerItemDone(bundleRow);
}
public static bool IsPOCompleted(EMDataSet.POHeaderTblRow headerRow,
ref TreeNode headerNode,IsContainerBundleCompleted isBundleCompleted)
{
return IsPOCompleted(headerRow,ref headerNode,isBundleCompleted,90);
}
public static bool IsPOCompleted(EMDataSet.POHeaderTblRow headerRow,
ref TreeNode headerNode,IsContainerBundleCompleted isBundleCompleted,
int percentageRequired)
{
EMDataSet emDataSet = (EMDataSet)headerRow.Table.DataSet;
bool isMetric = DataInterface.IsMetric(headerRow);
int completed = 0;
int total = 0;
EMDataSet.BalanceRptPORow newBalancePORow = emDataSet.BalanceRptPO.NewBalanceRptPORow();
newBalancePORow.POID = headerRow.POID;
newBalancePORow.PONumber = headerRow.PONumber;
foreach (EMDataSet.POItemTblRow itemRow in headerRow.GetPOItemTblRows())
{
EMDataSet.BalanceRtpPOItemRow newBalancePOItemRow = emDataSet.BalanceRtpPOItem.NewBalanceRtpPOItemRow();
newBalancePOItemRow.POID = headerRow.POID;
newBalancePOItemRow.POItemNumber = itemRow.POItemNumber;
newBalancePOItemRow["SizeOfITem"] = itemRow["SizeOfItem"];
newBalancePOItemRow.Grade = HelperFunctions.GetItemName(itemRow);
TreeNode poItemNode = new TreeNode();
if (itemRow.IsQtyNull() || itemRow.Qty == 0)
continue;
++total;
decimal totalQty = itemRow.Qty;
decimal totalQtyInClosedContainers = 0;
string select = "POItemNumber = " + itemRow.POItemNumber.ToString();
EMDataSet.ContBundleTblRow[] bundleRows = (EMDataSet.ContBundleTblRow[])
emDataSet.ContBundleTbl.Select(select);
ArrayList listOfContIDs = new ArrayList();
foreach (EMDataSet.ContBundleTblRow bundleRow in bundleRows)
{
listOfContIDs.Add(bundleRow.ContID);
}
AdapterHelper.Unique(ref listOfContIDs);
foreach (int contID in listOfContIDs)
{
EMDataSet.ContainerTblRow contRow =
emDataSet.ContainerTbl.FindByContID(contID);
decimal totalCompletedInContainer = 0;
TreeNode containerNode = new TreeNode();
foreach (EMDataSet.ContBundleTblRow bundleRow in bundleRows)
{
if (bundleRow.ContID != contID)
continue;
decimal bundleQty = 0;
if (isMetric && !bundleRow.IsMetricShipQtyNull())
bundleQty = bundleRow.MetricShipQty;
else
if (!bundleRow.IsEnglishShipQtyNull())
bundleQty = bundleRow.EnglishShipQty;
decimal percentage = 100*bundleQty / totalQty;
string bundleDesc = "Bundle " + bundleRow.BundleSeqNumber +
": " + bundleQty.ToString("N0") +
": " + percentage.ToString("N2") + "%";
if (isBundleCompleted(bundleRow))
{
if (isMetric && !bundleRow.IsMetricShipQtyNull())
{
totalCompletedInContainer += bundleRow.MetricShipQty;
}
if (!isMetric && !bundleRow.IsEnglishShipQtyNull())
{
totalCompletedInContainer += bundleRow.EnglishShipQty;
}
bundleDesc += " (Completed)";
}
else
bundleDesc += " (Not completed)";
TreeNode bundleNode = new TreeNode(bundleDesc);
bundleNode.Tag = new POCompletedNodeTag(headerRow.POID,contID);
containerNode.Nodes.Add(bundleNode);
}
totalQtyInClosedContainers += totalCompletedInContainer;
decimal percentageInContainer = 100*
totalCompletedInContainer / totalQty;
containerNode.Text = "Container: " + contRow.ContNumber +
" " + totalCompletedInContainer.ToString("N0")
+ " " + percentageInContainer.ToString("N2") + "%";
containerNode.Tag = new POCompletedNodeTag(headerRow.POID, contID);
poItemNode.Nodes.Add(containerNode);
}
decimal itemPercentage = totalQtyInClosedContainers / totalQty * 100;
poItemNode.Text = "Row:" + itemRow.SeqNumber.ToString() + " " +
HelperFunctions.GetItemName(itemRow) +
" " + totalQtyInClosedContainers.ToString("N0") + " out of " +
itemRow.Qty.ToString("N0") +
" " + itemPercentage.ToString("N2") + "%";
poItemNode.Tag = new POCompletedNodeTag(headerRow.POID, -1);
if (itemPercentage >= percentageRequired)
{
completed++;
newBalancePOItemRow.FinishedItem = 1;
}
newBalancePOItemRow.ClosedPercent = itemPercentage;
newBalancePOItemRow.ClosedCount = totalQtyInClosedContainers;
newBalancePOItemRow.TotalCount = totalQty;
newBalancePOItemRow["UM"] = itemRow["UM"];
emDataSet.BalanceRtpPOItem.AddBalanceRtpPOItemRow(newBalancePOItemRow);
headerNode.Tag = new POCompletedNodeTag(headerRow.POID, -1);
headerNode.Nodes.Add(poItemNode);
}
headerNode.Text = headerRow.PONumber;
if (completed != total)
{
headerNode.Text += "(" + completed.ToString() + "/" + total.ToString();
}
newBalancePORow.ClosedCount = completed;
newBalancePORow.TotalCount = total;
if (completed == total)
newBalancePORow.FinishedPO = 1;
emDataSet.BalanceRptPO.AddBalanceRptPORow(newBalancePORow);
return completed==total;
}
static public bool IsCompleted(EMDataSet.ContainerTblRow row)
{
string reason;
return IsCompleted(row,out reason);
}
static public bool IsCompleted(EMDataSet.ContainerTblRow row, out string reason)
{
reason = null;
if (!row.IsApplyClosingToEntireContainerNull() &&
row.ApplyClosingToEntireContainer!=0)
{
if (!row.IsContainerPickupTerminalNull() &&
row.ContainerPickupTerminal != "")
return true;
if (!row.IsContainerProofOfDeliveryNull() &&
row.ContainerProofOfDelivery != "")
return true;
reason = "You must enter either a terminal or a bill of lading before closing.";
return false;
}
foreach (EMDataSet.ContBundleTblRow bundleRow in row.GetContBundleTblRows())
{
// 2 options: either, each row has a bil of lading item
// or it has a pick up date and terminal
if (IsContainerItemDone(bundleRow,out reason))
continue;
return false;
}
if (row.GetContBundleTblRows().Length == 0)
return false;
return true;
}
static public string TranslateToConstraint(ArrayList listOfConstraints,
ArrayList orConstraints)
{
if (listOfConstraints.Count == 0 && orConstraints.Count == 0)
return "";
string query = " WHERE (";
for (int i=0;i<listOfConstraints.Count;i++)
{
string constraint = (string)listOfConstraints[i];
query += " " + constraint + " ";
if (i != listOfConstraints.Count -1)
{
query += " AND ";
}
}
if (listOfConstraints.Count != 0)
{
query += ") ";
if (orConstraints.Count != 0)
query += "AND ";
}
if (orConstraints.Count != 0)
query += "( ";
for (int i = 0; i < orConstraints.Count; i++)
{
string constraint = (string)orConstraints[i];
query += " " + constraint + " ";
if (i != orConstraints.Count - 1)
{
query += " OR ";
}
}
if (orConstraints.Count != 0)
query += ")";
return query;
}
static public string TranslateToConstraint(ArrayList listOfConstraints)
{
return TranslateToConstraint(listOfConstraints, new ArrayList());
}
// private const decimal conversion = 0.45359237M;
private const decimal conversion = 0.4536M;// GSB's conversion rate
public static decimal ConvertToKG(decimal lbs)
{
return lbs * conversion;
}
public static decimal ConvertToLbs(decimal kg)
{
return kg / conversion;
}
static Control[] GetAllControls(System.Windows.Forms.Form f)
{
ArrayList allControls = new ArrayList();
Queue queue = new Queue();
queue.Enqueue(f.Controls);
while (queue.Count > 0)
{
Control.ControlCollection controls = (Control.ControlCollection)
queue.Dequeue();
if (controls == null || controls.Count == 0) continue;
foreach (Control control in controls)
{
allControls.Add(control);
queue.Enqueue(control.Controls);
}
}
return (Control[])allControls.ToArray(typeof(Control));
}
public static int GetNextKeyNumbers(string tableName,int countRequested)
{
string query = "SELECT * FROM tblKeyNumbers WHERE " +
"tblName = '" + tableName + "'";
OleDbConnection emConnection = AdapterHelper.Connection;
using (new OpenConnection(IsWrite.Yes,emConnection))
{
OleDbDataAdapter adaptor =
new OleDbDataAdapter(query,emConnection);
adaptor.SelectCommand.Transaction = emConnection.BeginTransaction();
EMDataSet dataSet = new EMDataSet();
EMDataSet.KeyNumberTbleDataTable table =
dataSet.KeyNumberTble;
adaptor.Fill(table);
EMDataSet.KeyNumberTbleRow row = (EMDataSet.KeyNumberTbleRow)table.Rows[0];
int number = row.nextKeyNumber+1;
row.nextKeyNumber += countRequested;
new OleDbCommandBuilder(adaptor);
adaptor.Update(table);
adaptor.SelectCommand.Transaction.Commit();
return number;
}
}
public static int GetNextKeyNumber(string tableName)
{
return GetNextKeyNumbers(tableName,1);
}
public static void ApplyTable(DataTable table,string sqlCommand)
{
string strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=m:\\EM_Prog_2002.mdb";
using (OleDbConnection conn = new OleDbConnection(strConnect))
{
using (OleDbDataAdapter adaptor = new OleDbDataAdapter(sqlCommand,conn))
{
new System.Data.OleDb.OleDbCommandBuilder(adaptor);
adaptor.Update(table);
}
}
}
public static bool IsRowAlive(DataRow row)
{
if (row.RowState == DataRowState.Detached)
return false;
if (row.RowState == DataRowState.Deleted)
return false;
return true;
}
public static string GetExternalPO(int internalPO,EMDataSet dataSet)
{
EMDataSet.POHeaderTblRow[] row = (EMDataSet.POHeaderTblRow[])
dataSet.POHeaderTbl.Select("POID ="+internalPO.ToString());
if (row.Length != 1)
throw new Exception("Bad");
return row[0].PONumber;
}
public static DataTable GetTable(string sqlCommand)
{
string strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=m:\\EM_Prog_2002.mdb";
using (OleDbConnection conn = new OleDbConnection(strConnect))
{
using (OleDbDataAdapter adaptor = new OleDbDataAdapter(sqlCommand,conn))
{
DataTable table = new DataTable();
adaptor.Fill(table);
return table;
}
}
}
public static void UpdateTable(OleDbDataAdapter adapter,
System.Data.DataTable changedTable,DataRowState typeOfChanges)
{
DataTable onlyChanges = changedTable.GetChanges(typeOfChanges);
if (onlyChanges == null)
return;
adapter.Update(onlyChanges);
}
public static void UpdateTableDelete(OleDbDataAdapter adapter,System.Data.DataTable changedTable)
{
UpdateTable(adapter,changedTable,DataRowState.Deleted);
}
public static void UpdateTableAdd(OleDbDataAdapter adapter,System.Data.DataTable changedTable)
{
UpdateTable(adapter,changedTable,DataRowState.Modified | DataRowState.Added);
}
public static void UpdateTable(OleDbDataAdapter adapter,DataTable changedTable)
{
UpdateTableDelete(adapter,changedTable);
UpdateTableAdd(adapter,changedTable);
}
public static void UpdateComboBox(DataView table, string keyField,string fieldName,ComboBox box,
DataRow currentRow)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
int currentPosition = 0;
int selectedPosition = -1;
foreach (DataRowView row in table)
{
int key = (int)row[keyField];
string title = (string)row[fieldName];
list.Add(new TaggedItem(key,title));
if (currentRow != null)
if (!currentRow.IsNull(keyField))
if (key == (int)currentRow[keyField])
selectedPosition = currentPosition;
++currentPosition;
}
box.Items.Clear();
box.Items.AddRange(list.ToArray());
if (selectedPosition != -1)
box.SelectedIndex = selectedPosition;
}
public static void UpdateComboBox(DataView table, string fieldName,ComboBox box)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
foreach (DataRowView row in table)
{
list.Add(row[fieldName]);
}
box.Items.Clear();
box.Items.AddRange(list.ToArray());
}
public static void UpdateComboBox(DataTable table,string fieldName,ComboBox box)
{
UpdateComboBox(ToView(table),fieldName,box);
}
public static void UpdateListBox(DataView table, string fieldName,ListBox box)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
foreach (DataRowView row in table)
{
list.Add(row[fieldName]);
}
box.Items.Clear();
box.Items.AddRange(list.ToArray());
if (box.Items.Count != 0)
box.SelectedIndex = 0;
}
public static void UpdateListBox(DataTable table, string fieldName,ListBox box)
{
UpdateListBox(ToView(table),fieldName,box);
}
public static void SelectComboBox(ComboBox box,string name)
{
for (int i=0;i<box.Items.Count;i++)
{
string item = (string)box.Items[i];
if (item == name)
{
box.SelectedIndex = i;
return;
}
}
}
public static void SelectComboBox(ComboBox box, int key)
{
for (int i=0;i<box.Items.Count;i++)
{
TaggedItem item = (TaggedItem)box.Items[i];
if (item.key == key)
{
box.SelectedIndex = i;
return;
}
}
}
public static void InitializeAdapterWithParameter(OleDbDataAdapter adapter,string parameterName)
{
adapter.SelectCommand.Parameters.Add("@ + parameterName",System.Data.OleDb.OleDbType.Integer);
if (adapter.UpdateCommand!=null)
adapter.UpdateCommand.Parameters.Add("@ + parameterName",System.Data.OleDb.OleDbType.Integer);
if (adapter.DeleteCommand!=null)
adapter.DeleteCommand.Parameters.Add("@ + parameterName",System.Data.OleDb.OleDbType.Integer);
}
public static void InitializeAdapterWithStringParameter(OleDbDataAdapter adapter,string parameterName)
{
adapter.SelectCommand.Parameters.Add("@ + parameterName",System.Data.OleDb.OleDbType.VarChar);
if (adapter.UpdateCommand!=null)
adapter.UpdateCommand.Parameters.Add("@ + parameterName",System.Data.OleDb.OleDbType.VarChar);
if (adapter.DeleteCommand!=null)
adapter.DeleteCommand.Parameters.Add("@ + parameterName",System.Data.OleDb.OleDbType.VarChar);
}
public static void FillAdapterWithParameter(OleDbDataAdapter adapter,object val)
{
FillAdapterWithParameters(adapter, val);
}
public static void FillAdapterWithParameters(OleDbDataAdapter adapter, params object[] values)
{
for (int i =0;i<values.Length;i++)
{
adapter.SelectCommand.Parameters[i].Value = values[i];
if (adapter.UpdateCommand != null)
adapter.UpdateCommand.Parameters[i].Value = values[i];
if (adapter.DeleteCommand != null)
adapter.DeleteCommand.Parameters[i].Value = values[i];
}
}
public static void FillAdapterWithStringParameter(OleDbDataAdapter adapter, string val)
{
FillAdapterWithParameters(adapter, val);
}
public static DataView ToView(DataTable table)
{
return table.DefaultView;
}
public static void UpdateListCtrl(ListView ctrl,DataView view,string key,string first,params string[] fields)
{
ctrl.Items.Clear();
foreach (DataRowView rowView in view)
{
DataRow row = rowView.Row;
ListViewItem item = new ListViewItem();
item.Tag = key;
item.Text = first;
item.SubItems.AddRange(fields);
ctrl.Items.Add(item);
}
}
public static string GetMetricString(bool isMetric)
{
if (isMetric)
return "kg";
else
return "lbs";
}
public static void ConformMetric(EMDataSet.POItemTblDataTable table,
EMDataSet.POItemTblRow row)
{
row.UM = GetMetricString(IsMetric(table));
}
public static bool DefaultMetric = false;
public static bool IsMetric(string um)
{
if (um == "kg")
return true;
return false;
}
public static bool IsMetric(EMDataSet.POItemTblRow row)
{
if (!row.IsUMNull())
{
return IsMetric(row.UM);
}
return DefaultMetric;
}
public static bool IsMetric(EMDataSet.POItemTblDataTable table)
{
foreach (EMDataSet.POItemTblRow row in table.Rows)
{
if (!DataInterface.IsRowAlive(row))
continue;
if (!row.IsUMNull())
return IsMetric(row);
}
return DefaultMetric;
}
public static bool IsMetric(EMDataSet.POHeaderTblRow headerRow)
{
foreach (EMDataSet.POItemTblRow row in headerRow.GetPOItemTblRows())
{
if (!DataInterface.IsRowAlive(row))
continue;
if (!row.IsUMNull())
return IsMetric(row);
}
return DefaultMetric;
}
public static int FindIndex(DataTable table,string fieldName,int fieldID)
{
DataColumn column = table.Columns[fieldName];
for (int i=0;i<table.Rows.Count;i++)
{
if ((int)(table.Rows[i][column]) == fieldID)
return i;
}
return 0;
}
public static int GetIndex(DataTable table, string fieldName,int keyValue)
{
for (int i=0;i<table.Rows.Count;i++)
{
int rowValue = (int)table.Rows[i][fieldName];
if (keyValue == rowValue)
return i;
}
return 0;
}
static public string ExpandQuotes(string query)
{
System.Text.StringBuilder b = new System.Text.StringBuilder();
foreach (char c in query)
{
if (c == '\'')
b.Append("''");
else
b.Append(c);
}
return b.ToString();
}
public static int GetCompID(EMDataSet dataSet,string compName)
{
EMDataSet.CompanyTblDataTable table = dataSet.CompanyTbl;
string query = "CompName = '" + ExpandQuotes(compName) + "'";
DataRow[] rows = table.Select(query);
if (rows.Length == 0)
return -1;
EMDataSet.CompanyTblRow row = (EMDataSet.CompanyTblRow)rows[0];
return row.CompID;
}
static Random m_randomizer = new Random();
static void Is50()
{
return;
/*int number = m_randomizer.Next(2);
if (number == 1)
throw new FakeException("Fake update in progress");*/
}
public class FakeException : Exception
{
public FakeException(string message) :base(message)
{
}
}
public class NoMappedDriveException : Exception
{
public NoMappedDriveException(string message):base(message){}
}
static void CheckMappedDrive()
{
try
{
using (new FileStream("m:\\test",FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite)){}
}
catch(Exception ex)
{
throw new NoMappedDriveException("The M: drive wasn't properly mapped:\n" + ex.Message);
}
}
static FileStream CreateSharedFileImpl(string filename, FileShare shareType)
{
try
{
int i=0;
while (true)
{
++i;
try
{
CheckMappedDrive();
Is50();
FileStream str = new FileStream("m:\\" + filename,FileMode.OpenOrCreate,
FileAccess.ReadWrite, shareType);
return str;
}
catch(Exception)
{
if (i==10)
throw;
System.Threading.Thread.Sleep(100);
}
}
}
catch (NoMappedDriveException)
{
throw;
}
catch (FakeException)
{
throw;
}
catch(Exception e)
{
throw new Exception("Update in progress. Try again in a moment.",e);
}
}
public static FileStream CreateLockFile(string filename)
{
return CreateSharedFileImpl(filename,FileShare.None);
}
public static FileStream CreateSharedLockFile(string filename)
{
return CreateSharedFileImpl(filename,FileShare.ReadWrite);
}
public static int GetKeyFromField(OleDbConnection connection,
string tableName,
string keyName,
string fieldName,
string fieldContents)
{
using (new OpenConnection(IsWrite.No,connection))
{
string keyColumn = keyName;
string query = "SELECT " + keyColumn + " FROM " +
tableName +" WHERE " + fieldName + "=" + fieldContents;
OleDbCommand command = new OleDbCommand(query,connection);
using (OleDbDataReader reader = command.ExecuteReader())
{
if (!reader.HasRows)
return -1;
reader.Read();
return reader.GetInt32(0);
}
}
}
public static void CheckForChanges(
string keyField,
string descriptionTitle,
string descriptionField,
string seqNumberField,
DataTable databaseTable,
DataTable currentTable)
{
ArrayList listOfCurrentChildren = new ArrayList();
int numberOfCurrentRows = 0;
foreach (DataRow row in currentTable.Rows)
{
if (row.RowState != DataRowState.Added)
numberOfCurrentRows++;
}
if (numberOfCurrentRows != databaseTable.Rows.Count)
{
throw new Exception("Unable to save. The number of items/bundles " +
"is out of date. This is a result of another user " +
"making a change while you were editing this record.");
}
foreach (DataRow databaseRow in databaseTable.Rows)
{
object keyValue = databaseRow[keyField];
DataRow currentRow = null;
foreach (DataRow findRow in currentTable.Rows)
{
if (findRow[keyField,DataRowVersion.Original].Equals(keyValue))
{
currentRow = findRow;
break;
}
}
if (currentRow == null)
{
System.Diagnostics.Debug.Assert(false);
throw new Exception("Unable to save. A row was added by some other user." +
"This is the result of another "+
"user making a change while you were editing this record.\n"+
"Notify Johan of this error. It doesn't seem possible.");
}
// Compare the seqnumbers
int seq1 = (int)
databaseRow[seqNumberField,DataRowVersion.Original];
int seq2 = (int)
currentRow[seqNumberField,DataRowVersion.Original];
if (seq1 != seq2)
throw new Exception("Unable to save. The positions of the items/bundes has " +
"out of date with the database. This is the result of another user " +
"making a change while you were editing this record.");
foreach (DataColumn column in databaseTable.Columns)
{
string columnName = column.ColumnName;
object database = databaseRow[columnName];
object current = currentRow[columnName,DataRowVersion.Original];
if (database.GetType() != current.GetType() ||
!database.Equals(current))
{
string message = "Unable to save. At least some of the data in the list " +
"of items is out of date with the latest in the " +
"database. You must cancel your change and re-save. This is a " +
"result of another user making a change while you were editing " +
"this record.\n";
message += descriptionTitle + ":";
if (!currentRow.IsNull(descriptionField))
{
message += currentRow[descriptionField];
}
message += "\n";
message += "Field Name:" + columnName + "\n";
message += "Database Value:";
if (!databaseRow.IsNull(columnName))
{
message += databaseRow[columnName].ToString();
}
message += "\n";
message += "Your original value:";
object val = currentRow[columnName,DataRowVersion.Original];
if (val.GetType() != typeof(DBNull))
{
message += val.ToString();
}
message += "\n";
throw new Exception(message);
}
}
}
}
public static void MakeSureRefreshGoesThrough(BaseEMForm form)
{
while (true)
{
try
{
form.Refresh();
return;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}