프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ eb44d82c

이력 | 보기 | 이력해설 | 다운로드 (64.1 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Data;
4
using System.Data.Common;
5
using System.Data.SQLite;
6
using System.Linq;
7
using System.Text.RegularExpressions;
8

    
9
namespace ID2PSN
10
{
11
    public class DB
12
    {
13
        const string PSN_TOPOLOGY_RULE = "T_PSN_TOPOLOGY_RULE";
14
        const string PSN_HEADER_SETTING = "T_PSN_HEADER_SETTING";
15
        const string PSN_VENTDRAIN_SETTING = "T_PSN_VENTDRAIN_SETTING";
16
        const string PSN_VIEW = "T_PSN_VIEW";
17
        const string PSN_TRANSFORMKEYWORD_SETTING = "T_PSN_TRANSFORMKEYWORD_SETTING"; 
18

    
19
        const string PSN_PATHITEMS = "SPPIDPathItem";
20
        const string PSN_SEQUENCEDATA = "SPPIDSequenceData";
21
        const string PSN_PIPESYSTEMNETWORK = "SPPIDPipeSystemNetwork";
22
        const string PSN_EQUIPMENT = "SPPIDEquipment";
23
        const string PSN_NOZZLE = "SPPIDNozzle";
24
        const string PSN_FLUIDCODE = "SPPIDFluidCode";
25
        const string PSN_PIPINGMATLCLASS = "SPPIDPipingMatlClass";
26
        const string PSN_TOPOLOGYSET = "SPPIDTopologySet";
27

    
28
        /// <summary>
29
        ///  ID2 Project.db 데이터를 가져온다. 
30
        ///  DB 접속 정보 및 DBType (Sqlite, Mssql) 정보를 가져옴
31
        ///  - JY
32
        /// </summary>
33
        /// <returns></returns>
34
        public static DataTable GetProject()
35
        {
36
            DataTable dt = new DataTable();
37
            ID2Info id2Info = ID2Info.GetInstance();
38
            try
39
            {
40
                using (SQLiteConnection connection = new SQLiteConnection(@"Data Source = C:\ProgramData\Digital PID\Project.db", true))
41
                {
42
                    connection.Open();
43
                    if (connection.State.Equals(ConnectionState.Open))
44
                    {
45
                        using (SQLiteCommand cmd = connection.CreateCommand())
46
                        {
47
                            cmd.CommandText = "SELECT p.[Id], p.Name, p.Path, s.DBTypes_UID, s.Host, s.[User], s.[Password], s.FilePath FROM DBSettings s, Projects p WHERE s.Projects_UID = p.[Id]";
48
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
49
                                dt.Load(dr);
50
                        }
51

    
52
                    }
53
                    connection.Close();
54
                }
55
            }
56
            catch (Exception ex)
57
            {
58
                System.Windows.Forms.MessageBox.Show(ex.Message);
59
            }
60

    
61
            dt.AcceptChanges();
62
            dt.DefaultView.Sort = "Name";
63
            dt = dt.DefaultView.ToTable();
64

    
65
            return dt;
66
        }
67

    
68
        /// <summary>
69
        ///  SQLite에 초기 DB 생성
70
        ///  - JY
71
        /// </summary>
72
        /// <returns></returns>
73
        public static bool ConnTestAndCreateTable()
74
        {
75
            bool result = false;
76
            ID2Info id2Info = ID2Info.GetInstance();
77

    
78
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
79
            {
80
                try
81
                {
82
                    var names = connection.GetTableNames();
83
                    var matched = names.FirstOrDefault(param => param == PSN_HEADER_SETTING);
84
                    if (matched == null)
85
                    {
86
                        var query = $"CREATE TABLE {PSN_HEADER_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
87
                        using (var cmd = connection.GetSqlStringCommand(query))
88
                        {
89
                            cmd.ExecuteNonQuery();
90
                        }
91
                    }
92

    
93
                    matched = names.FirstOrDefault(param => param == PSN_VENTDRAIN_SETTING);
94
                    if (matched == null)
95
                    {
96
                        var query = $"CREATE TABLE {PSN_VENTDRAIN_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
97
                        using (var cmd = connection.GetSqlStringCommand(query))
98
                        {
99
                            cmd.ExecuteNonQuery();
100
                        }
101
                    }
102

    
103
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGY_RULE);
104
                    if (matched == null)
105
                    {
106
                        var query = $"CREATE TABLE {PSN_TOPOLOGY_RULE} (UID TEXT)";
107
                        using (var cmd = connection.GetSqlStringCommand(query))
108
                        {
109
                            cmd.ExecuteNonQuery();
110
                        }
111

    
112
                        DataTable topologyRule = new DataTable();
113
                        topologyRule.Columns.Add("NAME", typeof(string));
114

    
115
                        topologyRule.Rows.Add("FluidCode");
116
                        topologyRule.Rows.Add("-");
117
                        topologyRule.Rows.Add("PipingMaterialsClass");
118
                        topologyRule.Rows.Add("-");
119
                        topologyRule.Rows.Add("Tag Seq No");
120

    
121
                        SaveTopologyRule(topologyRule);
122
                    }
123

    
124

    
125
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
126
                    if (matched == null)
127
                    {
128
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(50), Type NVARCHAR(20), OrderNumber NVARCHAR(20), Pipeline_OID NVARCHAR(125), FROM_DATA NVARCHAR(255), " +
129
                            "TO_DATA NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
130
                            "IncludingVirtualData NVARCHAR(10),  PSNAccuracy REAL)";
131
                        using (var cmd = connection.GetSqlStringCommand(query))
132
                        {
133
                            cmd.ExecuteNonQuery();
134
                        }
135
                    }
136

    
137
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
138
                    if (matched == null)
139
                    {
140
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(50), ITEMTAG NVARCHAR(50), XCOORDS REAL, YCOORDS REAL)";
141
                        using (var cmd = connection.GetSqlStringCommand(query))
142
                        {
143
                            cmd.ExecuteNonQuery();
144
                        }
145
                    }
146

    
147
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
148
                    if (matched == null)
149
                    {
150
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(50), ITEMTAG NVARCHAR(255), XCOORDS REAL, YCOORDS REAL, Equipment_OID NVARCHAR(255), " +
151
                            "FLUID NVARCHAR(255), NPD NVARCHAR(255), ROTATION REAL, FlowDirection NVARCHAR(255))";
152
                        using (var cmd = connection.GetSqlStringCommand(query))
153
                        {
154
                            cmd.ExecuteNonQuery();
155
                        }
156
                    }
157

    
158
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
159
                    if (matched == null)
160
                    {
161
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
162
                        using (var cmd = connection.GetSqlStringCommand(query))
163
                        {
164
                            cmd.ExecuteNonQuery();
165
                        }
166
                    }
167

    
168

    
169
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
170
                    if (matched == null)
171
                    {
172
                        var query = $"CREATE TABLE {PSN_PIPINGMATLCLASS} (UID NVARCHAR(50), Priority INTEGER, Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
173
                        using (var cmd = connection.GetSqlStringCommand(query))
174
                        {
175
                            cmd.ExecuteNonQuery();
176
                        }
177
                    }
178

    
179
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
180
                    if (matched == null)
181
                    {
182
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(125), SERIALNUMBER NVARCHAR(255), PathItem_OID NVARCHAR(255), TopologySet_OID_Key NVARCHAR(255))";
183
                        using (var cmd = connection.GetSqlStringCommand(query))
184
                        {
185
                            cmd.ExecuteNonQuery();
186
                        }
187
                    }
188

    
189
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
190
                    if (matched == null)
191
                    {
192
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(50), SequenceData_OID NVARCHAR(125), " +
193
                            "TopologySet_OID NVARCHAR(125), BranchTopologySet_OID NVARCHAR(125), PipeLine_OID NVARCHAR(125), ITEMNAME NVARCHAR(50), ITEMTAG NVARCHAR(125), " +
194
                            "Class NVARCHAR(80), SubClass NVARCHAR(80), TYPE NVARCHAR(80), PIDNAME NVARCHAR(10), NPD NVARCHAR(20), PipeSystemNetwork_OID NVARCHAR(20), PipeRun_OID NVARCHAR(255), " +
195
                            "ViewPipeSystemNetwork_OID NVARCHAR(255))";
196
                        using (var cmd = connection.GetSqlStringCommand(query))
197
                        {
198
                            cmd.ExecuteNonQuery();
199
                        }
200
                    }
201
                    
202
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
203
                    if (matched == null)
204
                    {
205
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
206
                        using (var cmd = connection.GetSqlStringCommand(query))
207
                        {
208
                            cmd.ExecuteNonQuery();
209
                        }
210
                    }
211

    
212
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
213
                    if (matched == null)
214
                    {
215
                        var query = $"CREATE TABLE { PSN_TOPOLOGYSET} (OID NVARCHAR(125), Type NVARCHAR(30), SubType NVARCHAR(30), HeadItemTag NVARCHAR(50), TailItemTag NVARCHAR(50), HeadItemSPID NVARCHAR(50), TailItemSPID NVARCHAR(50))";
216
                        using (var cmd = connection.GetSqlStringCommand(query))
217
                        {
218
                            cmd.ExecuteNonQuery();
219
                        }
220
                    }
221
                   
222
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
223
                    if (matched == null)
224
                    {
225
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
226
                        using (var cmd = connection.GetSqlStringCommand(query))
227
                        {
228
                            cmd.ExecuteNonQuery();
229
                        }
230
                    }
231

    
232
                    result = true;
233
                }
234
                catch (Exception ex)
235
                {
236
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
237
                }
238
            }
239

    
240
            return result;
241
        }
