프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ 0ef28d6e

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

    
57
                    }
58
                    connection.Close();
59
                }
60
            }
61
            catch (Exception ex)
62
            {
63
                System.Windows.Forms.MessageBox.Show(ex.Message);
64
            }
65

    
66
            dt.AcceptChanges();
67
            dt.DefaultView.Sort = "Name";
68
            dt = dt.DefaultView.ToTable();
69

    
70
            return dt;
71
        }
72

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

    
83
            using (IAbstractDatabase connection = id2Info.CreateConnection())
84
            {
85
                try
86
                {
87
                    var names = connection.GetTableNames();
88
                    var matched = names.FirstOrDefault(param => param == PSN_HEADER_SETTING);
89
                    Dictionary<string, string> dicColCheck = new Dictionary<string, string>();
90
                    dicColCheck.Add("GROUP_ID", "TEXT");
91
                    dicColCheck.Add("DESCRIPTION", "TEXT");
92
                    dicColCheck.Add("INDEX", "INTEGER");
93
                    dicColCheck.Add("NAME", "TEXT");
94

    
95
                    if (matched == null)
96
                    {
97
                        var query = $"CREATE TABLE {PSN_HEADER_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
98
                        using (var cmd = connection.GetSqlStringCommand(query))
99
                        {
100
                            cmd.ExecuteNonQuery();
101
                        }
102
                    }
103
                    else
104
                    {
105
                        AddColumn(PSN_HEADER_SETTING, dicColCheck);
106
                    }
107

    
108
                    matched = names.FirstOrDefault(param => param == PSN_VENTDRAIN_SETTING);
109
                    dicColCheck.Clear();
110
                    dicColCheck.Add("GROUP_ID", "TEXT");
111
                    dicColCheck.Add("DESCRIPTION", "TEXT");
112
                    dicColCheck.Add("INDEX", "INTEGER");
113
                    dicColCheck.Add("NAME", "TEXT");
114
                    if (matched == null)
115
                    {
116
                        var query = $"CREATE TABLE {PSN_VENTDRAIN_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
117
                        using (var cmd = connection.GetSqlStringCommand(query))
118
                        {
119
                            cmd.ExecuteNonQuery();
120
                        }
121
                    }
122
                    else
123
                    {
124
                        AddColumn(PSN_VENTDRAIN_SETTING, dicColCheck);
125
                    }
126

    
127
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGY_RULE);
128
                    dicColCheck.Clear();
129
                    dicColCheck.Add("UID", "TEXT");
130
                    if (matched == null)
131
                    {
132
                        var query = $"CREATE TABLE {PSN_TOPOLOGY_RULE} (UID TEXT)";
133
                        using (var cmd = connection.GetSqlStringCommand(query))
134
                        {
135
                            cmd.ExecuteNonQuery();
136
                        }
137

    
138
                        DataTable topologyRule = new DataTable();
139
                        topologyRule.Columns.Add("NAME", typeof(string));
140

    
141
                        topologyRule.Rows.Add("FluidCode");
142
                        topologyRule.Rows.Add("-");
143
                        topologyRule.Rows.Add("PipingMaterialsClass");
144
                        topologyRule.Rows.Add("-");
145
                        topologyRule.Rows.Add("Tag Seq No");
146

    
147
                        SaveTopologyRule(topologyRule);
148
                    }
149
                    //else
150
                    //{
151
                    //    AddColumn(PSN_TOPOLOGY_RULE, dicColCheck);
152
                    //}
153

    
154
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
155
                    dicColCheck.Clear();
156
                    dicColCheck.Add("OID",                      "NVARCHAR(255)");
157
                    dicColCheck.Add("Type",                     "NVARCHAR(255)");
158
                    dicColCheck.Add("OrderNumber",              "NVARCHAR(255)");
159
                    dicColCheck.Add("Pipeline_OID",             "NVARCHAR(255)");
160
                    dicColCheck.Add("From_Data",                "NVARCHAR(255)");
161
                    dicColCheck.Add("To_Data", "NVARCHAR(255)");
162
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(125)");
163
                    dicColCheck.Add("PSNRevisionNumber", "NVARCHAR(255)");
164
                    dicColCheck.Add("IsValid", "INT");
165
                    dicColCheck.Add("Status", "NVARCHAR(255)");
166
                    dicColCheck.Add("PBS", "NVARCHAR(255)");
167
                    dicColCheck.Add("Drawings", "NVARCHAR(255)");
168
                    dicColCheck.Add("IncludingVirtualData", "NVARCHAR(10)");
169
                    dicColCheck.Add("PSNAccuracy", "REAL"); 
170

    
171
                    if (matched == null)
172
                    {
173
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(255), Type NVARCHAR(255), OrderNumber NVARCHAR(255), Pipeline_OID NVARCHAR(255), From_Data NVARCHAR(255), " +
174
                            "To_Data NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
175
                            "IncludingVirtualData NVARCHAR(10),  PSNAccuracy REAL)";
176
                        using (var cmd = connection.GetSqlStringCommand(query))
177
                        {
178
                            cmd.ExecuteNonQuery();
179
                        }
180
                    }
181
                    else
182
                    {
183
                        AddColumn(PSN_PIPESYSTEMNETWORK, dicColCheck);
184
                    }
185

    
186
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
187
                    dicColCheck.Clear();
188
                    dicColCheck.Add("OID", "NVARCHAR(255)");
189
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
190
                    dicColCheck.Add("Xcoords", "REAL");
191
                    dicColCheck.Add("Ycoords", "REAL");
192
                    if (matched == null)
193
                    {
194
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL)";
195
                        using (var cmd = connection.GetSqlStringCommand(query))
196
                        {
197
                            cmd.ExecuteNonQuery();
198
                        }
199
                    }
200
                    else
201
                    {
202
                        AddColumn(PSN_EQUIPMENT, dicColCheck);
203
                    }
204

    
205
                    dicColCheck.Clear();
206
                    dicColCheck.Add("OID", "NVARCHAR(255)");
207
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
208
                    dicColCheck.Add("Xcoords", "REAL");
209
                    dicColCheck.Add("Ycoords", "REAL");
210
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
211
                    dicColCheck.Add("Fluid", "NVARCHAR(255)");
212
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
213
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
214
                    dicColCheck.Add("Rotation", "REAL");
215
                    dicColCheck.Add("FlowDirection", "NVARCHAR(255)");
216

    
217
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
218
                    if (matched == null)
219
                    {
220
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL, Equipment_OID NVARCHAR(255), " +
221
                            "Fluid NVARCHAR(255), NPD NVARCHAR(255), PMC NVARCHAR(255), Rotation REAL, FlowDirection NVARCHAR(255))";
222
                        using (var cmd = connection.GetSqlStringCommand(query))
223
                        {
224
                            cmd.ExecuteNonQuery();
225
                        }
226
                    }
227
                    else
228
                    {
229
                        AddColumn(PSN_NOZZLE, dicColCheck);
230
                    }
231

    
232
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
233
                    dicColCheck.Clear();
234
                    dicColCheck.Add("UID", "NVARCHAR(50)");
235
                    dicColCheck.Add("Code", "NVARCHAR(255)");
236
                    dicColCheck.Add("Description", "NVARCHAR(255)");
237
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
238
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
239
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
240
                    if (matched == null)
241
                    {
242
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
243
                        using (var cmd = connection.GetSqlStringCommand(query))
244
                        {
245
                            cmd.ExecuteNonQuery();
246
                        }
247
                    }
248
                    else
249
                    {
250
                        AddColumn(PSN_FLUIDCODE, dicColCheck);
251
                    }
252

    
253
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
254
                    dicColCheck.Clear();
255
                    dicColCheck.Add("UID", "NVARCHAR(50)");
256
                    dicColCheck.Add("Priority", "INTEGER");
257
                    dicColCheck.Add("Code", "NVARCHAR(255)");
258
                    dicColCheck.Add("Description", "NVARCHAR(255)");
259
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
260
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
261
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
262
                    if (matched == null)
263
                    {
264
                        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))";
265
                        using (var cmd = connection.GetSqlStringCommand(query))
266
                        {
267
                            cmd.ExecuteNonQuery();
268
                        }
269
                    }
270
                    else
271
                    {
272
                        AddColumn(PSN_PIPINGMATLCLASS, dicColCheck);
273
                    }
274

    
275
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
276
                    dicColCheck.Clear();
277
                    dicColCheck.Add("OID", "NVARCHAR(255)");
278
                    dicColCheck.Add("SERIALNUMBER", "NVARCHAR(255)");
279
                    dicColCheck.Add("PathItem_OID", "NVARCHAR(255)");
280
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(255)");
281
                    if (matched == null)
282
                    {
283
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(255), SERIALNUMBER NVARCHAR(255), PathItem_OID NVARCHAR(255), TopologySet_OID_Key NVARCHAR(255))";
284
                        using (var cmd = connection.GetSqlStringCommand(query))
285
                        {
286
                            cmd.ExecuteNonQuery();
287
                        }
288
                    }
289
                    else
290
                    {
291
                        AddColumn(PSN_SEQUENCEDATA, dicColCheck);
292
                    }
293

    
294
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
295
                    dicColCheck.Clear();
296
                    dicColCheck.Add("OID", "NVARCHAR(255)");
297
                    dicColCheck.Add("SequenceData_OID", "NVARCHAR(255)");
298
                    dicColCheck.Add("TopologySet_OID", "NVARCHAR(255)");
299
                    dicColCheck.Add("BranchTopologySet_OID", "NVARCHAR(255)");
300
                    dicColCheck.Add("PipeLine_OID", "NVARCHAR(255)");
301
                    dicColCheck.Add("ItemName", "NVARCHAR(255)");
302
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
303
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
304
                    dicColCheck.Add("CLASS", "NVARCHAR(255)");
305
                    dicColCheck.Add("SUBCLASS", "NVARCHAR(255)");
306
                    dicColCheck.Add("TYPE", "NVARCHAR(255)");
307
                    dicColCheck.Add("PIDNAME", "NVARCHAR(255)");
308
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
309
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
310
                    dicColCheck.Add("PipeSystemNetwork_OID", "NVARCHAR(255)");
311
                    dicColCheck.Add("PipeRun_OID", "NVARCHAR(255)");
