프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ e835aef5

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

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

    
10
namespace ID2PSN
11
{
12
    public class DB
13
    {
14
        const string PSN_TOPOLOGY_RULE = "T_PSN_TOPOLOGY_RULE";
15
        const string PSN_HEADER_SETTING = "T_PSN_HEADER_SETTING";
16
        const string PSN_VENTDRAIN_SETTING = "T_PSN_VENTDRAIN_SETTING";
17
        const string PSN_VIEW = "T_PSN_VIEW";
18
        const string PSN_TRANSFORMKEYWORD_SETTING = "T_PSN_TRANSFORMKEYWORD_SETTING";
19
        const string PSN_VALVEGROUP_SETTING = "SPPIDValveGroup";
20
        const string PSN_MULTIWAY_SETTING = "T_PSN_MULTIWAY_SETTING";
21
        const string PSN_PATHITEMS = "SPPIDPathItem";
22
        const string PSN_SEQUENCEDATA = "SPPIDSequenceData";
23
        const string PSN_PIPESYSTEMNETWORK = "SPPIDPipeSystemNetwork";
24
        const string PSN_EQUIPMENT = "SPPIDEquipment";
25
        const string PSN_NOZZLE = "SPPIDNozzle";
26
        const string PSN_FLUIDCODE = "SPPIDFluidCode";
27
        const string PSN_PIPINGMATLCLASS = "SPPIDPipingMatlClass";
28
        const string PSN_TOPOLOGYSET = "SPPIDTopologySet";
29
        //2021.11.17 추가 
30
        const string PSN_PIPELINE = "SPPIDPipeLine";
31
        //2021.11.26 추가
32
        const string PSN_INSULATIONPURPOSE = "SPPIDInsulationPurpose";
33
        //2022.01.12 추가 //ARS_COMMON DB
34
        const string PSN_REVISION = "PSNRevision";
35
        const string PSN_COMMON = "ARS_COMMON";
36
        //2022.01.19 추가
37
        const string PSN_PIPESYSTEM = "SPPIDPipeSystem";
38

    
39
        //2022.02.03 추가
40
        const string PSN_NOPOCKETSETTING = "T_PSN_NOPOCKET_SETTING";
41
        const string PSN_AIRFINCOOLERSETTING = "T_PSN_AIRFINCOOLER_SETTING";
42
        /// <summary>
43
        ///  ID2 Project.db 데이터를 가져온다. 
44
        ///  DB 접속 정보 및 DBType (Sqlite, Mssql) 정보를 가져옴
45
        ///  - JY
46
        /// </summary>
47
        /// <returns></returns>
48
        public static DataTable GetProject()
49
        {
50
            DataTable dt = new DataTable();
51
            ID2Info id2Info = ID2Info.GetInstance();
52
            try
53
            {
54
                using (SQLiteConnection connection = new SQLiteConnection(@"Data Source = C:\ProgramData\Digital PID\Project.db", true))
55
                {
56
                    connection.Open();
57
                    if (connection.State.Equals(ConnectionState.Open))
58
                    {
59
                        using (SQLiteCommand cmd = connection.CreateCommand())
60
                        {
61
                            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]";
62
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
63
                                dt.Load(dr);
64
                        }
65

    
66
                    }
67
                    connection.Close();
68
                }
69
            }
70
            catch (Exception ex)
71
            {
72
                System.Windows.Forms.MessageBox.Show(ex.Message);
73
            }
74

    
75
            dt.AcceptChanges();
76
            dt.DefaultView.Sort = "Name";
77
            dt = dt.DefaultView.ToTable();
78

    
79
            return dt;
80
        }
81

    
82
        /// <summary>
83
        ///  SQLite에 초기 DB 생성
84
        ///  - JY
85
        /// </summary>
86
        /// <returns></returns>
87
        public static bool ConnTestAndCreateTable()
88
        {
89
            bool result = false;
90
            ID2Info id2Info = ID2Info.GetInstance();
91

    
92
            using (IAbstractDatabase connection = id2Info.CreateConnection())
93
            {
94
                try
95
                {
96
                    var names = connection.GetTableNames();
97
                    var matched = names.FirstOrDefault(param => param == PSN_HEADER_SETTING);
98
                    Dictionary<string, string> dicColCheck = new Dictionary<string, string>();
99
                    dicColCheck.Add("GROUP_ID", "TEXT");
100
                    dicColCheck.Add("DESCRIPTION", "TEXT");
101
                    dicColCheck.Add("INDEX", "INTEGER");
102
                    dicColCheck.Add("NAME", "TEXT");
103

    
104
                    if (matched == null)
105
                    {
106
                        var query = $"CREATE TABLE {PSN_HEADER_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
107
                        using (var cmd = connection.GetSqlStringCommand(query))
108
                        {
109
                            cmd.ExecuteNonQuery();
110
                        }
111
                    }
112
                    else
113
                    {
114
                        AddColumn(PSN_HEADER_SETTING, dicColCheck);
115
                    }
116

    
117
                    matched = names.FirstOrDefault(param => param == PSN_VENTDRAIN_SETTING);
118
                    dicColCheck.Clear();
119
                    dicColCheck.Add("GROUP_ID", "TEXT");
120
                    dicColCheck.Add("DESCRIPTION", "TEXT");
121
                    dicColCheck.Add("INDEX", "INTEGER");
122
                    dicColCheck.Add("NAME", "TEXT");
123
                    if (matched == null)
124
                    {
125
                        var query = $"CREATE TABLE {PSN_VENTDRAIN_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
126
                        using (var cmd = connection.GetSqlStringCommand(query))
127
                        {
128
                            cmd.ExecuteNonQuery();
129
                        }
130
                    }
131
                    else
132
                    {
133
                        AddColumn(PSN_VENTDRAIN_SETTING, dicColCheck);
134
                    }
135

    
136
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGY_RULE);
137
                    dicColCheck.Clear();
138
                    dicColCheck.Add("UID", "TEXT");
139
                    if (matched == null)
140
                    {
141
                        var query = $"CREATE TABLE {PSN_TOPOLOGY_RULE} (UID TEXT)";
142
                        using (var cmd = connection.GetSqlStringCommand(query))
143
                        {
144
                            cmd.ExecuteNonQuery();
145
                        }
146

    
147
                        DataTable topologyRule = new DataTable();
148
                        topologyRule.Columns.Add("NAME", typeof(string));
149

    
150
                        topologyRule.Rows.Add("FluidCode");
151
                        topologyRule.Rows.Add("-");
152
                        topologyRule.Rows.Add("PipingMaterialsClass");
153
                        topologyRule.Rows.Add("-");
154
                        topologyRule.Rows.Add("Tag Seq No");
155

    
156
                        SaveTopologyRule(topologyRule);
157
                    }
158
                    //else
159
                    //{
160
                    //    AddColumn(PSN_TOPOLOGY_RULE, dicColCheck);
161
                    //}
162

    
163
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
164
                    dicColCheck.Clear();
165
                    dicColCheck.Add("OID", "NVARCHAR(255)");
166
                    dicColCheck.Add("Type", "NVARCHAR(255)");
167
                    dicColCheck.Add("OrderNumber", "NVARCHAR(255)");
168
                    dicColCheck.Add("Pipeline_OID", "NVARCHAR(255)");
169
                    dicColCheck.Add("From_Data", "NVARCHAR(255)");
170
                    dicColCheck.Add("To_Data", "NVARCHAR(255)");
171
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(125)");
172
                    dicColCheck.Add("PSNRevisionNumber", "NVARCHAR(255)");
173
                    dicColCheck.Add("IsValid", "INT");
174
                    dicColCheck.Add("Status", "NVARCHAR(255)");
175
                    dicColCheck.Add("PBS", "NVARCHAR(255)");
176
                    dicColCheck.Add("Drawings", "NVARCHAR(255)");
177
                    dicColCheck.Add("IncludingVirtualData", "NVARCHAR(10)");
178
                    dicColCheck.Add("PSNAccuracy", "REAL");
179
                    dicColCheck.Add("Pocket", "NVARCHAR(10)");
180
                    dicColCheck.Add("EGTag", "NVARCHAR(255)");
181
                    dicColCheck.Add("HasMLTags", "NVARCHAR(50)");
182
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
183

    
184
                    if (matched == null)
185
                    {
186
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(255), Type NVARCHAR(255), OrderNumber NVARCHAR(255), Pipeline_OID NVARCHAR(255), From_Data NVARCHAR(255), " +
187
                            "To_Data NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
188
                            "IncludingVirtualData NVARCHAR(10), PSNAccuracy REAL, Pocket NVARCHAR(10), EGTag NVARCHAR(255), HasMLTags NVARCHAR(50), GroundLevel NVARCHAR(50))";
189
                        using (var cmd = connection.GetSqlStringCommand(query))
190
                        {
191
                            cmd.ExecuteNonQuery();
192
                        }
193
                    }
194
                    else
195
                    {
196
                        AddColumn(PSN_PIPESYSTEMNETWORK, dicColCheck);
197
                    }
198

    
199
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
200
                    dicColCheck.Clear();
201
                    dicColCheck.Add("OID", "NVARCHAR(255)");
202
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
203
                    dicColCheck.Add("Xcoords", "REAL");
204
                    dicColCheck.Add("Ycoords", "REAL");
205
                    if (matched == null)
206
                    {
207
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL)";
208
                        using (var cmd = connection.GetSqlStringCommand(query))
209
                        {
210
                            cmd.ExecuteNonQuery();
211
                        }
212
                    }
213
                    else
214
                    {
215
                        AddColumn(PSN_EQUIPMENT, dicColCheck);
216
                    }
217

    
218
                    dicColCheck.Clear();
219
                    dicColCheck.Add("OID", "NVARCHAR(255)");
220
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
221
                    dicColCheck.Add("Xcoords", "REAL");
222
                    dicColCheck.Add("Ycoords", "REAL");
223
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
224
                    dicColCheck.Add("Fluid", "NVARCHAR(255)");
225
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
226
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
227
                    dicColCheck.Add("Rotation", "REAL");
228
                    dicColCheck.Add("FlowDirection", "NVARCHAR(255)");
229

    
230
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
231
                    if (matched == null)
232
                    {
233
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL, Equipment_OID NVARCHAR(255), " +
234
                            "Fluid NVARCHAR(255), NPD NVARCHAR(255), PMC NVARCHAR(255), Rotation REAL, FlowDirection NVARCHAR(255))";
235
                        using (var cmd = connection.GetSqlStringCommand(query))
236
                        {
237
                            cmd.ExecuteNonQuery();
238
                        }
239
                    }
240
                    else
241
                    {
242
                        AddColumn(PSN_NOZZLE, dicColCheck);
243
                    }
244

    
245
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
246
                    dicColCheck.Clear();
247
                    dicColCheck.Add("UID", "NVARCHAR(50)");
248
                    dicColCheck.Add("Code", "NVARCHAR(255)");
249
                    dicColCheck.Add("Description", "NVARCHAR(255)");
250
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
251
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
252
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
253
                    if (matched == null)
254
                    {
255
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
256
                        using (var cmd = connection.GetSqlStringCommand(query))
257
                        {
258
                            cmd.ExecuteNonQuery();
259
                        }
260
                    }
261
                    else
262
                    {
263
                        AddColumn(PSN_FLUIDCODE, dicColCheck);
264
                    }
265

    
266
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
267
                    dicColCheck.Clear();
268
                    dicColCheck.Add("UID", "NVARCHAR(50)");
269
                    dicColCheck.Add("Priority", "INTEGER");
270
                    dicColCheck.Add("Code", "NVARCHAR(255)");
271
                    dicColCheck.Add("Description", "NVARCHAR(255)");
272
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
273
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
274
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
275
                    if (matched == null)
276
                    {
277
                        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))";
278
                        using (var cmd = connection.GetSqlStringCommand(query))
279
                        {
280
                            cmd.ExecuteNonQuery();
281
                        }
282
                    }
283
                    else
284
                    {
285
                        AddColumn(PSN_PIPINGMATLCLASS, dicColCheck);
286
                    }
287

    
288
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
289
                    dicColCheck.Clear();
290
                    dicColCheck.Add("OID", "NVARCHAR(255)");
291
                    dicColCheck.Add("SERIALNUMBER", "NVARCHAR(255)");
292
                    dicColCheck.Add("PathItem_OID", "NVARCHAR(255)");
293
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(255)");
294
                    if (matched == null)
295
                    {
296
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(255), SERIALNUMBER NVARCHAR(255), PathItem_OID NVARCHAR(255), TopologySet_OID_Key NVARCHAR(255))";
297
                        using (var cmd = connection.GetSqlStringCommand(query))
298
                        {
299
                            cmd.ExecuteNonQuery();
300
                        }
301
                    }
302
                    else
303
                    {
304
                        AddColumn(PSN_SEQUENCEDATA, dicColCheck);
305
                    }
306

    
307
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
308
                    dicColCheck.Clear();
309
                    dicColCheck.Add("OID", "NVARCHAR(255)");
310
                    dicColCheck.Add("SequenceData_OID", "NVARCHAR(255)");
311
                    dicColCheck.Add("TopologySet_OID", "NVARCHAR(255)");
312
                    dicColCheck.Add("BranchTopologySet_OID", "NVARCHAR(255)");
313
                    dicColCheck.Add("PipeLine_OID", "NVARCHAR(255)");
314
                    dicColCheck.Add("ItemName", "NVARCHAR(255)");
315
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
316
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
317
                    dicColCheck.Add("CLASS", "NVARCHAR(255)");
318
                    dicColCheck.Add("SUBCLASS", "NVARCHAR(255)");
319
                    dicColCheck.Add("TYPE", "NVARCHAR(255)");
320
                    dicColCheck.Add("MULTIWAY", "NVARCHAR(255)");
321
                    dicColCheck.Add("PIDNAME", "NVARCHAR(255)");
322
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
323
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
324
                    dicColCheck.Add("GROUPTAG", "NVARCHAR(255)");
325
                    dicColCheck.Add("PipeSystemNetwork_OID_ID2", "NVARCHAR(255)");
326
                    dicColCheck.Add("PipeRun_OID", "NVARCHAR(255)");
327
                    dicColCheck.Add("PipeSystemNetwork_OID", "NVARCHAR(255)");
328

    
329
                    dicColCheck.Add("EqpGroupTag", "NVARCHAR(255)");
330
                    dicColCheck.Add("MainLineTag", "NVARCHAR(255)");
331

    
332
                    dicColCheck.Add("EGTConnectedPoint", "INT");
333
                    dicColCheck.Add("EGFlowDirection", "NVARCHAR(10)");
334

    
335
                   
336
                    if (matched == null)
337
                    {
338
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
339
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
340
                            "CLASS NVARCHAR(255), SUBCLASS NVARCHAR(255), TYPE NVARCHAR(255), MULTIWAY NVARCHAR(255), PIDNAME NVARCHAR(255), Equipment_OID NVARCHAR(255), NPD NVARCHAR(255), GROUPTAG NVARCHAR(255), PipeSystemNetwork_OID_ID2 NVARCHAR(255), PipeRun_OID NVARCHAR(255), " +
341
                            "PipeSystemNetwork_OID NVARCHAR(255), EqpGroupTag NVARCHAR(255), MainLineTag NVARCHAR(255), EGTConnectedPoint INT, EGFlowDirection NVARCHAR(10))";
342
                        using (var cmd = connection.GetSqlStringCommand(query))
343
                        {
344
                            cmd.ExecuteNonQuery();
345
                        }
346
                    }
347
                    else
348
                    {
349
                        AddColumn(PSN_PATHITEMS, dicColCheck);
350
                    }
351

    
352
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
353
                    dicColCheck.Clear();
354
                    dicColCheck.Add("OID", "TEXT");
355
                    if (matched == null)
356
                    {
357
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
358
                        using (var cmd = connection.GetSqlStringCommand(query))
359
                        {
360
                            cmd.ExecuteNonQuery();
361
                        }
362
                    }
363

    
364
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
365
                    dicColCheck.Clear();
366
                    dicColCheck.Add("OID", "NVARCHAR(255)");
367
                    dicColCheck.Add("Type", "NVARCHAR(255)");
368
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
369
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
370
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
371
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
372
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
373

    
374
                    if (matched == null)
375
                    {
376
                        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))";
377
                        using (var cmd = connection.GetSqlStringCommand(query))
378
                        {
379
                            cmd.ExecuteNonQuery();
380
                        }
381
                    }
382
                    else
383
                    {
384
                        AddColumn(PSN_TOPOLOGYSET, dicColCheck);
385
                    }
386

    
387
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
388
                    dicColCheck.Clear();
389
                    dicColCheck.Add("INDEX", "INTEGER");
390
                    dicColCheck.Add("NAME", "TEXT");
391
                    dicColCheck.Add("KEYWORD", "TEXT");
392
                    if (matched == null)
393
                    {
394
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
395
                        using (var cmd = connection.GetSqlStringCommand(query))
396
                        {
397
                            cmd.ExecuteNonQuery();
398
                        }
399
                    }
400
                    else
401
                    {
402
                        AddColumn(PSN_TRANSFORMKEYWORD_SETTING, dicColCheck);
403
                    }
