프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ 2ada3be8

이력 | 보기 | 이력해설 | 다운로드 (63.8 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 = "SPPIDPipingMatClass";
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
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
125
                    if (matched == null)
126
                    {
127
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(50), SequenceData_OID NVARCHAR(125), " +
128
                            "TopologySet_OID NVARCHAR(125), BranchTopologySet_OID NVARCHAR(125), PipeLine_OID NVARCHAR(125), ITEMNAME NVARCHAR(50), ITEMTAG NVARCHAR(125), " +
129
                            "Class NVARCHAR(80), SubClass NVARCHAR(80), TYPE NVARCHAR(80), PIDNAME NVARCHAR(10), NPD NVARCHAR(20), PipeSystemNetwork_OID NVARCHAR(20), " +
130
                            "ViewPipeSystemNetwork_OID NVARCHAR(255), PipeRun_OID NVARCHAR(255))";
131
                        using (var cmd = connection.GetSqlStringCommand(query))
132
                        {
133
                            cmd.ExecuteNonQuery();
134
                        }
135
                    }
136

    
137
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
138
                    if (matched == null)
139
                    {
140
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(125), SERIALNUMBER NVARCHAR(10), PathItem_OID NVARCHAR(50), TopologySet_OID_Key NVARCHAR(125))";
141
                        using (var cmd = connection.GetSqlStringCommand(query))
142
                        {
143
                            cmd.ExecuteNonQuery();
144
                        }
145
                    }
146

    
147
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
148
                    if (matched == null)
149
                    {
150
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(50), Type NVARCHAR(20), OrderNumber NVARCHAR(20), Pipeline_OID NVARCHAR(125), FROM_DATA NVARCHAR(255), " +
151
                            "TO_DATA NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), PBS NVARCHAR(255), PIDDrawings NVARCHAR(255), " +
152
                            "Validity INTEGER, Status NVARCHAR(255), IncludingVirtualData NVARCHAR(10),  PSNAccuracy REAL)";
153
                        using (var cmd = connection.GetSqlStringCommand(query))
154
                        {
155
                            cmd.ExecuteNonQuery();
156
                        }
157
                    }
158

    
159
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
160
                    if (matched == null)
161
                    {
162
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(50), ITEMTAG NVARCHAR(50), XCOORDS REAL, YCOORDS REAL)";
163
                        using (var cmd = connection.GetSqlStringCommand(query))
164
                        {
165
                            cmd.ExecuteNonQuery();
166
                        }
167
                    }
168

    
169
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
170
                    if (matched == null)
171
                    {
172
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(50), ITEMTAG NVARCHAR(50), XCOORDS REAL, YCOORDS REAL, Equipment_OID NVARCHAR(50), " +
173
                            "FLUID NVARCHAR(50), NPD NVARCHAR(20), ROTATION REAL, FlowDirection NVARCHAR(10))";
174
                        using (var cmd = connection.GetSqlStringCommand(query))
175
                        {
176
                            cmd.ExecuteNonQuery();
177
                        }
178
                    }
179

    
180
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
181
                    if (matched == null)
182
                    {
183
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(50), Description NVARCHAR(255), Condition NVARCHAR(50), Remarks NVARCHAR(255), GroundLevel NVARCHAR(50))";
184
                        using (var cmd = connection.GetSqlStringCommand(query))
185
                        {
186
                            cmd.ExecuteNonQuery();
187
                        }
188
                    }
189

    
190
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
191
                    if (matched == null)
192
                    {
193
                        var query = $"CREATE TABLE {PSN_PIPINGMATLCLASS} (UID NVARCHAR(50), Priority INTEGER, Code NVARCHAR(255), Description NVARCHAR(50), Condition NVARCHAR(255), Remarks NVARCHAR(80), GroundLevel NVARCHAR(50))";
194
                        using (var cmd = connection.GetSqlStringCommand(query))
195
                        {
196
                            cmd.ExecuteNonQuery();
197
                        }
198
                    }
199

    
200
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
201
                    if (matched == null)