242

    
243
        // ID2 DB 데이터
244
        /// <summary>
245
        /// ID2 데이타베이스에서 OPC 데이터를 조회
246
        /// </summary>
247
        /// <returns></returns>
248
        public static DataTable SelectOPCRelations()
249
        {
250
            DataTable dt = null;
251
            ID2Info id2Info = ID2Info.GetInstance();
252

    
253
            using (IAbstractDatabase connection = id2Info.CreateConnection())
254
            {
255
                try
256
                {
257
                    var query = "SELECT * FROM OPCRelations;";
258
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
259
                    {
260
                        dt = ds.Tables[0].Copy();
261
                    }
262
                }
263
                catch (Exception ex)
264
                {
265
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
266
                }
267
            }
268

    
269
            return dt;
270
        }
271

    
272
        /// <summary>
273
        /// ID2 데이타베이스에서 도면 데이터를 조회
274
        /// </summary>
275
        /// <returns></returns>
276
        public static DataTable SelectDrawings()
277
        {
278
            DataTable dt = null;
279
            ID2Info id2Info = ID2Info.GetInstance();
280

    
281
            using (IAbstractDatabase connection = id2Info.CreateConnection())
282
            {
283
                try
284
                {
285
                    var query = "SELECT * FROM Drawings";
286
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
287
                    {
288
                        dt = ds.Tables[0].Copy();
289
                    }
290
                }
291
                catch (Exception ex)
292
                {
293
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
294
                }
295
            }
296

    
297
            return dt;
298
        }
299

    
300
        public static DataTable SelectLineProperties()
301
        {
302
            DataTable dt = null;
303
            ID2Info id2Info = ID2Info.GetInstance();
304

    
305
            using (IAbstractDatabase connection = id2Info.CreateConnection())
306
            {
307
                try
308
                {
309
                    var query = "SELECT * FROM LineProperties";
310
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
311
                    {
312
                        dt = ds.Tables[0].Copy();
313
                    }
314
                }
315
                catch (Exception ex)
316
                {
317
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
318
                }
319
            }
320

    
321
            return dt;
322
        }
323

    
324
        public static DataTable SelectFluidCode()
325
        {
326
            DataTable dt = null;
327
            ID2Info id2Info = ID2Info.GetInstance();
328

    
329
            using (IAbstractDatabase connection = id2Info.CreateConnection())
330
            {
331
                try
332
                {
333
                    var query = "SELECT * FROM FluidCode";
334
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
335
                    {
336
                        dt = ds.Tables[0].Copy();
337
                    }
338
                }
339
                catch (Exception ex)
340
                {
341
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
342
                }
343
            }
344

    
345
            return dt;
346
        }