404

    
405
                    matched = names.FirstOrDefault(param => param == PSN_MULTIWAY_SETTING);
406
                    dicColCheck.Clear();
407
                    dicColCheck.Add("INDEX", "INTEGER");
408
                    dicColCheck.Add("NAME", "TEXT");
409
                    if (matched == null)
410
                    {
411
                        var query = $"CREATE TABLE {PSN_MULTIWAY_SETTING} ([INDEX] INTEGER, [NAME] TEXT)";
412
                        using (var cmd = connection.GetSqlStringCommand(query))
413
                        {
414
                            cmd.ExecuteNonQuery();
415
                        }
416
                    }
417
                    else
418
                    {
419
                        AddColumn(PSN_MULTIWAY_SETTING, dicColCheck);
420
                    }
421

    
422
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
423
                    dicColCheck.Clear();
424
                    dicColCheck.Add("OID", "NVARCHAR(255)");
425
                    dicColCheck.Add("PipeSystem_OID", "NVARCHAR(255)");
426
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
427
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
428
                    dicColCheck.Add("SEQNUMBER", "NVARCHAR(255)");
429
                    dicColCheck.Add("INSULATION", "NVARCHAR(255)");
430
                    dicColCheck.Add("FROM_DATA", "NVARCHAR(255)");
431
                    dicColCheck.Add("TO_DATA", "NVARCHAR(255)");
432
                    dicColCheck.Add("Unit", "NVARCHAR(100)");
433
                    if (matched == null)
434
                    {
435
                        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), " +
436
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
437
                        using (var cmd = connection.GetSqlStringCommand(query))
438
                        {
439
                            cmd.ExecuteNonQuery();
440
                        }
441
                    }
442
                    else
443
                    {
444
                        AddColumn(PSN_PIPELINE, dicColCheck);
445
                    }
446

    
447
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
448
                    dicColCheck.Clear();
449
                    dicColCheck.Add("UID", "NVARCHAR(50)");
450
                    dicColCheck.Add("Code", "NVARCHAR(255)");
451
                    dicColCheck.Add("Description", "NVARCHAR(255)");
452
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
453
                    if (matched == null)
454
                    {
455
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
456
                        using (var cmd = connection.GetSqlStringCommand(query))
457
                        {
458
                            cmd.ExecuteNonQuery();
459
                        }
460
                    }
461
                    else
462
                    {
463
                        AddColumn(PSN_INSULATIONPURPOSE, dicColCheck);
464
                    }
465

    
466
                    matched = names.FirstOrDefault(param => param == PSN_VALVEGROUP_SETTING);
467
                    dicColCheck.Clear();
468
                    dicColCheck.Add("OID", "NVARCHAR(255)");
469
                    dicColCheck.Add("GroupType", "NVARCHAR(255)");
470
                    dicColCheck.Add("TagIdentifier", "NVARCHAR(50)");
471
                    dicColCheck.Add("AttributeName", "NVARCHAR(255)");
472
                    dicColCheck.Add("SppidSymbolName", "NVARCHAR(255)");
473
                    if (matched == null)
474
                    {
475
                        var query = $"CREATE TABLE {PSN_VALVEGROUP_SETTING} (OID NVARCHAR(255), GroupType NVARCHAR(255), TagIdentifier NVARCHAR(50), AttributeName NVARCHAR(255), SppidSymbolName NVARCHAR(255))";
476
                        using (var cmd = connection.GetSqlStringCommand(query))
477
                        {
478
                            cmd.ExecuteNonQuery();
479
                        }
480
                    }
481
                    else
482
                    {
483
                        AddColumn(PSN_VALVEGROUP_SETTING, dicColCheck);
484
                    }
485

    
486
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEM);
487
                    dicColCheck.Clear();
488
                    dicColCheck.Add("OID", "NVARCHAR(255)");
489
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
490
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
491
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
492
                    dicColCheck.Add("PipeLineQty", "NVARCHAR(255)");
493
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
494
                    if (matched == null)
495
                    {
496
                        var query = $"CREATE TABLE {PSN_PIPESYSTEM} (OID NVARCHAR(255), DESCRIPTION NVARCHAR(255), FLUID NVARCHAR(255), PMC NVARCHAR(255), PipeLineQty NVARCHAR(255), GroundLevel NVARCHAR(20))";
497
                        using (var cmd = connection.GetSqlStringCommand(query))
498
                        {
499
                            cmd.ExecuteNonQuery();
500
                        }
501
                    }
502
                    else
503
                    {
504
                        AddColumn(PSN_PIPESYSTEM, dicColCheck);
505
                    }
506

    
507
                    matched = names.FirstOrDefault(param => param == PSN_NOPOCKETSETTING);
508
                    dicColCheck.Clear();
509
                    dicColCheck.Add("INDEX", "INTEGER");
510
                    dicColCheck.Add("TYPE", "TEXT");
511
                    dicColCheck.Add("NAME", "TEXT");
512
                    if (matched == null)
513
                    {
514
                        var query = $"CREATE TABLE {PSN_NOPOCKETSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [NAME] TEXT)";
515
                        using (var cmd = connection.GetSqlStringCommand(query))
516
                        {
517
                            cmd.ExecuteNonQuery();
518
                        }
519
                    }
520
                    else
521
                    {
522
                        AddColumn(PSN_NOPOCKETSETTING, dicColCheck);
523
                    }
524

    
525
                    matched = names.FirstOrDefault(param => param == PSN_AIRFINCOOLERSETTING);
526

    
527
                    dicColCheck.Clear();
528
                    dicColCheck.Add("INDEX", "INTEGER");
529
                    dicColCheck.Add("TYPE", "TEXT");
530
                    dicColCheck.Add("TagIdentifier", "TEXT");
531
                    dicColCheck.Add("AttributeName", "TEXT");
532
                    dicColCheck.Add("NAME", "TEXT");
533

    
534
                    if (matched == null)
535
                    {
536
                        var query = $"CREATE TABLE {PSN_AIRFINCOOLERSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [TagIdentifier] TEXT, [AttributeName] TEXT, [NAME] TEXT)";
537
                        using (var cmd = connection.GetSqlStringCommand(query))
538
                        {
539
                            cmd.ExecuteNonQuery();
540
                        }
541
                    }
542
                    else
543
                    {
544
                        AddColumn(PSN_AIRFINCOOLERSETTING, dicColCheck);
545
                    }
546

    
547

    
548
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
549
                    {
550
                        var colnames = connection.GetColumnNames(TableName);
551
                        bool check = false;
552
                        if (colnames != null)
553
                        {
554
                            foreach (KeyValuePair<string, string> col in dicCol)
555
                            {
556
                                check = false;
557
                                foreach (string c in colnames)
558
                                {
559
                                    if (col.Key.ToUpper().Equals(c.ToUpper()))
560
                                    {
561
                                        check = true;
562
                                        break;
563
                                    }
564
                                }
565

    
566
                                if (!check) //없으면 추가
567
                                {
568
                                    string i = string.Empty;
569
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
570
                                        i = "DEFAULT 0";
571

    
572
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
573
                                    using (var cmd = connection.GetSqlStringCommand(query))
574
                                    {
575
                                        cmd.ExecuteNonQuery();
576
                                    }
577
                                }
578
                            }
579
                        }
580
                    }
581

    
582
                    result = true;
583
                }
584
                catch (Exception ex)
585
                {
586
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
587
                }
588
            }
589

    
590
            return result;
591
        }
592

    
593
        // ID2 DB 데이터
594
        /// <summary>
595
        /// ID2 데이타베이스에서 OPC 데이터를 조회
596
        /// </summary>
597
        /// <returns></returns>
598
        public static DataTable SelectOPCRelations()
599
        {
600
            DataTable dt = null;
601
            ID2Info id2Info = ID2Info.GetInstance();
602

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

    
619
            return dt;
620
        }
621

    
622
        /// <summary>
623
        /// ID2 데이타베이스에서 도면 데이터를 조회
624
        /// </summary>
625
        /// <returns></returns>
626
        public static DataTable SelectDrawings()
627
        {
628
            DataTable dt = null;
629
            ID2Info id2Info = ID2Info.GetInstance();
630

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

    
647
            return dt;
648
        }
649

    
650
        public static DataTable AllDrawings()
651
        {
652
            DataTable dt = null;
653
            ID2Info id2Info = ID2Info.GetInstance();
654

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

    
671
            return dt;
672
        }
673

    
674
        public static DataTable SelectLineProperties()
675
        {
676
            DataTable dt = null;
677
            ID2Info id2Info = ID2Info.GetInstance();
678

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

    
695
            return dt;
696
        }
697

    
698
        public static DataTable SelectFluidCode()
699
        {
700
            DataTable dt = null;
701
            ID2Info id2Info = ID2Info.GetInstance();
702

    
703
            using (IAbstractDatabase connection = id2Info.CreateConnection())
704
            {
705
                try
706
                {
707
                    var query = "SELECT * FROM FluidCode";
708
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
709
                    {
710
                        dt = ds.Tables[0].Copy();
711
                    }
712
                }
713
                catch (Exception ex)
714
                {
715
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
716
                }
717
            }
718

    
719
            return dt;
720
        }
721

    
722
        public static DataTable SelectPipingMaterialsClass()
723
        {
724
            DataTable dt = null;
725
            ID2Info id2Info = ID2Info.GetInstance();
726

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

    
743
            return dt;
744
        }
745

    
746
        public static DataTable SelectPSNPIPINGMATLCLASS()
747
        {
748
            DataTable dt = null;
749
            ID2Info id2Info = ID2Info.GetInstance();
750

    
751
            using (IAbstractDatabase connection = id2Info.CreateConnection())
752
            {
753
                try
754
                {
755
                    var query = $"SELECT * FROM {PSN_PIPINGMATLCLASS}";
756
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
757
                    {
758
                        dt = ds.Tables[0].Copy();
759
                    }
760
                }
761
                catch (Exception ex)
762
                {
763
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
764
                }
765
            }
766

    
767
            return dt;
768
        }
769

    
770
        public static DataTable SelectInsulationPurpose()
771
        {
772
            DataTable dt = null;
773
            ID2Info id2Info = ID2Info.GetInstance();
774

    
775
            using (IAbstractDatabase connection = id2Info.CreateConnection())
776
            {
777
                try
778
                {
779
                    var query = "SELECT * FROM InsulationPurpose";
780
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
781
                    {
782
                        dt = ds.Tables[0].Copy();
783
                    }
784
                }
785
                catch (Exception ex)
786
                {
787
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
788
                }
789
            }
790

    
791
            return dt;
792
        }
793

    
794
        public static DataTable SelectPSNINSULATIONPURPOSE()
795
        {
796
            DataTable dt = null;
797
            ID2Info id2Info = ID2Info.GetInstance();
798

    
799
            using (IAbstractDatabase connection = id2Info.CreateConnection())
800
            {
801
                try
802
                {
803
                    var query = $"SELECT * FROM {PSN_INSULATIONPURPOSE}";
804
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
805
                    {
806
                        dt = ds.Tables[0].Copy();
807
                    }
808
                }
809
                catch (Exception ex)
810
                {
811
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
812
                }
813
            }
814

    
815
            return dt;
816
        }
817

    
818
        public static DataTable SelectNominalDiameter()
819
        {
820
            DataTable dt = null;
821
            ID2Info id2Info = ID2Info.GetInstance();
822

    
823
            using (IAbstractDatabase connection = id2Info.CreateConnection())
824
            {
825
                try
826
                {
827
                    var query = "SELECT * FROM NominalDiameter ORDER BY Metric DESC";
828
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
829
                    {
830
                        dt = ds.Tables[0].Copy();
831
                    }
832
                }
833
                catch (Exception ex)
834
                {
835
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
836
                }
837
            }
838

    
839
            ///TODO: need to check below code
840
            dt.Rows.RemoveAt(0);
841
            dt.Rows.RemoveAt(0);
842
            dt.Rows.RemoveAt(0);
843
            dt.Rows.RemoveAt(0);
844

    
845
            return dt;
846
        }
847

    
848
        public static DataTable SelectAllSymbolAttribute()
849
        {
850
            DataTable dt = null;
851
            ID2Info id2Info = ID2Info.GetInstance();
852

    
853
            using (IAbstractDatabase connection = id2Info.CreateConnection())
854
            {
855
                try
856
                {
857
                    var query = "SELECT B.Name AS SymbolName, Attribute AS SymbolAttribute, A.SymbolType_UID ,Attribute ,DisplayAttribute ,AttributeType, Expression FROM SymbolAttribute A INNER JOIN Symbol B ON A.SymbolType_UID = B.SymbolType_UID; ";
858
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
859
                    {
860
                        dt = ds.Tables[0].Copy();
861
                    }
862
                }
863
                catch (Exception ex)
864
                {
865
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
866
                }
867
            }
868

    
869
            return dt;
870
        }
871

    
872
        public static DataTable SelectSymbolAttribute()
873
        {
874
            DataTable dt = null;
875
            ID2Info id2Info = ID2Info.GetInstance();
876

    
877
            using (IAbstractDatabase connection = id2Info.CreateConnection())
878
            {
879
                try
880
                {
881
                    // var query = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
882
                    var query = "SELECT DISTINCT Attribute FROM SymbolAttribute;";
883
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
884
                    {
885
                        dt = ds.Tables[0].Copy();
886
                    }
887
                }
888
                catch (Exception ex)
889
                {
890
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
891
                }
892
            }
893

    
894
            return dt;
895
        }
896

    
897
        public static DataTable SelectTieInSymbolAttribute()
898
        {
899
            DataTable dt = null;
900
            ID2Info id2Info = ID2Info.GetInstance();
901

    
902
            using (IAbstractDatabase connection = id2Info.CreateConnection())
903
            {
904
                try
905
                {
906
                    // var query = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
907
                    var query = string.Empty;
908
                    if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
909
                        query = "SELECT DISTINCT Attribute FROM SymbolAttribute WHERE SymbolType_UID IN(SELECT SymbolType_UID FROM Symbol WHERE CONVERT(varchar(Max), isnull([Name], '')) = 'TIEINPOINT');"; //얘는 UPPER안해줘도 나옴
910
                    else
911
                        query = "SELECT DISTINCT Attribute FROM SymbolAttribute WHERE SymbolType_UID IN(SELECT SymbolType_UID FROM Symbol WHERE ifnull(UPPER([Name]), '') = 'TIEINPOINT');";
912

    
913
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
914
                    {
915
                        dt = ds.Tables[0].Copy();
916
                    }
917
                }
918
                catch (Exception ex)
919
                {
920
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
921
                }
922
            }
923

    
924
            return dt;
925
        }
926

    
927
        public static DataTable SelectSymbolName()
928
        {
929
            DataTable dt = null;
930
            ID2Info id2Info = ID2Info.GetInstance();
931

    
932
            using (IAbstractDatabase connection = id2Info.CreateConnection())
933
            {
934
                try
935
                {
936
                    var query = "SELECT * FROM SymbolName;";
937
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
938
                    {
939
                        dt = ds.Tables[0].Copy();
940
                    }
941
                }
942
                catch (Exception ex)
943
                {
944
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
945
                }
946
            }
947

    
948
            return dt;
949
        }
950

    
951
        public static double[] GetDrawingSize()
952
        {
953
            double[] result = null;
954

    
955
            ID2Info id2Info = ID2Info.GetInstance();
956
            using (IAbstractDatabase connection = id2Info.CreateConnection())
957
            {
958
                try
959
                {
960
                    var query = "SELECT value FROM Configuration WHERE Section = 'Area' AND [Key] = 'Drawing';";
961
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
962
                    {
963
                        if (ds.Tables[0].Rows.Count == 1)
964
                        {
965
                            string value = ds.Tables[0].Rows[0][0].ToString();
966
                            string[] split = value.Split(new char[] { ',' });
967
                            result = new double[] {
968
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", string.Empty)),
969
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", string.Empty)),
970
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", string.Empty)),
971
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", string.Empty))
972
                                };
973
                            result = new double[] {
974
                                Math.Min(result[0], result[2]),
975
                                Math.Min(result[1], result[3]),
976
                                Math.Max(result[0], result[2]),
977
                                Math.Max(result[1], result[3])
978
                                };
979
                        }
980
                    }
981
                }
982
                catch (Exception ex)
983
                {
984
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
985
                }
986
            }
987

    
988
            return result;
989
        }
990

    
991
        public static DataTable GetEquipmentType()
992
        {
993
            DataTable dt = null;
994
            ID2Info id2Info = ID2Info.GetInstance();
995

    
996
            using (IAbstractDatabase connection = id2Info.CreateConnection())
997
            {
998
                try
999
                {
1000
                    var query = "SELECT Type FROM SymbolType WHERE Category = 'Equipment';";
1001
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1002
                    {
1003
                        dt = ds.Tables[0].Copy();
1004
                    }
1005
                }
1006
                catch (Exception ex)
1007
                {
1008
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1009
                }
1010
            }
1011

    
1012
            return dt;
1013
        }
1014

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

    
1026
            bool result = true;