202
                    {
203
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
204
                        using (var cmd = connection.GetSqlStringCommand(query))
205
                        {
206
                            cmd.ExecuteNonQuery();
207
                        }
208
                    }
209

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

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

    
238
            return result;
239
        }
240

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

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

    
267
            return dt;
268
        }
269

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

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

    
295
            return dt;
296
        }
297

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

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

    
319
            return dt;
320
        }
321

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

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

    
343
            return dt;
344
        }
345

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

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

    
367
            return dt;
368
        }
369

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

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

    
391
            return dt;
392
        }
393

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

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

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

    
421
            return dt;
422
        }
423

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

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

    
446
            return dt;
447
        }
448

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

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

    
470
            return dt;
471
        }
472

    
473
        public static double[] GetDrawingSize()
474
        {
475
            double[] result = null;
476

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

    
510
            return result;
511
        }
512

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

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

    
534
            return dt;
535
        }
536

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

    
548
            bool result = true;
549

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

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

    
584
            return result;
585
        }
586

    
587
        public static bool DeleteView()
588
        {
589
            ID2Info id2Info = ID2Info.GetInstance();
590

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

    
618
            return result;
619
        }
620

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

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

    
643
            return dt;
644
        }
645

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

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

    
667
            return dt;
668
        }
669

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

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

    
691
            return dt;
692
        }
693

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

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

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

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

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

    
768
            return true;
769
        }
770

    
771
        public static bool SaveKeywordsSetting(List<KeywordInfo> keywordInfos)
772
        {
773
            ID2Info id2Info = ID2Info.GetInstance();
774
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
775
            {
776
                using (var txn = connection.BeginTransaction())
777
                {
778
                    try
779
                    {
780
                        var query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
781
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
782

    
783
                        foreach (KeywordInfo keywordInfo in keywordInfos)
784
                        {
785
                            foreach (KeywordItem item in keywordInfo.KeywordItems)
786
                            {
787
                                query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME, @KEYWORD)";
788
                                var cmd = connection.GetSqlStringCommand(query);
789
                                AddWithValue(cmd, "@GROUP_ID", keywordInfo.UID);
790
                                AddWithValue(cmd, "@DESCRIPTION", keywordInfo.Description);
791
                                AddWithValue(cmd, "@INDEX", item.Index);
792
                                AddWithValue(cmd, "@NAME", item.Name);
793
                                AddWithValue(cmd, "@KEYWORD", item.Keyword);
794
                                connection.ExecuteNonQuery(cmd, txn);
795
                            }
796
                        }
797
                        txn.Commit();
798
                    }
799
                    catch (Exception ex)
800
                    {
801
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
802
                        return false;
803
                    }
804
                }
805
            }
806

    
807
            return true;
808
        }
809

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

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

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

    
842
            return true;
843
        }
844

    
845
        public static DataTable SelectTopologyRule()
846
        {
847
            DataTable dt = null;
848

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

    
866
            return dt;
867
        }
868

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

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

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

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

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

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

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

    
979
                            // PSN
980
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
981
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
982
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
983
                            {
984
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} VALUES (@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber, " +
985
                                    $"@PBS, @PIDDrawings, @Validity, @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
                                AddWithValue(cmd, "@PBS", row["PBS"].ToString());
996
                                AddWithValue(cmd, "@PIDDrawings", row["PIDDrawings"].ToString());
997
                                int Validity = 0;
998
                                if (row["Validity"].ToString() == "OK")
999
                                    Validity = 0;
1000
                                else if (row["Validity"].ToString() == "InValid")
1001
                                    Validity = 1;
1002
                                else if (row["Validity"].ToString() == "Error")
1003
                                    Validity = -1;
1004
                                AddWithValue(cmd, "@Validity", Validity);
1005
                                AddWithValue(cmd, "@Status", row["Status"].ToString());
1006
                                AddWithValue(cmd, "@IncludingVirtualData", row["IncludingVirtualData"].ToString());
1007
                                AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString()); 
