프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ 4f02de16

이력 | 보기 | 이력해설 | 다운로드 (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("PIDNAME", "NVARCHAR(255)");
321
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
322
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
323
                    dicColCheck.Add("GROUPTAG", "NVARCHAR(255)");
324
                    dicColCheck.Add("PipeSystemNetwork_OID", "NVARCHAR(255)");
325
                    dicColCheck.Add("PipeRun_OID", "NVARCHAR(255)");
326
                    dicColCheck.Add("ViewPipeSystemNetwork_OID", "NVARCHAR(255)");
327

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

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

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

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

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

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

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

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

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

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

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

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

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

    
523
                    matched = names.FirstOrDefault(param => param == PSN_AIRFINCOOLERSETTING);
524

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

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

    
545

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

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

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

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

    
588
            return result;
589
        }
590

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

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

    
617
            return dt;
618
        }
619

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

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

    
645
            return dt;
646
        }
647

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

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

    
669
            return dt;
670
        }
671

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

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

    
693
            return dt;
694
        }
695

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

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

    
717
            return dt;
718
        }
719

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

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

    
741
            return dt;
742
        }
743

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

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

    
765
            return dt;
766
        }
767

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

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

    
789
            return dt;
790
        }
791

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

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

    
813
            return dt;
814
        }
815

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

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

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

    
843
            return dt;
844
        }
845

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

    
851
            using (IAbstractDatabase connection = id2Info.CreateConnection())
852
            {
853
                try
854
                {
855
                    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; ";
856
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
857
                    {
858
                        dt = ds.Tables[0].Copy();
859
                    }
860
                }
861
                catch (Exception ex)
862
                {
863
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
864
                }
865
            }
866

    
867
            return dt;
868
        }
869

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

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

    
892
            return dt;
893
        }
894

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

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

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

    
922
            return dt;
923
        }
924

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

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

    
946
            return dt;
947
        }
948

    
949
        public static double[] GetDrawingSize()
950
        {
951
            double[] result = null;
952

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

    
986
            return result;
987
        }
988

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

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

    
1010
            return dt;
1011
        }
1012

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

    
1024
            bool result = true;
1025

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

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

    
1060
            return result;
1061
        }
1062

    
1063
        public static bool DeleteView()
1064
        {
1065
            ID2Info id2Info = ID2Info.GetInstance();
1066

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

    
1094
            return result;
1095
        }
1096

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

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

    
1119
            return dt;
1120
        }
1121

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

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

    
1143
            return dt;
1144
        }
1145

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

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

    
1167
            return dt;
1168
        }
1169

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

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

    
1191
            return dt;
1192
        }
1193

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

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

    
1215
            return dt;
1216
        }
1217

    
1218

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

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

    
1240
            return dt;
1241
        }
1242

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

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

    
1264
            return dt;
1265
        }
1266

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

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

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

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

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

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

    
1341
            return true;
1342
        }
1343

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

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

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

    
1379
            return true;
1380
        }
1381

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

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

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

    
1414
            return true;
1415
        }
1416

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

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

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

    
1448
            return true;
1449
        }
1450

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

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

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

    
1483
            return true;
1484
        }
1485

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

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

    
1509
                        }
1510

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

    
1521
            return true;
1522
        }
1523

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

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

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

    
1557
            return true;
1558
        }
1559

    
1560
        public static DataTable SelectTopologyRule()
1561
        {
1562
            DataTable dt = null;
1563

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

    
1581
            return dt;
1582
        }
1583

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

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

    
1597
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1598
            {
1599
                try
1600
                {
1601
                    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'";
1602
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1603
                    {
1604
                        dt = ds.Tables[0].Copy();
1605
                    }
1606
                }
1607
                catch (Exception ex)
1608
                {
1609
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1610
                }
1611
            }
1612

    
1613
            return dt;
1614
        }
1615

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

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

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

    
1638
            return dt;
1639
        }
1640

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

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

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

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

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

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

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

    
1709
            return result;
1710
        }
1711

    
1712
        public static bool UpdateRevision(string TimeData, string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
1713
           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)
1714
        {
1715
            ID2Info id2Info = ID2Info.GetInstance();
1716
            bool result = true;
1717

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

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

    
1740
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
1741

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

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

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

    
1780
            return result;
1781
        }
1782

    
1783

    
1784
        public static bool SaveRevision(string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
1785
            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)