1027

    
1028
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1029
            {
1030
                try
1031
                {
1032
                    using (var txn = connection.BeginTransaction())
1033
                    {
1034
                        try
1035
                        {
1036
                            var query = $"DELETE FROM {PSN_VIEW}";
1037
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1038

    
1039
                            foreach (string value in values)
1040
                            {
1041
                                query = $"INSERT INTO {PSN_VIEW} (OID) VALUES (@OID)";
1042
                                var cmd = connection.GetSqlStringCommand(query);
1043
                                AddWithValue(cmd, "@OID", value);
1044
                                connection.ExecuteNonQuery(cmd, txn);
1045
                            }
1046
                            txn.Commit();
1047
                        }
1048
                        catch (Exception ex)
1049
                        {
1050
                            txn.Rollback();
1051
                            result = false;
1052
                        }
1053
                    }
1054
                }
1055
                catch (Exception ex)
1056
                {
1057
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1058
                    result = false;
1059
                }
1060
            }
1061

    
1062
            return result;
1063
        }
1064

    
1065
        public static bool DeleteView()
1066
        {
1067
            ID2Info id2Info = ID2Info.GetInstance();
1068

    
1069
            bool result = true;
1070
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1071
            {
1072
                try
1073
                {
1074
                    using (var txn = connection.BeginTransaction())
1075
                    {
1076
                        try
1077
                        {
1078
                            var query = $"DELETE FROM {PSN_VIEW}";
1079
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1080
                            txn.Commit();
1081
                        }
1082
                        catch (Exception ex)
1083
                        {
1084
                            txn.Rollback();
1085
                            result = false;
1086
                        }
1087
                    }
1088
                }
1089
                catch (Exception ex)
1090
                {
1091
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1092
                    result = false;
1093
                }
1094
            }
1095

    
1096
            return result;
1097
        }
1098

    
1099
        //PSN Sqlite 
1100
        public static DataTable SelectHeaderSetting()
1101
        {
1102
            DataTable dt = null;
1103
            ID2Info id2Info = ID2Info.GetInstance();
1104

    
1105
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1106
            {
1107
                try
1108
                {
1109
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_HEADER_SETTING};";
1110
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1111
                    {
1112
                        dt = ds.Tables[0].Copy();
1113
                    }
1114
                }
1115
                catch (Exception ex)
1116
                {
1117
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1118
                }
1119
            }
1120

    
1121
            return dt;
1122
        }
1123

    
1124
        public static DataTable SelectVentDrainSetting()
1125
        {
1126
            DataTable dt = null;
1127
            ID2Info id2Info = ID2Info.GetInstance();
1128

    
1129
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1130
            {
1131
                try
1132
                {
1133
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_VENTDRAIN_SETTING};";
1134
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1135
                    {
1136
                        dt = ds.Tables[0].Copy();
1137
                    }
1138
                }
1139
                catch (Exception ex)
1140
                {
1141
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1142
                }
1143
            }
1144

    
1145
            return dt;
1146
        }
1147

    
1148
        public static DataTable SelectKeywordsSetting()
1149
        {
1150
            DataTable dt = null;
1151
            ID2Info id2Info = ID2Info.GetInstance();
1152

    
1153
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1154
            {
1155
                try
1156
                {
1157
                    var query = $@"SELECT [KEYWORD], [INDEX], [NAME] FROM {PSN_TRANSFORMKEYWORD_SETTING};";
1158
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1159
                    {
1160
                        dt = ds.Tables[0].Copy();
1161
                    }
1162
                }
1163
                catch (Exception ex)
1164
                {
1165
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1166
                }
1167
            }
1168

    
1169
            return dt;
1170
        }
1171

    
1172
        public static DataTable SelectMultiwaysSetting()
1173
        {
1174
            DataTable dt = null;
1175
            ID2Info id2Info = ID2Info.GetInstance();
1176

    
1177
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1178
            {
1179
                try
1180
                {
1181
                    var query = $@"SELECT [INDEX], [NAME] FROM {PSN_MULTIWAY_SETTING};";
1182
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1183
                    {
1184
                        dt = ds.Tables[0].Copy();
1185
                    }
1186
                }
1187
                catch (Exception ex)
1188
                {
1189
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1190
                }
1191
            }
1192

    
1193
            return dt;
1194
        }
1195

    
1196
        public static DataTable SelectValveGroupItemsSetting()
1197
        {
1198
            DataTable dt = null;
1199
            ID2Info id2Info = ID2Info.GetInstance();
1200

    
1201
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1202
            {
1203
                try
1204
                {
1205
                    var query = $@"SELECT OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName FROM {PSN_VALVEGROUP_SETTING};";
1206
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1207
                    {
1208
                        dt = ds.Tables[0].Copy();
1209
                    }
1210
                }
1211
                catch (Exception ex)
1212
                {
1213
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1214
                }
1215
            }
1216

    
1217
            return dt;
1218
        }
1219

    
1220

    
1221
        public static DataTable SelectEquipmentNoPocketSetting()
1222
        {
1223
            DataTable dt = null;
1224
            ID2Info id2Info = ID2Info.GetInstance();
1225

    
1226
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1227
            {
1228
                try
1229
                {
1230
                    var query = $@"SELECT [INDEX], [TYPE], [NAME] FROM {PSN_NOPOCKETSETTING};";
1231
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1232
                    {
1233
                        dt = ds.Tables[0].Copy();
1234
                    }
1235
                }
1236
                catch (Exception ex)
1237
                {
1238
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1239
                }
1240
            }
1241

    
1242
            return dt;
1243
        }
1244

    
1245
        public static DataTable SelectAirFinCoolerSetting()
1246
        {
1247
            DataTable dt = null;
1248
            ID2Info id2Info = ID2Info.GetInstance();
1249

    
1250
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1251
            {
1252
                try
1253
                {
1254
                    var query = $@"SELECT [INDEX], [TYPE], [TagIdentifier], [AttributeName], [NAME] FROM {PSN_AIRFINCOOLERSETTING};";
1255
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1256
                    {
1257
                        dt = ds.Tables[0].Copy();
1258
                    }
1259
                }
1260
                catch (Exception ex)
1261
                {
1262
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1263
                }
1264
            }
1265

    
1266
            return dt;
1267
        }
1268

    
1269
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
1270
        {
1271
            ID2Info id2Info = ID2Info.GetInstance();
1272
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1273
            {
1274
                try
1275
                {
1276
                    using (var txn = connection.BeginTransaction())
1277
                    {
1278
                        var query = $"DELETE FROM {PSN_HEADER_SETTING}";
1279
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1280

    
1281
                        foreach (HeaderInfo headerInfo in headerInfos)
1282
                        {
1283
                            foreach (HeaderItem item in headerInfo.HeaderItems)
1284
                            {
1285
                                query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
1286
                                var cmd = connection.GetSqlStringCommand(query);
1287
                                AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
1288
                                AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
1289
                                AddWithValue(cmd, "@INDEX", item.Index);
1290
                                AddWithValue(cmd, "@NAME", item.Name);
1291
                                connection.ExecuteNonQuery(cmd, txn);
1292
                            }
1293
                        }
1294
                        txn.Commit();
1295
                    }
1296

    
1297
                }
1298
                catch (Exception ex)
1299
                {
1300
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1301
                    return false;
1302
                }
1303
            }
1304
            return true;
1305
        }
1306

    
1307
        public static bool SaveVentDrainSetting(List<VentDrainInfo> ventDrainInfos)
1308
        {
1309
            ID2Info id2Info = ID2Info.GetInstance();
1310
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1311
            {
1312
                using (var txn = connection.BeginTransaction())
1313
                {
1314
                    try
1315
                    {
1316
                        var query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
1317
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1318

    
1319
                        foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
1320
                        {
1321
                            foreach (VentDrainItem item in ventDrainInfo.VentDrainItems)
1322
                            {
1323
                                query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
1324
                                var cmd = connection.GetSqlStringCommand(query);
1325
                                AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
1326
                                AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
1327
                                AddWithValue(cmd, "@INDEX", item.Index);
1328
                                AddWithValue(cmd, "@NAME", item.Name);
1329
                                connection.ExecuteNonQuery(cmd, txn);
1330
                            }
1331
                        }
1332

    
1333
                        txn.Commit();
1334
                    }
1335
                    catch (Exception ex)
1336
                    {
1337
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1338
                        return false;
1339
                    }
1340
                }
1341
            }
1342

    
1343
            return true;
1344
        }
1345

    
1346
        public static bool SaveValveGroupItemsSetting(List<ValveGroupItem> valveGroupItems)
1347
        {
1348
            ID2Info id2Info = ID2Info.GetInstance();
1349
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1350
            {
1351
                using (var txn = connection.BeginTransaction())
1352
                {
1353
                    try
1354
                    {
1355
                        var query = $"DELETE FROM {PSN_VALVEGROUP_SETTING}";
1356
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1357

    
1358
                        foreach (ValveGroupItem item in valveGroupItems)
1359
                        {
1360
                            query = $"INSERT INTO {PSN_VALVEGROUP_SETTING} (OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName) " +
1361
                                $"VALUES (@OID, @GroupType, @TagIdentifier, @AttributeName, @SppidSymbolName)";
1362
                            var cmd = connection.GetSqlStringCommand(query);
1363
                            AddWithValue(cmd, "@OID", item.OID);
1364
                            AddWithValue(cmd, "@GroupType", item.GroupType);
1365
                            AddWithValue(cmd, "@TagIdentifier", item.TagIdentifier);
1366
                            AddWithValue(cmd, "@AttributeName", item.AttributeName);
1367
                            AddWithValue(cmd, "@SppidSymbolName", item.SppidSymbolName);
1368
                            connection.ExecuteNonQuery(cmd, txn);
1369
                        }
1370

    
1371
                        txn.Commit();
1372
                    }
1373
                    catch (Exception ex)
1374
                    {
1375
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1376
                        return false;
1377
                    }
1378
                }
1379
            }
1380

    
1381
            return true;
1382
        }
1383

    
1384
        public static bool SaveKeywordsSetting(List<KeywordItem> keywordItems)
1385
        {
1386
            ID2Info id2Info = ID2Info.GetInstance();
1387
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1388
            {
1389
                using (var txn = connection.BeginTransaction())
1390
                {
1391
                    try
1392
                    {
1393
                        var query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
1394
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1395

    
1396
                        foreach (KeywordItem item in keywordItems)
1397
                        {
1398
                            query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
1399
                            var cmd = connection.GetSqlStringCommand(query);
1400
                            AddWithValue(cmd, "@INDEX", item.Index);
1401
                            AddWithValue(cmd, "@NAME", item.Name);
1402
                            AddWithValue(cmd, "@KEYWORD", item.Keyword);
1403
                            connection.ExecuteNonQuery(cmd, txn);
1404
                        }
1405

    
1406
                        txn.Commit();
1407
                    }
1408
                    catch (Exception ex)
1409
                    {
1410
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1411
                        return false;
1412
                    }
1413
                }
1414
            }
1415

    
1416
            return true;
1417
        }
1418

    
1419
        public static bool SaveMultiwaysSetting(List<MultiwayItem> multiwayItems)
1420
        {
1421
            ID2Info id2Info = ID2Info.GetInstance();
1422
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1423
            {
1424
                using (var txn = connection.BeginTransaction())
1425
                {
1426
                    try
1427
                    {
1428
                        var query = $"DELETE FROM {PSN_MULTIWAY_SETTING}";
1429
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1430

    
1431
                        foreach (MultiwayItem item in multiwayItems)
1432
                        {
1433
                            query = $"INSERT INTO {PSN_MULTIWAY_SETTING} ([INDEX], NAME) VALUES (@INDEX, @NAME)";
1434
                            var cmd = connection.GetSqlStringCommand(query);
1435
                            AddWithValue(cmd, "@INDEX", item.Index);
1436
                            AddWithValue(cmd, "@NAME", item.Name);
1437
                            connection.ExecuteNonQuery(cmd, txn);
1438
                        }
1439

    
1440
                        txn.Commit();
1441
                    }
1442
                    catch (Exception ex)
1443
                    {
1444
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1445
                        return false;
1446
                    }
1447
                }
1448
            }
1449

    
1450
            return true;
1451
        }
1452

    
1453
        public static bool SaveEquipmentNopocketSetting(List<EquipmentNoPocketItem> keywordItems)
1454
        {
1455
            ID2Info id2Info = ID2Info.GetInstance();
1456
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1457
            {
1458
                using (var txn = connection.BeginTransaction())
1459
                {
1460
                    try
1461
                    {
1462
                        var query = $"DELETE FROM {PSN_NOPOCKETSETTING}";
1463
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1464

    
1465
                        foreach (EquipmentNoPocketItem item in keywordItems)
1466
                        {
1467
                            query = $"INSERT INTO {PSN_NOPOCKETSETTING} ([INDEX], [TYPE], [NAME]) VALUES (@INDEX, @TYPE, @NAME)";
1468
                            var cmd = connection.GetSqlStringCommand(query);
1469
                            AddWithValue(cmd, "@INDEX", item.Index);
1470
                            AddWithValue(cmd, "@TYPE", item.Type);
1471
                            AddWithValue(cmd, "@NAME", item.Name);
1472
                            connection.ExecuteNonQuery(cmd, txn);
1473
                        }
1474

    
1475
                        txn.Commit();
1476
                    }
1477
                    catch (Exception ex)
1478
                    {
1479
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1480
                        return false;
1481
                    }
1482
                }
1483
            }
1484

    
1485
            return true;
1486
        }
1487

    
1488
        public static bool SaveAirFinCoolerSetting(List<EquipmentAirFinCoolerItem> keywordItems)
1489
        {
1490
            ID2Info id2Info = ID2Info.GetInstance();
1491
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1492
            {
1493
                using (var txn = connection.BeginTransaction())
1494
                {
1495
                    try
1496
                    {
1497
                        var query = $"DELETE FROM {PSN_AIRFINCOOLERSETTING}";
1498
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1499

    
1500
                        foreach (EquipmentAirFinCoolerItem item in keywordItems)
1501
                        {
1502
                            query = $"INSERT INTO {PSN_AIRFINCOOLERSETTING} ([INDEX], [TYPE], [TagIdentifier], [AttributeName], [NAME]) VALUES (@INDEX, @TYPE, @TagIdentifier, @AttributeName, @NAME)";
1503
                            var cmd = connection.GetSqlStringCommand(query);
1504
                            AddWithValue(cmd, "@INDEX", item.Index);
1505
                            AddWithValue(cmd, "@TYPE", item.Type);
1506
                            AddWithValue(cmd, "@TagIdentifier", item.TagIdentifier);
1507
                            AddWithValue(cmd, "@AttributeName", item.AttributeName);
1508
                            AddWithValue(cmd, "@NAME", item.Name);
1509
                            connection.ExecuteNonQuery(cmd, txn);
1510

    
1511
                        }
1512

    
1513
                        txn.Commit();
1514
                    }
1515
                    catch (Exception ex)
1516
                    {
1517
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1518
                        return false;
1519
                    }
1520
                }
1521
            }
1522

    
1523
            return true;
1524
        }
1525

    
1526
        public static bool SaveTopologyRule(DataTable dt)
1527
        {
1528
            ID2Info id2Info = ID2Info.GetInstance();
1529
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1530
            {
1531
                using (var txn = connection.BeginTransaction())
1532
                {
1533
                    try
1534
                    {
1535
                        var query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
1536
                        var cmd = connection.GetSqlStringCommand(query);
1537
                        //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1538
                        connection.ExecuteNonQuery(cmd, txn);
1539

    
1540
                        foreach (DataRow row in dt.Rows)
1541
                        {
1542
                            query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
1543
                            cmd = connection.GetSqlStringCommand(query);
1544
                            AddWithValue(cmd, "@UID", row["NAME"].ToString());
1545
                            connection.ExecuteNonQuery(cmd, txn);
1546
                        }
1547

    
1548
                        txn.Commit();
1549
                    }
1550
                    catch (Exception ex)
1551
                    {
1552
                        txn.Rollback();
1553
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1554
                        return false;
1555
                    }
1556
                }
1557
            }
1558

    
1559
            return true;
1560
        }
1561

    
1562
        public static DataTable SelectTopologyRule()
1563
        {
1564
            DataTable dt = null;
1565

    
1566
            ID2Info id2Info = ID2Info.GetInstance();
1567
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1568
            {
1569
                try
1570
                {
1571
                    var query = $"SELECT * FROM {PSN_TOPOLOGY_RULE}";
1572
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1573
                    {
1574
                        dt = ds.Tables[0].Copy();
1575
                    }
1576
                }
1577
                catch (Exception ex)
1578
                {
1579
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1580
                }
1581
            }
1582

    
1583
            return dt;
1584
        }
1585

    
1586
        private static void AddWithValue(DbCommand cmd, string PropName, object Value)
1587
        {
1588
            var param = cmd.CreateParameter();
1589
            param.ParameterName = PropName;
1590
            param.Value = Value;
1591
            cmd.Parameters.Add(param);
1592
        }
1593

    
1594
        public static DataTable SelectRevisionTable()