312
                    dicColCheck.Add("ViewPipeSystemNetwork_OID", "NVARCHAR(255)");
313
                    if (matched == null)
314
                    {
315
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
316
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
317
                            "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), " +
318
                            "ViewPipeSystemNetwork_OID NVARCHAR(255))";
319
                        using (var cmd = connection.GetSqlStringCommand(query))
320
                        {
321
                            cmd.ExecuteNonQuery();
322
                        }
323
                    }
324
                    else
325
                    {
326
                        AddColumn(PSN_PATHITEMS, dicColCheck);
327
                    }
328

    
329
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
330
                    dicColCheck.Clear();
331
                    dicColCheck.Add("OID", "TEXT");
332
                    if (matched == null)
333
                    {
334
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
335
                        using (var cmd = connection.GetSqlStringCommand(query))
336
                        {
337
                            cmd.ExecuteNonQuery();
338
                        }
339
                    }
340

    
341
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
342
                    dicColCheck.Clear();
343
                    dicColCheck.Add("OID", "NVARCHAR(255)");
344
                    dicColCheck.Add("Type", "NVARCHAR(255)");
345
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
346
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
347
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
348
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
349
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
350

    
351
                    if (matched == null)
352
                    {
353
                        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))";
354
                        using (var cmd = connection.GetSqlStringCommand(query))
355
                        {
356
                            cmd.ExecuteNonQuery();
357
                        }
358
                    }
359
                    else
360
                    {
361
                        AddColumn(PSN_TOPOLOGYSET, dicColCheck);
362
                    }
363

    
364
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
365
                    dicColCheck.Clear();
366
                    dicColCheck.Add("[INDEX]", "INTEGER");
367
                    dicColCheck.Add("[NAME]", "TEXT");
368
                    dicColCheck.Add("[KEYWORD]", "TEXT");
369
                    if (matched == null)
370
                    {
371
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
372
                        using (var cmd = connection.GetSqlStringCommand(query))
373
                        {
374
                            cmd.ExecuteNonQuery();
375
                        }
376
                    }
377
                    else
378
                    {
379
                        AddColumn(PSN_TRANSFORMKEYWORD_SETTING, dicColCheck);
380
                    }
381

    
382
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
383
                    dicColCheck.Clear();
384
                    dicColCheck.Add("OID", "NVARCHAR(255)");
385
                    dicColCheck.Add("PipeSystem_OID", "NVARCHAR(255)");
386
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
387
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
388
                    dicColCheck.Add("SEQNUMBER", "NVARCHAR(255)");
389
                    dicColCheck.Add("INSULATION", "NVARCHAR(255)");
390
                    dicColCheck.Add("FROM_DATA", "NVARCHAR(255)");
391
                    dicColCheck.Add("TO_DATA", "NVARCHAR(255)");
392
                    dicColCheck.Add("Unit", "NVARCHAR(100)");
393
                    if (matched == null)
394
                    {
395
                        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), " +
396
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
397
                        using (var cmd = connection.GetSqlStringCommand(query))
398
                        {
399
                            cmd.ExecuteNonQuery();
400
                        }
401
                    }
402
                    else
403
                    {
404
                        AddColumn(PSN_PIPELINE, dicColCheck);
405
                    }
406

    
407
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
408
                    dicColCheck.Clear();
409
                    dicColCheck.Add("UID", "NVARCHAR(50)");
410
                    dicColCheck.Add("Code", "NVARCHAR(255)");
411
                    dicColCheck.Add("Description", "NVARCHAR(255)");
412
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
413
                    if (matched == null)
414
                    {
415
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
416
                        using (var cmd = connection.GetSqlStringCommand(query))
417
                        {
418
                            cmd.ExecuteNonQuery();
419
                        }
420
                    }
421
                    else
422
                    {
423
                        AddColumn(PSN_INSULATIONPURPOSE, dicColCheck);
424
                    }
425

    
426
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
427
                    {
428
                        var colnames = connection.GetColumnNames(TableName);
429
                        bool check = false;
430
                        if (colnames != null)
431
                        {
432
                            foreach (KeyValuePair<string, string> col in dicCol)
433
                            {
434
                                check = false;
435
                                foreach (string c in colnames)
436
                                {
437
                                    if (col.Key.Contains(c))
438
                                    {
439
                                        check = true;
440
                                        break;
441
                                    }
442
                                }
443

    
444
                                if (!check) //없으면 추가
445
                                {
446
                                    string i = string.Empty;
447
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
448
                                        i = "DEFAULT 0";
449

    
450
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
451
                                    using (var cmd = connection.GetSqlStringCommand(query))
452
                                    {
453
                                        cmd.ExecuteNonQuery();
454
                                    }
455
                                }
456
                            }
457
                        }
458
                    }
459

    
460
                    result = true;
461
                }
462
                catch (Exception ex)
463
                {
464
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
465
                }
466
            }
467

    
468
            return result;
469
        }
470

    
471
        // ID2 DB 데이터
472
        /// <summary>
473
        /// ID2 데이타베이스에서 OPC 데이터를 조회
474
        /// </summary>
475
        /// <returns></returns>
476
        public static DataTable SelectOPCRelations()
477
        {
478
            DataTable dt = null;
479
            ID2Info id2Info = ID2Info.GetInstance();
480

    
481
            using (IAbstractDatabase connection = id2Info.CreateConnection())
482
            {
483
                try
484
                {
485
                    var query = "SELECT * FROM OPCRelations;";
486
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
487
                    {
488
                        dt = ds.Tables[0].Copy();
489
                    }
490
                }
491
                catch (Exception ex)
492
                {
493
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
494
                }
495
            }
496

    
497
            return dt;
498
        }
499

    
500
        /// <summary>
501
        /// ID2 데이타베이스에서 도면 데이터를 조회
502
        /// </summary>
503
        /// <returns></returns>
504
        public static DataTable SelectDrawings()
505
        {
506
            DataTable dt = null;
507
            ID2Info id2Info = ID2Info.GetInstance();
508

    
509
            using (IAbstractDatabase connection = id2Info.CreateConnection())
510
            {
511
                try
512
                {
513
                    var query = "SELECT * FROM Drawings";
514
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
515
                    {
516
                        dt = ds.Tables[0].Copy();
517
                    }
518
                }
519
                catch (Exception ex)
520
                {
521
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
522
                }
523
            }
524

    
525
            return dt;
526
        }
527

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

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

    
549
            return dt;
550
        }
551

    
552
        public static DataTable SelectFluidCode()
553
        {
554
            DataTable dt = null;
555
            ID2Info id2Info = ID2Info.GetInstance();
556

    
557
            using (IAbstractDatabase connection = id2Info.CreateConnection())
558
            {
559
                try
560
                {
561
                    var query = "SELECT * FROM FluidCode";
562
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
563
                    {
564
                        dt = ds.Tables[0].Copy();
565
                    }
566
                }
567
                catch (Exception ex)
568
                {
569
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
570
                }
571
            }
572

    
573
            return dt;
574
        }
575

    
576
        public static DataTable SelectPipingMaterialsClass()
577
        {
578
            DataTable dt = null;
579
            ID2Info id2Info = ID2Info.GetInstance();
580

    
581
            using (IAbstractDatabase connection = id2Info.CreateConnection())
582
            {
583
                try
584
                {
585
                    var query = "SELECT * FROM PipingMaterialsClass";
586
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
587
                    {
588
                        dt = ds.Tables[0].Copy();
589
                    }
590
                }
591
                catch (Exception ex)
592
                {
593
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
594
                }
595
            }
596

    
597
            return dt;
598
        }
599

    
600
        public static DataTable SelectPSNPIPINGMATLCLASS()
601
        {
602
            DataTable dt = null;
603
            ID2Info id2Info = ID2Info.GetInstance();
604

    
605
            using (IAbstractDatabase connection = id2Info.CreateConnection())
606
            {
607
                try
608
                {
609
                    var query = $"SELECT * FROM {PSN_PIPINGMATLCLASS}";
610
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
611
                    {
612
                        dt = ds.Tables[0].Copy();
613
                    }
614
                }
615
                catch (Exception ex)
616
                {
617
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
618
                }
619
            }
620

    
621
            return dt;
622
        }
623
        
624
        public static DataTable SelectInsulationPurpose()
625
        {
626
            DataTable dt = null;
627
            ID2Info id2Info = ID2Info.GetInstance();
628

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

    
645
            return dt;
646
        }
647

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

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

    
669
            return dt;
670
        }
671

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

    
677
            using (IAbstractDatabase connection = id2Info.CreateConnection())
678
            {
679
                try
680
                {
681
                    var query = "SELECT * FROM NominalDiameter ORDER BY Metric DESC";
682
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
683
                    {
684
                        dt = ds.Tables[0].Copy();
685
                    }
686
                }
687
                catch (Exception ex)
688
                {
689
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
690
                }
691
            }
692

    
693
            ///TODO: need to check below code
694
            dt.Rows.RemoveAt(0);
695
            dt.Rows.RemoveAt(0);
696
            dt.Rows.RemoveAt(0);
697
            dt.Rows.RemoveAt(0);
698

    
699
            return dt;
700
        }
701

    
702
        public static DataTable SelectSymbolAttribute()
703
        {
704
            DataTable dt = null;
705
            ID2Info id2Info = ID2Info.GetInstance();
706

    
707
            using (IAbstractDatabase connection = id2Info.CreateConnection())
708
            {
709
                try
710
                {
711
                   // var query = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
712
                    var query = "SELECT DISTINCT Attribute FROM SymbolAttribute;";
713
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
714
                    {
715
                        dt = ds.Tables[0].Copy();
716
                    }
717
                }
718
                catch (Exception ex)
719
                {
720
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
721
                }
722
            }
723

    
724
            return dt;
725
        }
726

    
727
        public static DataTable SelectSymbolName()
728
        {
729
            DataTable dt = null;
730
            ID2Info id2Info = ID2Info.GetInstance();
731

    
732
            using (IAbstractDatabase connection = id2Info.CreateConnection())
733
            {
734
                try
735
                {
736
                    var query = "SELECT * FROM SymbolName;";
737
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
738
                    {
739
                        dt = ds.Tables[0].Copy();
740
                    }
741
                }
742
                catch (Exception ex)
743
                {
744
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
745
                }
746
            }
747

    
748
            return dt;
749
        }
