프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ abc4250b

이력 | 보기 | 이력해설 | 다운로드 (70.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 = "SPPIDPipingMatlClass";
26
        const string PSN_TOPOLOGYSET = "SPPIDTopologySet";
27
        //2021.11.17 추가 
28
        const string PSN_PIPELINE = "SPPIDPipeLine";
29
        /// <summary>
30
        ///  ID2 Project.db 데이터를 가져온다. 
31
        ///  DB 접속 정보 및 DBType (Sqlite, Mssql) 정보를 가져옴
32
        ///  - JY
33
        /// </summary>
34
        /// <returns></returns>
35
        public static DataTable GetProject()
36
        {
37
            DataTable dt = new DataTable();
38
            ID2Info id2Info = ID2Info.GetInstance();
39
            try
40
            {
41
                using (SQLiteConnection connection = new SQLiteConnection(@"Data Source = C:\ProgramData\Digital PID\Project.db", true))
42
                {
43
                    connection.Open();
44
                    if (connection.State.Equals(ConnectionState.Open))
45
                    {
46
                        using (SQLiteCommand cmd = connection.CreateCommand())
47
                        {
48
                            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]";
49
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
50
                                dt.Load(dr);
51
                        }
52

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

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

    
66
            return dt;
67
        }
68

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

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

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

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

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

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

    
122
                        SaveTopologyRule(topologyRule);
123
                    }
124

    
125

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

    
138
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
139
                    if (matched == null)
140
                    {
141
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL)";
142
                        using (var cmd = connection.GetSqlStringCommand(query))
143
                        {
144
                            cmd.ExecuteNonQuery();
145
                        }
146
                    }
147

    
148
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
149
                    if (matched == null)
150
                    {
151
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL, Equipment_OID NVARCHAR(255), " +
152
                            "Fluid NVARCHAR(255), NPD NVARCHAR(255), Rotation REAL, FlowDirection NVARCHAR(255))";
153
                        using (var cmd = connection.GetSqlStringCommand(query))
154
                        {
155
                            cmd.ExecuteNonQuery();
156
                        }
157
                    }
158

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

    
169

    
170
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
171
                    if (matched == null)
172
                    {
173
                        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))";
174
                        using (var cmd = connection.GetSqlStringCommand(query))
175
                        {
176
                            cmd.ExecuteNonQuery();
177
                        }
178
                    }
179

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

    
190
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
191
                    if (matched == null)
192
                    {
193
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
194
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
195
                            "CLASS NVARCHAR(255), SUBCLASS NVARCHAR(255), TYPE NVARCHAR(255), PIDNAME NVARCHAR(255), Equipment_OID NVARCHAR(255), NPD NVARCHAR(255), PipeSystemNetwork_OID NVARCHAR(255), PipeRun_OID NVARCHAR(255), " +
196
                            "ViewPipeSystemNetwork_OID NVARCHAR(255))";
197
                        using (var cmd = connection.GetSqlStringCommand(query))
198
                        {
199
                            cmd.ExecuteNonQuery();
200
                        }
201
                    }
202
                    
203
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
204
                    if (matched == null)
205
                    {
206
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
207
                        using (var cmd = connection.GetSqlStringCommand(query))
208
                        {
209
                            cmd.ExecuteNonQuery();
210
                        }
211
                    }
212

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

    
233
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
234
                    if (matched == null)
235
                    {
236
                        var query = $"CREATE TABLE {PSN_PIPELINE} (OID NVARCHAR(255), PipeSystem_OID NVARCHAR(255), FLUID NVARCHAR(255), PMC NVARCHAR(255), SEQNUMBER NVARCHAR(255), INSULATION NVARCHAR(255), " +
237
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
238
                        using (var cmd = connection.GetSqlStringCommand(query))
239
                        {
240
                            cmd.ExecuteNonQuery();
241
                        }
242
                    }
243

    
244
                    result = true;
245
                }
246
                catch (Exception ex)
247
                {
248
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
249
                }
250
            }
251

    
252
            return result;
253
        }
254

    
255
        // ID2 DB 데이터
256
        /// <summary>
257
        /// ID2 데이타베이스에서 OPC 데이터를 조회
258
        /// </summary>
259
        /// <returns></returns>
260
        public static DataTable SelectOPCRelations()
261
        {
262
            DataTable dt = null;
263
            ID2Info id2Info = ID2Info.GetInstance();
264

    
265
            using (IAbstractDatabase connection = id2Info.CreateConnection())
266
            {
267
                try
268
                {
269
                    var query = "SELECT * FROM OPCRelations;";
270
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
271
                    {
272
                        dt = ds.Tables[0].Copy();
273
                    }
274
                }
275
                catch (Exception ex)
276
                {
277
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
278
                }
279
            }
280

    
281
            return dt;
282
        }
283

    
284
        /// <summary>
285
        /// ID2 데이타베이스에서 도면 데이터를 조회
286
        /// </summary>
287
        /// <returns></returns>
288
        public static DataTable SelectDrawings()