1595
        {
1596
            DataTable dt = null;
1597
            ID2Info id2Info = ID2Info.GetInstance();
1598

    
1599
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1600
            {
1601
                try
1602
                {
1603
                    var query = $"SELECT T.name AS table_name, C.name AS column_name FROM ARS_COMMON.sys.tables AS T INNER JOIN ARS_COMMON.sys.columns AS C ON T.object_id = C.object_id WHERE T.name = 'PSNREVISION'";
1604
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1605
                    {
1606
                        dt = ds.Tables[0].Copy();
1607
                    }
1608
                }
1609
                catch (Exception ex)
1610
                {
1611
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1612
                }
1613
            }
1614

    
1615
            return dt;
1616
        }
1617

    
1618
        public static DataTable SelectRevision()
1619
        {
1620
            DataTable dt = null;
1621
            ID2Info id2Info = ID2Info.GetInstance();
1622

    
1623
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1624
            {
1625
                try
1626
                {
1627
                    var query = $"SELECT * FROM ARS_COMMON.dbo.PSNREVISION";
1628

    
1629
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1630
                    {
1631
                        dt = ds.Tables[0].Copy();
1632
                    }
1633
                }
1634
                catch (Exception ex)
1635
                {
1636
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1637
                }
1638
            }
1639

    
1640
            return dt;
1641
        }
1642

    
1643
        public static bool ConnCreateRevisionTable(Dictionary<string, string> dicColCheck, List<string> names)
1644
        {
1645
            bool result = false;
1646
            ID2Info id2Info = ID2Info.GetInstance();
1647

    
1648
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1649
            {
1650
                try
1651
                {
1652
                    if (names.Count == 0)
1653
                    {
1654

    
1655
                        var query = $"CREATE TABLE ARS_COMMON.dbo.PSNRevision ([OID] [uniqueidentifier] ,[Project_OID] [uniqueidentifier] ," +
1656
                            $"[ProjectCode] nvarchar(255), [RevNumber] int ,[UserName] nvarchar(255) ,[TimeData] nvarchar(255) ," +
1657
                            $"[PSNDatabasePath] nvarchar(255) ,	[PsnByPBSFilter] varchar(20) ,[PsnByNPDFilter] varchar(20) ,[PidDrawings] int ,	" +
1658
                            $"[PipeSystems] int ,[PipeLines] int ,[Topologies] int ,[PipeSystemNetworks] int ,[InValidPSNs] int ,[E2E] int ," +
1659
                            $"[E2B] int ,[B2E] int ,[HDE] int ,[HD2] int ,[HDB] int ,[B2B] int, [LastModificationdate] nvarchar(255), [ID2_PSN] nvarchar(10))";
1660
                        using (var cmd = connection.GetSqlStringCommand(query))
1661
                        {
1662
                            cmd.ExecuteNonQuery();
1663
                        }
1664
                    }
1665
                    else
1666
                    {
1667
                        AddColumn(PSN_REVISION, dicColCheck, names);
1668
                    }
1669

    
1670
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
1671
                    {
1672
                        bool check = false;
1673
                        if (colnames != null)
1674
                        {
1675
                            foreach (KeyValuePair<string, string> col in dicCol)
1676
                            {
1677
                                check = false;
1678
                                foreach (string c in colnames)
1679
                                {
1680
                                    if (col.Key.ToUpper().Equals(c.ToUpper()))
1681
                                    {
1682
                                        check = true;
1683
                                        break;
1684
                                    }
1685
                                }
1686

    
1687
                                if (!check) //없으면 추가
1688
                                {
1689
                                    string i = string.Empty;
1690
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
1691
                                        i = "DEFAULT 0";
1692

    
1693
                                    var queryalter = $"ALTER TABLE ARS_COMMON.dbo.PSNRevision ADD [" + col.Key + "] " + col.Value + " " + i + ";";
1694
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
1695
                                    {
1696
                                        cmd.ExecuteNonQuery();
1697
                                    }
1698
                                }
1699
                            }
1700
                        }
1701
                    }
1702

    
1703
                    result = true;
1704
                }
1705
                catch (Exception ex)
1706
                {
1707
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1708
                }
1709
            }
1710

    
1711
            return result;
1712
        }
1713

    
1714
        public static bool UpdateRevision(string TimeData, string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
1715
           int PidDrawings, int PipeSystems, int PipeLines, int Topologies, int PipeSystemNetworks, int InValidPSNs, int E2E, int E2B, int B2E, int HDE, int HD2, int HDB, int B2B)
1716
        {
1717
            ID2Info id2Info = ID2Info.GetInstance();
1718
            bool result = true;
1719

    
1720
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1721
            {
1722
                try
1723
                {
1724
                    using (var txn = connection.BeginTransaction())
1725
                    {
1726
                        try
1727
                        {
1728
                            string where = string.Format("DELETE FROM ARS_COMMON.dbo.PSNRevision WHERE ProjectCode = '{0}' AND PSNDatabasePath = '{1}' AND RevNumber = {2}", ProjectCode, PSNDatabasePath, RevNumber);
1729
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(where), txn);
1730
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
1731
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
1732
                                $"HD2, HDB, B2B, LastModificationdate, ID2_PSN) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
1733
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
1734
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B, @LastModificationdate, @ID2_PSN)";
1735
                            var cmd = connection.GetSqlStringCommand(query);
1736

    
1737
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
1738
                            AddWithValue(cmd, "@RevNumber", RevNumber);
1739
                            AddWithValue(cmd, "@UserName", UserName);
1740
                            AddWithValue(cmd, "@TimeData", TimeData); //String.Format("{0:G}", dt) // DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
1741

    
1742
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
1743

    
1744
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
1745
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
1746
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
1747
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
1748
                            AddWithValue(cmd, "@PipeLines", PipeLines);
1749

    
1750
                            AddWithValue(cmd, "@Topologies", Topologies);
1751
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
1752
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
1753
                            AddWithValue(cmd, "@E2E", E2E);
1754
                            AddWithValue(cmd, "@E2B", E2B);
1755
                            AddWithValue(cmd, "@B2E", B2E);
1756
                            AddWithValue(cmd, "@HDE", HDE);
1757
                            AddWithValue(cmd, "@HD2", HD2);
1758
                            AddWithValue(cmd, "@HDB", HDB);
1759
                            AddWithValue(cmd, "@B2B", B2B);
1760
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
1761
                            DateTime oDateTime = DateTime.Now;
1762
                            AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
1763
                            AddWithValue(cmd, "@ID2_PSN", "Y");
1764
                            connection.ExecuteNonQuery(cmd, txn);
1765

    
1766
                            txn.Commit();
1767
                        }
1768
                        catch (Exception ex)
1769
                        {
1770
                            txn.Rollback();
1771
                            result = false;
1772
                        }
1773
                    }
1774
                }
1775
                catch (Exception ex)
1776
                {
1777
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1778
                    result = false;
1779
                }
1780
            }
1781

    
1782
            return result;
1783
        }
1784

    
1785

    
1786
        public static bool SaveRevision(string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
1787
            int PidDrawings, int PipeSystems, int PipeLines, int Topologies, int PipeSystemNetworks, int InValidPSNs, int E2E, int E2B, int B2E, int HDE, int HD2, int HDB, int B2B)
1788
        {
1789
            ID2Info id2Info = ID2Info.GetInstance();
1790

    
1791
            bool result = true;
1792

    
1793
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1794
            {
1795
                try
1796
                {
1797
                    using (var txn = connection.BeginTransaction())
1798
                    {
1799
                        try
1800
                        {
1801

    
1802
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
1803
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
1804
                                $"HD2, HDB, B2B, LastModificationdate, ID2_PSN) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
1805
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
1806
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B, @LastModificationdate, @ID2_PSN)";
1807
                            var cmd = connection.GetSqlStringCommand(query);
1808
                            AddWithValue(cmd, "@OID", Guid.NewGuid().ToString());
1809
                            AddWithValue(cmd, "@Project_OID", Guid.NewGuid().ToString());
1810
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
1811
                            AddWithValue(cmd, "@RevNumber", RevNumber);
1812
                            AddWithValue(cmd, "@UserName", UserName);
1813

    
1814
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
1815
                            DateTime oDateTime = DateTime.Now;
1816

    
1817
                            AddWithValue(cmd, "@TimeData", oDateTime.ToString(ci)); //String.Format("{0:G}", dt) // DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
1818

    
1819
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
1820

    
1821
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
1822
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
1823
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
1824
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
1825
                            AddWithValue(cmd, "@PipeLines", PipeLines);
1826

    
1827
                            AddWithValue(cmd, "@Topologies", Topologies);
1828
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
1829
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
1830
                            AddWithValue(cmd, "@E2E", E2E);
1831
                            AddWithValue(cmd, "@E2B", E2B);
1832
                            AddWithValue(cmd, "@B2E", B2E);
1833
                            AddWithValue(cmd, "@HDE", HDE);
1834
                            AddWithValue(cmd, "@HD2", HD2);
1835
                            AddWithValue(cmd, "@HDB", HDB);
1836
                            AddWithValue(cmd, "@B2B", B2B);
1837
                            // AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
1838
                            AddWithValue(cmd, "@LastModificationdate", "");
1839
                            AddWithValue(cmd, "@ID2_PSN", "Y");
1840

    
1841
                            connection.ExecuteNonQuery(cmd, txn);
1842

    
1843
                            txn.Commit();
1844
                        }
1845
                        catch (Exception ex)
1846
                        {
1847
                            txn.Rollback();
1848
                            result = false;
1849
                        }
1850
                    }
1851
                }
1852
                catch (Exception ex)
1853
                {
1854
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1855
                    result = false;
1856
                }
1857
            }
1858

    
1859
            return result;
1860
        }
1861

    
1862
        public static bool SavePSNData(PSN item)
1863
        {
1864
            ID2Info id2Info = ID2Info.GetInstance();
1865

    
1866
            bool result = true;
1867

    
1868
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1869
            {
1870
                try
1871
                {
1872
                    using (var txn = connection.BeginTransaction())
1873
                    {
1874
                        try
1875
                        {
1876
                            // Path Items
1877
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
1878
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1879
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
1880
                            {
1881
                                DataRow row = item.PathItems.Rows[i];
1882
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
1883
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, MULTIWAY," +
1884
                                    $"PIDNAME, Equipment_OID, NPD, GROUPTAG, PipeSystemNetwork_OID_ID2, PipeRun_OID, PipeSystemNetwork_OID, EqpGroupTag, MainLineTag, EGTConnectedPoint, EGFlowDirection) VALUES " +
1885
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @MULTIWAY, @PIDNAME, " +
1886
                                    $"@Equipment_OID, @NPD, @GROUPTAG, @PipeSystemNetwork_OID_ID2,@PipeRun_OID, @PipeSystemNetwork_OID, @EqpGroupTag , @MainLineTag, @EGTConnectedPoint, @EGFlowDirection)";
1887
                                var cmd = connection.GetSqlStringCommand(query);
1888
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1889
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
1890
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
1891
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
1892
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
1893
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
1894
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1895
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString());
1896
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
1897
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
1898
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
1899
                                AddWithValue(cmd, "@MULTIWAY", string.IsNullOrEmpty(row["MULTIWAY"].ToString()) ? "" : row["MULTIWAY"].ToString());
1900
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
1901
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1902
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1903
                                AddWithValue(cmd, "@GROUPTAG", string.IsNullOrEmpty(row["GROUPTAG"].ToString()) ? "" : row["GROUPTAG"].ToString());
1904
                                AddWithValue(cmd, "@PipeSystemNetwork_OID_ID2", string.IsNullOrEmpty(row["PipeSystemNetwork_OID_ID2"].ToString()) ? "" : row["PipeSystemNetwork_OID_ID2"].ToString());
1905
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
1906
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
1907
                                AddWithValue(cmd, "@EqpGroupTag", string.IsNullOrEmpty(row["EqpGroupTag"].ToString()) ? "" : row["EqpGroupTag"].ToString());
1908
                                AddWithValue(cmd, "@MainLineTag", string.IsNullOrEmpty(row["MainLineTag"].ToString()) ? "" : row["MainLineTag"].ToString());
1909

    
1910
                                AddWithValue(cmd, "@EGTConnectedPoint", string.IsNullOrEmpty(row["EGTConnectedPoint"].ToString()) ? "0" : row["EGTConnectedPoint"].ToString());
1911
                                AddWithValue(cmd, "@EGFlowDirection", string.IsNullOrEmpty(row["EGFlowDirection"].ToString()) ? "" : row["EGFlowDirection"].ToString());
1912
                                connection.ExecuteNonQuery(cmd, txn);
1913
                            }
1914

    
1915
                            // Sequence
1916
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
1917
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1918
                            foreach (DataRow row in item.SequenceData.Rows)
1919
                            {
1920
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
1921
                                var cmd = connection.GetSqlStringCommand(query);
1922
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1923
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
1924
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
1925
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1926
                                connection.ExecuteNonQuery(cmd, txn);
1927
                            }
1928

    
1929
                            // Nozzle
1930
                            query = $"DELETE FROM {PSN_NOZZLE}";
1931
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1932
                            foreach (DataRow row in item.Nozzle.Rows)
1933
                            {
1934
                                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)";
1935
                                var cmd = connection.GetSqlStringCommand(query);
1936
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1937
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1938

    
1939
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1940
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1941
                                else
1942
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1943

    
1944
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1945
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1946
                                else
1947
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1948

    
1949
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1950
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
1951
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1952
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
1953

    
1954
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
1955
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
1956
                                else
1957
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
1958

    
1959
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
1960
                                connection.ExecuteNonQuery(cmd, txn);
1961
                            }
1962

    
1963
                            //Equipment
1964
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
1965
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1966
                            foreach (DataRow row in item.Equipment.Rows)
1967
                            {
1968
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
1969
                                var cmd = connection.GetSqlStringCommand(query);
1970
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1971
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1972

    
1973
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1974
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1975
                                else
1976
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1977

    
1978
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1979
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1980
                                else
1981
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1982

    
1983
                                connection.ExecuteNonQuery(cmd, txn);
1984
                            }
1985

    
1986
                            // TopologySet
1987
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
1988
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1989
                            foreach (DataRow row in item.TopologySet.Rows)
1990
                            {
1991
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
1992
                                var cmd = connection.GetSqlStringCommand(query);
1993
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1994
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1995
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
1996
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
1997
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
1998
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
1999
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
2000
                                connection.ExecuteNonQuery(cmd, txn);
2001
                            }
2002

    
2003
                            // PSN
2004
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
2005
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2006
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
2007
                            {
2008
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
2009
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy, Pocket, EGTag, HasMLTags, GroundLevel) VALUES " +
2010
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy, @Pocket, @EGTag, @HasMLTags, @GroundLevel)";
2011
                                var cmd = connection.GetSqlStringCommand(query);
2012
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2013
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
2014
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
2015
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
2016
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
2017
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
2018
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
2019
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
2020

    
2021
                                int IsValid = 0;
2022
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
2023
                                    IsValid = 0;
2024
                                else if (row["IsValid"].ToString() == "InValid")
2025
                                    IsValid = 1;
2026
                                else if (row["IsValid"].ToString() == "Error")
2027
                                    IsValid = -1;
2028

    
2029
                                AddWithValue(cmd, "@IsValid", IsValid);
2030
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
2031

    
2032
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
2033
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
2034

    
2035
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
2036
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
2037
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
2038
                                else
2039
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString().Replace("%", ""));
2040

    
2041
                                AddWithValue(cmd, "@Pocket", string.IsNullOrEmpty(row["Pocket"].ToString()) ? "Yes" : row["Pocket"].ToString());
2042

    
2043
                                AddWithValue(cmd, "@EGTag", string.IsNullOrEmpty(row["EGTag"].ToString()) ? "" : row["EGTag"].ToString());
2044
                                AddWithValue(cmd, "@HasMLTags", string.IsNullOrEmpty(row["HasMLTags"].ToString()) ? "False" : row["HasMLTags"].ToString());
2045
                                AddWithValue(cmd, "@GroundLevel", string.IsNullOrEmpty(row["GroundLevel"].ToString()) ? "" : row["GroundLevel"].ToString());
2046
                                connection.ExecuteNonQuery(cmd, txn);
2047
                            }
2048

    
2049
                            //Pipeline
2050
                            query = $"DELETE FROM {PSN_PIPELINE}";
2051
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2052
                            foreach (DataRow row in item.PipeLine.Rows)
2053
                            {
2054
                                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)";
2055
                                var cmd = connection.GetSqlStringCommand(query);
2056
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
2057
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
2058
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
2059
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
2060
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
2061
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
2062
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
2063
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
2064
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
2065
                                connection.ExecuteNonQuery(cmd, txn);
2066
                            }
2067

    
2068
                            //PipeSystem
2069
                            query = $"DELETE FROM {PSN_PIPESYSTEM}";
2070
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2071
                            foreach (DataRow row in item.PipeSystem.Rows)