750

    
751
        public static double[] GetDrawingSize()
752
        {
753
            double[] result = null;
754

    
755
            ID2Info id2Info = ID2Info.GetInstance();
756
            using (IAbstractDatabase connection = id2Info.CreateConnection())
757
            {
758
                try
759
                {
760
                    var query = "SELECT value FROM Configuration WHERE Section = 'Area' AND [Key] = 'Drawing';";
761
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
762
                    {
763
                        if (ds.Tables[0].Rows.Count == 1)
764
                        {
765
                            string value = ds.Tables[0].Rows[0][0].ToString();
766
                            string[] split = value.Split(new char[] { ',' });
767
                            result = new double[] {
768
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", string.Empty)),
769
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", string.Empty)),
770
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", string.Empty)),
771
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", string.Empty))
772
                                };
773
                            result = new double[] {
774
                                Math.Min(result[0], result[2]),
775
                                Math.Min(result[1], result[3]),
776
                                Math.Max(result[0], result[2]),
777
                                Math.Max(result[1], result[3])
778
                                };
779
                        }
780
                    }
781
                }
782
                catch (Exception ex)
783
                {
784
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
785
                }
786
            }
787

    
788
            return result;
789
        }
790

    
791
        public static DataTable GetEquipmentType()
792
        {
793
            DataTable dt = null;
794
            ID2Info id2Info = ID2Info.GetInstance();
795

    
796
            using (IAbstractDatabase connection = id2Info.CreateConnection())
797
            {
798
                try
799
                {
800
                    var query = "SELECT Type FROM SymbolType WHERE Category = 'Equipment';";
801
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
802
                    {
803
                        dt = ds.Tables[0].Copy();
804
                    }
805
                }
806
                catch (Exception ex)
807
                {
808
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
809
                }
810
            }
811

    
812
            return dt;
813
        }
814

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

    
826
            bool result = true;
827

    
828
            using (IAbstractDatabase connection = id2Info.CreateConnection())
829
            {
830
                try
831
                {
832
                    using (var txn = connection.BeginTransaction())
833
                    {
834
                        try
835
                        {
836
                            var query = $"DELETE FROM {PSN_VIEW}";
837
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
838

    
839
                            foreach (string value in values)
840
                            {
841
                                query = $"INSERT INTO {PSN_VIEW} (OID) VALUES (@OID)";
842
                                var cmd = connection.GetSqlStringCommand(query);
843
                                AddWithValue(cmd, "@OID", value);
844
                                connection.ExecuteNonQuery(cmd, txn);
845
                            }
846
                            txn.Commit();
847
                        }
848
                        catch (Exception ex)
849
                        {
850
                            txn.Rollback();
851
                            result = false;
852
                        }
853
                    }
854
                }
855
                catch (Exception ex)
856
                {
857
                    System.Windows.Forms.MessageBox.Show(ex.Message);
858
                    result = false;
859
                }
860
            }
861

    
862
            return result;
863
        }
864

    
865
        public static bool DeleteView()
866
        {
867
            ID2Info id2Info = ID2Info.GetInstance();
868

    
869
            bool result = true;
870
            using (IAbstractDatabase connection = id2Info.CreateConnection())
871
            {
872
                try
873
                {
874
                    using (var txn = connection.BeginTransaction())
875
                    {
876
                        try
877
                        {
878
                            var query = $"DELETE FROM {PSN_VIEW}";
879
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
880
                            txn.Commit();
881
                        }
882
                        catch (Exception ex)
883
                        {
884
                            txn.Rollback();
885
                            result = false;
886
                        }
887
                    }
888
                }
889
                catch (Exception ex)
890
                {
891
                    System.Windows.Forms.MessageBox.Show(ex.Message);
892
                    result = false;
893
                }
894
            }
895

    
896
            return result;
897
        }
898

    
899
        //PSN Sqlite 
900
        public static DataTable SelectHeaderSetting()
901
        {
902
            DataTable dt = null;
903
            ID2Info id2Info = ID2Info.GetInstance();
904

    
905
            using (IAbstractDatabase connection = id2Info.CreateConnection())
906
            {
907
                try
908
                {
909
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_HEADER_SETTING};";
910
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
911
                    {
912
                        dt = ds.Tables[0].Copy();
913
                    }
914
                }
915
                catch (Exception ex)
916
                {
917
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
918
                }
919
            }
920

    
921
            return dt;
922
        }
923

    
924
        public static DataTable SelectVentDrainSetting()
925
        {
926
            DataTable dt = null;
927
            ID2Info id2Info = ID2Info.GetInstance();
928

    
929
            using (IAbstractDatabase connection = id2Info.CreateConnection())
930
            {
931
                try
932
                {
933
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_VENTDRAIN_SETTING};";
934
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
935
                    {
936
                        dt = ds.Tables[0].Copy();
937
                    }
938
                }
939
                catch (Exception ex)
940
                {
941
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
942
                }
943
            }
944

    
945
            return dt;
946
        }
947

    
948
        public static DataTable SelectKeywordsSetting()
949
        {
950
            DataTable dt = null;
951
            ID2Info id2Info = ID2Info.GetInstance();
952

    
953
            using (IAbstractDatabase connection = id2Info.CreateConnection())
954
            {
955
                try
956
                {
957
                    var query = $@"SELECT [KEYWORD], [INDEX], [NAME] FROM {PSN_TRANSFORMKEYWORD_SETTING};";
958
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
959
                    {
960
                        dt = ds.Tables[0].Copy();
961
                    }
962
                }
963
                catch (Exception ex)
964
                {
965
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
966
                }
967
            }
968

    
969
            return dt;
970
        }
971

    
972
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
973
        {
974
            ID2Info id2Info = ID2Info.GetInstance();
975
            using (IAbstractDatabase connection = id2Info.CreateConnection())
976
            {
977
                try
978
                {
979
                    using (var txn = connection.BeginTransaction())
980
                    {
981
                        var query = $"DELETE FROM {PSN_HEADER_SETTING}";
982
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
983

    
984
                        foreach (HeaderInfo headerInfo in headerInfos)
985
                        {
986
                            foreach (HeaderItem item in headerInfo.HeaderItems)
987
                            {
988
                                query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
989
                                var cmd = connection.GetSqlStringCommand(query);
990
                                AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
991
                                AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
992
                                AddWithValue(cmd, "@INDEX", item.Index);
993
                                AddWithValue(cmd, "@NAME", item.Name);
994
                                connection.ExecuteNonQuery(cmd, txn);
995
                            }
996
                        }
997
                        txn.Commit();
998
                    }
999
                    
1000
                }
1001
                catch (Exception ex)
1002
                {
1003
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1004
                    return false;
1005
                }
1006
            }
1007
            return true;
1008
        }
1009

    
1010
        public static bool SaveVentDrainSetting(List<VentDrainInfo> ventDrainInfos)
1011
        {
1012
            ID2Info id2Info = ID2Info.GetInstance();
1013
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1014
            {
1015
                using (var txn = connection.BeginTransaction())
1016
                {
1017
                    try
1018
                    {
1019
                        var query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
1020
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1021

    
1022
                        foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
1023
                        {
1024
                            foreach (VentDrainItem item in ventDrainInfo.VentDrainItems)
1025
                            {
1026
                                query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
1027
                                var cmd = connection.GetSqlStringCommand(query);
1028
                                AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
1029
                                AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
1030
                                AddWithValue(cmd, "@INDEX", item.Index);
1031
                                AddWithValue(cmd, "@NAME", item.Name);
1032
                                connection.ExecuteNonQuery(cmd, txn);
1033
                            }
1034
                        }
1035

    
1036
                        txn.Commit();
1037
                    }
1038
                    catch (Exception ex)
1039
                    {
1040
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1041
                        return false;
1042
                    }
1043
                }
1044
            }
1045

    
1046
            return true;
1047
        }
1048

    
1049
        public static bool SaveKeywordsSetting(List<KeywordItem> keywordItems)
1050
        {
1051
            ID2Info id2Info = ID2Info.GetInstance();
1052
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1053
            {
1054
                using (var txn = connection.BeginTransaction())
1055
                {
1056
                    try
1057
                    {
1058
                        var query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
1059
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1060
                        
1061
                        foreach (KeywordItem item in keywordItems)
1062
                        {
1063
                            query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
1064
                            var cmd = connection.GetSqlStringCommand(query);
1065
                            AddWithValue(cmd, "@INDEX", item.Index);
1066
                            AddWithValue(cmd, "@NAME", item.Name);
1067
                            AddWithValue(cmd, "@KEYWORD", item.Keyword);                            
1068
                            connection.ExecuteNonQuery(cmd, txn);
1069
                        }
1070
                        
1071
                        txn.Commit();
1072
                    }
1073
                    catch (Exception ex)
1074
                    {
1075
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1076
                        return false;
1077
                    }
1078
                }
1079
            }
1080

    
1081
            return true;
1082
        }
1083

    
1084
        public static bool SaveTopologyRule(DataTable dt)
1085
        {
1086
            ID2Info id2Info = ID2Info.GetInstance();
1087
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1088
            {
1089
                using (var txn = connection.BeginTransaction())
1090
                {
1091
                    try
1092
                    {
1093
                        var query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
1094
                        var cmd = connection.GetSqlStringCommand(query);
1095
                        //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1096
                        connection.ExecuteNonQuery(cmd, txn);
1097

    
1098
                        foreach (DataRow row in dt.Rows)
1099
                        {
1100
                            query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
1101
                            cmd = connection.GetSqlStringCommand(query);
1102
                            AddWithValue(cmd, "@UID", row["NAME"].ToString());
1103
                            connection.ExecuteNonQuery(cmd, txn);
1104
                        }
1105

    
1106
                        txn.Commit();
1107
                    }
1108
                    catch (Exception ex)
1109
                    {
1110
                        txn.Rollback();
1111
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1112
                        return false;
1113
                    }
1114
                }
1115
            }
1116

    
1117
            return true;
1118
        }
1119

    
1120
        public static DataTable SelectTopologyRule()