289
        {
290
            DataTable dt = null;
291
            ID2Info id2Info = ID2Info.GetInstance();
292

    
293
            using (IAbstractDatabase connection = id2Info.CreateConnection())
294
            {
295
                try
296
                {
297
                    var query = "SELECT * FROM Drawings";
298
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
299
                    {
300
                        dt = ds.Tables[0].Copy();
301
                    }
302
                }
303
                catch (Exception ex)
304
                {
305
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
306
                }
307
            }
308

    
309
            return dt;
310
        }
311

    
312
        public static DataTable SelectLineProperties()
313
        {
314
            DataTable dt = null;
315
            ID2Info id2Info = ID2Info.GetInstance();
316

    
317
            using (IAbstractDatabase connection = id2Info.CreateConnection())
318
            {
319
                try
320
                {
321
                    var query = "SELECT * FROM LineProperties";
322
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
323
                    {
324
                        dt = ds.Tables[0].Copy();
325
                    }
326
                }
327
                catch (Exception ex)
328
                {
329
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
330
                }
331
            }
332

    
333
            return dt;
334
        }
335

    
336
        public static DataTable SelectFluidCode()
337
        {
338
            DataTable dt = null;
339
            ID2Info id2Info = ID2Info.GetInstance();
340

    
341
            using (IAbstractDatabase connection = id2Info.CreateConnection())
342
            {
343
                try
344
                {
345
                    var query = "SELECT * FROM FluidCode";
346
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
347
                    {
348
                        dt = ds.Tables[0].Copy();
349
                    }
350
                }
351
                catch (Exception ex)
352
                {
353
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
354
                }
355
            }
356

    
357
            return dt;
358
        }
359

    
360
        public static DataTable SelectPipingMaterialsClass()
361
        {
362
            DataTable dt = null;
363
            ID2Info id2Info = ID2Info.GetInstance();
364

    
365
            using (IAbstractDatabase connection = id2Info.CreateConnection())
366
            {
367
                try
368
                {
369
                    var query = "SELECT * FROM PipingMaterialsClass";
370
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
371
                    {
372
                        dt = ds.Tables[0].Copy();
373
                    }
374
                }
375
                catch (Exception ex)
376
                {
377
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
378
                }
379
            }
380

    
381
            return dt;
382
        }
383

    
384
        public static DataTable SelectPSNPIPINGMATLCLASS()
385
        {
386
            DataTable dt = null;
387
            ID2Info id2Info = ID2Info.GetInstance();
388

    
389
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
390
            {
391
                try
392
                {
393
                    var query = $"SELECT * FROM {PSN_PIPINGMATLCLASS}";
394
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
395
                    {
396
                        dt = ds.Tables[0].Copy();
397
                    }
398
                }
399
                catch (Exception ex)
400
                {
401
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
402
                }
403
            }
404

    
405
            return dt;
406
        }
407

    
408
        public static DataTable SelectNominalDiameter()
409
        {
410
            DataTable dt = null;
411
            ID2Info id2Info = ID2Info.GetInstance();
412

    
413
            using (IAbstractDatabase connection = id2Info.CreateConnection())
414
            {
415
                try
416
                {
417
                    var query = "SELECT * FROM NominalDiameter ORDER BY Metric DESC";
418
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
419
                    {
420
                        dt = ds.Tables[0].Copy();
421
                    }
422
                }
423
                catch (Exception ex)
424
                {
425
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
426
                }
427
            }
428

    
429
            ///TODO: need to check below code
430
            dt.Rows.RemoveAt(0);
431
            dt.Rows.RemoveAt(0);
432
            dt.Rows.RemoveAt(0);
433
            dt.Rows.RemoveAt(0);
434

    
435
            return dt;
436
        }
437

    
438
        public static DataTable SelectSymbolAttribute()
439
        {
440
            DataTable dt = null;
441
            ID2Info id2Info = ID2Info.GetInstance();
442

    
443
            using (IAbstractDatabase connection = id2Info.CreateConnection())
444
            {
445
                try
446
                {
447
                   // var query = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
448
                    var query = "SELECT DISTINCT Attribute FROM SymbolAttribute;";
449
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
450
                    {
451
                        dt = ds.Tables[0].Copy();
452
                    }
453
                }
454
                catch (Exception ex)
455
                {
456
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
457
                }
458
            }
459

    
460
            return dt;
461
        }
462

    
463
        public static DataTable SelectSymbolName()
464
        {
465
            DataTable dt = null;
466
            ID2Info id2Info = ID2Info.GetInstance();
467

    
468
            using (IAbstractDatabase connection = id2Info.CreateConnection())
469
            {
470
                try
471
                {
472
                    var query = "SELECT * FROM SymbolName;";
473
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
474
                    {
475
                        dt = ds.Tables[0].Copy();
476
                    }
477
                }
478
                catch (Exception ex)
479
                {
480
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
481
                }
482
            }
483

    
484
            return dt;
485
        }
486

    
487
        public static double[] GetDrawingSize()