2072
                            {
2073
                                query = $"INSERT INTO {PSN_PIPESYSTEM} (OID, DESCRIPTION, FLUID, PMC, PipeLineQty, GroundLevel) VALUES (@OID, @DESCRIPTION, @FLUID, @PMC, @PipeLineQty, @GroundLevel)";
2074
                                var cmd = connection.GetSqlStringCommand(query);
2075
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
2076
                                AddWithValue(cmd, "@DESCRIPTION", row["DESCRIPTION"].ToString());
2077
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
2078
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
2079
                                AddWithValue(cmd, "@PipeLineQty", row["PipeLineQty"].ToString());
2080
                                AddWithValue(cmd, "@GroundLevel", row["GroundLevel"].ToString());
2081
                                connection.ExecuteNonQuery(cmd, txn);
2082
                            }
2083

    
2084
                            if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
2085
                            {
2086
                                query = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
2087
                                connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2088
                            }
2089

    
2090

    
2091
                            txn.Commit();
2092
                        }
2093
                        catch (Exception ex)
2094
                        {
2095
                            txn.Rollback();
2096
                            result = false;
2097
                        }
2098

    
2099
                    }
2100
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
2101
                    {
2102
                        bool check = false;
2103
                        if (colnames != null)
2104
                        {
2105
                            foreach (KeyValuePair<string, string> col in dicCol)
2106
                            {
2107
                                check = false;
2108
                                foreach (string c in colnames)
2109
                                {
2110
                                    if (col.Key.ToUpper().Equals(c.ToUpper()))
2111
                                    {
2112
                                        check = true;
2113
                                        break;
2114
                                    }
2115
                                }
2116

    
2117
                                if (!check) //없으면 추가
2118
                                {
2119
                                    string i = string.Empty;
2120
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
2121
                                        i = "DEFAULT 0";
2122

    
2123
                                    var queryalter = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
2124
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
2125
                                    {
2126
                                        cmd.ExecuteNonQuery();
2127
                                    }
2128
                                }
2129
                            }
2130
                        }
2131
                    }
2132
                }
2133
                catch (Exception ex)
2134
                {
2135
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2136
                    result = false;
2137
                }
2138
            }
2139

    
2140
            return result;
2141
        }
2142

    
2143
        public static bool SavePSNFluidCode(DataTable dt)
2144
        {
2145
            ID2Info id2Info = ID2Info.GetInstance();
2146

    
2147
            bool result = true;
2148
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2149
            {
2150
                try
2151
                {
2152
                    using (var txn = connection.BeginTransaction())
2153
                    {
2154
                        try
2155
                        {
2156
                            var query = $"DELETE FROM {PSN_FLUIDCODE}";
2157
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2158

    
2159
                            foreach (DataRow row in dt.Rows)
2160
                            {
2161
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
2162
                                var cmd = connection.GetSqlStringCommand(query);
2163
                                cmd.Parameters.Clear();
2164

    
2165
                                {
2166
                                    var param = cmd.CreateParameter();
2167
                                    param.ParameterName = "@UID";
2168
                                    param.Value = row["UID"].ToString();
2169
                                    cmd.Parameters.Add(param);
2170
                                }
2171

    
2172
                                {
2173
                                    var param = cmd.CreateParameter();
2174
                                    param.ParameterName = "@Code";
2175
                                    param.Value = row["Code"].ToString();
2176
                                    cmd.Parameters.Add(param);
2177
                                }
2178

    
2179
                                {
2180
                                    var param = cmd.CreateParameter();
2181
                                    param.ParameterName = "@Description";
2182
                                    param.Value = row["Description"].ToString();
2183
                                    cmd.Parameters.Add(param);
2184
                                }
2185

    
2186
                                {
2187
                                    var param = cmd.CreateParameter();
2188
                                    param.ParameterName = "@Condition";
2189
                                    param.Value = row["Condition"].ToString();
2190
                                    cmd.Parameters.Add(param);
2191
                                }
2192

    
2193
                                {
2194
                                    var param = cmd.CreateParameter();
2195
                                    param.ParameterName = "@Remarks";
2196
                                    param.Value = row["Remarks"].ToString();
2197
                                    cmd.Parameters.Add(param);
2198
                                }
2199

    
2200
                                {
2201
                                    var param = cmd.CreateParameter();
2202
                                    param.ParameterName = "@GroundLevel";
2203
                                    param.Value = row["GroundLevel"].ToString();
2204
                                    cmd.Parameters.Add(param);
2205
                                }
2206

    
2207
                                connection.ExecuteNonQuery(cmd, txn);
2208
                            }
2209
                            txn.Commit();
2210
                        }
2211
                        catch (Exception ex)
2212
                        {
2213
                            txn.Rollback();
2214
                            result = false;
2215
                        }
2216
                    }
2217
                }
2218
                catch (Exception ex)
2219
                {
2220
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2221
                    result = false;
2222
                }
2223
            }
2224

    
2225
            return result;
2226
        }
2227

    
2228
        public static DataTable SelectPSNFluidCode()
2229
        {
2230
            DataTable dt = null;
2231
            ID2Info id2Info = ID2Info.GetInstance();
2232

    
2233
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2234
            {
2235
                try
2236
                {
2237
                    var query = $"SELECT * FROM {PSN_FLUIDCODE}";
2238
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2239
                    {
2240
                        dt = ds.Tables[0].Copy();
2241
                    }
2242
                }
2243
                catch (Exception ex)
2244
                {
2245
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2246
                }
2247
            }
2248

    
2249
            return dt;
2250
        }
2251

    
2252
        public static bool SavePSNPMC(DataTable dt)
2253
        {
2254
            ID2Info id2Info = ID2Info.GetInstance();
2255

    
2256
            bool result = true;
2257
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2258
            {
2259
                try
2260
                {
2261
                    using (var txn = connection.BeginTransaction())
2262
                    {
2263
                        try
2264
                        {
2265
                            var query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
2266
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2267

    
2268
                            foreach (DataRow row in dt.Rows)
2269
                            {
2270
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
2271
                                var cmd = connection.GetSqlStringCommand(query);
2272
                                cmd.Parameters.Clear();
2273

    
2274
                                {
2275
                                    var param = cmd.CreateParameter();
2276
                                    param.ParameterName = "@UID";
2277
                                    param.Value = row["UID"].ToString();
2278
                                    cmd.Parameters.Add(param);
2279
                                }
2280

    
2281
                                {
2282
                                    var param = cmd.CreateParameter();
2283
                                    param.ParameterName = "@Priority";
2284
                                    param.Value = row["Priority"].ToString();
2285
                                    cmd.Parameters.Add(param);
2286
                                }
2287

    
2288
                                {
2289
                                    var param = cmd.CreateParameter();
2290
                                    param.ParameterName = "@Code";
2291
                                    param.Value = row["Code"].ToString();
2292
                                    cmd.Parameters.Add(param);
2293
                                }
2294

    
2295
                                {
2296
                                    var param = cmd.CreateParameter();
2297
                                    param.ParameterName = "@Description";
2298
                                    param.Value = row["Description"].ToString();
2299
                                    cmd.Parameters.Add(param);
2300
                                }
2301

    
2302
                                {
2303
                                    var param = cmd.CreateParameter();
2304
                                    param.ParameterName = "@Condition";
2305
                                    param.Value = row["Condition"].ToString();
2306
                                    cmd.Parameters.Add(param);
2307
                                }
2308

    
2309
                                {
2310
                                    var param = cmd.CreateParameter();
2311
                                    param.ParameterName = "@Remarks";
2312
                                    param.Value = row["Remarks"].ToString();
2313
                                    cmd.Parameters.Add(param);
2314
                                }
2315

    
2316
                                {
2317
                                    var param = cmd.CreateParameter();
2318
                                    param.ParameterName = "@GroundLevel";
2319
                                    param.Value = row["GroundLevel"].ToString();
2320
                                    cmd.Parameters.Add(param);
2321
                                }
2322

    
2323
                                connection.ExecuteNonQuery(cmd, txn);
2324
                            }
2325

    
2326
                            txn.Commit();
2327
                        }
2328
                        catch (Exception ex)
2329
                        {
2330
                            txn.Rollback();
2331
                            result = false;
2332
                        }
2333
                    }
2334
                }
2335
                catch (Exception ex)
2336
                {
2337
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2338
                    result = false;
2339
                }
2340
            }
2341

    
2342
            return result;
2343
        }
2344

    
2345
        public static bool SavePSNInsulation(DataTable dt)
2346
        {
2347
            ID2Info id2Info = ID2Info.GetInstance();
2348

    
2349
            bool result = true;
2350
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2351
            {
2352
                try
2353
                {
2354
                    using (var txn = connection.BeginTransaction())
2355
                    {
2356
                        try
2357
                        {
2358
                            var query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
2359
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2360

    
2361
                            foreach (DataRow row in dt.Rows)
2362
                            {
2363
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
2364
                                var cmd = connection.GetSqlStringCommand(query);
2365
                                cmd.Parameters.Clear();
2366

    
2367
                                {
2368
                                    var param = cmd.CreateParameter();
2369
                                    param.ParameterName = "@UID";
2370
                                    param.Value = row["UID"].ToString();
2371
                                    cmd.Parameters.Add(param);
2372
                                }
2373

    
2374
                                {
2375
                                    var param = cmd.CreateParameter();
2376
                                    param.ParameterName = "@Code";
2377
                                    param.Value = row["Code"].ToString();
2378
                                    cmd.Parameters.Add(param);
2379
                                }
2380

    
2381
                                {
2382
                                    var param = cmd.CreateParameter();
2383
                                    param.ParameterName = "@Description";
2384
                                    param.Value = row["Description"].ToString();
2385
                                    cmd.Parameters.Add(param);
2386
                                }
2387

    
2388
                                {
2389
                                    var param = cmd.CreateParameter();
2390
                                    param.ParameterName = "@Remarks";
2391
                                    param.Value = row["Remarks"].ToString();
2392
                                    cmd.Parameters.Add(param);
2393
                                }
2394

    
2395
                                connection.ExecuteNonQuery(cmd, txn);
2396
                            }
2397

    
2398
                            txn.Commit();
2399
                        }
2400
                        catch (Exception ex)
2401
                        {
2402
                            txn.Rollback();
2403
                            result = false;
2404
                        }
2405
                    }
2406
                }
2407
                catch (Exception ex)
2408
                {
2409
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2410
                    result = false;
2411
                }
2412
            }
2413

    
2414
            return result;
2415
        }
2416

    
2417
        public static PSN GetDBPSN()
2418
        {
2419
            PSN result = new PSN();
2420
            ID2Info id2Info = ID2Info.GetInstance();
2421

    
2422
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2423
            {
2424
                try
2425
                {
2426
                    //query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK}";
2427
                    //using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2428
                    //{
2429
                    //    result.PipeSystemNetwork = ds.Tables[0].Copy();
2430
                    //}
2431

    
2432
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
2433
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2434
                    {
2435
                        result.PipeSystemNetwork = ds.Tables[0].Clone();
2436
                        result.PipeSystemNetwork.Columns["IsValid"].DataType = typeof(string);
2437

    
2438
                        foreach (DataRow row in ds.Tables[0].Rows)
2439
                        {
2440
                            DataRow newRow = result.PipeSystemNetwork.NewRow();
2441
                            newRow["OID"] = row["OID"].ToString();
2442
                            newRow["Type"] = row["Type"].ToString();
2443
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
2444
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
2445
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
2446
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
2447
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
2448
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
2449

    
2450
                            string IsValid = string.Empty;
2451

    
2452
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
2453
                                IsValid = string.Empty;//"OK";
2454
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
2455
                                IsValid = "InValid";
2456
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
2457
                                IsValid = "Error";
2458

    
2459
                            newRow["IsValid"] = IsValid;
2460

    
2461
                            newRow["Status"] = row["Status"].ToString();
2462
                            newRow["PBS"] = row["PBS"].ToString();
2463
                            newRow["Drawings"] = row["Drawings"].ToString();
2464

    
2465
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2466
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
2467

    
2468
                            newRow["Pocket"] = row["Pocket"].ToString();
2469

    
2470
                            newRow["EGTag"] = row["EGTag"].ToString();
2471
                            newRow["HasMLTags"] = row["HasMLTags"].ToString();
2472
                            newRow["GroundLevel"] = row["GroundLevel"].ToString();
2473
                            result.PipeSystemNetwork.Rows.Add(newRow);
2474
                        }
2475
                    }
2476

    
2477
                    query = $"SELECT * FROM {PSN_EQUIPMENT}";
2478
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2479
                    {
2480
                        result.Equipment = ds.Tables[0].Copy();
2481
                    }
2482

    
2483
                    query = $"SELECT * FROM {PSN_NOZZLE}";
2484
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2485
                    {
2486
                        result.Nozzle = ds.Tables[0].Copy();
2487
                    }
2488

    
2489
                    query = $"SELECT * FROM {PSN_PATHITEMS}";
2490
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2491
                    {
2492
                        result.PathItems = ds.Tables[0].Copy();
2493
                    }
2494

    
2495
                    query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
2496
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2497
                    {
2498
                        result.SequenceData = ds.Tables[0].Copy();
2499
                    }
2500

    
2501
                    query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
2502
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2503
                    {
2504
                        result.TopologySet = ds.Tables[0].Copy();
2505
                    }
2506

    
2507
                    query = $"SELECT * FROM {PSN_PIPELINE}";
2508
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2509
                    {
2510
                        result.PipeLine = ds.Tables[0].Copy();
2511
                    }
2512

    
2513
                    query = $"SELECT * FROM {PSN_PIPESYSTEM}";
2514
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2515
                    {
2516
                        result.PipeSystem = ds.Tables[0].Copy();
2517
                    }
2518

    
2519
                    result.Revision = GetRevision();
2520
                }
2521
                catch (Exception ex)
2522
                {
2523
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2524
                    result = null;
2525
                }
2526
            }
2527

    
2528
            return result;
2529
        }
2530

    
2531
        public static int GetRevision()
2532
        {
2533
            int result = 0;
2534
            ID2Info id2Info = ID2Info.GetInstance();
2535

    
2536
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2537
            {
2538
                try
2539
                {
2540
                    var query = $"SELECT DISTINCT PSNRevisionNumber FROM {PSN_PIPESYSTEMNETWORK};";
2541
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2542
                    {
2543
                        foreach (DataRow row in ds.Tables[0].Rows)
2544
                        {
2545
                            string value = row["PSNRevisionNumber"].ToString();
2546
                            if (value.StartsWith("V"))
2547
                                value = value.Remove(0, 1);
2548
                            int revisionNumber = Convert.ToInt32(value);
2549
                            if (result < revisionNumber)
2550
                                result = revisionNumber;
2551
                        }
2552
                    }
2553
                }
2554
                catch (Exception ex)
2555
                {
2556
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2557
                    result = -1;
2558
                }
2559
            }
2560

    
2561
            return result;
2562
        }
2563

    
2564
        public static DataTable GetPathItem()
2565
        {
2566
            DataTable dt = null;
2567

    
2568
            ID2Info id2Info = ID2Info.GetInstance();
2569
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2570
            {
2571
                try
2572
                {
2573
                    var query = $"SELECT * FROM {PSN_PATHITEMS}";
2574
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2575
                    {
2576
                        dt = ds.Tables[0].Copy();
2577
                    }
2578
                }
2579
                catch (Exception ex)
2580
                {
2581
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2582
                }
2583
            }
2584

    
2585
            return dt;
2586
        }
2587

    
2588
        public static DataTable GetTopologySet()
2589
        {
2590
            DataTable dt = null;
2591

    
2592
            ID2Info id2Info = ID2Info.GetInstance();
2593
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2594
            {
2595
                try
2596
                {
2597
                    var query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
2598
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2599
                    {
2600
                        dt = ds.Tables[0].Clone();
2601
                        foreach (DataRow row in ds.Tables[0].Rows)
2602
                        {
2603
                            DataRow newRow = dt.NewRow();
2604
                            newRow["OID"] = row["OID"].ToString();
2605
                            newRow["Type"] = row["Type"].ToString();
2606
                            newRow["SubType"] = row["SubType"].ToString();
2607
                            newRow["HeadItemTag"] = row["HeadItemTag"].ToString();
2608
                            newRow["TailItemTag"] = row["TailItemTag"].ToString();
2609
                            newRow["HeadItemSPID"] = row["HeadItemSPID"].ToString();
2610
                            newRow["TailItemSPID"] = row["TailItemSPID"].ToString();
2611
                            dt.Rows.Add(newRow);
2612
                        }
2613
                    }
2614
                }
2615
                catch (Exception ex)
2616
                {
2617
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2618
                }
2619
            }
2620

    
2621
            return dt;
2622
        }
2623

    
2624
        public static DataTable GetPipeSystemNetwork()