347

    
348
        public static DataTable SelectPipingMaterialsClass()
349
        {
350
            DataTable dt = null;
351
            ID2Info id2Info = ID2Info.GetInstance();
352

    
353
            using (IAbstractDatabase connection = id2Info.CreateConnection())
354
            {
355
                try
356
                {
357
                    var query = "SELECT * FROM PipingMaterialsClass";
358
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
359
                    {
360
                        dt = ds.Tables[0].Copy();
361
                    }
362
                }
363
                catch (Exception ex)
364
                {
365
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
366
                }
367
            }
368

    
369
            return dt;
370
        }
371

    
372
        public static DataTable SelectPSNPIPINGMATLCLASS()
373
        {
374
            DataTable dt = null;
375
            ID2Info id2Info = ID2Info.GetInstance();
376

    
377
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
378
            {
379
                try
380
                {
381
                    var query = $"SELECT * FROM {PSN_PIPINGMATLCLASS}";
382
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
383
                    {
384
                        dt = ds.Tables[0].Copy();
385
                    }
386
                }
387
                catch (Exception ex)
388
                {
389
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
390
                }
391
            }
392

    
393
            return dt;
394
        }
395

    
396
        public static DataTable SelectNominalDiameter()
397
        {
398
            DataTable dt = null;
399
            ID2Info id2Info = ID2Info.GetInstance();
400

    
401
            using (IAbstractDatabase connection = id2Info.CreateConnection())
402
            {
403
                try
404
                {
405
                    var query = "SELECT * FROM NominalDiameter ORDER BY Metric DESC";
406
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
407
                    {
408
                        dt = ds.Tables[0].Copy();
409
                    }
410
                }
411
                catch (Exception ex)
412
                {
413
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
414
                }
415
            }
416

    
417
            ///TODO: need to check below code
418
            dt.Rows.RemoveAt(0);
419
            dt.Rows.RemoveAt(0);
420
            dt.Rows.RemoveAt(0);
421
            dt.Rows.RemoveAt(0);
422

    
423
            return dt;
424
        }
425

    
426
        public static DataTable SelectSymbolAttribute()
427
        {
428
            DataTable dt = null;
429
            ID2Info id2Info = ID2Info.GetInstance();
430

    
431
            using (IAbstractDatabase connection = id2Info.CreateConnection())
432
            {
433
                try
434
                {
435
                   // var query = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
436
                    var query = "SELECT DISTINCT Attribute FROM SymbolAttribute;";
437
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
438
                    {
439
                        dt = ds.Tables[0].Copy();
440
                    }
441
                }
442
                catch (Exception ex)
443
                {
444
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
445
                }
446
            }
447

    
448
            return dt;
449
        }
450

    
451
        public static DataTable SelectSymbolName()
452
        {
453
            DataTable dt = null;
454
            ID2Info id2Info = ID2Info.GetInstance();
455

    
456
            using (IAbstractDatabase connection = id2Info.CreateConnection())
457
            {
458
                try
459
                {
460
                    var query = "SELECT * FROM SymbolName;";
461
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
462
                    {
463
                        dt = ds.Tables[0].Copy();
464
                    }
465
                }
466
                catch (Exception ex)
467
                {
468
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
469
                }
470
            }
471

    
472
            return dt;
473
        }
474

    
475
        public static double[] GetDrawingSize()
476
        {
477
            double[] result = null;
478

    
479
            ID2Info id2Info = ID2Info.GetInstance();
480
            using (IAbstractDatabase connection = id2Info.CreateConnection())
481
            {
482
                try
483
                {
484
                    var query = "SELECT value FROM Configuration WHERE Section = 'Area' AND [Key] = 'Drawing';";
485
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
486
                    {
487
                        if (ds.Tables[0].Rows.Count == 1)
488
                        {
489
                            string value = ds.Tables[0].Rows[0][0].ToString();
490
                            string[] split = value.Split(new char[] { ',' });
491
                            result = new double[] {
492
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", string.Empty)),
493
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", string.Empty)),
494
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", string.Empty)),
495
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", string.Empty))
496
                                };
497
                            result = new double[] {
498
                                Math.Min(result[0], result[2]),
499
                                Math.Min(result[1], result[3]),
500
                                Math.Max(result[0], result[2]),
501
                                Math.Max(result[1], result[3])
502
                                };
503
                        }
504
                    }
505
                }
506
                catch (Exception ex)
507
                {
508
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
509
                }
510
            }
511

    
512
            return result;
513
        }
514

    
515
        public static DataTable GetEquipmentType()
516
        {
517
            DataTable dt = null;
518
            ID2Info id2Info = ID2Info.GetInstance();
519

    
520
            using (IAbstractDatabase connection = id2Info.CreateConnection())
521
            {
522
                try
523
                {
524
                    var query = "SELECT Type FROM SymbolType WHERE Category = 'Equipment';";
525
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
526
                    {
527
                        dt = ds.Tables[0].Copy();
528
                    }
529
                }
530
                catch (Exception ex)
531
                {
532
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
533
                }
534
            }
535

    
536
            return dt;
537
        }
538

    
539
        /// <summary>
540
        ///  Zoom 을 위하여 저장 T_PSN_VIEW 데이터로 PSN 데이터지만 Msqql 과 Sqlite 둘다 저장되어 있음
541
        ///  => ID2 가 DB 타입에 따라서 바라보는 DB가 mssql이냐 sqlite냐로 지정되기 때문에 zoom 기능을 그대로 사용하려면 해당 데이터는 psn도 id2 dbtype에 맞춰서 저장 및 로드해야함
542
        ///  => 더 이상 사용하지 않음
543
        /// </summary>
544
        /// <param name="values"></param>
545
        /// <returns></returns>
546
        public static bool SaveView(List<string> values)