488
        {
489
            double[] result = null;
490

    
491
            ID2Info id2Info = ID2Info.GetInstance();
492
            using (IAbstractDatabase connection = id2Info.CreateConnection())
493
            {
494
                try
495
                {
496
                    var query = "SELECT value FROM Configuration WHERE Section = 'Area' AND [Key] = 'Drawing';";
497
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
498
                    {
499
                        if (ds.Tables[0].Rows.Count == 1)
500
                        {
501
                            string value = ds.Tables[0].Rows[0][0].ToString();
502
                            string[] split = value.Split(new char[] { ',' });
503
                            result = new double[] {
504
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", string.Empty)),
505
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", string.Empty)),
506
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", string.Empty)),
507
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", string.Empty))
508
                                };
509
                            result = new double[] {
510
                                Math.Min(result[0], result[2]),
511
                                Math.Min(result[1], result[3]),
512
                                Math.Max(result[0], result[2]),
513
                                Math.Max(result[1], result[3])
514
                                };
515
                        }
516
                    }
517
                }
518
                catch (Exception ex)
519
                {
520
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
521
                }
522
            }
523

    
524
            return result;
525
        }
526

    
527
        public static DataTable GetEquipmentType()
528
        {
529
            DataTable dt = null;
530
            ID2Info id2Info = ID2Info.GetInstance();
531

    
532
            using (IAbstractDatabase connection = id2Info.CreateConnection())
533
            {
534
                try
535
                {
536
                    var query = "SELECT Type FROM SymbolType WHERE Category = 'Equipment';";
537
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
538
                    {
539
                        dt = ds.Tables[0].Copy();
540
                    }
541
                }
542
                catch (Exception ex)
543
                {
544
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
545
                }
546
            }
547

    
548
            return dt;
549
        }
550

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

    
562
            bool result = true;
563

    
564
            using (IAbstractDatabase connection = id2Info.CreateConnection())
565
            {
566
                try
567
                {
568
                    using (var txn = connection.BeginTransaction())
569
                    {
570
                        try
571
                        {
572
                            var query = $"DELETE FROM {PSN_VIEW}";
573
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
574

    
575
                            foreach (string value in values)
576
                            {
577
                                query = $"INSERT INTO {PSN_VIEW} VALUES (@OID)";
578
                                var cmd = connection.GetSqlStringCommand(query);
579
                                AddWithValue(cmd, "@OID", value);
580
                                connection.ExecuteNonQuery(cmd, txn);
581
                            }
582
                            txn.Commit();
583
                        }
584
                        catch (Exception ex)
585
                        {
586
                            txn.Rollback();
587
                            result = false;
588
                        }
589
                    }
590
                }
591
                catch (Exception ex)
592
                {
593
                    System.Windows.Forms.MessageBox.Show(ex.Message);
594
                    result = false;
595
                }
596
            }
597

    
598
            return result;
599
        }
600

    
601
        public static bool DeleteView()
602
        {
603
            ID2Info id2Info = ID2Info.GetInstance();
604

    
605
            bool result = true;
606
            using (IAbstractDatabase connection = id2Info.CreateConnection())
607
            {
608
                try
609
                {
610
                    using (var txn = connection.BeginTransaction())
611
                    {
612
                        try
613
                        {
614
                            var query = $"DELETE FROM {PSN_VIEW}";
615
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
616
                            txn.Commit();
617
                        }
618
                        catch (Exception ex)
619
                        {
620
                            txn.Rollback();
621
                            result = false;
622
                        }
623
                    }
624
                }
625
                catch (Exception ex)
626
                {
627
                    System.Windows.Forms.MessageBox.Show(ex.Message);
628
                    result = false;
629
                }
630
            }
631

    
632
            return result;
633
        }
634

    
635
        //PSN Sqlite 
636
        public static DataTable SelectHeaderSetting()
637
        {
638
            DataTable dt = null;
639
            ID2Info id2Info = ID2Info.GetInstance();
640

    
641
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
642
            {
643
                try
644
                {
645
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_HEADER_SETTING};";
646
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
647
                    {
648
                        dt = ds.Tables[0].Copy();
649
                    }
650
                }
651
                catch (Exception ex)
652
                {
653
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
654
                }
655
            }
656

    
657
            return dt;
658
        }
659

    
660
        public static DataTable SelectVentDrainSetting()
661
        {
662
            DataTable dt = null;
663
            ID2Info id2Info = ID2Info.GetInstance();
664

    
665
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
666
            {
667
                try
668
                {
669
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_VENTDRAIN_SETTING};";
670
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
671
                    {
672
                        dt = ds.Tables[0].Copy();
673
                    }
674
                }
675
                catch (Exception ex)
676
                {
677
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
678
                }
679
            }
680

    
681
            return dt;
682
        }
683

    
684
        public static DataTable SelectKeywordsSetting()
685
        {
686
            DataTable dt = null;
687
            ID2Info id2Info = ID2Info.GetInstance();
688

    
689
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
690
            {
691
                try
692
                {
693
                    var query = $@"SELECT [KEYWORD], [INDEX], [NAME] FROM {PSN_TRANSFORMKEYWORD_SETTING};";
694
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
695
                    {
696
                        dt = ds.Tables[0].Copy();
697
                    }
698
                }
699
                catch (Exception ex)
700
                {
701
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
702
                }
703
            }
704

    
705
            return dt;
706
        }