2625
        {
2626
            DataTable dt = null;
2627

    
2628
            ID2Info id2Info = ID2Info.GetInstance();
2629
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2630
            {
2631
                try
2632
                {
2633
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}' ORDER BY OID, OrderNumber";
2634
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2635
                    {
2636
                        dt = ds.Tables[0].Clone();
2637
                        dt.Columns["IsValid"].DataType = typeof(string);
2638
                        foreach (DataRow row in ds.Tables[0].Rows)
2639
                        {
2640
                            DataRow newRow = dt.NewRow();
2641
                            newRow["OID"] = row["OID"].ToString();
2642
                            newRow["Type"] = row["Type"].ToString();
2643
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
2644
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
2645
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
2646
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
2647
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
2648
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
2649

    
2650
                            string IsValid = string.Empty;
2651

    
2652
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
2653
                                IsValid = string.Empty;//"OK";
2654
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
2655
                                IsValid = "InValid";
2656
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
2657
                                IsValid = "Error";
2658

    
2659
                            newRow["IsValid"] = IsValid;
2660
                            newRow["Status"] = row["Status"].ToString();
2661

    
2662
                            newRow["PBS"] = row["PBS"].ToString();
2663
                            newRow["Drawings"] = row["Drawings"].ToString();
2664

    
2665
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2666
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
2667

    
2668
                            newRow["Pocket"] = row["Pocket"].ToString();
2669
                            newRow["EGTag"] = row["EGTag"].ToString();
2670
                            newRow["HasMLTags"] = row["HasMLTags"].ToString();
2671
                            newRow["GroundLevel"] = row["GroundLevel"].ToString();
2672
                            dt.Rows.Add(newRow);
2673
                        }
2674
                    }
2675
                }
2676
                catch (Exception ex)
2677
                {
2678
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2679
                }
2680
            }
2681

    
2682
            return dt;
2683
        }
2684

    
2685
        public static DataTable GetSequenceData()
2686
        {
2687
            DataTable dt = null;
2688

    
2689
            ID2Info id2Info = ID2Info.GetInstance();
2690
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2691
            {
2692
                try
2693
                {
2694
                    var query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
2695
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2696
                    {
2697
                        dt = ds.Tables[0].Copy();
2698
                    }
2699
                }
2700
                catch (Exception ex)
2701
                {
2702
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2703
                }
2704
            }
2705

    
2706
            return dt;
2707
        }
2708

    
2709

    
2710
        //Anohter DB
2711
        public static bool ConnTestAndCreateAnotherTable()
2712
        {
2713
            bool result = false;
2714
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
2715

    
2716
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2717
            {
2718
                try
2719
                {
2720
                    var names = connection.GetTableNames();
2721
                    var matched = names.FirstOrDefault(param => param == PSN_HEADER_SETTING);
2722
                    Dictionary<string, string> dicColCheck = new Dictionary<string, string>();
2723
                    dicColCheck.Add("GROUP_ID", "TEXT");
2724
                    dicColCheck.Add("DESCRIPTION", "TEXT");
2725
                    dicColCheck.Add("INDEX", "INTEGER");
2726
                    dicColCheck.Add("NAME", "TEXT");
2727

    
2728
                    if (matched == null)
2729
                    {
2730
                        var query = $"CREATE TABLE {PSN_HEADER_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
2731
                        using (var cmd = connection.GetSqlStringCommand(query))
2732
                        {
2733
                            cmd.ExecuteNonQuery();
2734
                        }
2735
                    }
2736
                    else
2737
                    {
2738
                        AddColumn(PSN_HEADER_SETTING, dicColCheck);
2739
                    }
2740

    
2741
                    matched = names.FirstOrDefault(param => param == PSN_VENTDRAIN_SETTING);
2742
                    dicColCheck.Clear();
2743
                    dicColCheck.Add("GROUP_ID", "TEXT");
2744
                    dicColCheck.Add("DESCRIPTION", "TEXT");
2745
                    dicColCheck.Add("INDEX", "INTEGER");
2746
                    dicColCheck.Add("NAME", "TEXT");
2747
                    if (matched == null)
2748
                    {
2749
                        var query = $"CREATE TABLE {PSN_VENTDRAIN_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
2750
                        using (var cmd = connection.GetSqlStringCommand(query))
2751
                        {
2752
                            cmd.ExecuteNonQuery();
2753
                        }
2754
                    }
2755
                    else
2756
                    {
2757
                        AddColumn(PSN_VENTDRAIN_SETTING, dicColCheck);
2758
                    }
2759

    
2760
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGY_RULE);
2761
                    dicColCheck.Clear();
2762
                    dicColCheck.Add("UID", "TEXT");
2763
                    if (matched == null)
2764
                    {
2765
                        var query = $"CREATE TABLE {PSN_TOPOLOGY_RULE} (UID TEXT)";
2766
                        using (var cmd = connection.GetSqlStringCommand(query))
2767
                        {
2768
                            cmd.ExecuteNonQuery();
2769
                        }
2770

    
2771
                        DataTable topologyRule = new DataTable();
2772
                        topologyRule.Columns.Add("NAME", typeof(string));
2773

    
2774
                        topologyRule.Rows.Add("FluidCode");
2775
                        topologyRule.Rows.Add("-");
2776
                        topologyRule.Rows.Add("PipingMaterialsClass");
2777
                        topologyRule.Rows.Add("-");
2778
                        topologyRule.Rows.Add("Tag Seq No");
2779

    
2780
                        SaveTopologyRule(topologyRule);
2781
                    }
2782
                    //else
2783
                    //{
2784
                    //    AddColumn(PSN_TOPOLOGY_RULE, dicColCheck);
2785
                    //}
2786

    
2787
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
2788
                    dicColCheck.Clear();
2789
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2790
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2791
                    dicColCheck.Add("OrderNumber", "NVARCHAR(255)");
2792
                    dicColCheck.Add("Pipeline_OID", "NVARCHAR(255)");
2793
                    dicColCheck.Add("From_Data", "NVARCHAR(255)");
2794
                    dicColCheck.Add("To_Data", "NVARCHAR(255)");
2795
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(125)");
2796
                    dicColCheck.Add("PSNRevisionNumber", "NVARCHAR(255)");
2797
                    dicColCheck.Add("IsValid", "INT");
2798
                    dicColCheck.Add("Status", "NVARCHAR(255)");
2799
                    dicColCheck.Add("PBS", "NVARCHAR(255)");
2800
                    dicColCheck.Add("Drawings", "NVARCHAR(255)");
2801
                    dicColCheck.Add("IncludingVirtualData", "NVARCHAR(10)");
2802
                    dicColCheck.Add("PSNAccuracy", "REAL");
2803
                    dicColCheck.Add("Pocket", "NVARCHAR(10)");
2804
                    dicColCheck.Add("EGTag", "NVARCHAR(255)");
2805
                    dicColCheck.Add("HasMLTags", "NVARCHAR(50)");
2806
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2807
                    if (matched == null)
2808
                    {
2809
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(255), Type NVARCHAR(255), OrderNumber NVARCHAR(255), Pipeline_OID NVARCHAR(255), From_Data NVARCHAR(255), " +
2810
                            "To_Data NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
2811
                            "IncludingVirtualData NVARCHAR(10),  PSNAccuracy REAL, Pocket NVARCHAR(10), EGTag NVARCHAR(255), HasMLTags NVARCHAR(50), GroundLevel NVARCHAR(50))";
2812
                        using (var cmd = connection.GetSqlStringCommand(query))
2813
                        {
2814
                            cmd.ExecuteNonQuery();
2815
                        }
2816
                    }
2817
                    else
2818
                    {
2819
                        AddColumn(PSN_PIPESYSTEMNETWORK, dicColCheck);
2820
                    }
2821

    
2822
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
2823
                    dicColCheck.Clear();
2824
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2825
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2826
                    dicColCheck.Add("Xcoords", "REAL");
2827
                    dicColCheck.Add("Ycoords", "REAL");
2828
                    if (matched == null)
2829
                    {
2830
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL)";
2831
                        using (var cmd = connection.GetSqlStringCommand(query))
2832
                        {
2833
                            cmd.ExecuteNonQuery();
2834
                        }
2835
                    }
2836
                    else
2837
                    {
2838
                        AddColumn(PSN_EQUIPMENT, dicColCheck);
2839
                    }
2840

    
2841
                    dicColCheck.Clear();
2842
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2843
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2844
                    dicColCheck.Add("Xcoords", "REAL");
2845
                    dicColCheck.Add("Ycoords", "REAL");
2846
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2847
                    dicColCheck.Add("Fluid", "NVARCHAR(255)");
2848
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2849
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2850
                    dicColCheck.Add("Rotation", "REAL");
2851
                    dicColCheck.Add("FlowDirection", "NVARCHAR(255)");
2852

    
2853
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
2854
                    if (matched == null)
2855
                    {
2856
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL, Equipment_OID NVARCHAR(255), " +
2857
                            "Fluid NVARCHAR(255), NPD NVARCHAR(255), PMC NVARCHAR(255), Rotation REAL, FlowDirection NVARCHAR(255))";
2858
                        using (var cmd = connection.GetSqlStringCommand(query))
2859
                        {
2860
                            cmd.ExecuteNonQuery();
2861
                        }
2862
                    }
2863
                    else
2864
                    {
2865
                        AddColumn(PSN_NOZZLE, dicColCheck);
2866
                    }
2867

    
2868
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
2869
                    dicColCheck.Clear();
2870
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2871
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2872
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2873
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2874
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2875
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2876
                    if (matched == null)
2877
                    {
2878
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
2879
                        using (var cmd = connection.GetSqlStringCommand(query))
2880
                        {
2881
                            cmd.ExecuteNonQuery();
2882
                        }
2883
                    }
2884
                    else
2885
                    {
2886
                        AddColumn(PSN_FLUIDCODE, dicColCheck);
2887
                    }
2888

    
2889
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
2890
                    dicColCheck.Clear();
2891
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2892
                    dicColCheck.Add("Priority", "INTEGER");
2893
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2894
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2895
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2896
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2897
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2898
                    if (matched == null)
2899
                    {
2900
                        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))";
2901
                        using (var cmd = connection.GetSqlStringCommand(query))
2902
                        {
2903
                            cmd.ExecuteNonQuery();
2904
                        }
2905
                    }
2906
                    else
2907
                    {
2908
                        AddColumn(PSN_PIPINGMATLCLASS, dicColCheck);
2909
                    }
2910

    
2911
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
2912
                    dicColCheck.Clear();
2913
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2914
                    dicColCheck.Add("SERIALNUMBER", "NVARCHAR(255)");
2915
                    dicColCheck.Add("PathItem_OID", "NVARCHAR(255)");
2916
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(255)");
2917
                    if (matched == null)
2918
                    {
2919
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(255), SERIALNUMBER NVARCHAR(255), PathItem_OID NVARCHAR(255), TopologySet_OID_Key NVARCHAR(255))";
2920
                        using (var cmd = connection.GetSqlStringCommand(query))
2921
                        {
2922
                            cmd.ExecuteNonQuery();
2923
                        }
2924
                    }
2925
                    else
2926
                    {
2927
                        AddColumn(PSN_SEQUENCEDATA, dicColCheck);
2928
                    }
2929

    
2930
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
2931
                    dicColCheck.Clear();
2932
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2933
                    dicColCheck.Add("SequenceData_OID", "NVARCHAR(255)");
2934
                    dicColCheck.Add("TopologySet_OID", "NVARCHAR(255)");
2935
                    dicColCheck.Add("BranchTopologySet_OID", "NVARCHAR(255)");
2936
                    dicColCheck.Add("PipeLine_OID", "NVARCHAR(255)");
2937
                    dicColCheck.Add("ItemName", "NVARCHAR(255)");
2938
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2939
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
2940
                    dicColCheck.Add("CLASS", "NVARCHAR(255)");
2941
                    dicColCheck.Add("SUBCLASS", "NVARCHAR(255)");
2942
                    dicColCheck.Add("TYPE", "NVARCHAR(255)");
2943
                    dicColCheck.Add("MULTIWAY", "NVARCHAR(255)");
2944
                    dicColCheck.Add("PIDNAME", "NVARCHAR(255)");
2945
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2946
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2947
                    dicColCheck.Add("GROUPTAG", "NVARCHAR(255)");
2948
                    dicColCheck.Add("PipeSystemNetwork_OID_ID2", "NVARCHAR(255)");
2949
                    dicColCheck.Add("PipeRun_OID", "NVARCHAR(255)");
2950
                    dicColCheck.Add("PipeSystemNetwork_OID", "NVARCHAR(255)");
2951
                    dicColCheck.Add("EqpGroupTag", "NVARCHAR(255)");
2952
                    dicColCheck.Add("MainLineTag", "NVARCHAR(255)");
2953
                    dicColCheck.Add("EGTConnectedPoint", "INT");
2954
                    dicColCheck.Add("EGFlowDirection", "NVARCHAR(10)");
2955
                    if (matched == null)
2956
                    {
2957
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
2958
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
2959
                            "CLASS NVARCHAR(255), SUBCLASS NVARCHAR(255), TYPE NVARCHAR(255), MULTIWAY NVARCHAR(255), PIDNAME NVARCHAR(255), Equipment_OID NVARCHAR(255), NPD NVARCHAR(255), GROUPTAG NVARCHAR(255), PipeSystemNetwork_OID_ID2 NVARCHAR(255), PipeRun_OID NVARCHAR(255), " +
2960
                            "PipeSystemNetwork_OID NVARCHAR(255), EqpGroupTag NVARCHAR(255), MainLineTag NVARCHAR(255), EGTConnectedPoint INT, EGFlowDirection NVARCHAR(10))";
2961
                        using (var cmd = connection.GetSqlStringCommand(query))
2962
                        {
2963
                            cmd.ExecuteNonQuery();
2964
                        }
2965
                    }
2966
                    else
2967
                    {
2968
                        AddColumn(PSN_PATHITEMS, dicColCheck);
2969
                    }
2970

    
2971
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
2972
                    dicColCheck.Clear();
2973
                    dicColCheck.Add("OID", "TEXT");
2974
                    if (matched == null)
2975
                    {
2976
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
2977
                        using (var cmd = connection.GetSqlStringCommand(query))
2978
                        {
2979
                            cmd.ExecuteNonQuery();
2980
                        }
2981
                    }
2982

    
2983
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
2984
                    dicColCheck.Clear();
2985
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2986
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2987
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
2988
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
2989
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
2990
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
2991
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
2992

    
2993
                    if (matched == null)
2994
                    {
2995
                        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))";
2996
                        using (var cmd = connection.GetSqlStringCommand(query))
2997
                        {
2998
                            cmd.ExecuteNonQuery();
2999
                        }
3000
                    }
3001
                    else
3002
                    {
3003
                        AddColumn(PSN_TOPOLOGYSET, dicColCheck);
3004
                    }
3005

    
3006
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
3007
                    dicColCheck.Clear();
3008
                    dicColCheck.Add("INDEX", "INTEGER");
3009
                    dicColCheck.Add("NAME", "TEXT");
3010
                    dicColCheck.Add("KEYWORD", "TEXT");
3011
                    if (matched == null)
3012
                    {
3013
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
3014
                        using (var cmd = connection.GetSqlStringCommand(query))
3015
                        {
3016
                            cmd.ExecuteNonQuery();
3017
                        }
3018
                    }
3019
                    else
3020
                    {
3021
                        AddColumn(PSN_TRANSFORMKEYWORD_SETTING, dicColCheck);
3022
                    }
3023

    
3024
                    matched = names.FirstOrDefault(param => param == PSN_MULTIWAY_SETTING);
3025
                    dicColCheck.Clear();
3026
                    dicColCheck.Add("INDEX", "INTEGER");
3027
                    dicColCheck.Add("NAME", "TEXT");
3028
                    if (matched == null)
3029
                    {
3030
                        var query = $"CREATE TABLE {PSN_MULTIWAY_SETTING} ([INDEX] INTEGER, [NAME] TEXT)";
3031
                        using (var cmd = connection.GetSqlStringCommand(query))
3032
                        {
3033
                            cmd.ExecuteNonQuery();
3034
                        }
3035
                    }
3036
                    else
3037
                    {
3038
                        AddColumn(PSN_MULTIWAY_SETTING, dicColCheck);
3039
                    }
3040

    
3041
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
3042
                    dicColCheck.Clear();
3043
                    dicColCheck.Add("OID", "NVARCHAR(255)");
3044
                    dicColCheck.Add("PipeSystem_OID", "NVARCHAR(255)");
3045
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
3046
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
3047
                    dicColCheck.Add("SEQNUMBER", "NVARCHAR(255)");
3048
                    dicColCheck.Add("INSULATION", "NVARCHAR(255)");
3049
                    dicColCheck.Add("FROM_DATA", "NVARCHAR(255)");
3050
                    dicColCheck.Add("TO_DATA", "NVARCHAR(255)");
3051
                    dicColCheck.Add("Unit", "NVARCHAR(100)");
3052
                    if (matched == null)
3053
                    {
3054
                        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), " +
3055
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
3056
                        using (var cmd = connection.GetSqlStringCommand(query))
3057
                        {
3058
                            cmd.ExecuteNonQuery();
3059
                        }
3060
                    }
3061
                    else
3062
                    {
3063
                        AddColumn(PSN_PIPELINE, dicColCheck);
3064
                    }
3065

    
3066
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
3067
                    dicColCheck.Clear();
3068
                    dicColCheck.Add("UID", "NVARCHAR(50)");
3069
                    dicColCheck.Add("Code", "NVARCHAR(255)");
3070
                    dicColCheck.Add("Description", "NVARCHAR(255)");
3071
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
3072
                    if (matched == null)