1008
                                connection.ExecuteNonQuery(cmd, txn);
1009
                            }
1010

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

    
1027
            return result;
1028
        }
1029

    
1030
        public static bool SavePSNFluidCode(DataTable dt)
1031
        {
1032
            ID2Info id2Info = ID2Info.GetInstance();
1033

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

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

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

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

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

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

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

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

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

    
1112
            return result;
1113
        }
1114

    
1115
        public static DataTable SelectPSNFluidCode()
1116
        {
1117
            DataTable dt = null;
1118
            ID2Info id2Info = ID2Info.GetInstance();
1119

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

    
1136
            return dt;
1137
        }
1138

    
1139
        public static bool SavePSNPMC(DataTable dt)
1140
        {
1141
            ID2Info id2Info = ID2Info.GetInstance();
1142

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

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

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

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

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

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

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

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

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

    
1210
                                connection.ExecuteNonQuery(cmd, txn);
1211
                            }
1212

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

    
1229
            return result;
1230
        }
1231

    
1232
        public static PSN GetDBPSN()
1233
        {
1234
            PSN result = new PSN();
1235
            ID2Info id2Info = ID2Info.GetInstance();
1236

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

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

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

    
1269
                            newRow["Validity"] = Validity;
1270

    
1271
                            newRow["Status"] = row["Status"].ToString();
1272
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1273
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
1274

    
1275
                            result.PipeSystemNetwork.Rows.Add(newRow);
1276
                        }
1277
                    }
1278

    
1279
                    query = $"SELECT * FROM {PSN_EQUIPMENT}";
1280
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1281
                    {
1282
                        result.Equipment = ds.Tables[0].Copy();
1283
                    }
1284

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

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

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

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

    
1309
                    result.Revision = GetRevision();
1310
                }
1311
                catch (Exception ex)
1312
                {
1313
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1314
                    result = null;
1315
                }
1316
            }
1317

    
1318
            return result;
1319
        }
1320

    
1321
        public static int GetRevision()
1322
        {
1323
            int result = 0;
1324
            ID2Info id2Info = ID2Info.GetInstance();
1325

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

    
1351
            return result;
1352
        }
1353

    
1354
        public static DataTable GetPathItem()
1355
        {
1356
            DataTable dt = null;
1357

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

    
1375
            return dt;
1376
        }
1377

    
1378
        public static DataTable GetTopologySet()
1379
        {
1380
            DataTable dt = null;
1381

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

    
1411
            return dt;
1412
        }
1413

    
1414
        public static DataTable GetPipeSystemNetwork()
1415
        {
1416
            DataTable dt = null;
1417

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

    
1443
                            if (Convert.ToInt32(row["Validity"].ToString()) == 0)
1444
                                Validity = string.Empty;//"OK";
1445
                            else if (Convert.ToInt32(row["Validity"].ToString()) == 1)
1446
                                Validity = "InValid";
1447
                            else if (Convert.ToInt32(row["Validity"].ToString()) == -1)
1448
                                Validity = "Error";
1449

    
1450
                            newRow["Validity"] = Validity;
1451

    
1452
                            newRow["Status"] = row["Status"].ToString();
1453
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1454
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
1455
                            dt.Rows.Add(newRow);
1456
                        }
1457
                    }
1458
                }
1459
                catch (Exception ex)
1460
                {
1461
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1462
                }
1463
            }
1464

    
1465
            return dt;
1466
        }
1467

    
1468
        public static DataTable GetSequenceData()
1469
        {
1470
            DataTable dt = null;
1471

    
1472
            ID2Info id2Info = ID2Info.GetInstance();
1473
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1474
            {
1475
                try
1476
                {
1477
                    var query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
1478
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1479
                    {
1480
                        dt = ds.Tables[0].Copy();
1481
                    }
1482
                }
1483
                catch (Exception ex)
1484
                {
1485
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1486
                }
1487
            }
1488

    
1489
            return dt;
1490
        }
1491
    }
1492
}
클립보드 이미지 추가 (최대 크기: 500 MB)