1786
        {
1787
            ID2Info id2Info = ID2Info.GetInstance();
1788

    
1789
            bool result = true;
1790

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

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

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

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

    
1817
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
1818

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

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

    
1839
                            connection.ExecuteNonQuery(cmd, txn);
1840

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

    
1857
            return result;
1858
        }
1859

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

    
1864
            bool result = true;
1865

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

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

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

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

    
1936
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1937
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1938
                                else
1939
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1940

    
1941
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1942
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1943
                                else
1944
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1945

    
1946
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1947
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
1948
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1949
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
1950

    
1951
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
1952
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
1953
                                else
1954
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
1955

    
1956
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
1957
                                connection.ExecuteNonQuery(cmd, txn);
1958
                            }
1959

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

    
1970
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1971
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1972
                                else
1973
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1974

    
1975
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1976
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1977
                                else
1978
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1979

    
1980
                                connection.ExecuteNonQuery(cmd, txn);
1981
                            }
1982

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

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

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

    
2026
                                AddWithValue(cmd, "@IsValid", IsValid);
2027
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
2028

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

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

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

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

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

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

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

    
2087

    
2088
                            txn.Commit();
2089
                        }
2090
                        catch (Exception ex)
2091
                        {
2092
                            txn.Rollback();
2093
                            result = false;
2094
                        }
2095

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

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

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

    
2137
            return result;
2138
        }
2139

    
2140
        public static bool SavePSNFluidCode(DataTable dt)
2141
        {
2142
            ID2Info id2Info = ID2Info.GetInstance();
2143

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

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

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

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

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

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

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

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

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

    
2222
            return result;
2223
        }
2224

    
2225
        public static DataTable SelectPSNFluidCode()
2226
        {
2227
            DataTable dt = null;
2228
            ID2Info id2Info = ID2Info.GetInstance();
2229

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

    
2246
            return dt;
2247
        }
2248

    
2249
        public static bool SavePSNPMC(DataTable dt)
2250
        {
2251
            ID2Info id2Info = ID2Info.GetInstance();
2252

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

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

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

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

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

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

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

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

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

    
2320
                                connection.ExecuteNonQuery(cmd, txn);
2321
                            }
2322

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

    
2339
            return result;
2340
        }
2341

    
2342
        public static bool SavePSNInsulation(DataTable dt)
2343
        {
2344
            ID2Info id2Info = ID2Info.GetInstance();
2345

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

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

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

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

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

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

    
2392
                                connection.ExecuteNonQuery(cmd, txn);
2393
                            }
2394

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

    
2411
            return result;
2412
        }
2413

    
2414
        public static PSN GetDBPSN()
2415
        {
2416
            PSN result = new PSN();
2417
            ID2Info id2Info = ID2Info.GetInstance();
2418

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

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

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

    
2447
                            string IsValid = string.Empty;
2448

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

    
2456
                            newRow["IsValid"] = IsValid;
2457

    
2458
                            newRow["Status"] = row["Status"].ToString();
2459
                            newRow["PBS"] = row["PBS"].ToString();
2460
                            newRow["Drawings"] = row["Drawings"].ToString();
2461

    
2462
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2463
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
2464

    
2465
                            newRow["Pocket"] = row["Pocket"].ToString();
2466

    
2467
                            newRow["EGTag"] = row["EGTag"].ToString();
2468
                            newRow["HasMLTags"] = row["HasMLTags"].ToString();
2469
                            newRow["GroundLevel"] = row["GroundLevel"].ToString();
2470
                            result.PipeSystemNetwork.Rows.Add(newRow);
2471
                        }
2472
                    }
2473

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

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

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

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

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

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

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

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

    
2525
            return result;
2526
        }
2527

    
2528
        public static int GetRevision()
2529
        {
2530
            int result = 0;
2531
            ID2Info id2Info = ID2Info.GetInstance();
2532

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

    
2558
            return result;
2559
        }
2560

    
2561
        public static DataTable GetPathItem()
2562
        {
2563
            DataTable dt = null;
2564

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

    
2582
            return dt;
2583
        }
2584

    
2585
        public static DataTable GetTopologySet()
2586
        {
2587
            DataTable dt = null;
2588

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

    
2618
            return dt;
2619
        }
2620

    
2621
        public static DataTable GetPipeSystemNetwork()