1121
        {
1122
            DataTable dt = null;
1123

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

    
1141
            return dt;
1142
        }
1143

    
1144
        private static void AddWithValue(DbCommand cmd, string PropName, object Value)
1145
        {
1146
            var param = cmd.CreateParameter();
1147
            param.ParameterName = PropName;
1148
            param.Value = Value;
1149
            cmd.Parameters.Add(param);
1150
        }
1151
             
1152
        public static bool SavePSNData(PSN item)
1153
        {
1154
            ID2Info id2Info = ID2Info.GetInstance();
1155

    
1156
            bool result = true;
1157
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1158
            {
1159
                try
1160
                {
1161
                    using (var txn = connection.BeginTransaction())
1162
                    {
1163
                        try
1164
                        {
1165
                            // Path Items
1166
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
1167
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1168
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
1169
                            {
1170
                                DataRow row = item.PathItems.Rows[i];
1171
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
1172
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, " +
1173
                                    $"PIDNAME, Equipment_OID, NPD, PipeSystemNetwork_OID, PipeRun_OID, ViewPipeSystemNetwork_OID) VALUES " +
1174
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME, @Equipment_OID, @NPD, @PipeSystemNetwork_OID,@PipeRun_OID, @ViewPipeSystemNetwork_OID)";
1175
                                var cmd = connection.GetSqlStringCommand(query);
1176
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1177
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
1178
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
1179
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
1180
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
1181
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
1182
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1183
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString()); 
1184
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
1185
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
1186
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
1187
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
1188
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString()); 
1189
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1190
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
1191
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
1192
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
1193
                                connection.ExecuteNonQuery(cmd, txn);
1194
                            }
1195

    
1196
                            // Sequence
1197
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
1198
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1199
                            foreach (DataRow row in item.SequenceData.Rows)
1200
                            {
1201
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
1202
                                var cmd = connection.GetSqlStringCommand(query);
1203
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1204
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
1205
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
1206
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1207
                                connection.ExecuteNonQuery(cmd, txn);
1208
                            }
1209

    
1210
                            // Nozzle
1211
                            query = $"DELETE FROM {PSN_NOZZLE}";
1212
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1213
                            foreach (DataRow row in item.Nozzle.Rows)
1214
                            {
1215
                                query = $"INSERT INTO {PSN_NOZZLE} (OID, ItemTag, Xcoords, Ycoords, Equipment_OID, Fluid, NPD, PMC, Rotation, FlowDirection) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS, @Equipment_OID, @FLUID, @NPD, @PMC, @ROTATION, @FlowDirection)";
1216
                                var cmd = connection.GetSqlStringCommand(query);
1217
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1218
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1219

    
1220
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1221
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1222
                                else
1223
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1224

    
1225
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1226
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1227
                                else
1228
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1229

    
1230
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1231
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
1232
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1233
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
1234

    
1235
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
1236
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
1237
                                else
1238
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
1239
                                
1240
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
1241
                                connection.ExecuteNonQuery(cmd, txn);
1242
                            }
1243

    
1244
                            //Equipment
1245
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
1246
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1247
                            foreach (DataRow row in item.Equipment.Rows)
1248
                            {
1249
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
1250
                                var cmd = connection.GetSqlStringCommand(query);
1251
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1252
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1253

    
1254
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1255
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1256
                                else
1257
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1258

    
1259
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1260
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1261
                                else
1262
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1263

    
1264
                                connection.ExecuteNonQuery(cmd, txn);
1265
                            }
1266

    
1267
                            // TopologySet
1268
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
1269
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1270
                            foreach (DataRow row in item.TopologySet.Rows)
1271
                            {
1272
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
1273
                                var cmd = connection.GetSqlStringCommand(query);
1274
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1275
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1276
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
1277
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
1278
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
1279
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
1280
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
1281
                                connection.ExecuteNonQuery(cmd, txn);
1282
                            }
1283

    
1284
                            // PSN
1285
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
1286
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1287
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
1288
                            {
1289
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
1290
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy) VALUES " +
1291
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy)";
1292
                                var cmd = connection.GetSqlStringCommand(query);
1293
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1294
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1295
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
1296
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
1297
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
1298
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
1299
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1300
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
1301

    
1302
                                int IsValid = 0;
1303
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
1304
                                    IsValid = 0;
1305
                                else if (row["IsValid"].ToString() == "InValid")
1306
                                    IsValid = 1;
1307
                                else if (row["IsValid"].ToString() == "Error")
1308
                                    IsValid = -1;
1309

    
1310
                                AddWithValue(cmd, "@IsValid", IsValid);
1311
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
1312

    
1313
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
1314
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
1315

    
1316
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
1317
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
1318
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
1319
                                else
1320
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString());
1321

    
1322
                                connection.ExecuteNonQuery(cmd, txn);
1323
                            }
1324

    
1325
                            //Pipeline
1326
                            query = $"DELETE FROM {PSN_PIPELINE}";
1327
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1328
                            foreach (DataRow row in item.PipeLine.Rows)
1329
                            {
1330
                                query = $"INSERT INTO {PSN_PIPELINE} (OID, PipeSystem_OID, FLUID, PMC, SEQNUMBER, INSULATION, FROM_DATA, TO_DATA, Unit) VALUES (@OID, @PipeSystem_OID, @FLUID, @PMC, @SEQNUMBER, @INSULATION, @FROM_DATA, @TO_DATA, @Unit)";
1331
                                var cmd = connection.GetSqlStringCommand(query);
1332
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
1333
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
1334
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
1335
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
1336
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
1337
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
1338
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
1339
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
1340
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
1341
                                connection.ExecuteNonQuery(cmd, txn);
1342
                            }
1343

    
1344
                            txn.Commit();
1345
                        }
1346
                        catch (Exception ex)
1347
                        {
1348
                            txn.Rollback();
1349
                            result = false;
1350
                        }
1351
                    }
1352
                }
1353
                catch (Exception ex)
1354
                {
1355
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1356
                    result = false;
1357
                }
1358
            }
1359

    
1360
            return result;
1361
        }
1362

    
1363
        public static bool SavePSNFluidCode(DataTable dt)
1364
        {
1365
            ID2Info id2Info = ID2Info.GetInstance();
1366

    
1367
            bool result = true;
1368
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1369
            {
1370
                try
1371
                {
1372
                    using (var txn = connection.BeginTransaction())
1373
                    {
1374
                        try
1375
                        {
1376
                            var query = $"DELETE FROM {PSN_FLUIDCODE}";
1377
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1378

    
1379
                            foreach (DataRow row in dt.Rows)
1380
                            {
1381
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1382
                                var cmd = connection.GetSqlStringCommand(query);
1383
                                cmd.Parameters.Clear();
1384

    
1385
                                {
1386
                                    var param = cmd.CreateParameter();
1387
                                    param.ParameterName = "@UID";
1388
                                    param.Value = row["UID"].ToString();
1389
                                    cmd.Parameters.Add(param);
1390
                                }
1391

    
1392
                                {
1393
                                    var param = cmd.CreateParameter();
1394
                                    param.ParameterName = "@Code";
1395
                                    param.Value = row["Code"].ToString();
1396
                                    cmd.Parameters.Add(param);
1397
                                }
1398

    
1399
                                {
1400
                                    var param = cmd.CreateParameter();
1401
                                    param.ParameterName = "@Description";
1402
                                    param.Value = row["Description"].ToString();
1403
                                    cmd.Parameters.Add(param);
1404
                                }
1405

    
1406
                                {
1407
                                    var param = cmd.CreateParameter();
1408
                                    param.ParameterName = "@Condition";
1409
                                    param.Value = row["Condition"].ToString();
1410
                                    cmd.Parameters.Add(param);
1411
                                }
1412

    
1413
                                {
1414
                                    var param = cmd.CreateParameter();
1415
                                    param.ParameterName = "@Remarks";
1416
                                    param.Value = row["Remarks"].ToString();
1417
                                    cmd.Parameters.Add(param);
1418
                                }
1419

    
1420
                                {
1421
                                    var param = cmd.CreateParameter();
1422
                                    param.ParameterName = "@GroundLevel";
1423
                                    param.Value = row["GroundLevel"].ToString();
1424
                                    cmd.Parameters.Add(param);
1425
                                }
1426

    
1427
                                connection.ExecuteNonQuery(cmd, txn);
1428
                            }
1429
                            txn.Commit();
1430
                        }
1431
                        catch (Exception ex)
1432
                        {
1433
                            txn.Rollback();
1434
                            result = false;
1435
                        }
1436
                    }
1437
                }
1438
                catch (Exception ex)
1439
                {
1440
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1441
                    result = false;
1442
                }
1443
            }
1444

    
1445
            return result;
1446
        }
1447

    
1448
        public static DataTable SelectPSNFluidCode()
1449
        {
1450
            DataTable dt = null;
1451
            ID2Info id2Info = ID2Info.GetInstance();
1452

    
1453
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1454
            {
1455
                try
1456
                {
1457
                    var query = $"SELECT * FROM {PSN_FLUIDCODE}";
1458
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1459
                    {
1460
                        dt = ds.Tables[0].Copy();
1461
                    }
1462
                }
1463
                catch (Exception ex)
1464
                {
1465
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1466
                }
1467
            }
1468

    
1469
            return dt;
1470
        }
1471

    
1472
        public static bool SavePSNPMC(DataTable dt)