707

    
708
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
709
        {
710
            ID2Info id2Info = ID2Info.GetInstance();
711
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
712
            {
713
                try
714
                {
715
                    using (var txn = connection.BeginTransaction())
716
                    {
717
                        var query = $"DELETE FROM {PSN_HEADER_SETTING}";
718
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
719

    
720
                        foreach (HeaderInfo headerInfo in headerInfos)
721
                        {
722
                            foreach (HeaderItem item in headerInfo.HeaderItems)
723
                            {
724
                                query = $"INSERT INTO {PSN_HEADER_SETTING} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
725
                                var cmd = connection.GetSqlStringCommand(query);
726
                                AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
727
                                AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
728
                                AddWithValue(cmd, "@INDEX", item.Index);
729
                                AddWithValue(cmd, "@NAME", item.Name);
730
                                connection.ExecuteNonQuery(cmd, txn);
731
                            }
732
                        }
733
                        txn.Commit();
734
                    }
735
                    
736
                }
737
                catch (Exception ex)
738
                {
739
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
740
                    return false;
741
                }
742
            }
743
            return true;
744
        }
745

    
746
        public static bool SaveVentDrainSetting(List<VentDrainInfo> ventDrainInfos)
747
        {
748
            ID2Info id2Info = ID2Info.GetInstance();
749
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
750
            {
751
                using (var txn = connection.BeginTransaction())
752
                {
753
                    try
754
                    {
755
                        var query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
756
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
757

    
758
                        foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
759
                        {
760
                            foreach (VentDrainItem item in ventDrainInfo.VentDrainItems)
761
                            {
762
                                query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
763
                                var cmd = connection.GetSqlStringCommand(query);
764
                                AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
765
                                AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
766
                                AddWithValue(cmd, "@INDEX", item.Index);
767
                                AddWithValue(cmd, "@NAME", item.Name);
768
                                connection.ExecuteNonQuery(cmd, txn);
769
                            }
770
                        }
771

    
772
                        txn.Commit();
773
                    }
774
                    catch (Exception ex)
775
                    {
776
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
777
                        return false;
778
                    }
779
                }
780
            }
781

    
782
            return true;
783
        }
784

    
785
        public static bool SaveKeywordsSetting(List<KeywordItem> keywordItems)
786
        {
787
            ID2Info id2Info = ID2Info.GetInstance();
788
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
789
            {
790
                using (var txn = connection.BeginTransaction())
791
                {
792
                    try
793
                    {
794
                        var query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
795
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
796
                        
797
                        foreach (KeywordItem item in keywordItems)
798
                        {
799
                            query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} VALUES (@INDEX, @NAME, @KEYWORD)";
800
                            var cmd = connection.GetSqlStringCommand(query);
801
                            AddWithValue(cmd, "@INDEX", item.Index);
802
                            AddWithValue(cmd, "@NAME", item.Name);
803
                            AddWithValue(cmd, "@KEYWORD", item.Keyword);                            
804
                            connection.ExecuteNonQuery(cmd, txn);
805
                        }
806
                        
807
                        txn.Commit();
808
                    }
809
                    catch (Exception ex)
810
                    {
811
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
812
                        return false;
813
                    }
814
                }
815
            }
816

    
817
            return true;
818
        }
819

    
820
        public static bool SaveTopologyRule(DataTable dt)
821
        {
822
            ID2Info id2Info = ID2Info.GetInstance();
823
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
824
            {
825
                using (var txn = connection.BeginTransaction())
826
                {
827
                    try
828
                    {
829
                        var query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
830
                        var cmd = connection.GetSqlStringCommand(query);
831
                        cmd.ExecuteNonQuery();
832

    
833
                        foreach (DataRow row in dt.Rows)
834
                        {
835
                            query = $"INSERT INTO {PSN_TOPOLOGY_RULE} VALUES (@UID)";
836
                            cmd = connection.GetSqlStringCommand(query);
837
                            AddWithValue(cmd, "@UID", row["NAME"].ToString());
838
                            connection.ExecuteNonQuery(cmd, txn);
839
                        }
840

    
841
                        txn.Commit();
842
                    }
843
                    catch (Exception ex)
844
                    {
845
                        txn.Rollback();
846
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
847
                        return false;
848
                    }
849
                }
850
            }
851

    
852
            return true;
853
        }
854

    
855
        public static DataTable SelectTopologyRule()
856
        {
857
            DataTable dt = null;
858

    
859
            ID2Info id2Info = ID2Info.GetInstance();
860
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
861
            {
862
                try
863
                {
864
                    var query = $"SELECT * FROM {PSN_TOPOLOGY_RULE}";
865
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
866
                    {
867
                        dt = ds.Tables[0].Copy();
868
                    }
869
                }
870
                catch (Exception ex)
871
                {
872
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
873
                }
874
            }
875

    
876
            return dt;
877
        }