547
        {
548
            ID2Info id2Info = ID2Info.GetInstance();
549

    
550
            bool result = true;
551

    
552
            using (IAbstractDatabase connection = id2Info.CreateConnection())
553
            {
554
                try
555
                {
556
                    using (var txn = connection.BeginTransaction())
557
                    {
558
                        try
559
                        {
560
                            var query = $"DELETE FROM {PSN_VIEW}";
561
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
562

    
563
                            foreach (string value in values)
564
                            {
565
                                query = $"INSERT INTO {PSN_VIEW} VALUES (@OID)";
566
                                var cmd = connection.GetSqlStringCommand(query);
567
                                AddWithValue(cmd, "@OID", value);
568
                                connection.ExecuteNonQuery(cmd, txn);
569
                            }
570
                            txn.Commit();
571
                        }
572
                        catch (Exception ex)
573
                        {
574
                            txn.Rollback();
575
                            result = false;
576
                        }
577
                    }
578
                }
579
                catch (Exception ex)
580
                {
581
                    System.Windows.Forms.MessageBox.Show(ex.Message);
582
                    result = false;
583
                }
584
            }
585

    
586
            return result;
587
        }
588

    
589
        public static bool DeleteView()
590
        {
591
            ID2Info id2Info = ID2Info.GetInstance();
592

    
593
            bool result = true;
594
            using (IAbstractDatabase connection = id2Info.CreateConnection())
595
            {
596
                try
597
                {
598
                    using (var txn = connection.BeginTransaction())
599
                    {
600
                        try
601
                        {
602
                            var query = $"DELETE FROM {PSN_VIEW}";
603
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
604
                            txn.Commit();
605
                        }
606
                        catch (Exception ex)
607
                        {
608
                            txn.Rollback();
609
                            result = false;
610
                        }
611
                    }
612
                }
613
                catch (Exception ex)
614
                {
615
                    System.Windows.Forms.MessageBox.Show(ex.Message);
616
                    result = false;
617
                }
618
            }
619

    
620
            return result;
621
        }
622

    
623
        //PSN Sqlite 
624
        public static DataTable SelectHeaderSetting()
625
        {
626
            DataTable dt = null;
627
            ID2Info id2Info = ID2Info.GetInstance();
628

    
629
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
630
            {
631
                try
632
                {
633
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_HEADER_SETTING};";
634
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
635
                    {
636
                        dt = ds.Tables[0].Copy();
637
                    }
638
                }
639
                catch (Exception ex)
640
                {
641
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
642
                }
643
            }
644

    
645
            return dt;
646
        }
647

    
648
        public static DataTable SelectVentDrainSetting()
649
        {
650
            DataTable dt = null;
651
            ID2Info id2Info = ID2Info.GetInstance();
652

    
653
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
654
            {
655
                try
656
                {
657
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_VENTDRAIN_SETTING};";
658
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
659
                    {
660
                        dt = ds.Tables[0].Copy();
661
                    }
662
                }
663
                catch (Exception ex)
664
                {
665
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
666
                }
667
            }
668

    
669
            return dt;
670
        }
671

    
672
        public static DataTable SelectKeywordsSetting()
673
        {
674
            DataTable dt = null;
675
            ID2Info id2Info = ID2Info.GetInstance();
676

    
677
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
678
            {
679
                try
680
                {
681
                    var query = $@"SELECT [KEYWORD], [INDEX], [NAME] FROM {PSN_TRANSFORMKEYWORD_SETTING};";
682
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
683
                    {
684
                        dt = ds.Tables[0].Copy();
685
                    }
686
                }
687
                catch (Exception ex)
688
                {
689
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
690
                }
691
            }
692

    
693
            return dt;
694
        }
695

    
696
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
697
        {
698
            ID2Info id2Info = ID2Info.GetInstance();
699
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
700
            {
701
                try
702
                {
703
                    using (var txn = connection.BeginTransaction())
704
                    {
705
                        var query = $"DELETE FROM {PSN_HEADER_SETTING}";
706
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
707

    
708
                        foreach (HeaderInfo headerInfo in headerInfos)
709
                        {
710
                            foreach (HeaderItem item in headerInfo.HeaderItems)
711
                            {
712
                                query = $"INSERT INTO {PSN_HEADER_SETTING} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
713
                                var cmd = connection.GetSqlStringCommand(query);
714
                                AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
715
                                AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
716
                                AddWithValue(cmd, "@INDEX", item.Index);
717
                                AddWithValue(cmd, "@NAME", item.Name);
718
                                connection.ExecuteNonQuery(cmd, txn);
719
                            }
720
                        }
721
                        txn.Commit();
722
                    }
723
                    
724
                }
725
                catch (Exception ex)
726
                {
727
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
728
                    return false;
729
                }
730
            }
731
            return true;
732
        }
733

    
734
        public static bool SaveVentDrainSetting(List<VentDrainInfo> ventDrainInfos)
735
        {
736
            ID2Info id2Info = ID2Info.GetInstance();
737
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
738
            {
739
                using (var txn = connection.BeginTransaction())
740
                {
741
                    try
742
                    {
743
                        var query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
744
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
745

    
746
                        foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
747
                        {
748
                            foreach (VentDrainItem item in ventDrainInfo.VentDrainItems)
749
                            {
750
                                query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
751
                                var cmd = connection.GetSqlStringCommand(query);
752
                                AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
753
                                AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
754
                                AddWithValue(cmd, "@INDEX", item.Index);
755
                                AddWithValue(cmd, "@NAME", item.Name);
756
                                connection.ExecuteNonQuery(cmd, txn);
757
                            }
758
                        }
759

    
760
                        txn.Commit();
761
                    }
762
                    catch (Exception ex)
763
                    {
764
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
765
                        return false;
766
                    }
767
                }
768
            }
769

    
770
            return true;
771
        }
772

    
773
        public static bool SaveKeywordsSetting(List<KeywordItem> keywordItems)