1473
        {
1474
            ID2Info id2Info = ID2Info.GetInstance();
1475

    
1476
            bool result = true;
1477
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1478
            {
1479
                try
1480
                {
1481
                    using (var txn = connection.BeginTransaction())
1482
                    {
1483
                        try
1484
                        {
1485
                            var query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
1486
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1487

    
1488
                            foreach (DataRow row in dt.Rows)
1489
                            {
1490
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1491
                                var cmd = connection.GetSqlStringCommand(query);
1492
                                cmd.Parameters.Clear();
1493

    
1494
                                {
1495
                                    var param = cmd.CreateParameter();
1496
                                    param.ParameterName = "@UID";
1497
                                    param.Value = row["UID"].ToString();
1498
                                    cmd.Parameters.Add(param);
1499
                                }
1500

    
1501
                                {
1502
                                    var param = cmd.CreateParameter();
1503
                                    param.ParameterName = "@Priority";
1504
                                    param.Value = row["Priority"].ToString();
1505
                                    cmd.Parameters.Add(param);
1506
                                }
1507

    
1508
                                {
1509
                                    var param = cmd.CreateParameter();
1510
                                    param.ParameterName = "@Code";
1511
                                    param.Value = row["Code"].ToString();
1512
                                    cmd.Parameters.Add(param);
1513
                                }
1514

    
1515
                                {
1516
                                    var param = cmd.CreateParameter();
1517
                                    param.ParameterName = "@Description";
1518
                                    param.Value = row["Description"].ToString();
1519
                                    cmd.Parameters.Add(param);
1520
                                }
1521

    
1522
                                {
1523
                                    var param = cmd.CreateParameter();
1524
                                    param.ParameterName = "@Condition";
1525
                                    param.Value = row["Condition"].ToString();
1526
                                    cmd.Parameters.Add(param);
1527
                                }
1528

    
1529
                                {
1530
                                    var param = cmd.CreateParameter();
1531
                                    param.ParameterName = "@Remarks";
1532
                                    param.Value = row["Remarks"].ToString();
1533
                                    cmd.Parameters.Add(param);
1534
                                }
1535

    
1536
                                {
1537
                                    var param = cmd.CreateParameter();
1538
                                    param.ParameterName = "@GroundLevel";
1539
                                    param.Value = row["GroundLevel"].ToString();
1540
                                    cmd.Parameters.Add(param);
1541
                                }
1542

    
1543
                                connection.ExecuteNonQuery(cmd, txn);
1544
                            }
1545

    
1546
                            txn.Commit();
1547
                        }
1548
                        catch (Exception ex)
1549
                        {
1550
                            txn.Rollback();
1551
                            result = false;
1552
                        }
1553
                    }
1554
                }
1555
                catch (Exception ex)
1556
                {
1557
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1558
                    result = false;
1559
                }
1560
            }
1561

    
1562
            return result;
1563
        }
1564

    
1565
        public static bool SavePSNInsulation(DataTable dt)
1566
        {
1567
            ID2Info id2Info = ID2Info.GetInstance();
1568

    
1569
            bool result = true;
1570
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1571
            {
1572
                try
1573
                {
1574
                    using (var txn = connection.BeginTransaction())
1575
                    {
1576
                        try
1577
                        {
1578
                            var query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
1579
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1580

    
1581
                            foreach (DataRow row in dt.Rows)
1582
                            {
1583
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
1584
                                var cmd = connection.GetSqlStringCommand(query);
1585
                                cmd.Parameters.Clear();
1586

    
1587
                                {
1588
                                    var param = cmd.CreateParameter();
1589
                                    param.ParameterName = "@UID";
1590
                                    param.Value = row["UID"].ToString();
1591
                                    cmd.Parameters.Add(param);
1592
                                }
1593
                                
1594
                                {
1595
                                    var param = cmd.CreateParameter();
1596
                                    param.ParameterName = "@Code";
1597
                                    param.Value = row["Code"].ToString();
1598
                                    cmd.Parameters.Add(param);
1599
                                }
1600

    
1601
                                {
1602
                                    var param = cmd.CreateParameter();
1603
                                    param.ParameterName = "@Description";
1604
                                    param.Value = row["Description"].ToString();
1605
                                    cmd.Parameters.Add(param);
1606
                                }
1607
                                
1608
                                {
1609
                                    var param = cmd.CreateParameter();
1610
                                    param.ParameterName = "@Remarks";
1611
                                    param.Value = row["Remarks"].ToString();
1612
                                    cmd.Parameters.Add(param);
1613
                                }
1614

    
1615
                                connection.ExecuteNonQuery(cmd, txn);
1616
                            }
1617

    
1618
                            txn.Commit();
1619
                        }
1620
                        catch (Exception ex)
1621
                        {
1622
                            txn.Rollback();
1623
                            result = false;
1624
                        }
1625
                    }
1626
                }
1627
                catch (Exception ex)
1628
                {
1629
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1630
                    result = false;
1631
                }
1632
            }
1633

    
1634
            return result;
1635
        }
1636

    
1637
        public static PSN GetDBPSN()
1638
        {
1639
            PSN result = new PSN();
1640
            ID2Info id2Info = ID2Info.GetInstance();
1641

    
1642
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1643
            {
1644
                try
1645
                {
1646
                    //query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK}";
1647
                    //using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1648
                    //{
1649
                    //    result.PipeSystemNetwork = ds.Tables[0].Copy();
1650
                    //}
1651

    
1652
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
1653
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1654
                    {
1655
                        result.PipeSystemNetwork = ds.Tables[0].Clone();
1656
                        result.PipeSystemNetwork.Columns["IsValid"].DataType = typeof(string);
1657

    
1658
                        foreach (DataRow row in ds.Tables[0].Rows)
1659
                        {
1660
                            DataRow newRow = result.PipeSystemNetwork.NewRow();
1661
                            newRow["OID"] = row["OID"].ToString();
1662
                            newRow["Type"] = row["Type"].ToString();
1663
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
1664
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
1665
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
1666
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
1667
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
1668
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
1669

    
1670
                            string IsValid = string.Empty;
1671

    
1672
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
1673
                                IsValid = string.Empty;//"OK";
1674
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
1675
                                IsValid = "InValid";
1676
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
1677
                                IsValid = "Error";
1678

    
1679
                            newRow["IsValid"] = IsValid;
1680

    
1681
                            newRow["Status"] = row["Status"].ToString();
1682
                            newRow["PBS"] = row["PBS"].ToString();
1683
                            newRow["Drawings"] = row["Drawings"].ToString();
1684

    
1685
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1686
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
1687

    
1688
                            result.PipeSystemNetwork.Rows.Add(newRow);
1689
                        }
1690
                    }
1691

    
1692
                    query = $"SELECT * FROM {PSN_EQUIPMENT}";
1693
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1694
                    {
1695
                        result.Equipment = ds.Tables[0].Copy();
1696
                    }
1697

    
1698
                    query = $"SELECT * FROM {PSN_NOZZLE}";
1699
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1700
                    {
1701
                        result.Nozzle = ds.Tables[0].Copy();
1702
                    }
1703

    
1704
                    query = $"SELECT * FROM {PSN_PATHITEMS}";
1705
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1706
                    {
1707
                        result.PathItems = ds.Tables[0].Copy();
1708
                    }
1709

    
1710
                    query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
1711
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1712
                    {
1713
                        result.SequenceData = ds.Tables[0].Copy();
1714
                    }
1715

    
1716
                    query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
1717
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1718
                    {
1719
                        result.TopologySet = ds.Tables[0].Copy();
1720
                    }
1721

    
1722
                   
1723

    
1724
                    query = $"SELECT * FROM {PSN_PIPELINE}";
1725
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1726
                    {
1727
                        result.PipeLine = ds.Tables[0].Copy();
1728
                    }                    
1729

    
1730
                    result.Revision = GetRevision();
1731
                }
1732
                catch (Exception ex)
1733
                {
1734
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1735
                    result = null;
1736
                }
1737
            }
1738

    
1739
            return result;
1740
        }
1741

    
1742
        public static int GetRevision()
1743
        {
1744
            int result = 0;
1745
            ID2Info id2Info = ID2Info.GetInstance();
1746

    
1747
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1748
            {
1749
                try
1750
                {
1751
                    var query = $"SELECT DISTINCT PSNRevisionNumber FROM {PSN_PIPESYSTEMNETWORK};";
1752
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1753
                    {
1754
                        foreach (DataRow row in ds.Tables[0].Rows)
1755
                        {
1756
                            string value = row["PSNRevisionNumber"].ToString();
1757
                            if (value.StartsWith("V"))
1758
                                value = value.Remove(0, 1);
1759
                            int revisionNumber = Convert.ToInt32(value);
1760
                            if (result < revisionNumber)
1761
                                result = revisionNumber;
1762
                        }
1763
                    }
1764
                }
1765
                catch (Exception ex)
1766
                {
1767
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1768
                    result = -1;
1769
                }
1770
            }
1771

    
1772
            return result;
1773
        }
1774

    
1775
        public static DataTable GetPathItem()
1776
        {
1777
            DataTable dt = null;
1778

    
1779
            ID2Info id2Info = ID2Info.GetInstance();
1780
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1781
            {
1782
                try
1783
                {
1784
                    var query = $"SELECT * FROM {PSN_PATHITEMS}";
1785
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1786
                    {
1787
                        dt = ds.Tables[0].Copy();
1788
                    }
1789
                }
1790
                catch (Exception ex)
1791
                {
1792
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1793
                }
1794
            }
1795

    
1796
            return dt;
1797
        }
1798

    
1799
        public static DataTable GetTopologySet()
1800
        {
1801
            DataTable dt = null;
1802

    
1803
            ID2Info id2Info = ID2Info.GetInstance();
1804
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1805
            {
1806
                try
1807
                {
1808
                    var query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
1809
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1810
                    {
1811
                        dt = ds.Tables[0].Clone();
1812
                        foreach (DataRow row in ds.Tables[0].Rows)
1813
                        {
1814
                            DataRow newRow = dt.NewRow();
1815
                            newRow["OID"] = row["OID"].ToString();
1816
                            newRow["Type"] = row["Type"].ToString();
1817
                            newRow["SubType"] = row["SubType"].ToString();
1818
                            newRow["HeadItemTag"] = row["HeadItemTag"].ToString();
1819
                            newRow["TailItemTag"] = row["TailItemTag"].ToString();
1820
                            newRow["HeadItemSPID"] = row["HeadItemSPID"].ToString();
1821
                            newRow["TailItemSPID"] = row["TailItemSPID"].ToString();
1822
                            dt.Rows.Add(newRow);
1823
                        }
1824
                    }
1825
                }
1826
                catch (Exception ex)
1827
                {
1828
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1829
                }
1830
            }
1831

    
1832
            return dt;
1833
        }
1834

    
1835
        public static DataTable GetPipeSystemNetwork()