3073
                    {
3074
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
3075
                        using (var cmd = connection.GetSqlStringCommand(query))
3076
                        {
3077
                            cmd.ExecuteNonQuery();
3078
                        }
3079
                    }
3080
                    else
3081
                    {
3082
                        AddColumn(PSN_INSULATIONPURPOSE, dicColCheck);
3083
                    }
3084

    
3085
                    matched = names.FirstOrDefault(param => param == PSN_VALVEGROUP_SETTING);
3086
                    dicColCheck.Clear();
3087
                    dicColCheck.Add("OID", "NVARCHAR(255)");
3088
                    dicColCheck.Add("GroupType", "NVARCHAR(255)");
3089
                    dicColCheck.Add("TagIdentifier", "NVARCHAR(50)");
3090
                    dicColCheck.Add("AttributeName", "NVARCHAR(255)");
3091
                    dicColCheck.Add("SppidSymbolName", "NVARCHAR(255)");
3092
                    if (matched == null)
3093
                    {
3094
                        var query = $"CREATE TABLE {PSN_VALVEGROUP_SETTING} (OID NVARCHAR(50), GroupType NVARCHAR(255), TagIdentifier NVARCHAR(50), AttributeName NVARCHAR(255), SppidSymbolName NVARCHAR(255))";
3095
                        using (var cmd = connection.GetSqlStringCommand(query))
3096
                        {
3097
                            cmd.ExecuteNonQuery();
3098
                        }
3099
                    }
3100
                    else
3101
                    {
3102
                        AddColumn(PSN_VALVEGROUP_SETTING, dicColCheck);
3103
                    }
3104

    
3105
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEM);
3106
                    dicColCheck.Clear();
3107
                    dicColCheck.Add("OID", "NVARCHAR(255)");
3108
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
3109
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
3110
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
3111
                    dicColCheck.Add("PipeLineQty", "NVARCHAR(255)");
3112
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
3113
                    if (matched == null)
3114
                    {
3115
                        var query = $"CREATE TABLE {PSN_PIPESYSTEM} (OID NVARCHAR(255), DESCRIPTION NVARCHAR(255), FLUID NVARCHAR(255), PMC NVARCHAR(255), PipeLineQty NVARCHAR(255), GroundLevel NVARCHAR(20))";
3116
                        using (var cmd = connection.GetSqlStringCommand(query))
3117
                        {
3118
                            cmd.ExecuteNonQuery();
3119
                        }
3120
                    }
3121
                    else
3122
                    {
3123
                        AddColumn(PSN_PIPESYSTEM, dicColCheck);
3124
                    }
3125

    
3126
                    matched = names.FirstOrDefault(param => param == PSN_NOPOCKETSETTING);
3127
                    dicColCheck.Clear();
3128
                    dicColCheck.Add("INDEX", "INTEGER");
3129
                    dicColCheck.Add("TYPE", "TEXT");
3130
                    dicColCheck.Add("NAME", "TEXT");
3131
                    if (matched == null)
3132
                    {
3133
                        var query = $"CREATE TABLE {PSN_NOPOCKETSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [NAME] TEXT)";
3134
                        using (var cmd = connection.GetSqlStringCommand(query))
3135
                        {
3136
                            cmd.ExecuteNonQuery();
3137
                        }
3138
                    }
3139
                    else
3140
                    {
3141
                        AddColumn(PSN_NOPOCKETSETTING, dicColCheck);
3142
                    }
3143

    
3144
                    matched = names.FirstOrDefault(param => param == PSN_AIRFINCOOLERSETTING);
3145
                    dicColCheck.Clear();
3146
                    dicColCheck.Add("INDEX", "INTEGER");
3147
                    dicColCheck.Add("TYPE", "TEXT");
3148
                    dicColCheck.Add("TagIdentifier", "NVARCHAR(50)");
3149
                    dicColCheck.Add("AttributeName", "NVARCHAR(255)");
3150
                    dicColCheck.Add("NAME", "TEXT");
3151

    
3152
                    if (matched == null)
3153
                    {
3154
                        var query = $"CREATE TABLE {PSN_AIRFINCOOLERSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [TagIdentifier] TEXT, [AttributeName] TEXT, [NAME] TEXT)";
3155
                       
3156
                        using (var cmd = connection.GetSqlStringCommand(query))
3157
                        {
3158
                            cmd.ExecuteNonQuery();
3159
                        }
3160
                    }
3161
                    else
3162
                    {
3163
                        AddColumn(PSN_AIRFINCOOLERSETTING, dicColCheck);
3164
                    }
3165

    
3166

    
3167
                    var query2 = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
3168
                    if (id2Info.ID2DBType == AnotherID2DB_Type.MSSQL)
3169
                    {
3170
                        //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3171
                        using (var cmd = connection.GetSqlStringCommand(query2))
3172
                        {
3173
                            cmd.ExecuteNonQuery();
3174
                        }
3175
                    }
3176

    
3177
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
3178
                    {
3179
                        var colnames = connection.GetColumnNames(TableName);
3180
                        bool check = false;
3181
                        if (colnames != null)
3182
                        {
3183
                            foreach (KeyValuePair<string, string> col in dicCol)
3184
                            {
3185
                                check = false;
3186
                                foreach (string c in colnames)
3187
                                {
3188
                                    if (col.Key.ToUpper().Equals(c.ToUpper()))
3189
                                    {
3190
                                        check = true;
3191
                                        break;
3192
                                    }
3193
                                }
3194

    
3195
                                if (!check) //없으면 추가
3196
                                {
3197
                                    string i = string.Empty;
3198
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
3199
                                        i = "DEFAULT 0";
3200

    
3201
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
3202
                                    using (var cmd = connection.GetSqlStringCommand(query))
3203
                                    {
3204
                                        cmd.ExecuteNonQuery();
3205
                                    }
3206
                                }
3207
                            }
3208
                        }
3209
                    }
3210

    
3211
                    result = true;
3212
                }
3213
                catch (Exception ex)
3214
                {
3215
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3216
                }
3217
            }
3218

    
3219
            return result;
3220
        }
3221

    
3222
        public static bool SaveAnotherRevision(string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
3223
         int PidDrawings, int PipeSystems, int PipeLines, int Topologies, int PipeSystemNetworks, int InValidPSNs, int E2E, int E2B, int B2E, int HDE, int HD2, int HDB, int B2B)
3224
        {
3225
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3226

    
3227
            bool result = true;
3228

    
3229
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3230
            {
3231
                try
3232
                {
3233
                    using (var txn = connection.BeginTransaction())
3234
                    {
3235
                        try
3236
                        {
3237

    
3238
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
3239
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
3240
                                $"HD2, HDB, B2B, LastModificationdate, ID2_PSN) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
3241
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
3242
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B, @LastModificationdate, @ID2_PSN)";
3243
                            var cmd = connection.GetSqlStringCommand(query);
3244
                            AddWithValue(cmd, "@OID", Guid.NewGuid().ToString());
3245
                            AddWithValue(cmd, "@Project_OID", Guid.NewGuid().ToString());
3246
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
3247
                            AddWithValue(cmd, "@RevNumber", RevNumber);
3248
                            AddWithValue(cmd, "@UserName", UserName);
3249

    
3250
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
3251
                            DateTime oDateTime = DateTime.Now;
3252

    
3253
                            AddWithValue(cmd, "@TimeData", oDateTime.ToString(ci)); //String.Format("{0:G}", dt) // DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
3254

    
3255
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
3256

    
3257
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
3258
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
3259
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
3260
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
3261
                            AddWithValue(cmd, "@PipeLines", PipeLines);
3262

    
3263
                            AddWithValue(cmd, "@Topologies", Topologies);
3264
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
3265
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
3266
                            AddWithValue(cmd, "@E2E", E2E);
3267
                            AddWithValue(cmd, "@E2B", E2B);
3268
                            AddWithValue(cmd, "@B2E", B2E);
3269
                            AddWithValue(cmd, "@HDE", HDE);
3270
                            AddWithValue(cmd, "@HD2", HD2);
3271
                            AddWithValue(cmd, "@HDB", HDB);
3272
                            AddWithValue(cmd, "@B2B", B2B);
3273

    
3274
                            AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
3275
                            AddWithValue(cmd, "@ID2_PSN", "Y");
3276

    
3277
                            connection.ExecuteNonQuery(cmd, txn);
3278

    
3279
                            txn.Commit();
3280
                        }
3281
                        catch (Exception ex)
3282
                        {
3283
                            txn.Rollback();
3284
                            result = false;
3285
                        }
3286
                    }
3287
                }
3288
                catch (Exception ex)
3289
                {
3290
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3291
                    result = false;
3292
                }
3293
            }
3294

    
3295
            return result;
3296
        }
3297

    
3298
        public static bool ConnAnotherCreateRevisionTable(Dictionary<string, string> dicColCheck, List<string> names)
3299
        {
3300
            bool result = false;
3301
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3302

    
3303
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3304
            {
3305
                try
3306
                {
3307
                    if (names.Count == 0)
3308
                    {
3309

    
3310
                        var query = $"CREATE TABLE ARS_COMMON.dbo.PSNRevision ([OID] [uniqueidentifier] ,[Project_OID] [uniqueidentifier] ," +
3311
                            $"[ProjectCode] nvarchar(255), [RevNumber] int ,[UserName] nvarchar(255) ,[TimeData] nvarchar(255) ," +
3312
                            $"[PSNDatabasePath] nvarchar(255) ,	[PsnByPBSFilter] varchar(20) ,[PsnByNPDFilter] varchar(20) ,[PidDrawings] int ,	" +
3313
                            $"[PipeSystems] int ,[PipeLines] int ,[Topologies] int ,[PipeSystemNetworks] int ,[InValidPSNs] int ,[E2E] int ," +
3314
                            $"[E2B] int ,[B2E] int ,[HDE] int ,[HD2] int ,[HDB] int ,[B2B] int , [LastModificationdate] nvarchar(255), [ID2_PSN] nvarchar(10))";
3315
                        using (var cmd = connection.GetSqlStringCommand(query))
3316
                        {
3317
                            cmd.ExecuteNonQuery();
3318
                        }
3319
                    }
3320
                    else
3321
                    {
3322
                        AddColumn(PSN_REVISION, dicColCheck, names);
3323
                    }
3324

    
3325
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
3326
                    {
3327
                        bool check = false;
3328
                        if (colnames != null)
3329
                        {
3330
                            foreach (KeyValuePair<string, string> col in dicCol)
3331
                            {
3332
                                check = false;
3333
                                foreach (string c in colnames)
3334
                                {
3335
                                    if (col.Key.ToUpper().Equals(c.ToUpper()))
3336
                                    {
3337
                                        check = true;
3338
                                        break;
3339
                                    }
3340
                                }
3341

    
3342
                                if (!check) //없으면 추가
3343
                                {
3344
                                    string i = string.Empty;
3345
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
3346
                                        i = "DEFAULT 0";
3347

    
3348
                                    var queryalter = $"ALTER TABLE ARS_COMMON.dbo.PSNRevision ADD [" + col.Key + "] " + col.Value + " " + i + ";";
3349
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
3350
                                    {
3351
                                        cmd.ExecuteNonQuery();
3352
                                    }
3353
                                }
3354
                            }
3355
                        }
3356
                    }
3357

    
3358
                    result = true;
3359
                }
3360
                catch (Exception ex)
3361
                {
3362
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3363
                }
3364
            }
3365

    
3366
            return result;
3367
        }
3368

    
3369
        public static DataTable SelectAnotherRevision()
3370
        {
3371
            DataTable dt = null;
3372
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3373

    
3374
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3375
            {
3376
                try
3377
                {
3378
                    var query = $"SELECT * FROM ARS_COMMON.dbo.PSNREVISION";
3379

    
3380
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
3381
                    {
3382
                        dt = ds.Tables[0].Copy();
3383
                    }
3384
                }
3385
                catch (Exception ex)
3386
                {
3387
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3388
                }
3389
            }
3390

    
3391
            return dt;
3392
        }
3393

    
3394
        public static DataTable SelectAnotherRevisionTable()
3395
        {
3396
            DataTable dt = null;
3397
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3398

    
3399
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3400
            {
3401
                try
3402
                {
3403
                    var query = $"SELECT T.name AS table_name, C.name AS column_name FROM ARS_COMMON.sys.tables AS T INNER JOIN ARS_COMMON.sys.columns AS C ON T.object_id = C.object_id WHERE T.name = 'PSNREVISION'";
3404
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
3405
                    {
3406
                        dt = ds.Tables[0].Copy();
3407
                    }
3408
                }
3409
                catch (Exception ex)
3410
                {
3411
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3412
                }
3413
            }
3414

    
3415
            return dt;
3416
        }
3417

    
3418
        public static bool SaveAnotherPSNData(PSN item, List<HeaderInfo> headerInfos, List<VentDrainInfo> ventDrainInfos,
3419
          List<KeywordItem> keywordItems, DataTable dtTopologyRule, DataTable dtFluidCode, DataTable dtPMC, DataTable dtInsulation, DataTable dtvalvegrouping, DataTable dtnopocket, DataTable dtafc,
3420
          List<MultiwayItem> multiwayItems)
3421
        {
3422
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3423

    
3424
            bool result = true;
3425
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3426
            {
3427
                try
3428
                {
3429
                    using (var txn = connection.BeginTransaction())
3430
                    {
3431
                        try
3432
                        {
3433
                            // Path Items
3434
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
3435
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3436
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
3437
                            {
3438
                                DataRow row = item.PathItems.Rows[i];
3439
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
3440
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, MULTIWAY, " +
3441
                                    $"PIDNAME, Equipment_OID, NPD, GROUPTAG, PipeSystemNetwork_OID_ID2, PipeRun_OID, PipeSystemNetwork_OID, EqpGroupTag , MainLineTag, EGTConnectedPoint, EGFlowDirection ) VALUES " +
3442
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @MULTIWAY, @PIDNAME," +
3443
                                    $" @Equipment_OID, @NPD, @GROUPTAG, @PipeSystemNetwork_OID_ID2,@PipeRun_OID, @PipeSystemNetwork_OID, @EqpGroupTag , @MainLineTag, @EGTConnectedPoint, @EGFlowDirection )";
3444
                                var cmd = connection.GetSqlStringCommand(query);
3445
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3446
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
3447
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
3448
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
3449
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
3450
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
3451
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
3452
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString());
3453
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
3454
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
3455
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
3456
                                AddWithValue(cmd, "@MULTIWAY", string.IsNullOrEmpty(row["MULTIWAY"].ToString()) ? "" : row["MULTIWAY"].ToString());
3457
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
3458
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
3459
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
3460
                                AddWithValue(cmd, "@GROUPTAG", string.IsNullOrEmpty(row["GROUPTAG"].ToString()) ? "" : row["GROUPTAG"].ToString());
3461
                                AddWithValue(cmd, "@PipeSystemNetwork_OID_ID2", string.IsNullOrEmpty(row["PipeSystemNetwork_OID_ID2"].ToString()) ? "" : row["PipeSystemNetwork_OID_ID2"].ToString());
3462
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
3463
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
3464
                                AddWithValue(cmd, "@EqpGroupTag", string.IsNullOrEmpty(row["EqpGroupTag"].ToString()) ? "" : row["EqpGroupTag"].ToString());
3465
                                AddWithValue(cmd, "@MainLineTag", string.IsNullOrEmpty(row["MainLineTag"].ToString()) ? "" : row["MainLineTag"].ToString());
3466
                                AddWithValue(cmd, "@EGTConnectedPoint", string.IsNullOrEmpty(row["EGTConnectedPoint"].ToString()) ? "0" : row["EGTConnectedPoint"].ToString());
3467
                                AddWithValue(cmd, "@EGFlowDirection", string.IsNullOrEmpty(row["EGFlowDirection"].ToString()) ? "" : row["EGFlowDirection"].ToString());
3468

    
3469
                                connection.ExecuteNonQuery(cmd, txn);
3470
                            }
3471

    
3472
                            // Sequence
3473
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
3474
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3475
                            foreach (DataRow row in item.SequenceData.Rows)
3476
                            {
3477
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
3478
                                var cmd = connection.GetSqlStringCommand(query);
3479
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3480
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
3481
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
3482
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
3483
                                connection.ExecuteNonQuery(cmd, txn);
3484
                            }
3485

    
3486
                            // Nozzle
3487
                            query = $"DELETE FROM {PSN_NOZZLE}";
3488
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3489
                            foreach (DataRow row in item.Nozzle.Rows)
3490
                            {
3491
                                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)";
3492
                                var cmd = connection.GetSqlStringCommand(query);
3493
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3494
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
3495

    
3496
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
3497
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
3498
                                else
3499
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
3500

    
3501
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
3502
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
3503
                                else
3504
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
3505

    
3506
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
3507
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
3508
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
3509
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
3510

    
3511
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
3512
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
3513
                                else
3514
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
3515

    
3516
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
3517
                                connection.ExecuteNonQuery(cmd, txn);
3518
                            }
3519

    
3520
                            //Equipment
3521
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
3522
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3523
                            foreach (DataRow row in item.Equipment.Rows)
3524
                            {
3525
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
3526
                                var cmd = connection.GetSqlStringCommand(query);
3527
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3528
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
3529

    
3530
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
3531
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
3532
                                else
3533
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
3534

    
3535
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
3536
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
3537
                                else
3538
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
3539

    
3540
                                connection.ExecuteNonQuery(cmd, txn);
3541
                            }
3542

    
3543
                            // TopologySet
3544
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
3545
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3546
                            foreach (DataRow row in item.TopologySet.Rows)
3547
                            {
3548
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
3549
                                var cmd = connection.GetSqlStringCommand(query);
3550
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3551
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
3552
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
3553
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
3554
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
3555
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
3556
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
3557
                                connection.ExecuteNonQuery(cmd, txn);
3558
                            }
3559

    
3560
                            // PSN
3561
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
3562
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3563
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
3564
                            {
3565
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
3566
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy, Pocket, EGTag, HasMLTags, GroundLevel) VALUES " +
3567
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy, @Pocket, @EGTag, @HasMLTags, @GroundLevel)";
3568
                                var cmd = connection.GetSqlStringCommand(query);
3569
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3570
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
3571
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
3572
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
3573
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
3574
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
3575
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
3576
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
3577

    
3578
                                int IsValid = 0;
3579
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
3580
                                    IsValid = 0;
3581
                                else if (row["IsValid"].ToString() == "InValid")
3582
                                    IsValid = 1;
3583
                                else if (row["IsValid"].ToString() == "Error")
3584
                                    IsValid = -1;
3585

    
3586
                                AddWithValue(cmd, "@IsValid", IsValid);
3587
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
3588

    
3589
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
3590
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
3591

    
3592
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
3593
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
3594
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
3595
                                else
3596
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString().Replace("%", ""));
3597

    
3598
                                AddWithValue(cmd, "@Pocket", string.IsNullOrEmpty(row["Pocket"].ToString()) ? "Yes" : row["Pocket"].ToString());
3599
                                AddWithValue(cmd, "@EGTag", string.IsNullOrEmpty(row["EGTag"].ToString()) ? "" : row["EGTag"].ToString());
3600
                                AddWithValue(cmd, "@HasMLTags", string.IsNullOrEmpty(row["HasMLTags"].ToString()) ? "False" : row["HasMLTags"].ToString());
3601
                                AddWithValue(cmd, "@GroundLevel", string.IsNullOrEmpty(row["GroundLevel"].ToString()) ? "" : row["GroundLevel"].ToString());
3602
                                connection.ExecuteNonQuery(cmd, txn);
3603
                            }