878

    
879
        private static void AddWithValue(DbCommand cmd, string PropName, object Value)
880
        {
881
            var param = cmd.CreateParameter();
882
            param.ParameterName = PropName;
883
            param.Value = Value;
884
            cmd.Parameters.Add(param);
885
        }
886

    
887
        public static bool SavePSNData(PSN item)
888
        {
889
            ID2Info id2Info = ID2Info.GetInstance();
890

    
891
            bool result = true;
892
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
893
            {
894
                try
895
                {
896
                    using (var txn = connection.BeginTransaction())
897
                    {
898
                        try
899
                        {
900
                            // Path Items
901
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
902
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
903
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
904
                            {
905
                                DataRow row = item.PathItems.Rows[i];
906
                                query = $"INSERT INTO {PSN_PATHITEMS} VALUES (@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME, @Equipment_OID, @NPD, @PipeSystemNetwork_OID, @ViewPipeSystemNetwork_OID, @PipeRun_OID)";
907
                                var cmd = connection.GetSqlStringCommand(query);
908
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
909
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
910
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
911
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
912
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
913
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
914
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
915
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString()); 
916
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
917
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
918
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
919
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
920
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString()); 
921
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
922
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
923
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
924
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
925
                                connection.ExecuteNonQuery(cmd, txn);
926
                            }
927

    
928
                            // Sequence
929
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
930
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
931
                            foreach (DataRow row in item.SequenceData.Rows)
932
                            {
933
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
934
                                var cmd = connection.GetSqlStringCommand(query);
935
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
936
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
937
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
938
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
939
                                connection.ExecuteNonQuery(cmd, txn);
940
                            }
941

    
942
                            // Nozzle
943
                            query = $"DELETE FROM {PSN_NOZZLE}";
944
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
945
                            foreach (DataRow row in item.Nozzle.Rows)
946
                            {
947
                                query = $"INSERT INTO {PSN_NOZZLE} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS, @Equipment_OID, @FLUID, @NPD, @ROTATION, @FlowDirection)";
948
                                var cmd = connection.GetSqlStringCommand(query);
949
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
950
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
951

    
952
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
953
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
954
                                else
955
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
956

    
957
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
958
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
959
                                else
960
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
961

    
962
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
963
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
964
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
965

    
966
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
967
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
968
                                else
969
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
970
                                
971
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
972
                                connection.ExecuteNonQuery(cmd, txn);
973
                            }
974

    
975
                            //Equipment
976
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
977
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
978
                            foreach (DataRow row in item.Equipment.Rows)
979
                            {
980
                                query = $"INSERT INTO {PSN_EQUIPMENT} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
981
                                var cmd = connection.GetSqlStringCommand(query);
982
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
983
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
984

    
985
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
986
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
987
                                else
988
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
989

    
990
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
991
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
992
                                else
993
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
994

    
995
                                connection.ExecuteNonQuery(cmd, txn);
996
                            }
997

    
998
                            // TopologySet
999
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
1000
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1001
                            foreach (DataRow row in item.TopologySet.Rows)
1002
                            {
1003
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
1004
                                var cmd = connection.GetSqlStringCommand(query);
1005
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1006
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1007
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
1008
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
1009
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
1010
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
1011
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
1012
                                connection.ExecuteNonQuery(cmd, txn);
1013
                            }
1014

    
1015
                            // PSN
1016
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
1017
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1018
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
1019
                            {
1020
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
1021
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy) VALUES " +
1022
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy)";
1023
                                var cmd = connection.GetSqlStringCommand(query);
1024
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1025
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1026
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
1027
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
1028
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
1029
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
1030
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1031
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
1032

    
1033
                                int IsValid = 0;
1034
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
1035
                                    IsValid = 0;
1036
                                else if (row["IsValid"].ToString() == "InValid")
1037
                                    IsValid = 1;
1038
                                else if (row["IsValid"].ToString() == "Error")
1039
                                    IsValid = -1;
1040

    
1041
                                AddWithValue(cmd, "@IsValid", IsValid);
1042
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
1043

    
1044
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
1045
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
1046

    
1047
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
1048
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
1049
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
1050
                                else
1051
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString());
1052

    
1053
                                connection.ExecuteNonQuery(cmd, txn);
1054
                            }
1055

    
1056
                            //Pipeline
1057
                            query = $"DELETE FROM {PSN_PIPELINE}";
1058
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1059
                            foreach (DataRow row in item.PipeLine.Rows)
1060
                            {
1061
                                query = $"INSERT INTO {PSN_PIPELINE} VALUES (@OID, @PipeSystem_OID, @FLUID, @PMC, @SEQNUMBER, @INSULATION, @FROM_DATA, @TO_DATA, @Unit)";
1062
                                var cmd = connection.GetSqlStringCommand(query);
1063
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
1064
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
1065
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
1066
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
1067
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
1068
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
1069
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
1070
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
1071
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
1072
                                connection.ExecuteNonQuery(cmd, txn);
1073
                            }