774
        {
775
            ID2Info id2Info = ID2Info.GetInstance();
776
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
777
            {
778
                using (var txn = connection.BeginTransaction())
779
                {
780
                    try
781
                    {
782
                        var query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
783
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
784
                        
785
                        foreach (KeywordItem item in keywordItems)
786
                        {
787
                            query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} VALUES (@INDEX, @NAME, @KEYWORD)";
788
                            var cmd = connection.GetSqlStringCommand(query);
789
                            AddWithValue(cmd, "@INDEX", item.Index);
790
                            AddWithValue(cmd, "@NAME", item.Name);
791
                            AddWithValue(cmd, "@KEYWORD", item.Keyword);                            
792
                            connection.ExecuteNonQuery(cmd, txn);
793
                        }
794
                        
795
                        txn.Commit();
796
                    }
797
                    catch (Exception ex)
798
                    {
799
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
800
                        return false;
801
                    }
802
                }
803
            }
804

    
805
            return true;
806
        }
807

    
808
        public static bool SaveTopologyRule(DataTable dt)
809
        {
810
            ID2Info id2Info = ID2Info.GetInstance();
811
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
812
            {
813
                using (var txn = connection.BeginTransaction())
814
                {
815
                    try
816
                    {
817
                        var query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
818
                        var cmd = connection.GetSqlStringCommand(query);
819
                        cmd.ExecuteNonQuery();
820

    
821
                        foreach (DataRow row in dt.Rows)
822
                        {
823
                            query = $"INSERT INTO {PSN_TOPOLOGY_RULE} VALUES (@UID)";
824
                            cmd = connection.GetSqlStringCommand(query);
825
                            AddWithValue(cmd, "@UID", row["NAME"].ToString());
826
                            connection.ExecuteNonQuery(cmd, txn);
827
                        }
828

    
829
                        txn.Commit();
830
                    }
831
                    catch (Exception ex)
832
                    {
833
                        txn.Rollback();
834
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
835
                        return false;
836
                    }
837
                }
838
            }
839

    
840
            return true;
841
        }
842

    
843
        public static DataTable SelectTopologyRule()
844
        {
845
            DataTable dt = null;
846

    
847
            ID2Info id2Info = ID2Info.GetInstance();
848
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
849
            {
850
                try
851
                {
852
                    var query = $"SELECT * FROM {PSN_TOPOLOGY_RULE}";
853
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
854
                    {
855
                        dt = ds.Tables[0].Copy();
856
                    }
857
                }
858
                catch (Exception ex)
859
                {
860
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
861
                }
862
            }
863

    
864
            return dt;
865
        }
866

    
867
        private static void AddWithValue(DbCommand cmd, string PropName, object Value)
868
        {
869
            var param = cmd.CreateParameter();
870
            param.ParameterName = PropName;
871
            param.Value = Value;
872
            cmd.Parameters.Add(param);
873
        }
874

    
875
        public static bool SavePSNData(PSN item)