2622
        {
2623
            DataTable dt = null;
2624

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

    
2647
                            string IsValid = string.Empty;
2648

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

    
2656
                            newRow["IsValid"] = IsValid;
2657
                            newRow["Status"] = row["Status"].ToString();
2658

    
2659
                            newRow["PBS"] = row["PBS"].ToString();
2660
                            newRow["Drawings"] = row["Drawings"].ToString();
2661

    
2662
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2663
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
2664

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

    
2679
            return dt;
2680
        }
2681

    
2682
        public static DataTable GetSequenceData()
2683
        {
2684
            DataTable dt = null;
2685

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

    
2703
            return dt;
2704
        }
2705

    
2706

    
2707
        //Anohter DB
2708
        public static bool ConnTestAndCreateAnotherTable()
2709
        {
2710
            bool result = false;
2711
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
2712

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

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

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

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

    
2768
                        DataTable topologyRule = new DataTable();
2769
                        topologyRule.Columns.Add("NAME", typeof(string));
2770

    
2771
                        topologyRule.Rows.Add("FluidCode");
2772
                        topologyRule.Rows.Add("-");
2773
                        topologyRule.Rows.Add("PipingMaterialsClass");
2774
                        topologyRule.Rows.Add("-");
2775
                        topologyRule.Rows.Add("Tag Seq No");
2776

    
2777
                        SaveTopologyRule(topologyRule);
2778
                    }
2779
                    //else
2780
                    //{
2781
                    //    AddColumn(PSN_TOPOLOGY_RULE, dicColCheck);
2782
                    //}
2783

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

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

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

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

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

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

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

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

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

    
2979
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
2980
                    dicColCheck.Clear();
2981
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2982
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2983
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
2984
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
2985
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
2986
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
2987
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
2988

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

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

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

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

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

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

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

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

    
3140
                    matched = names.FirstOrDefault(param => param == PSN_AIRFINCOOLERSETTING);
3141
                    dicColCheck.Clear();
3142
                    dicColCheck.Add("INDEX", "INTEGER");
3143
                    dicColCheck.Add("TYPE", "TEXT");
3144
                    dicColCheck.Add("TagIdentifier", "NVARCHAR(50)");
3145
                    dicColCheck.Add("AttributeName", "NVARCHAR(255)");
3146
                    dicColCheck.Add("NAME", "TEXT");
3147

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

    
3162

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

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

    
3191
                                if (!check) //없으면 추가
3192
                                {
3193
                                    string i = string.Empty;
3194
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
3195
                                        i = "DEFAULT 0";
3196

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

    
3207
                    result = true;
3208
                }
3209
                catch (Exception ex)
3210
                {
3211
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3212
                }
3213
            }
3214

    
3215
            return result;
3216
        }
3217

    
3218
        public static bool SaveAnotherRevision(string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
3219
         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)