1074

    
1075
                            txn.Commit();
1076
                        }
1077
                        catch (Exception ex)
1078
                        {
1079
                            txn.Rollback();
1080
                            result = false;
1081
                        }
1082
                    }
1083
                }
1084
                catch (Exception ex)
1085
                {
1086
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1087
                    result = false;
1088
                }
1089
            }
1090

    
1091
            return result;
1092
        }
1093

    
1094
        public static bool SavePSNFluidCode(DataTable dt)
1095
        {
1096
            ID2Info id2Info = ID2Info.GetInstance();
1097

    
1098
            bool result = true;
1099
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1100
            {
1101
                try
1102
                {
1103
                    using (var txn = connection.BeginTransaction())
1104
                    {
1105
                        try
1106
                        {
1107
                            var query = $"DELETE FROM {PSN_FLUIDCODE}";
1108
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1109

    
1110
                            foreach (DataRow row in dt.Rows)
1111
                            {
1112
                                query = $"INSERT INTO {PSN_FLUIDCODE} VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1113
                                var cmd = connection.GetSqlStringCommand(query);
1114
                                cmd.Parameters.Clear();
1115

    
1116
                                {
1117
                                    var param = cmd.CreateParameter();
1118
                                    param.ParameterName = "@UID";
1119
                                    param.Value = row["UID"].ToString();
1120
                                    cmd.Parameters.Add(param);
1121
                                }
1122

    
1123
                                {
1124
                                    var param = cmd.CreateParameter();
1125
                                    param.ParameterName = "@Code";
1126
                                    param.Value = row["Code"].ToString();
1127
                                    cmd.Parameters.Add(param);
1128
                                }
1129

    
1130
                                {
1131
                                    var param = cmd.CreateParameter();
1132
                                    param.ParameterName = "@Description";
1133
                                    param.Value = row["Description"].ToString();
1134
                                    cmd.Parameters.Add(param);
1135
                                }
1136

    
1137
                                {
1138
                                    var param = cmd.CreateParameter();
1139
                                    param.ParameterName = "@Condition";
1140
                                    param.Value = row["Condition"].ToString();
1141
                                    cmd.Parameters.Add(param);
1142
                                }
1143

    
1144
                                {
1145
                                    var param = cmd.CreateParameter();
1146
                                    param.ParameterName = "@Remarks";
1147
                                    param.Value = row["Remarks"].ToString();
1148
                                    cmd.Parameters.Add(param);
1149
                                }
1150

    
1151
                                {
1152
                                    var param = cmd.CreateParameter();
1153
                                    param.ParameterName = "@GroundLevel";
1154
                                    param.Value = row["GroundLevel"].ToString();
1155
                                    cmd.Parameters.Add(param);
1156
                                }
1157

    
1158
                                connection.ExecuteNonQuery(cmd, txn);
1159
                            }
1160
                            txn.Commit();
1161
                        }
1162
                        catch (Exception ex)
1163
                        {
1164
                            txn.Rollback();
1165
                            result = false;
1166
                        }
1167
                    }
1168
                }
1169
                catch (Exception ex)
1170
                {
1171
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1172
                    result = false;
1173
                }
1174
            }
1175

    
1176
            return result;
1177
        }
1178

    
1179
        public static DataTable SelectPSNFluidCode()
1180
        {
1181
            DataTable dt = null;
1182
            ID2Info id2Info = ID2Info.GetInstance();
1183

    
1184
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1185
            {
1186
                try
1187
                {
1188
                    var query = $"SELECT * FROM {PSN_FLUIDCODE}";
1189
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1190
                    {
1191
                        dt = ds.Tables[0].Copy();
1192
                    }
1193
                }
1194
                catch (Exception ex)
1195
                {
1196
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1197
                }
1198
            }
1199

    
1200
            return dt;
1201
        }
1202

    
1203
        public static bool SavePSNPMC(DataTable dt)