876
        {
877
            ID2Info id2Info = ID2Info.GetInstance();
878

    
879
            bool result = true;
880
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
881
            {
882
                try
883
                {
884
                    using (var txn = connection.BeginTransaction())
885
                    {
886
                        try
887
                        {
888
                            // Path Items
889
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
890
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
891
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
892
                            {
893
                                DataRow row = item.PathItems.Rows[i];
894
                                query = $"INSERT INTO {PSN_PATHITEMS} VALUES (@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @Class, @SubClass, @TYPE, @PIDNAME, @NPD, @PipeSystemNetwork_OID, @ViewPipeSystemNetwork_OID, @PipeRun_OID)";
895
                                var cmd = connection.GetSqlStringCommand(query);
896
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
897
                                AddWithValue(cmd, "@SequenceData_OID", row["SequenceData_OID"].ToString());
898
                                AddWithValue(cmd, "@TopologySet_OID", row["TopologySet_OID"].ToString());
899
                                AddWithValue(cmd, "@BranchTopologySet_OID", row["BranchTopologySet_OID"].ToString());
900
                                AddWithValue(cmd, "@PipeLine_OID", row["PipeLine_OID"].ToString());
901
                                AddWithValue(cmd, "@ITEMNAME", row["ITEMNAME"].ToString());
902
                                AddWithValue(cmd, "@ITEMTAG", row["ITEMTAG"].ToString());
903
                                AddWithValue(cmd, "@Class", row["Class"].ToString());
904
                                AddWithValue(cmd, "@SubClass", row["SubClass"].ToString());
905
                                AddWithValue(cmd, "@TYPE", row["TYPE"].ToString());
906
                                AddWithValue(cmd, "@PIDNAME", row["PIDNAME"].ToString());
907
                                AddWithValue(cmd, "@NPD", row["NPD"].ToString());
908
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", row["PipeSystemNetwork_OID"].ToString());
909
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", row["ViewPipeSystemNetwork_OID"].ToString());
910
                                AddWithValue(cmd, "@PipeRun_OID", row["PipeRun_OID"].ToString());
911
                                connection.ExecuteNonQuery(cmd, txn);
912
                            }
913

    
914
                            // Sequence
915
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
916
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
917
                            foreach (DataRow row in item.SequenceData.Rows)
918
                            {
919
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
920
                                var cmd = connection.GetSqlStringCommand(query);
921
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
922
                                AddWithValue(cmd, "@SERIALNUMBER", row["SERIALNUMBER"].ToString());
923
                                AddWithValue(cmd, "@PathItem_OID", row["PathItem_OID"].ToString());
924
                                AddWithValue(cmd, "@TopologySet_OID_Key", row["TopologySet_OID_Key"].ToString());
925
                                connection.ExecuteNonQuery(cmd, txn);
926
                            }
927

    
928
                            // Nozzle
929
                            query = $"DELETE FROM {PSN_NOZZLE}";
930
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
931
                            foreach (DataRow row in item.Nozzle.Rows)
932
                            {
933
                                query = $"INSERT INTO {PSN_NOZZLE} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS, @Equipment_OID, @FLUID, @NPD, @ROTATION, @FlowDirection)";
934
                                var cmd = connection.GetSqlStringCommand(query);
935
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
936
                                AddWithValue(cmd, "@ITEMTAG", row["ITEMTAG"].ToString());
937
                                AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
938
                                AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
939
                                AddWithValue(cmd, "@Equipment_OID", row["Equipment_OID"].ToString());
940
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
941
                                AddWithValue(cmd, "@NPD", row["NPD"].ToString());
942
                                AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
943
                                AddWithValue(cmd, "@FlowDirection", row["FlowDirection"].ToString());
944
                                connection.ExecuteNonQuery(cmd, txn);
945
                            }
946

    
947
                            //Equipment
948
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
949
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
950
                            foreach (DataRow row in item.Equipment.Rows)
951
                            {
952
                                query = $"INSERT INTO {PSN_EQUIPMENT} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
953
                                var cmd = connection.GetSqlStringCommand(query);
954
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
955
                                AddWithValue(cmd, "@ITEMTAG", row["ITEMTAG"].ToString());
956
                                AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
957
                                AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
958
                                connection.ExecuteNonQuery(cmd, txn);
959
                            }
960

    
961
                            // TopologySet
962
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
963
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
964
                            foreach (DataRow row in item.TopologySet.Rows)
965
                            {
966
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
967
                                var cmd = connection.GetSqlStringCommand(query);
968
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
969
                                AddWithValue(cmd, "@Type", row["Type"].ToString());
970
                                AddWithValue(cmd, "@SubType", row["SubType"].ToString());
971
                                AddWithValue(cmd, "@HeadItemTag", row["HeadItemTag"].ToString());
972
                                AddWithValue(cmd, "@TailItemTag", row["TailItemTag"].ToString());
973
                                AddWithValue(cmd, "@HeadItemSPID", row["HeadItemSPID"].ToString());
974
                                AddWithValue(cmd, "@TailItemSPID", row["TailItemSPID"].ToString());
975
                                connection.ExecuteNonQuery(cmd, txn);
976
                            }
977

    
978
                            // PSN
979
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
980
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
981
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
982
                            {
983
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
984
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy) VALUES " +
985
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy)";
986
                                var cmd = connection.GetSqlStringCommand(query);
987
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
988
                                AddWithValue(cmd, "@Type", row["Type"].ToString());
989
                                AddWithValue(cmd, "@OrderNumber", row["OrderNumber"].ToString());
990
                                AddWithValue(cmd, "@Pipeline_OID", row["Pipeline_OID"].ToString());
991
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
992
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
993
                                AddWithValue(cmd, "@TopologySet_OID_Key", row["TopologySet_OID_Key"].ToString());
994
                                AddWithValue(cmd, "@PSNRevisionNumber", row["PSNRevisionNumber"].ToString());
995

    
996
                                int IsValid = 0;
997
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
998
                                    IsValid = 0;
999
                                else if (row["IsValid"].ToString() == "InValid")
1000
                                    IsValid = 1;
1001
                                else if (row["IsValid"].ToString() == "Error")
1002
                                    IsValid = -1;
1003
                                AddWithValue(cmd, "@IsValid", IsValid);
1004
                                AddWithValue(cmd, "@Status", row["Status"].ToString());
1005

    
1006
                                AddWithValue(cmd, "@PBS", row["PBS"].ToString());
1007
                                AddWithValue(cmd, "@Drawings", row["Drawings"].ToString());
1008
                             
1009
                                AddWithValue(cmd, "@IncludingVirtualData", row["IncludingVirtualData"].ToString());
1010
                                AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString()); 
1011
                                connection.ExecuteNonQuery(cmd, txn);
1012
                            }
1013

    
1014
                            txn.Commit();
1015
                        }
1016
                        catch (Exception ex)
1017
                        {
1018
                            txn.Rollback();
1019
                            result = false;
1020
                        }
1021
                    }
1022
                }
1023
                catch (Exception ex)
1024
                {
1025
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1026
                    result = false;
1027
                }
1028
            }
1029

    
1030
            return result;
1031
        }
1032

    
1033
        public static bool SavePSNFluidCode(DataTable dt)
1034
        {
1035
            ID2Info id2Info = ID2Info.GetInstance();
1036

    
1037
            bool result = true;
1038
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1039
            {
1040
                try
1041
                {
1042
                    using (var txn = connection.BeginTransaction())
1043
                    {
1044
                        try
1045
                        {
1046
                            var query = $"DELETE FROM {PSN_FLUIDCODE}";
1047
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1048

    
1049
                            foreach (DataRow row in dt.Rows)
1050
                            {
1051
                                query = $"INSERT INTO {PSN_FLUIDCODE} VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1052
                                var cmd = connection.GetSqlStringCommand(query);
1053
                                cmd.Parameters.Clear();
1054

    
1055
                                {
1056
                                    var param = cmd.CreateParameter();
1057
                                    param.ParameterName = "@UID";
1058
                                    param.Value = row["UID"].ToString();
1059
                                    cmd.Parameters.Add(param);
1060
                                }
1061

    
1062
                                {
1063
                                    var param = cmd.CreateParameter();
1064
                                    param.ParameterName = "@Code";
1065
                                    param.Value = row["Code"].ToString();
1066
                                    cmd.Parameters.Add(param);
1067
                                }
1068

    
1069
                                {
1070
                                    var param = cmd.CreateParameter();
1071
                                    param.ParameterName = "@Description";
1072
                                    param.Value = row["Description"].ToString();
1073
                                    cmd.Parameters.Add(param);
1074
                                }
1075

    
1076
                                {
1077
                                    var param = cmd.CreateParameter();
1078
                                    param.ParameterName = "@Condition";
1079
                                    param.Value = row["Condition"].ToString();
1080
                                    cmd.Parameters.Add(param);
1081
                                }
1082

    
1083
                                {
1084
                                    var param = cmd.CreateParameter();
1085
                                    param.ParameterName = "@Remarks";
1086
                                    param.Value = row["Remarks"].ToString();
1087
                                    cmd.Parameters.Add(param);
1088
                                }
1089

    
1090
                                {
1091
                                    var param = cmd.CreateParameter();
1092
                                    param.ParameterName = "@GroundLevel";
1093
                                    param.Value = row["GroundLevel"].ToString();
1094
                                    cmd.Parameters.Add(param);
1095
                                }
1096

    
1097
                                connection.ExecuteNonQuery(cmd, txn);
1098
                            }
1099
                            txn.Commit();
1100
                        }
1101
                        catch (Exception ex)
1102
                        {
1103
                            txn.Rollback();
1104
                            result = false;
1105
                        }
1106
                    }
1107
                }
1108
                catch (Exception ex)
1109
                {
1110
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1111
                    result = false;
1112
                }
1113
            }
1114

    
1115
            return result;
1116
        }