1836
        {
1837
            DataTable dt = null;
1838

    
1839
            ID2Info id2Info = ID2Info.GetInstance();
1840
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1841
            {
1842
                try
1843
                {
1844
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
1845
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1846
                    {
1847
                        dt = ds.Tables[0].Clone();
1848
                        dt.Columns["IsValid"].DataType = typeof(string);
1849
                        foreach (DataRow row in ds.Tables[0].Rows)
1850
                        {
1851
                            DataRow newRow = dt.NewRow();
1852
                            newRow["OID"] = row["OID"].ToString();
1853
                            newRow["Type"] = row["Type"].ToString();
1854
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
1855
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
1856
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
1857
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
1858
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
1859
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
1860

    
1861
                            string IsValid = string.Empty;
1862

    
1863
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
1864
                                IsValid = string.Empty;//"OK";
1865
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
1866
                                IsValid = "InValid";
1867
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
1868
                                IsValid = "Error";
1869

    
1870
                            newRow["IsValid"] = IsValid;
1871
                            newRow["Status"] = row["Status"].ToString();
1872

    
1873
                            newRow["PBS"] = row["PBS"].ToString();
1874
                            newRow["Drawings"] = row["Drawings"].ToString();
1875
                            
1876
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1877
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();                                           
1878
                                                        
1879
                            dt.Rows.Add(newRow);
1880
                        }
1881
                    }
1882
                }
1883
                catch (Exception ex)
1884
                {
1885
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1886
                }
1887
            }
1888

    
1889
            return dt;
1890
        }
1891

    
1892
        public static DataTable GetSequenceData()
1893
        {
1894
            DataTable dt = null;
1895

    
1896
            ID2Info id2Info = ID2Info.GetInstance();
1897
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1898
            {
1899
                try
1900
                {
1901
                    var query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
1902
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1903
                    {
1904
                        dt = ds.Tables[0].Copy();
1905
                    }
1906
                }
1907
                catch (Exception ex)
1908
                {
1909
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1910
                }
1911
            }
1912

    
1913
            return dt;
1914
        }
1915

    
1916
        //Anohter DB
1917
        public static bool ConnTestAndCreateAnotherTable()
1918
        {
1919
            bool result = false;
1920
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
1921

    
1922
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1923
            {
1924
                try
1925
                {
1926
                    var names = connection.GetTableNames();
1927
                    var matched = names.FirstOrDefault(param => param == PSN_HEADER_SETTING);
1928
                    Dictionary<string, string> dicColCheck = new Dictionary<string, string>();
1929
                    dicColCheck.Add("GROUP_ID", "TEXT");
1930
                    dicColCheck.Add("DESCRIPTION", "TEXT");
1931
                    dicColCheck.Add("INDEX", "INTEGER");
1932
                    dicColCheck.Add("NAME", "TEXT");
1933

    
1934
                    if (matched == null)
1935
                    {
1936
                        var query = $"CREATE TABLE {PSN_HEADER_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
1937
                        using (var cmd = connection.GetSqlStringCommand(query))
1938
                        {
1939
                            cmd.ExecuteNonQuery();
1940
                        }
1941
                    }
1942
                    else
1943
                    {
1944
                        AddColumn(PSN_HEADER_SETTING, dicColCheck);
1945
                    }
1946

    
1947
                    matched = names.FirstOrDefault(param => param == PSN_VENTDRAIN_SETTING);
1948
                    dicColCheck.Clear();
1949
                    dicColCheck.Add("GROUP_ID", "TEXT");
1950
                    dicColCheck.Add("DESCRIPTION", "TEXT");
1951
                    dicColCheck.Add("INDEX", "INTEGER");
1952
                    dicColCheck.Add("NAME", "TEXT");
1953
                    if (matched == null)
1954
                    {
1955
                        var query = $"CREATE TABLE {PSN_VENTDRAIN_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
1956
                        using (var cmd = connection.GetSqlStringCommand(query))
1957
                        {
1958
                            cmd.ExecuteNonQuery();
1959
                        }
1960
                    }
1961
                    else
1962
                    {
1963
                        AddColumn(PSN_VENTDRAIN_SETTING, dicColCheck);
1964
                    }
1965

    
1966
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGY_RULE);
1967
                    dicColCheck.Clear();
1968
                    dicColCheck.Add("UID", "TEXT");
1969
                    if (matched == null)
1970
                    {
1971
                        var query = $"CREATE TABLE {PSN_TOPOLOGY_RULE} (UID TEXT)";
1972
                        using (var cmd = connection.GetSqlStringCommand(query))
1973
                        {
1974
                            cmd.ExecuteNonQuery();
1975
                        }
1976

    
1977
                        DataTable topologyRule = new DataTable();
1978
                        topologyRule.Columns.Add("NAME", typeof(string));
1979

    
1980
                        topologyRule.Rows.Add("FluidCode");
1981
                        topologyRule.Rows.Add("-");
1982
                        topologyRule.Rows.Add("PipingMaterialsClass");
1983
                        topologyRule.Rows.Add("-");
1984
                        topologyRule.Rows.Add("Tag Seq No");
1985

    
1986
                        SaveTopologyRule(topologyRule);
1987
                    }
1988
                    //else
1989
                    //{
1990
                    //    AddColumn(PSN_TOPOLOGY_RULE, dicColCheck);
1991
                    //}
1992

    
1993
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
1994
                    dicColCheck.Clear();
1995
                    dicColCheck.Add("OID", "NVARCHAR(255)");
1996
                    dicColCheck.Add("Type", "NVARCHAR(255)");
1997
                    dicColCheck.Add("OrderNumber", "NVARCHAR(255)");
1998
                    dicColCheck.Add("Pipeline_OID", "NVARCHAR(255)");
1999
                    dicColCheck.Add("From_Data", "NVARCHAR(255)");
2000
                    dicColCheck.Add("To_Data", "NVARCHAR(255)");
2001
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(125)");
2002
                    dicColCheck.Add("PSNRevisionNumber", "NVARCHAR(255)");
2003
                    dicColCheck.Add("IsValid", "INT");
2004
                    dicColCheck.Add("Status", "NVARCHAR(255)");
2005
                    dicColCheck.Add("PBS", "NVARCHAR(255)");
2006
                    dicColCheck.Add("Drawings", "NVARCHAR(255)");
2007
                    dicColCheck.Add("IncludingVirtualData", "NVARCHAR(10)");
2008
                    dicColCheck.Add("PSNAccuracy", "REAL");
2009

    
2010
                    if (matched == null)
2011
                    {
2012
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(255), Type NVARCHAR(255), OrderNumber NVARCHAR(255), Pipeline_OID NVARCHAR(255), From_Data NVARCHAR(255), " +
2013
                            "To_Data NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
2014
                            "IncludingVirtualData NVARCHAR(10),  PSNAccuracy REAL)";
2015
                        using (var cmd = connection.GetSqlStringCommand(query))
2016
                        {
2017
                            cmd.ExecuteNonQuery();
2018
                        }
2019
                    }
2020
                    else
2021
                    {
2022
                        AddColumn(PSN_PIPESYSTEMNETWORK, dicColCheck);
2023
                    }
2024

    
2025
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
2026
                    dicColCheck.Clear();
2027
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2028
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2029
                    dicColCheck.Add("Xcoords", "REAL");
2030
                    dicColCheck.Add("Ycoords", "REAL");
2031
                    if (matched == null)
2032
                    {
2033
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL)";
2034
                        using (var cmd = connection.GetSqlStringCommand(query))
2035
                        {
2036
                            cmd.ExecuteNonQuery();
2037
                        }
2038
                    }
2039
                    else
2040
                    {
2041
                        AddColumn(PSN_EQUIPMENT, dicColCheck);
2042
                    }
2043

    
2044
                    dicColCheck.Clear();
2045
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2046
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2047
                    dicColCheck.Add("Xcoords", "REAL");
2048
                    dicColCheck.Add("Ycoords", "REAL");
2049
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2050
                    dicColCheck.Add("Fluid", "NVARCHAR(255)");
2051
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2052
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2053
                    dicColCheck.Add("Rotation", "REAL");
2054
                    dicColCheck.Add("FlowDirection", "NVARCHAR(255)");
2055

    
2056
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
2057
                    if (matched == null)
2058
                    {
2059
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL, Equipment_OID NVARCHAR(255), " +
2060
                            "Fluid NVARCHAR(255), NPD NVARCHAR(255), PMC NVARCHAR(255), Rotation REAL, FlowDirection NVARCHAR(255))";
2061
                        using (var cmd = connection.GetSqlStringCommand(query))
2062
                        {
2063
                            cmd.ExecuteNonQuery();
2064
                        }
2065
                    }
2066
                    else
2067
                    {
2068
                        AddColumn(PSN_NOZZLE, dicColCheck);
2069
                    }
2070

    
2071
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
2072
                    dicColCheck.Clear();
2073
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2074
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2075
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2076
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2077
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2078
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2079
                    if (matched == null)
2080
                    {
2081
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
2082
                        using (var cmd = connection.GetSqlStringCommand(query))
2083
                        {
2084
                            cmd.ExecuteNonQuery();
2085
                        }
2086
                    }
2087
                    else
2088
                    {
2089
                        AddColumn(PSN_FLUIDCODE, dicColCheck);
2090
                    }
2091

    
2092
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
2093
                    dicColCheck.Clear();
2094
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2095
                    dicColCheck.Add("Priority", "INTEGER");
2096
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2097
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2098
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2099
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2100
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2101
                    if (matched == null)
2102
                    {
2103
                        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))";
2104
                        using (var cmd = connection.GetSqlStringCommand(query))
2105
                        {
2106
                            cmd.ExecuteNonQuery();
2107
                        }
2108
                    }
2109
                    else
2110
                    {
2111
                        AddColumn(PSN_PIPINGMATLCLASS, dicColCheck);
2112
                    }
2113

    
2114
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
2115
                    dicColCheck.Clear();
2116
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2117
                    dicColCheck.Add("SERIALNUMBER", "NVARCHAR(255)");
2118
                    dicColCheck.Add("PathItem_OID", "NVARCHAR(255)");
2119
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(255)");
2120
                    if (matched == null)
2121
                    {
2122
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(255), SERIALNUMBER NVARCHAR(255), PathItem_OID NVARCHAR(255), TopologySet_OID_Key NVARCHAR(255))";
2123
                        using (var cmd = connection.GetSqlStringCommand(query))