1204
        {
1205
            ID2Info id2Info = ID2Info.GetInstance();
1206

    
1207
            bool result = true;
1208
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1209
            {
1210
                try
1211
                {
1212
                    using (var txn = connection.BeginTransaction())
1213
                    {
1214
                        try
1215
                        {
1216
                            var query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
1217
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1218

    
1219
                            foreach (DataRow row in dt.Rows)
1220
                            {
1221
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1222
                                var cmd = connection.GetSqlStringCommand(query);
1223
                                cmd.Parameters.Clear();
1224

    
1225
                                {
1226
                                    var param = cmd.CreateParameter();
1227
                                    param.ParameterName = "@UID";
1228
                                    param.Value = row["UID"].ToString();
1229
                                    cmd.Parameters.Add(param);
1230
                                }
1231

    
1232
                                {
1233
                                    var param = cmd.CreateParameter();
1234
                                    param.ParameterName = "@Priority";
1235
                                    param.Value = row["Priority"].ToString();
1236
                                    cmd.Parameters.Add(param);
1237
                                }
1238

    
1239
                                {
1240
                                    var param = cmd.CreateParameter();
1241
                                    param.ParameterName = "@Code";
1242
                                    param.Value = row["Code"].ToString();
1243
                                    cmd.Parameters.Add(param);
1244
                                }
1245

    
1246
                                {
1247
                                    var param = cmd.CreateParameter();
1248
                                    param.ParameterName = "@Description";
1249
                                    param.Value = row["Description"].ToString();
1250
                                    cmd.Parameters.Add(param);
1251
                                }
1252

    
1253
                                {
1254
                                    var param = cmd.CreateParameter();
1255
                                    param.ParameterName = "@Condition";
1256
                                    param.Value = row["Condition"].ToString();
1257
                                    cmd.Parameters.Add(param);
1258
                                }
1259

    
1260
                                {
1261
                                    var param = cmd.CreateParameter();
1262
                                    param.ParameterName = "@Remarks";
1263
                                    param.Value = row["Remarks"].ToString();
1264
                                    cmd.Parameters.Add(param);
1265
                                }
1266

    
1267
                                {
1268
                                    var param = cmd.CreateParameter();
1269
                                    param.ParameterName = "@GroundLevel";
1270
                                    param.Value = row["GroundLevel"].ToString();
1271
                                    cmd.Parameters.Add(param);
1272
                                }
1273

    
1274
                                connection.ExecuteNonQuery(cmd, txn);
1275
                            }
1276

    
1277
                            txn.Commit();
1278
                        }
1279
                        catch (Exception ex)
1280
                        {
1281
                            txn.Rollback();
1282
                            result = false;
1283
                        }
1284
                    }
1285
                }
1286
                catch (Exception ex)
1287
                {
1288
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1289
                    result = false;
1290
                }
1291
            }
1292

    
1293
            return result;
1294
        }
1295

    
1296
        public static PSN GetDBPSN()
1297
        {
1298
            PSN result = new PSN();
1299
            ID2Info id2Info = ID2Info.GetInstance();
1300

    
1301
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1302
            {
1303
                try
1304
                {
1305
                    //var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
1306
                    //using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1307
                    //{
1308
                    //    result.PipeSystemNetwork = ds.Tables[0].Clone();
1309
                    //    result.PipeSystemNetwork.Columns["IsValid"].DataType = typeof(string);
1310

    
1311
                    //    foreach (DataRow row in ds.Tables[0].Rows)
1312
                    //    {
1313
                    //        DataRow newRow = result.PipeSystemNetwork.NewRow();
1314
                    //        newRow["OID"] = row["OID"].ToString();
1315
                    //        newRow["Type"] = row["Type"].ToString();
1316
                    //        newRow["OrderNumber"] = row["OrderNumber"].ToString();
1317
                    //        newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
1318
                    //        newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
1319
                    //        newRow["TO_DATA"] = row["TO_DATA"].ToString();
1320
                    //        newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
1321
                    //        newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
1322
                           
1323
                    //        string IsValid = string.Empty;
1324

    
1325
                    //        if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
1326
                    //            IsValid = string.Empty;//"OK";
1327
                    //        else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
1328
                    //            IsValid = "InValid";
1329
                    //        else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
1330
                    //            IsValid = "Error";
1331

    
1332
                    //        newRow["IsValid"] = IsValid;
1333

    
1334
                    //        newRow["Status"] = row["Status"].ToString();
1335
                    //        newRow["PBS"] = row["PBS"].ToString();
1336
                    //        newRow["Drawings"] = row["Drawings"].ToString();
1337

    
1338
                    //        newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1339
                    //        newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
1340

    
1341
                    //        result.PipeSystemNetwork.Rows.Add(newRow);
1342
                    //    }
1343
                    //}
1344

    
1345
                    var query = $"SELECT * FROM {PSN_EQUIPMENT}";
1346
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1347
                    {
1348
                        result.Equipment = ds.Tables[0].Copy();
1349
                    }
1350

    
1351
                    query = $"SELECT * FROM {PSN_NOZZLE}";
1352
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1353
                    {
1354
                        result.Nozzle = ds.Tables[0].Copy();
1355
                    }
1356

    
1357
                    query = $"SELECT * FROM {PSN_PATHITEMS}";
1358
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1359
                    {
1360
                        result.PathItems = ds.Tables[0].Copy();
1361
                    }
1362

    
1363
                    query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
1364
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1365
                    {
1366
                        result.SequenceData = ds.Tables[0].Copy();
1367
                    }
1368

    
1369
                    query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
1370
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1371
                    {
1372
                        result.TopologySet = ds.Tables[0].Copy();
1373
                    }
1374

    
1375
                    query = $"SELECT * FROM {PSN_PIPELINE}";
1376
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1377
                    {
1378
                        result.PipeLine = ds.Tables[0].Copy();
1379
                    }                    
1380

    
1381
                    result.Revision = GetRevision();
1382
                }
1383
                catch (Exception ex)
1384
                {
1385
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1386
                    result = null;
1387
                }
1388
            }
1389

    
1390
            return result;
1391
        }
1392

    
1393
        public static int GetRevision()