1117

    
1118
        public static DataTable SelectPSNFluidCode()
1119
        {
1120
            DataTable dt = null;
1121
            ID2Info id2Info = ID2Info.GetInstance();
1122

    
1123
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1124
            {
1125
                try
1126
                {
1127
                    var query = $"SELECT * FROM {PSN_FLUIDCODE}";
1128
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1129
                    {
1130
                        dt = ds.Tables[0].Copy();
1131
                    }
1132
                }
1133
                catch (Exception ex)
1134
                {
1135
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1136
                }
1137
            }
1138

    
1139
            return dt;
1140
        }
1141

    
1142
        public static bool SavePSNPMC(DataTable dt)
1143
        {
1144
            ID2Info id2Info = ID2Info.GetInstance();
1145

    
1146
            bool result = true;
1147
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1148
            {
1149
                try
1150
                {
1151
                    using (var txn = connection.BeginTransaction())
1152
                    {
1153
                        try
1154
                        {
1155
                            var query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
1156
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1157

    
1158
                            foreach (DataRow row in dt.Rows)
1159
                            {
1160
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1161
                                var cmd = connection.GetSqlStringCommand(query);
1162
                                cmd.Parameters.Clear();
1163

    
1164
                                {
1165
                                    var param = cmd.CreateParameter();
1166
                                    param.ParameterName = "@UID";
1167
                                    param.Value = row["UID"].ToString();
1168
                                    cmd.Parameters.Add(param);
1169
                                }
1170

    
1171
                                {
1172
                                    var param = cmd.CreateParameter();
1173
                                    param.ParameterName = "@Priority";
1174
                                    param.Value = row["Priority"].ToString();
1175
                                    cmd.Parameters.Add(param);
1176
                                }
1177

    
1178
                                {
1179
                                    var param = cmd.CreateParameter();
1180
                                    param.ParameterName = "@Code";
1181
                                    param.Value = row["Code"].ToString();
1182
                                    cmd.Parameters.Add(param);
1183
                                }
1184

    
1185
                                {
1186
                                    var param = cmd.CreateParameter();
1187
                                    param.ParameterName = "@Description";
1188
                                    param.Value = row["Description"].ToString();
1189
                                    cmd.Parameters.Add(param);
1190
                                }
1191

    
1192
                                {
1193
                                    var param = cmd.CreateParameter();
1194
                                    param.ParameterName = "@Condition";
1195
                                    param.Value = row["Condition"].ToString();
1196
                                    cmd.Parameters.Add(param);
1197
                                }
1198

    
1199
                                {
1200
                                    var param = cmd.CreateParameter();
1201
                                    param.ParameterName = "@Remarks";
1202
                                    param.Value = row["Remarks"].ToString();
1203
                                    cmd.Parameters.Add(param);
1204
                                }
1205

    
1206
                                {
1207
                                    var param = cmd.CreateParameter();
1208
                                    param.ParameterName = "@GroundLevel";
1209
                                    param.Value = row["GroundLevel"].ToString();
1210
                                    cmd.Parameters.Add(param);
1211
                                }
1212

    
1213
                                connection.ExecuteNonQuery(cmd, txn);
1214
                            }
1215

    
1216
                            txn.Commit();
1217
                        }
1218
                        catch (Exception ex)
1219
                        {
1220
                            txn.Rollback();
1221
                            result = false;
1222
                        }
1223
                    }
1224
                }
1225
                catch (Exception ex)
1226
                {
1227
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1228
                    result = false;
1229
                }
1230
            }
1231

    
1232
            return result;
1233
        }
1234

    
1235
        public static PSN GetDBPSN()
1236
        {
1237
            PSN result = new PSN();
1238
            ID2Info id2Info = ID2Info.GetInstance();
1239

    
1240
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1241
            {
1242
                try
1243
                {
1244
                    //var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
1245
                    //using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1246
                    //{
1247
                    //    result.PipeSystemNetwork = ds.Tables[0].Clone();
1248
                    //    result.PipeSystemNetwork.Columns["IsValid"].DataType = typeof(string);
1249

    
1250
                    //    foreach (DataRow row in ds.Tables[0].Rows)
1251
                    //    {
1252
                    //        DataRow newRow = result.PipeSystemNetwork.NewRow();
1253
                    //        newRow["OID"] = row["OID"].ToString();
1254
                    //        newRow["Type"] = row["Type"].ToString();
1255
                    //        newRow["OrderNumber"] = row["OrderNumber"].ToString();
1256
                    //        newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
1257
                    //        newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
1258
                    //        newRow["TO_DATA"] = row["TO_DATA"].ToString();
1259
                    //        newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
1260
                    //        newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
1261
                           
1262
                    //        string IsValid = string.Empty;
1263

    
1264
                    //        if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
1265
                    //            IsValid = string.Empty;//"OK";
1266
                    //        else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
1267
                    //            IsValid = "InValid";
1268
                    //        else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
1269
                    //            IsValid = "Error";
1270

    
1271
                    //        newRow["IsValid"] = IsValid;
1272

    
1273
                    //        newRow["Status"] = row["Status"].ToString();
1274
                    //        newRow["PBS"] = row["PBS"].ToString();
1275
                    //        newRow["Drawings"] = row["Drawings"].ToString();
1276

    
1277
                    //        newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1278
                    //        newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
1279

    
1280
                    //        result.PipeSystemNetwork.Rows.Add(newRow);
1281
                    //    }
1282
                    //}
1283

    
1284
                    var query = $"SELECT * FROM {PSN_EQUIPMENT}";
1285
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1286
                    {
1287
                        result.Equipment = ds.Tables[0].Copy();
1288
                    }
1289

    
1290
                    query = $"SELECT * FROM {PSN_NOZZLE}";
1291
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1292
                    {
1293
                        result.Nozzle = ds.Tables[0].Copy();
1294
                    }
1295

    
1296
                    query = $"SELECT * FROM {PSN_PATHITEMS}";
1297
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1298
                    {
1299
                        result.PathItems = ds.Tables[0].Copy();
1300
                    }
1301

    
1302
                    query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
1303
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1304
                    {
1305
                        result.SequenceData = ds.Tables[0].Copy();
1306
                    }
1307

    
1308
                    query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
1309
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1310
                    {
1311
                        result.TopologySet = ds.Tables[0].Copy();
1312
                    }
1313

    
1314
                    result.Revision = GetRevision();
1315
                }
1316
                catch (Exception ex)
1317
                {
1318
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1319
                    result = null;
1320
                }
1321
            }
1322

    
1323
            return result;
1324
        }