3604

    
3605
                            //Pipeline
3606
                            query = $"DELETE FROM {PSN_PIPELINE}";
3607
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3608
                            foreach (DataRow row in item.PipeLine.Rows)
3609
                            {
3610
                                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)";
3611
                                var cmd = connection.GetSqlStringCommand(query);
3612
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3613
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
3614
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
3615
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
3616
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
3617
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
3618
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
3619
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
3620
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
3621
                                connection.ExecuteNonQuery(cmd, txn);
3622
                            }
3623

    
3624
                            //PipeSystem
3625
                            query = $"DELETE FROM {PSN_PIPESYSTEM}";
3626
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3627
                            foreach (DataRow row in item.PipeSystem.Rows)
3628
                            {
3629
                                query = $"INSERT INTO {PSN_PIPESYSTEM} (OID, DESCRIPTION, FLUID, PMC, PipeLineQty, GroundLevel) VALUES (@OID, @DESCRIPTION, @FLUID, @PMC, @PipeLineQty, @GroundLevel)";
3630
                                var cmd = connection.GetSqlStringCommand(query);
3631
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3632
                                AddWithValue(cmd, "@DESCRIPTION", row["DESCRIPTION"].ToString());
3633
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
3634
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
3635
                                AddWithValue(cmd, "@PipeLineQty", row["PipeLineQty"].ToString());
3636
                                AddWithValue(cmd, "@GroundLevel", row["GroundLevel"].ToString());
3637
                                connection.ExecuteNonQuery(cmd, txn);
3638
                            }
3639

    
3640
                            //Header Setting
3641
                            query = $"DELETE FROM {PSN_HEADER_SETTING}";
3642
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3643

    
3644
                            foreach (HeaderInfo headerInfo in headerInfos)
3645
                            {
3646
                                foreach (HeaderItem itemheader in headerInfo.HeaderItems)
3647
                                {
3648
                                    query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
3649
                                    var cmd = connection.GetSqlStringCommand(query);
3650
                                    AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
3651
                                    AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
3652
                                    AddWithValue(cmd, "@INDEX", itemheader.Index);
3653
                                    AddWithValue(cmd, "@NAME", itemheader.Name);
3654
                                    connection.ExecuteNonQuery(cmd, txn);
3655
                                }
3656
                            }
3657

    
3658
                            //Vent/Drain Setting
3659
                            query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
3660
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3661

    
3662
                            foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
3663
                            {
3664
                                foreach (VentDrainItem itemventdrain in ventDrainInfo.VentDrainItems)
3665
                                {
3666
                                    query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
3667
                                    var cmd = connection.GetSqlStringCommand(query);
3668
                                    AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
3669
                                    AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
3670
                                    AddWithValue(cmd, "@INDEX", itemventdrain.Index);
3671
                                    AddWithValue(cmd, "@NAME", itemventdrain.Name);
3672
                                    connection.ExecuteNonQuery(cmd, txn);
3673
                                }
3674
                            }
3675

    
3676
                            //Keyword Setting
3677
                            query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
3678
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3679

    
3680
                            foreach (KeywordItem itemKeyword in keywordItems)
3681
                            {
3682
                                query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
3683
                                var cmd = connection.GetSqlStringCommand(query);
3684
                                AddWithValue(cmd, "@INDEX", itemKeyword.Index);
3685
                                AddWithValue(cmd, "@NAME", itemKeyword.Name);
3686
                                AddWithValue(cmd, "@KEYWORD", itemKeyword.Keyword);
3687
                                connection.ExecuteNonQuery(cmd, txn);
3688
                            }
3689

    
3690

    
3691
                            //Multiway Setting
3692
                            query = $"DELETE FROM {PSN_MULTIWAY_SETTING}";
3693
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3694

    
3695
                            foreach (MultiwayItem itemMultiway in multiwayItems)
3696
                            {
3697
                                query = $"INSERT INTO {PSN_MULTIWAY_SETTING} ([INDEX], NAME) VALUES (@INDEX, @NAME)";
3698
                                var cmd = connection.GetSqlStringCommand(query);
3699
                                AddWithValue(cmd, "@INDEX", itemMultiway.Index);
3700
                                AddWithValue(cmd, "@NAME", itemMultiway.Name);
3701
                                connection.ExecuteNonQuery(cmd, txn);
3702
                            }
3703

    
3704
                            //FulidCode
3705
                            query = $"DELETE FROM {PSN_FLUIDCODE}";
3706
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3707

    
3708
                            foreach (DataRow row in dtFluidCode.Rows)
3709
                            {
3710
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
3711
                                var cmd = connection.GetSqlStringCommand(query);
3712
                                cmd.Parameters.Clear();
3713

    
3714
                                {
3715
                                    var param = cmd.CreateParameter();
3716
                                    param.ParameterName = "@UID";
3717
                                    param.Value = row["UID"].ToString();
3718
                                    cmd.Parameters.Add(param);
3719
                                }
3720

    
3721
                                {
3722
                                    var param = cmd.CreateParameter();
3723
                                    param.ParameterName = "@Code";
3724
                                    param.Value = row["Code"].ToString();
3725
                                    cmd.Parameters.Add(param);
3726
                                }
3727

    
3728
                                {
3729
                                    var param = cmd.CreateParameter();
3730
                                    param.ParameterName = "@Description";
3731
                                    param.Value = row["Description"].ToString();
3732
                                    cmd.Parameters.Add(param);
3733
                                }
3734

    
3735
                                {
3736
                                    var param = cmd.CreateParameter();
3737
                                    param.ParameterName = "@Condition";
3738
                                    param.Value = row["Condition"].ToString();
3739
                                    cmd.Parameters.Add(param);
3740
                                }
3741

    
3742
                                {
3743
                                    var param = cmd.CreateParameter();
3744
                                    param.ParameterName = "@Remarks";
3745
                                    param.Value = row["Remarks"].ToString();
3746
                                    cmd.Parameters.Add(param);
3747
                                }
3748

    
3749
                                {
3750
                                    var param = cmd.CreateParameter();
3751
                                    param.ParameterName = "@GroundLevel";
3752
                                    param.Value = row["GroundLevel"].ToString();
3753
                                    cmd.Parameters.Add(param);
3754
                                }
3755

    
3756
                                connection.ExecuteNonQuery(cmd, txn);
3757
                            }
3758

    
3759
                            //PMC
3760
                            query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
3761
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3762

    
3763
                            foreach (DataRow row in dtPMC.Rows)
3764
                            {
3765
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
3766
                                var cmd = connection.GetSqlStringCommand(query);
3767
                                cmd.Parameters.Clear();
3768

    
3769
                                {
3770
                                    var param = cmd.CreateParameter();
3771
                                    param.ParameterName = "@UID";
3772
                                    param.Value = row["UID"].ToString();
3773
                                    cmd.Parameters.Add(param);
3774
                                }
3775

    
3776
                                {
3777
                                    var param = cmd.CreateParameter();
3778
                                    param.ParameterName = "@Priority";
3779
                                    param.Value = row["Priority"].ToString();
3780
                                    cmd.Parameters.Add(param);
3781
                                }
3782

    
3783
                                {
3784
                                    var param = cmd.CreateParameter();
3785
                                    param.ParameterName = "@Code";
3786
                                    param.Value = row["Code"].ToString();
3787
                                    cmd.Parameters.Add(param);
3788
                                }
3789

    
3790
                                {
3791
                                    var param = cmd.CreateParameter();
3792
                                    param.ParameterName = "@Description";
3793
                                    param.Value = row["Description"].ToString();
3794
                                    cmd.Parameters.Add(param);
3795
                                }
3796

    
3797
                                {
3798
                                    var param = cmd.CreateParameter();
3799
                                    param.ParameterName = "@Condition";
3800
                                    param.Value = row["Condition"].ToString();
3801
                                    cmd.Parameters.Add(param);
3802
                                }
3803

    
3804
                                {
3805
                                    var param = cmd.CreateParameter();
3806
                                    param.ParameterName = "@Remarks";
3807
                                    param.Value = row["Remarks"].ToString();
3808
                                    cmd.Parameters.Add(param);
3809
                                }
3810

    
3811
                                {
3812
                                    var param = cmd.CreateParameter();
3813
                                    param.ParameterName = "@GroundLevel";
3814
                                    param.Value = row["GroundLevel"].ToString();
3815
                                    cmd.Parameters.Add(param);
3816
                                }
3817

    
3818
                                connection.ExecuteNonQuery(cmd, txn);
3819
                            }
3820

    
3821
                            //Insulation
3822
                            query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
3823
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3824

    
3825
                            foreach (DataRow row in dtInsulation.Rows)
3826
                            {
3827
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
3828
                                var cmd = connection.GetSqlStringCommand(query);
3829
                                cmd.Parameters.Clear();
3830

    
3831
                                {
3832
                                    var param = cmd.CreateParameter();
3833
                                    param.ParameterName = "@UID";
3834
                                    param.Value = row["UID"].ToString();
3835
                                    cmd.Parameters.Add(param);
3836
                                }
3837

    
3838
                                {
3839
                                    var param = cmd.CreateParameter();
3840
                                    param.ParameterName = "@Code";
3841
                                    param.Value = row["Code"].ToString();
3842
                                    cmd.Parameters.Add(param);
3843
                                }
3844

    
3845
                                {
3846
                                    var param = cmd.CreateParameter();
3847
                                    param.ParameterName = "@Description";
3848
                                    param.Value = row["Description"].ToString();
3849
                                    cmd.Parameters.Add(param);
3850
                                }
3851

    
3852
                                {
3853
                                    var param = cmd.CreateParameter();
3854
                                    param.ParameterName = "@Remarks";
3855
                                    param.Value = row["Remarks"].ToString();
3856
                                    cmd.Parameters.Add(param);
3857
                                }
3858

    
3859
                                connection.ExecuteNonQuery(cmd, txn);
3860
                            }
3861

    
3862
                            //Topology Rule
3863
                            query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
3864
                            var cmdtopology = connection.GetSqlStringCommand(query);
3865
                            //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3866
                            connection.ExecuteNonQuery(cmdtopology, txn);
3867

    
3868
                            foreach (DataRow row in dtTopologyRule.Rows)
3869
                            {
3870
                                query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
3871
                                cmdtopology = connection.GetSqlStringCommand(query);
3872
                                AddWithValue(cmdtopology, "@UID", row["UID"].ToString());
3873
                                connection.ExecuteNonQuery(cmdtopology, txn);
3874
                            }
3875

    
3876
                            //valve grouping
3877
                            query = $"DELETE FROM {PSN_VALVEGROUP_SETTING}";
3878
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3879

    
3880
                            foreach (DataRow row in dtvalvegrouping.Rows)
3881
                            {
3882
                                query = $"INSERT INTO {PSN_VALVEGROUP_SETTING} (OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName) " +
3883
                                    $"VALUES (@OID, @GroupType, @TagIdentifier, @AttributeName, @SppidSymbolName)";
3884
                                var cmd = connection.GetSqlStringCommand(query);
3885
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3886
                                AddWithValue(cmd, "@GroupType", row["GroupType"].ToString());
3887
                                AddWithValue(cmd, "@TagIdentifier", row["TagIdentifier"].ToString());
3888
                                AddWithValue(cmd, "@AttributeName", row["AttributeName"].ToString());
3889
                                AddWithValue(cmd, "@SppidSymbolName", row["SppidSymbolName"].ToString());
3890
                                connection.ExecuteNonQuery(cmd, txn);
3891
                            }
3892

    
3893
                            //no pocket Setting
3894
                            query = $"DELETE FROM {PSN_NOPOCKETSETTING}";
3895
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3896

    
3897
                            foreach (DataRow row in dtnopocket.Rows)
3898
                            {
3899
                                query = $"INSERT INTO {PSN_NOPOCKETSETTING} ([INDEX], [TYPE], [NAME]) VALUES (@INDEX, @TYPE, @NAME)";
3900
                                var cmd = connection.GetSqlStringCommand(query);
3901
                                AddWithValue(cmd, "@INDEX", row["INDEX"].ToString());
3902
                                AddWithValue(cmd, "@TYPE", row["TYPE"].ToString());
3903
                                AddWithValue(cmd, "@NAME", row["NAME"].ToString());
3904
                                connection.ExecuteNonQuery(cmd, txn);
3905
                            }
3906

    
3907
                            //air fin cooler Setting
3908
                            query = $"DELETE FROM {PSN_AIRFINCOOLERSETTING}";
3909
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3910
                             
3911
                            foreach (DataRow row in dtafc.Rows)
3912
                            {
3913
                                query = $"INSERT INTO {PSN_AIRFINCOOLERSETTING} ([INDEX], [TYPE], [TagIdentifier], [AttributeName], [NAME]) VALUES (@INDEX, @TYPE, @TagIdentifier, @AttributeName, @NAME)";
3914
                                var cmd = connection.GetSqlStringCommand(query);
3915
                                AddWithValue(cmd, "@INDEX", row["INDEX"].ToString());
3916
                                AddWithValue(cmd, "@TYPE", row["TYPE"].ToString());
3917
                                AddWithValue(cmd, "@TagIdentifier", row["TagIdentifier"].ToString());
3918
                                AddWithValue(cmd, "@AttributeName", row["AttributeName"].ToString());
3919
                                AddWithValue(cmd, "@NAME", row["NAME"].ToString());
3920
                                connection.ExecuteNonQuery(cmd, txn);
3921
                            }
3922

    
3923

    
3924
                            txn.Commit();
3925
                        }
3926
                        catch (Exception ex)
3927
                        {
3928
                            txn.Rollback();
3929
                            result = false;
3930
                        }
3931
                    }
3932
                }
3933
                catch (Exception ex)
3934
                {
3935
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3936
                    result = false;
3937
                }
3938
            }
3939

    
3940
            return result;
3941
        }
3942

    
3943
        public static bool CreatePSN_COMMON()
3944
        {
3945
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3946

    
3947
            bool result = true;
3948
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3949
            {
3950
                try
3951
                {
3952
                    using (var txn = connection.BeginTransaction())
3953
                    {
3954
                        try
3955
                        {
3956

    
3957
                            var query = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
3958
                            if (id2Info.ID2DBType == AnotherID2DB_Type.MSSQL)
3959
                            {
3960
                                connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3961
                            }
3962

    
3963
                            txn.Commit();
3964
                        }
3965
                        catch (Exception ex)
3966
                        {
3967
                            txn.Rollback();
3968
                            result = false;
3969
                        }
3970
                    }
3971
                }
3972
                catch (Exception ex)
3973
                {
3974
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3975
                    result = false;
3976
                }
3977
            }
3978

    
3979
            return result;
3980
        }
3981
    }
3982
}
3983

    
3984

    
클립보드 이미지 추가 (최대 크기: 500 MB)