1394
        {
1395
            int result = 0;
1396
            ID2Info id2Info = ID2Info.GetInstance();
1397

    
1398
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1399
            {
1400
                try
1401
                {
1402
                    var query = $"SELECT DISTINCT PSNRevisionNumber FROM {PSN_PIPESYSTEMNETWORK};";
1403
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1404
                    {
1405
                        foreach (DataRow row in ds.Tables[0].Rows)
1406
                        {
1407
                            string value = row["PSNRevisionNumber"].ToString();
1408
                            if (value.StartsWith("V"))
1409
                                value = value.Remove(0, 1);
1410
                            int revisionNumber = Convert.ToInt32(value);
1411
                            if (result < revisionNumber)
1412
                                result = revisionNumber;
1413
                        }
1414
                    }
1415
                }
1416
                catch (Exception ex)
1417
                {
1418
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1419
                    result = -1;
1420
                }
1421
            }
1422

    
1423
            return result;
1424
        }
1425

    
1426
        public static DataTable GetPathItem()
1427
        {
1428
            DataTable dt = null;
1429

    
1430
            ID2Info id2Info = ID2Info.GetInstance();
1431
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1432
            {
1433
                try
1434
                {
1435
                    var query = $"SELECT * FROM {PSN_PATHITEMS}";
1436
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1437
                    {
1438
                        dt = ds.Tables[0].Copy();
1439
                    }
1440
                }
1441
                catch (Exception ex)
1442
                {
1443
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1444
                }
1445
            }
1446

    
1447
            return dt;
1448
        }
1449

    
1450
        public static DataTable GetTopologySet()
1451
        {
1452
            DataTable dt = null;
1453

    
1454
            ID2Info id2Info = ID2Info.GetInstance();
1455
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1456
            {
1457
                try
1458
                {
1459
                    var query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
1460
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1461
                    {
1462
                        dt = ds.Tables[0].Clone();
1463
                        foreach (DataRow row in ds.Tables[0].Rows)
1464
                        {
1465
                            DataRow newRow = dt.NewRow();
1466
                            newRow["OID"] = row["OID"].ToString();
1467
                            newRow["Type"] = row["Type"].ToString();
1468
                            newRow["SubType"] = row["SubType"].ToString();
1469
                            newRow["HeadItemTag"] = row["HeadItemTag"].ToString();
1470
                            newRow["TailItemTag"] = row["TailItemTag"].ToString();
1471
                            newRow["HeadItemSPID"] = row["HeadItemSPID"].ToString();
1472
                            newRow["TailItemSPID"] = row["TailItemSPID"].ToString();
1473
                            dt.Rows.Add(newRow);
1474
                        }
1475
                    }
1476
                }
1477
                catch (Exception ex)
1478
                {
1479
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1480
                }
1481
            }
1482

    
1483
            return dt;
1484
        }
1485

    
1486
        public static DataTable GetPipeSystemNetwork()
1487
        {
1488
            DataTable dt = null;
1489

    
1490
            ID2Info id2Info = ID2Info.GetInstance();
1491
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1492
            {
1493
                try
1494
                {
1495
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
1496
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1497
                    {
1498
                        dt = ds.Tables[0].Clone();
1499
                        dt.Columns["IsValid"].DataType = typeof(string);
1500
                        foreach (DataRow row in ds.Tables[0].Rows)
1501
                        {
1502
                            DataRow newRow = dt.NewRow();
1503
                            newRow["OID"] = row["OID"].ToString();
1504
                            newRow["Type"] = row["Type"].ToString();
1505
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
1506
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
1507
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
1508
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
1509
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
1510
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
1511

    
1512
                            string IsValid = string.Empty;
1513

    
1514
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
1515
                                IsValid = string.Empty;//"OK";
1516
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
1517
                                IsValid = "InValid";
1518
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
1519
                                IsValid = "Error";
1520

    
1521
                            newRow["IsValid"] = IsValid;
1522
                            newRow["Status"] = row["Status"].ToString();
1523

    
1524
                            newRow["PBS"] = row["PBS"].ToString();
1525
                            newRow["Drawings"] = row["Drawings"].ToString();
1526
                            
1527
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1528
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();                                           
1529
                                                        
1530
                            dt.Rows.Add(newRow);
1531
                        }
1532
                    }
1533
                }
1534
                catch (Exception ex)
1535
                {
1536
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1537
                }
1538
            }
1539

    
1540
            return dt;
1541
        }
1542

    
1543
        public static DataTable GetSequenceData()
1544
        {
1545
            DataTable dt = null;
1546

    
1547
            ID2Info id2Info = ID2Info.GetInstance();
1548
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1549
            {
1550
                try
1551
                {
1552
                    var query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
1553
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1554
                    {
1555
                        dt = ds.Tables[0].Copy();
1556
                    }
1557
                }
1558
                catch (Exception ex)
1559
                {
1560
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1561
                }
1562
            }
1563

    
1564
            return dt;
1565
        }
1566
    }
1567
}
클립보드 이미지 추가 (최대 크기: 500 MB)