2124
                        {
2125
                            cmd.ExecuteNonQuery();
2126
                        }
2127
                    }
2128
                    else
2129
                    {
2130
                        AddColumn(PSN_SEQUENCEDATA, dicColCheck);
2131
                    }
2132

    
2133
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
2134
                    dicColCheck.Clear();
2135
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2136
                    dicColCheck.Add("SequenceData_OID", "NVARCHAR(255)");
2137
                    dicColCheck.Add("TopologySet_OID", "NVARCHAR(255)");
2138
                    dicColCheck.Add("BranchTopologySet_OID", "NVARCHAR(255)");
2139
                    dicColCheck.Add("PipeLine_OID", "NVARCHAR(255)");
2140
                    dicColCheck.Add("ItemName", "NVARCHAR(255)");
2141
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2142
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
2143
                    dicColCheck.Add("CLASS", "NVARCHAR(255)");
2144
                    dicColCheck.Add("SUBCLASS", "NVARCHAR(255)");
2145
                    dicColCheck.Add("TYPE", "NVARCHAR(255)");
2146
                    dicColCheck.Add("PIDNAME", "NVARCHAR(255)");
2147
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2148
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2149
                    dicColCheck.Add("PipeSystemNetwork_OID", "NVARCHAR(255)");
2150
                    dicColCheck.Add("PipeRun_OID", "NVARCHAR(255)");
2151
                    dicColCheck.Add("ViewPipeSystemNetwork_OID", "NVARCHAR(255)");
2152
                    if (matched == null)
2153
                    {
2154
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
2155
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
2156
                            "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), " +
2157
                            "ViewPipeSystemNetwork_OID NVARCHAR(255))";
2158
                        using (var cmd = connection.GetSqlStringCommand(query))
2159
                        {
2160
                            cmd.ExecuteNonQuery();
2161
                        }
2162
                    }
2163
                    else
2164
                    {
2165
                        AddColumn(PSN_PATHITEMS, dicColCheck);
2166
                    }
2167

    
2168
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
2169
                    dicColCheck.Clear();
2170
                    dicColCheck.Add("OID", "TEXT");
2171
                    if (matched == null)
2172
                    {
2173
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
2174
                        using (var cmd = connection.GetSqlStringCommand(query))
2175
                        {
2176
                            cmd.ExecuteNonQuery();
2177
                        }
2178
                    }
2179

    
2180
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
2181
                    dicColCheck.Clear();
2182
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2183
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2184
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
2185
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
2186
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
2187
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
2188
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
2189

    
2190
                    if (matched == null)
2191
                    {
2192
                        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))";
2193
                        using (var cmd = connection.GetSqlStringCommand(query))
2194
                        {
2195
                            cmd.ExecuteNonQuery();
2196
                        }
2197
                    }
2198
                    else
2199
                    {
2200
                        AddColumn(PSN_TOPOLOGYSET, dicColCheck);
2201
                    }
2202

    
2203
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
2204
                    dicColCheck.Clear();
2205
                    dicColCheck.Add("[INDEX]", "INTEGER");
2206
                    dicColCheck.Add("[NAME]", "TEXT");
2207
                    dicColCheck.Add("[KEYWORD]", "TEXT");
2208
                    if (matched == null)
2209
                    {
2210
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
2211
                        using (var cmd = connection.GetSqlStringCommand(query))
2212
                        {
2213
                            cmd.ExecuteNonQuery();
2214
                        }
2215
                    }
2216
                    else
2217
                    {
2218
                        AddColumn(PSN_TRANSFORMKEYWORD_SETTING, dicColCheck);
2219
                    }
2220

    
2221
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
2222
                    dicColCheck.Clear();
2223
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2224
                    dicColCheck.Add("PipeSystem_OID", "NVARCHAR(255)");
2225
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
2226
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2227
                    dicColCheck.Add("SEQNUMBER", "NVARCHAR(255)");
2228
                    dicColCheck.Add("INSULATION", "NVARCHAR(255)");
2229
                    dicColCheck.Add("FROM_DATA", "NVARCHAR(255)");
2230
                    dicColCheck.Add("TO_DATA", "NVARCHAR(255)");
2231
                    dicColCheck.Add("Unit", "NVARCHAR(100)");
2232
                    if (matched == null)
2233
                    {
2234
                        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), " +
2235
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
2236
                        using (var cmd = connection.GetSqlStringCommand(query))
2237
                        {
2238
                            cmd.ExecuteNonQuery();
2239
                        }
2240
                    }
2241
                    else
2242
                    {
2243
                        AddColumn(PSN_PIPELINE, dicColCheck);
2244
                    }
2245

    
2246
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
2247
                    dicColCheck.Clear();
2248
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2249
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2250
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2251
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2252
                    if (matched == null)
2253
                    {
2254
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
2255
                        using (var cmd = connection.GetSqlStringCommand(query))
2256
                        {
2257
                            cmd.ExecuteNonQuery();
2258
                        }
2259
                    }
2260
                    else
2261
                    {
2262
                        AddColumn(PSN_INSULATIONPURPOSE, dicColCheck);
2263
                    }
2264

    
2265
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
2266
                    {
2267
                        var colnames = connection.GetColumnNames(TableName);
2268
                        bool check = false;
2269
                        if (colnames != null)
2270
                        {
2271
                            foreach (KeyValuePair<string, string> col in dicCol)
2272
                            {
2273
                                check = false;
2274
                                foreach (string c in colnames)
2275
                                {
2276
                                    if (col.Key.ToUpper().Contains(c.ToUpper()))
2277
                                    {
2278
                                        check = true;
2279
                                        break;
2280
                                    }
2281
                                }
2282

    
2283
                                if (!check) //없으면 추가
2284
                                {
2285
                                    string i = string.Empty;
2286
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
2287
                                        i = "DEFAULT 0";
2288

    
2289
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
2290
                                    using (var cmd = connection.GetSqlStringCommand(query))
2291
                                    {
2292
                                        cmd.ExecuteNonQuery();
2293
                                    }
2294
                                }
2295
                            }
2296
                        }
2297
                    }
2298

    
2299
                    result = true;
2300
                }
2301
                catch (Exception ex)
2302
                {
2303
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2304
                }
2305
            }
2306

    
2307
            return result;
2308
        }
2309

    
2310
        public static bool SaveAnotherPSNData(PSN item, List<HeaderInfo> headerInfos, List<VentDrainInfo> ventDrainInfos,
2311
          List<KeywordItem> keywordItems, DataTable dtTopologyRule, DataTable dtFluidCode, DataTable dtPMC, DataTable dtInsulation)
2312
        {
2313
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
2314

    
2315
            bool result = true;
2316
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2317
            {
2318
                try
2319
                {
2320
                    using (var txn = connection.BeginTransaction())
2321
                    {
2322
                        try
2323
                        {
2324
                            // Path Items
2325
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
2326
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2327
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
2328
                            {
2329
                                DataRow row = item.PathItems.Rows[i];
2330
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
2331
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, " +
2332
                                    $"PIDNAME, Equipment_OID, NPD, PipeSystemNetwork_OID, PipeRun_OID, ViewPipeSystemNetwork_OID) VALUES " +
2333
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME, @Equipment_OID, @NPD, @PipeSystemNetwork_OID,@PipeRun_OID, @ViewPipeSystemNetwork_OID)";
2334
                                var cmd = connection.GetSqlStringCommand(query);
2335
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2336
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
2337
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
2338
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
2339
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
2340
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
2341
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
2342
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString());
2343
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
2344
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
2345
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
2346
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
2347
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
2348
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
2349
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
2350
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
2351
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
2352
                                connection.ExecuteNonQuery(cmd, txn);
2353
                            }
2354

    
2355
                            // Sequence
2356
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
2357
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2358
                            foreach (DataRow row in item.SequenceData.Rows)
2359
                            {
2360
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
2361
                                var cmd = connection.GetSqlStringCommand(query);
2362
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2363
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
2364
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
2365
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
2366
                                connection.ExecuteNonQuery(cmd, txn);
2367
                            }
2368

    
2369
                            // Nozzle
2370
                            query = $"DELETE FROM {PSN_NOZZLE}";
2371
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2372
                            foreach (DataRow row in item.Nozzle.Rows)
2373
                            {
2374
                                query = $"INSERT INTO {PSN_NOZZLE} (OID, ItemTag, Xcoords, Ycoords, Equipment_OID, Fluid, NPD, PMC, Rotation, FlowDirection) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS, @Equipment_OID, @FLUID, @NPD, @PMC, @ROTATION, @FlowDirection)";
2375
                                var cmd = connection.GetSqlStringCommand(query);
2376
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2377
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
2378

    
2379
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
2380
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
2381
                                else
2382
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
2383

    
2384
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
2385
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
2386
                                else
2387
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
2388

    
2389
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
2390
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
2391
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
2392
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
2393

    
2394
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
2395
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
2396
                                else
2397
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
2398

    
2399
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
2400
                                connection.ExecuteNonQuery(cmd, txn);
2401
                            }
2402

    
2403
                            //Equipment
2404
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
2405
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2406
                            foreach (DataRow row in item.Equipment.Rows)
2407
                            {
2408
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
2409
                                var cmd = connection.GetSqlStringCommand(query);
2410
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2411
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
2412

    
2413
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
2414
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
2415
                                else
2416
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
2417

    
2418
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
2419
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
2420
                                else
2421
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
2422

    
2423
                                connection.ExecuteNonQuery(cmd, txn);
2424
                            }
2425

    
2426
                            // TopologySet
2427
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
2428
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2429
                            foreach (DataRow row in item.TopologySet.Rows)
2430
                            {
2431
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
2432
                                var cmd = connection.GetSqlStringCommand(query);
2433
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2434
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
2435
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
2436
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
2437
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
2438
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
2439
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
2440
                                connection.ExecuteNonQuery(cmd, txn);
2441
                            }
2442

    
2443
                            // PSN
2444
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
2445
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2446
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
2447
                            {
2448
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
2449
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy) VALUES " +
2450
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy)";
2451
                                var cmd = connection.GetSqlStringCommand(query);
2452
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2453
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
2454
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
2455
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
2456
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
2457
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
2458
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
2459
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
2460

    
2461
                                int IsValid = 0;
2462
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
2463
                                    IsValid = 0;
2464
                                else if (row["IsValid"].ToString() == "InValid")
2465
                                    IsValid = 1;
2466
                                else if (row["IsValid"].ToString() == "Error")
2467
                                    IsValid = -1;
2468

    
2469
                                AddWithValue(cmd, "@IsValid", IsValid);
2470
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
2471

    
2472
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
2473
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
2474

    
2475
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
2476
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
2477
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
2478
                                else
2479
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString());
2480

    
2481
                                connection.ExecuteNonQuery(cmd, txn);
2482
                            }