1325

    
1326
        public static int GetRevision()
1327
        {
1328
            int result = 0;
1329
            ID2Info id2Info = ID2Info.GetInstance();
1330

    
1331
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1332
            {
1333
                try
1334
                {
1335
                    var query = $"SELECT DISTINCT PSNRevisionNumber FROM {PSN_PIPESYSTEMNETWORK};";
1336
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1337
                    {
1338
                        foreach (DataRow row in ds.Tables[0].Rows)
1339
                        {
1340
                            string value = row["PSNRevisionNumber"].ToString();
1341
                            if (value.StartsWith("V"))
1342
                                value = value.Remove(0, 1);
1343
                            int revisionNumber = Convert.ToInt32(value);
1344
                            if (result < revisionNumber)
1345
                                result = revisionNumber;
1346
                        }
1347
                    }
1348
                }
1349
                catch (Exception ex)
1350
                {
1351
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1352
                    result = -1;
1353
                }
1354
            }
1355

    
1356
            return result;
1357
        }
1358

    
1359
        public static DataTable GetPathItem()
1360
        {
1361
            DataTable dt = null;
1362

    
1363
            ID2Info id2Info = ID2Info.GetInstance();
1364
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1365
            {
1366
                try
1367
                {
1368
                    var query = $"SELECT * FROM {PSN_PATHITEMS}";
1369
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1370
                    {
1371
                        dt = ds.Tables[0].Copy();
1372
                    }
1373
                }
1374
                catch (Exception ex)
1375
                {
1376
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1377
                }
1378
            }
1379

    
1380
            return dt;
1381
        }
1382

    
1383
        public static DataTable GetTopologySet()
1384
        {
1385
            DataTable dt = null;
1386

    
1387
            ID2Info id2Info = ID2Info.GetInstance();
1388
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1389
            {
1390
                try
1391
                {
1392
                    var query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
1393
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1394
                    {
1395
                        dt = ds.Tables[0].Clone();
1396
                        foreach (DataRow row in ds.Tables[0].Rows)
1397
                        {
1398
                            DataRow newRow = dt.NewRow();
1399
                            newRow["OID"] = row["OID"].ToString();
1400
                            newRow["Type"] = row["Type"].ToString();
1401
                            newRow["SubType"] = row["SubType"].ToString();
1402
                            newRow["HeadItemTag"] = row["HeadItemTag"].ToString();
1403
                            newRow["TailItemTag"] = row["TailItemTag"].ToString();
1404
                            newRow["HeadItemSPID"] = row["HeadItemSPID"].ToString();
1405
                            newRow["TailItemSPID"] = row["TailItemSPID"].ToString();
1406
                            dt.Rows.Add(newRow);
1407
                        }
1408
                    }
1409
                }
1410
                catch (Exception ex)
1411
                {
1412
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1413
                }
1414
            }
1415

    
1416
            return dt;
1417
        }
1418

    
1419
        public static DataTable GetPipeSystemNetwork()
1420
        {
1421
            DataTable dt = null;
1422

    
1423
            ID2Info id2Info = ID2Info.GetInstance();
1424
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1425
            {
1426
                try
1427
                {
1428
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
1429
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1430
                    {
1431
                        dt = ds.Tables[0].Clone();
1432
                        dt.Columns["IsValid"].DataType = typeof(string);
1433
                        foreach (DataRow row in ds.Tables[0].Rows)
1434
                        {
1435
                            DataRow newRow = dt.NewRow();
1436
                            newRow["OID"] = row["OID"].ToString();
1437
                            newRow["Type"] = row["Type"].ToString();
1438
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
1439
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
1440
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
1441
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
1442
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
1443
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
1444

    
1445
                            string IsValid = string.Empty;
1446

    
1447
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
1448
                                IsValid = string.Empty;//"OK";
1449
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
1450
                                IsValid = "InValid";
1451
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
1452
                                IsValid = "Error";
1453

    
1454
                            newRow["IsValid"] = IsValid;
1455
                            newRow["Status"] = row["Status"].ToString();
1456

    
1457
                            newRow["PBS"] = row["PBS"].ToString();
1458
                            newRow["Drawings"] = row["Drawings"].ToString();
1459
                            
1460
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1461
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();                                           
1462
                                                        
1463
                            dt.Rows.Add(newRow);
1464
                        }
1465
                    }
1466
                }
1467
                catch (Exception ex)
1468
                {
1469
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1470
                }
1471
            }
1472

    
1473
            return dt;
1474
        }
1475

    
1476
        public static DataTable GetSequenceData()
1477
        {
1478
            DataTable dt = null;
1479

    
1480
            ID2Info id2Info = ID2Info.GetInstance();
1481
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1482
            {
1483
                try
1484
                {
1485
                    var query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
1486
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1487
                    {
1488
                        dt = ds.Tables[0].Copy();
1489
                    }
1490
                }
1491
                catch (Exception ex)
1492
                {
1493
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1494
                }
1495
            }
1496

    
1497
            return dt;
1498
        }
1499
    }
1500
}
클립보드 이미지 추가 (최대 크기: 500 MB)