3220
        {
3221
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3222

    
3223
            bool result = true;
3224

    
3225
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3226
            {
3227
                try
3228
                {
3229
                    using (var txn = connection.BeginTransaction())
3230
                    {
3231
                        try
3232
                        {
3233

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

    
3246
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
3247
                            DateTime oDateTime = DateTime.Now;
3248

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

    
3251
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
3252

    
3253
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
3254
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
3255
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
3256
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
3257
                            AddWithValue(cmd, "@PipeLines", PipeLines);
3258

    
3259
                            AddWithValue(cmd, "@Topologies", Topologies);
3260
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
3261
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
3262
                            AddWithValue(cmd, "@E2E", E2E);
3263
                            AddWithValue(cmd, "@E2B", E2B);
3264
                            AddWithValue(cmd, "@B2E", B2E);
3265
                            AddWithValue(cmd, "@HDE", HDE);
3266
                            AddWithValue(cmd, "@HD2", HD2);
3267
                            AddWithValue(cmd, "@HDB", HDB);
3268
                            AddWithValue(cmd, "@B2B", B2B);
3269

    
3270
                            AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
3271
                            AddWithValue(cmd, "@ID2_PSN", "Y");
3272

    
3273
                            connection.ExecuteNonQuery(cmd, txn);
3274

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

    
3291
            return result;
3292
        }
3293

    
3294
        public static bool ConnAnotherCreateRevisionTable(Dictionary<string, string> dicColCheck, List<string> names)
3295
        {
3296
            bool result = false;
3297
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3298

    
3299
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3300
            {
3301
                try
3302
                {
3303
                    if (names.Count == 0)
3304
                    {
3305

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

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

    
3338
                                if (!check) //없으면 추가
3339
                                {
3340
                                    string i = string.Empty;
3341
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
3342
                                        i = "DEFAULT 0";
3343

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

    
3354
                    result = true;
3355
                }
3356
                catch (Exception ex)
3357
                {
3358
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3359
                }
3360
            }
3361

    
3362
            return result;
3363
        }
3364

    
3365
        public static DataTable SelectAnotherRevision()
3366
        {
3367
            DataTable dt = null;
3368
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3369

    
3370
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3371
            {
3372
                try
3373
                {
3374
                    var query = $"SELECT * FROM ARS_COMMON.dbo.PSNREVISION";
3375

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

    
3387
            return dt;
3388
        }
3389

    
3390
        public static DataTable SelectAnotherRevisionTable()
3391
        {
3392
            DataTable dt = null;
3393
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3394

    
3395
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3396
            {
3397
                try
3398
                {
3399
                    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'";
3400
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
3401
                    {
3402
                        dt = ds.Tables[0].Copy();
3403
                    }
3404
                }
3405
                catch (Exception ex)
3406
                {
3407
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3408
                }
3409
            }
3410

    
3411
            return dt;
3412
        }
3413

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

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

    
3464
                                connection.ExecuteNonQuery(cmd, txn);
3465
                            }
3466

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

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

    
3491
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
3492
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
3493
                                else
3494
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
3495

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

    
3501
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
3502
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
3503
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
3504
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
3505

    
3506
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
3507
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
3508
                                else
3509
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
3510

    
3511
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
3512
                                connection.ExecuteNonQuery(cmd, txn);
3513
                            }
3514

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

    
3525
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
3526
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
3527
                                else
3528
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
3529

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

    
3535
                                connection.ExecuteNonQuery(cmd, txn);
3536
                            }
3537

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

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

    
3573
                                int IsValid = 0;
3574
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
3575
                                    IsValid = 0;
3576
                                else if (row["IsValid"].ToString() == "InValid")
3577
                                    IsValid = 1;
3578
                                else if (row["IsValid"].ToString() == "Error")
3579
                                    IsValid = -1;
3580

    
3581
                                AddWithValue(cmd, "@IsValid", IsValid);
3582
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
3583

    
3584
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
3585
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
3586

    
3587
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
3588
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
3589
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
3590
                                else
3591
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString().Replace("%", ""));
3592

    
3593
                                AddWithValue(cmd, "@Pocket", string.IsNullOrEmpty(row["Pocket"].ToString()) ? "Yes" : row["Pocket"].ToString());
3594
                                AddWithValue(cmd, "@EGTag", string.IsNullOrEmpty(row["EGTag"].ToString()) ? "" : row["EGTag"].ToString());
3595
                                AddWithValue(cmd, "@HasMLTags", string.IsNullOrEmpty(row["HasMLTags"].ToString()) ? "False" : row["HasMLTags"].ToString());
3596
                                AddWithValue(cmd, "@GroundLevel", string.IsNullOrEmpty(row["GroundLevel"].ToString()) ? "" : row["GroundLevel"].ToString());
3597
                                connection.ExecuteNonQuery(cmd, txn);
3598
                            }
3599

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

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

    
3635
                            //Header Setting
3636
                            query = $"DELETE FROM {PSN_HEADER_SETTING}";
3637
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3638

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

    
3653
                            //Vent/Drain Setting
3654
                            query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
3655
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3656

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

    
3671
                            //Keyword Setting
3672
                            query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
3673
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3674

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

    
3685

    
3686
                            //Multiway Setting
3687
                            query = $"DELETE FROM {PSN_MULTIWAY_SETTING}";
3688
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3689

    
3690
                            foreach (MultiwayItem itemMultiway in multiwayItems)
3691
                            {
3692
                                query = $"INSERT INTO {PSN_MULTIWAY_SETTING} ([INDEX], NAME) VALUES (@INDEX, @NAME)";
3693
                                var cmd = connection.GetSqlStringCommand(query);
3694
                                AddWithValue(cmd, "@INDEX", itemMultiway.Index);
3695
                                AddWithValue(cmd, "@NAME", itemMultiway.Name);
3696
                                connection.ExecuteNonQuery(cmd, txn);
3697
                            }
3698

    
3699
                            //FulidCode
3700
                            query = $"DELETE FROM {PSN_FLUIDCODE}";
3701
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3702

    
3703
                            foreach (DataRow row in dtFluidCode.Rows)
3704
                            {
3705
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
3706
                                var cmd = connection.GetSqlStringCommand(query);
3707
                                cmd.Parameters.Clear();
3708

    
3709
                                {
3710
                                    var param = cmd.CreateParameter();
3711
                                    param.ParameterName = "@UID";
3712
                                    param.Value = row["UID"].ToString();
3713
                                    cmd.Parameters.Add(param);
3714
                                }
3715

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

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

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

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

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

    
3751
                                connection.ExecuteNonQuery(cmd, txn);
3752
                            }
3753

    
3754
                            //PMC
3755
                            query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
3756
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3757

    
3758
                            foreach (DataRow row in dtPMC.Rows)
3759
                            {
3760
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
3761
                                var cmd = connection.GetSqlStringCommand(query);
3762
                                cmd.Parameters.Clear();
3763

    
3764
                                {
3765
                                    var param = cmd.CreateParameter();
3766
                                    param.ParameterName = "@UID";
3767
                                    param.Value = row["UID"].ToString();
3768
                                    cmd.Parameters.Add(param);
3769
                                }
3770

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

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

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

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

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

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

    
3813
                                connection.ExecuteNonQuery(cmd, txn);
3814
                            }
3815

    
3816
                            //Insulation
3817
                            query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
3818
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3819

    
3820
                            foreach (DataRow row in dtInsulation.Rows)
3821
                            {
3822
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
3823
                                var cmd = connection.GetSqlStringCommand(query);
3824
                                cmd.Parameters.Clear();
3825

    
3826
                                {
3827
                                    var param = cmd.CreateParameter();
3828
                                    param.ParameterName = "@UID";
3829
                                    param.Value = row["UID"].ToString();
3830
                                    cmd.Parameters.Add(param);
3831
                                }
3832

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

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

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

    
3854
                                connection.ExecuteNonQuery(cmd, txn);
3855
                            }
3856

    
3857
                            //Topology Rule
3858
                            query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
3859
                            var cmdtopology = connection.GetSqlStringCommand(query);
3860
                            //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3861
                            connection.ExecuteNonQuery(cmdtopology, txn);
3862

    
3863
                            foreach (DataRow row in dtTopologyRule.Rows)
3864
                            {
3865
                                query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
3866
                                cmdtopology = connection.GetSqlStringCommand(query);
3867
                                AddWithValue(cmdtopology, "@UID", row["UID"].ToString());
3868
                                connection.ExecuteNonQuery(cmdtopology, txn);
3869
                            }
3870

    
3871
                            //valve grouping
3872
                            query = $"DELETE FROM {PSN_VALVEGROUP_SETTING}";
3873
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3874

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

    
3888
                            //no pocket Setting
3889
                            query = $"DELETE FROM {PSN_NOPOCKETSETTING}";
3890
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3891

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

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

    
3918

    
3919
                            txn.Commit();
3920
                        }
3921
                        catch (Exception ex)
3922
                        {
3923
                            txn.Rollback();
3924
                            result = false;
3925
                        }
3926
                    }
3927
                }
3928
                catch (Exception ex)
3929
                {
3930
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3931
                    result = false;
3932
                }
3933
            }
3934

    
3935
            return result;
3936
        }
3937

    
3938
        public static bool CreatePSN_COMMON()
3939
        {
3940
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3941

    
3942
            bool result = true;
3943
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3944
            {
3945
                try
3946
                {
3947
                    using (var txn = connection.BeginTransaction())
3948
                    {
3949
                        try
3950
                        {
3951

    
3952
                            var query = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
3953
                            if (id2Info.ID2DBType == AnotherID2DB_Type.MSSQL)
3954
                            {
3955
                                connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3956
                            }
3957

    
3958
                            txn.Commit();
3959
                        }
3960
                        catch (Exception ex)
3961
                        {
3962
                            txn.Rollback();
3963
                            result = false;
3964
                        }
3965
                    }
3966
                }
3967
                catch (Exception ex)
3968
                {
3969
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3970
                    result = false;
3971
                }
3972
            }
3973

    
3974
            return result;
3975
        }
3976
    }
3977
}
3978

    
3979

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