2483

    
2484
                            //Pipeline
2485
                            query = $"DELETE FROM {PSN_PIPELINE}";
2486
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2487
                            foreach (DataRow row in item.PipeLine.Rows)
2488
                            {
2489
                                query = $"INSERT INTO {PSN_PIPELINE} (OID, PipeSystem_OID, FLUID, PMC, SEQNUMBER, INSULATION, FROM_DATA, TO_DATA, Unit) VALUES (@OID, @PipeSystem_OID, @FLUID, @PMC, @SEQNUMBER, @INSULATION, @FROM_DATA, @TO_DATA, @Unit)";
2490
                                var cmd = connection.GetSqlStringCommand(query);
2491
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
2492
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
2493
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
2494
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
2495
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
2496
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
2497
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
2498
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
2499
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
2500
                                connection.ExecuteNonQuery(cmd, txn);
2501
                            }
2502

    
2503
                            //Header Setting
2504
                            query = $"DELETE FROM {PSN_HEADER_SETTING}";
2505
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2506

    
2507
                            foreach (HeaderInfo headerInfo in headerInfos)
2508
                            {
2509
                                foreach (HeaderItem itemheader in headerInfo.HeaderItems)
2510
                                {
2511
                                    query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
2512
                                    var cmd = connection.GetSqlStringCommand(query);
2513
                                    AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
2514
                                    AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
2515
                                    AddWithValue(cmd, "@INDEX", itemheader.Index);
2516
                                    AddWithValue(cmd, "@NAME", itemheader.Name);
2517
                                    connection.ExecuteNonQuery(cmd, txn);
2518
                                }
2519
                            }
2520

    
2521
                            //Vent/Drain Setting
2522
                            query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
2523
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2524

    
2525
                            foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
2526
                            {
2527
                                foreach (VentDrainItem itemventdrain in ventDrainInfo.VentDrainItems)
2528
                                {
2529
                                    query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
2530
                                    var cmd = connection.GetSqlStringCommand(query);
2531
                                    AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
2532
                                    AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
2533
                                    AddWithValue(cmd, "@INDEX", itemventdrain.Index);
2534
                                    AddWithValue(cmd, "@NAME", itemventdrain.Name);
2535
                                    connection.ExecuteNonQuery(cmd, txn);
2536
                                }
2537
                            }
2538

    
2539
                            //Keyword Setting
2540
                            query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
2541
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2542

    
2543
                            foreach (KeywordItem itemKeyword in keywordItems)
2544
                            {
2545
                                query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
2546
                                var cmd = connection.GetSqlStringCommand(query);
2547
                                AddWithValue(cmd, "@INDEX", itemKeyword.Index);
2548
                                AddWithValue(cmd, "@NAME", itemKeyword.Name);
2549
                                AddWithValue(cmd, "@KEYWORD", itemKeyword.Keyword);
2550
                                connection.ExecuteNonQuery(cmd, txn);
2551
                            }
2552

    
2553
                            //FulidCode
2554
                            query = $"DELETE FROM {PSN_FLUIDCODE}";
2555
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2556

    
2557
                            foreach (DataRow row in dtFluidCode.Rows)
2558
                            {
2559
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
2560
                                var cmd = connection.GetSqlStringCommand(query);
2561
                                cmd.Parameters.Clear();
2562

    
2563
                                {
2564
                                    var param = cmd.CreateParameter();
2565
                                    param.ParameterName = "@UID";
2566
                                    param.Value = row["UID"].ToString();
2567
                                    cmd.Parameters.Add(param);
2568
                                }
2569

    
2570
                                {
2571
                                    var param = cmd.CreateParameter();
2572
                                    param.ParameterName = "@Code";
2573
                                    param.Value = row["Code"].ToString();
2574
                                    cmd.Parameters.Add(param);
2575
                                }
2576

    
2577
                                {
2578
                                    var param = cmd.CreateParameter();
2579
                                    param.ParameterName = "@Description";
2580
                                    param.Value = row["Description"].ToString();
2581
                                    cmd.Parameters.Add(param);
2582
                                }
2583

    
2584
                                {
2585
                                    var param = cmd.CreateParameter();
2586
                                    param.ParameterName = "@Condition";
2587
                                    param.Value = row["Condition"].ToString();
2588
                                    cmd.Parameters.Add(param);
2589
                                }
2590

    
2591
                                {
2592
                                    var param = cmd.CreateParameter();
2593
                                    param.ParameterName = "@Remarks";
2594
                                    param.Value = row["Remarks"].ToString();
2595
                                    cmd.Parameters.Add(param);
2596
                                }
2597

    
2598
                                {
2599
                                    var param = cmd.CreateParameter();
2600
                                    param.ParameterName = "@GroundLevel";
2601
                                    param.Value = row["GroundLevel"].ToString();
2602
                                    cmd.Parameters.Add(param);
2603
                                }
2604

    
2605
                                connection.ExecuteNonQuery(cmd, txn);
2606
                            }
2607

    
2608
                            //PMC
2609
                            query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
2610
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2611

    
2612
                            foreach (DataRow row in dtPMC.Rows)
2613
                            {
2614
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
2615
                                var cmd = connection.GetSqlStringCommand(query);
2616
                                cmd.Parameters.Clear();
2617

    
2618
                                {
2619
                                    var param = cmd.CreateParameter();
2620
                                    param.ParameterName = "@UID";
2621
                                    param.Value = row["UID"].ToString();
2622
                                    cmd.Parameters.Add(param);
2623
                                }
2624

    
2625
                                {
2626
                                    var param = cmd.CreateParameter();
2627
                                    param.ParameterName = "@Priority";
2628
                                    param.Value = row["Priority"].ToString();
2629
                                    cmd.Parameters.Add(param);
2630
                                }
2631

    
2632
                                {
2633
                                    var param = cmd.CreateParameter();
2634
                                    param.ParameterName = "@Code";
2635
                                    param.Value = row["Code"].ToString();
2636
                                    cmd.Parameters.Add(param);
2637
                                }
2638

    
2639
                                {
2640
                                    var param = cmd.CreateParameter();
2641
                                    param.ParameterName = "@Description";
2642
                                    param.Value = row["Description"].ToString();
2643
                                    cmd.Parameters.Add(param);
2644
                                }
2645

    
2646
                                {
2647
                                    var param = cmd.CreateParameter();
2648
                                    param.ParameterName = "@Condition";
2649
                                    param.Value = row["Condition"].ToString();
2650
                                    cmd.Parameters.Add(param);
2651
                                }
2652

    
2653
                                {
2654
                                    var param = cmd.CreateParameter();
2655
                                    param.ParameterName = "@Remarks";
2656
                                    param.Value = row["Remarks"].ToString();
2657
                                    cmd.Parameters.Add(param);
2658
                                }
2659

    
2660
                                {
2661
                                    var param = cmd.CreateParameter();
2662
                                    param.ParameterName = "@GroundLevel";
2663
                                    param.Value = row["GroundLevel"].ToString();
2664
                                    cmd.Parameters.Add(param);
2665
                                }
2666

    
2667
                                connection.ExecuteNonQuery(cmd, txn);
2668
                            }
2669

    
2670
                            //Insulation
2671
                            query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
2672
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2673

    
2674
                            foreach (DataRow row in dtInsulation.Rows)
2675
                            {
2676
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
2677
                                var cmd = connection.GetSqlStringCommand(query);
2678
                                cmd.Parameters.Clear();
2679

    
2680
                                {
2681
                                    var param = cmd.CreateParameter();
2682
                                    param.ParameterName = "@UID";
2683
                                    param.Value = row["UID"].ToString();
2684
                                    cmd.Parameters.Add(param);
2685
                                }
2686

    
2687
                                {
2688
                                    var param = cmd.CreateParameter();
2689
                                    param.ParameterName = "@Code";
2690
                                    param.Value = row["Code"].ToString();
2691
                                    cmd.Parameters.Add(param);
2692
                                }
2693

    
2694
                                {
2695
                                    var param = cmd.CreateParameter();
2696
                                    param.ParameterName = "@Description";
2697
                                    param.Value = row["Description"].ToString();
2698
                                    cmd.Parameters.Add(param);
2699
                                }
2700

    
2701
                                {
2702
                                    var param = cmd.CreateParameter();
2703
                                    param.ParameterName = "@Remarks";
2704
                                    param.Value = row["Remarks"].ToString();
2705
                                    cmd.Parameters.Add(param);
2706
                                }
2707

    
2708
                                connection.ExecuteNonQuery(cmd, txn);
2709
                            }
2710

    
2711
                            //Topology Rule
2712
                            query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
2713
                            var cmdtopology = connection.GetSqlStringCommand(query);
2714
                            //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2715
                            connection.ExecuteNonQuery(cmdtopology, txn);
2716

    
2717
                            foreach (DataRow row in dtTopologyRule.Rows)
2718
                            {
2719
                                query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
2720
                                cmdtopology = connection.GetSqlStringCommand(query);
2721
                                AddWithValue(cmdtopology, "@UID", row["UID"].ToString());
2722
                                connection.ExecuteNonQuery(cmdtopology, txn);
2723
                            }
2724

    
2725
                            txn.Commit();
2726
                        }
2727
                        catch (Exception ex)
2728
                        {
2729
                            txn.Rollback();
2730
                            result = false;
2731
                        }
2732
                    }
2733
                }
2734
                catch (Exception ex)
2735
                {
2736
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2737
                    result = false;
2738
                }
2739
            }
2740

    
2741
            return result;
2742
        }
2743
    }
2744
}
클립보드 이미지 추가 (최대 크기: 500 MB)