프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ f2a63376

이력 | 보기 | 이력해설 | 다운로드 (186 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

    
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
                    
181
                    if (matched == null)
182
                    {
183
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(255), Type NVARCHAR(255), OrderNumber NVARCHAR(255), Pipeline_OID NVARCHAR(255), From_Data NVARCHAR(255), " +
184
                            "To_Data NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
185
                            "IncludingVirtualData NVARCHAR(10), PSNAccuracy REAL, Pocket NVARCHAR(10))";
186
                        using (var cmd = connection.GetSqlStringCommand(query))
187
                        {
188
                            cmd.ExecuteNonQuery();
189
                        }
190
                    }
191
                    else
192
                    {
193
                        AddColumn(PSN_PIPESYSTEMNETWORK, dicColCheck);
194
                    }
195

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

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

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

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

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

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

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

    
325
                    if (matched == null)
326
                    {
327
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
328
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
329
                            "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), " +
330
                            "ViewPipeSystemNetwork_OID NVARCHAR(255))";
331
                        using (var cmd = connection.GetSqlStringCommand(query))
332
                        {
333
                            cmd.ExecuteNonQuery();
334
                        }
335
                    }
336
                    else
337
                    {
338
                        AddColumn(PSN_PATHITEMS, dicColCheck);
339
                    }
340

    
341
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
342
                    dicColCheck.Clear();
343
                    dicColCheck.Add("OID", "TEXT");
344
                    if (matched == null)
345
                    {
346
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
347
                        using (var cmd = connection.GetSqlStringCommand(query))
348
                        {
349
                            cmd.ExecuteNonQuery();
350
                        }
351
                    }
352

    
353
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
354
                    dicColCheck.Clear();
355
                    dicColCheck.Add("OID", "NVARCHAR(255)");
356
                    dicColCheck.Add("Type", "NVARCHAR(255)");
357
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
358
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
359
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
360
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
361
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
362

    
363
                    if (matched == null)
364
                    {
365
                        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))";
366
                        using (var cmd = connection.GetSqlStringCommand(query))
367
                        {
368
                            cmd.ExecuteNonQuery();
369
                        }
370
                    }
371
                    else
372
                    {
373
                        AddColumn(PSN_TOPOLOGYSET, dicColCheck);
374
                    }
375

    
376
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
377
                    dicColCheck.Clear();
378
                    dicColCheck.Add("[INDEX]", "INTEGER");
379
                    dicColCheck.Add("[NAME]", "TEXT");
380
                    dicColCheck.Add("[KEYWORD]", "TEXT");
381
                    if (matched == null)
382
                    {
383
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
384
                        using (var cmd = connection.GetSqlStringCommand(query))
385
                        {
386
                            cmd.ExecuteNonQuery();
387
                        }
388
                    }
389
                    else
390
                    {
391
                        AddColumn(PSN_TRANSFORMKEYWORD_SETTING, dicColCheck);
392
                    }
393

    
394
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
395
                    dicColCheck.Clear();
396
                    dicColCheck.Add("OID", "NVARCHAR(255)");
397
                    dicColCheck.Add("PipeSystem_OID", "NVARCHAR(255)");
398
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
399
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
400
                    dicColCheck.Add("SEQNUMBER", "NVARCHAR(255)");
401
                    dicColCheck.Add("INSULATION", "NVARCHAR(255)");
402
                    dicColCheck.Add("FROM_DATA", "NVARCHAR(255)");
403
                    dicColCheck.Add("TO_DATA", "NVARCHAR(255)");
404
                    dicColCheck.Add("Unit", "NVARCHAR(100)");
405
                    if (matched == null)
406
                    {
407
                        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), " +
408
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
409
                        using (var cmd = connection.GetSqlStringCommand(query))
410
                        {
411
                            cmd.ExecuteNonQuery();
412
                        }
413
                    }
414
                    else
415
                    {
416
                        AddColumn(PSN_PIPELINE, dicColCheck);
417
                    }
418

    
419
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
420
                    dicColCheck.Clear();
421
                    dicColCheck.Add("UID", "NVARCHAR(50)");
422
                    dicColCheck.Add("Code", "NVARCHAR(255)");
423
                    dicColCheck.Add("Description", "NVARCHAR(255)");
424
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
425
                    if (matched == null)
426
                    {
427
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
428
                        using (var cmd = connection.GetSqlStringCommand(query))
429
                        {
430
                            cmd.ExecuteNonQuery();
431
                        }
432
                    }
433
                    else
434
                    {
435
                        AddColumn(PSN_INSULATIONPURPOSE, dicColCheck);
436
                    }
437

    
438
                    matched = names.FirstOrDefault(param => param == PSN_VALVEGROUP_SETTING);
439
                    dicColCheck.Clear();
440
                    dicColCheck.Add("OID", "NVARCHAR(255)");
441
                    dicColCheck.Add("GroupType", "NVARCHAR(255)");
442
                    dicColCheck.Add("TagIdentifier", "NVARCHAR(50)");
443
                    dicColCheck.Add("AttributeName", "NVARCHAR(255)");
444
                    dicColCheck.Add("SppidSymbolName", "NVARCHAR(255)");
445
                    if (matched == null)
446
                    {
447
                        var query = $"CREATE TABLE {PSN_VALVEGROUP_SETTING} (OID NVARCHAR(255), GroupType NVARCHAR(255), TagIdentifier NVARCHAR(50), AttributeName NVARCHAR(255), SppidSymbolName NVARCHAR(255))";
448
                        using (var cmd = connection.GetSqlStringCommand(query))
449
                        {
450
                            cmd.ExecuteNonQuery();
451
                        }
452
                    }
453
                    else
454
                    {
455
                        AddColumn(PSN_VALVEGROUP_SETTING, dicColCheck);
456
                    }
457

    
458
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEM);
459
                    dicColCheck.Clear();
460
                    dicColCheck.Add("OID", "NVARCHAR(255)");
461
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
462
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
463
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
464
                    dicColCheck.Add("PipeLineQty", "NVARCHAR(255)");
465
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
466
                    if (matched == null)
467
                    {
468
                        var query = $"CREATE TABLE {PSN_PIPESYSTEM} (OID NVARCHAR(255), DESCRIPTION NVARCHAR(255), FLUID NVARCHAR(255), PMC NVARCHAR(255), PipeLineQty NVARCHAR(255), GroundLevel NVARCHAR(20))";
469
                        using (var cmd = connection.GetSqlStringCommand(query))
470
                        {
471
                            cmd.ExecuteNonQuery();
472
                        }
473
                    }
474
                    else
475
                    {
476
                        AddColumn(PSN_PIPESYSTEM, dicColCheck);
477
                    }
478

    
479
                    matched = names.FirstOrDefault(param => param == PSN_NOPOCKETSETTING);
480
                    dicColCheck.Clear();
481
                    dicColCheck.Add("[INDEX]", "INTEGER");
482
                    dicColCheck.Add("[TYPE]", "TEXT");
483
                    dicColCheck.Add("[NAME]", "TEXT");
484
                    if (matched == null)
485
                    {
486
                        var query = $"CREATE TABLE {PSN_NOPOCKETSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [NAME] TEXT)";
487
                        using (var cmd = connection.GetSqlStringCommand(query))
488
                        {
489
                            cmd.ExecuteNonQuery();
490
                        }
491
                    }
492
                    else
493
                    {
494
                        AddColumn(PSN_NOPOCKETSETTING, dicColCheck);
495
                    }
496

    
497
                    matched = names.FirstOrDefault(param => param == PSN_AIRFINCOOLERSETTING);
498
                    dicColCheck.Clear();
499
                    dicColCheck.Add("[INDEX]", "INTEGER");
500
                    dicColCheck.Add("[TYPE]", "TEXT");
501
                    dicColCheck.Add("[NAME]", "TEXT");
502
                    if (matched == null)
503
                    {
504
                        var query = $"CREATE TABLE {PSN_AIRFINCOOLERSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [NAME] TEXT)";
505
                        using (var cmd = connection.GetSqlStringCommand(query))
506
                        {
507
                            cmd.ExecuteNonQuery();
508
                        }
509
                    }
510
                    else
511
                    {
512
                        AddColumn(PSN_AIRFINCOOLERSETTING, dicColCheck);
513
                    }
514

    
515

    
516
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
517
                    {
518
                        var colnames = connection.GetColumnNames(TableName);
519
                        bool check = false;
520
                        if (colnames != null)
521
                        {
522
                            foreach (KeyValuePair<string, string> col in dicCol)
523
                            {
524
                                check = false;
525
                                foreach (string c in colnames)
526
                                {
527
                                    if (col.Key.Contains(c))
528
                                    {
529
                                        check = true;
530
                                        break;
531
                                    }
532
                                }
533

    
534
                                if (!check) //없으면 추가
535
                                {
536
                                    string i = string.Empty;
537
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
538
                                        i = "DEFAULT 0";
539

    
540
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
541
                                    using (var cmd = connection.GetSqlStringCommand(query))
542
                                    {
543
                                        cmd.ExecuteNonQuery();
544
                                    }
545
                                }
546
                            }
547
                        }
548
                    }
549

    
550
                    result = true;
551
                }
552
                catch (Exception ex)
553
                {
554
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
555
                }
556
            }
557

    
558
            return result;
559
        }
560

    
561
        // ID2 DB 데이터
562
        /// <summary>
563
        /// ID2 데이타베이스에서 OPC 데이터를 조회
564
        /// </summary>
565
        /// <returns></returns>
566
        public static DataTable SelectOPCRelations()
567
        {
568
            DataTable dt = null;
569
            ID2Info id2Info = ID2Info.GetInstance();
570

    
571
            using (IAbstractDatabase connection = id2Info.CreateConnection())
572
            {
573
                try
574
                {
575
                    var query = "SELECT * FROM OPCRelations;";
576
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
577
                    {
578
                        dt = ds.Tables[0].Copy();
579
                    }
580
                }
581
                catch (Exception ex)
582
                {
583
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
584
                }
585
            }
586

    
587
            return dt;
588
        }
589

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

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

    
615
            return dt;
616
        }
617

    
618
        public static DataTable AllDrawings()
619
        {
620
            DataTable dt = null;
621
            ID2Info id2Info = ID2Info.GetInstance();
622

    
623
            using (IAbstractDatabase connection = id2Info.CreateConnection())
624
            {
625
                try
626
                {
627
                    var query = "SELECT * FROM [Drawings]";
628
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
629
                    {
630
                        dt = ds.Tables[0].Copy();
631
                    }
632
                }
633
                catch (Exception ex)
634
                {
635
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
636
                }
637
            }
638

    
639
            return dt;
640
        }
641

    
642
        public static DataTable SelectLineProperties()
643
        {
644
            DataTable dt = null;
645
            ID2Info id2Info = ID2Info.GetInstance();
646

    
647
            using (IAbstractDatabase connection = id2Info.CreateConnection())
648
            {
649
                try
650
                {
651
                    var query = "SELECT * FROM LineProperties";
652
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
653
                    {
654
                        dt = ds.Tables[0].Copy();
655
                    }
656
                }
657
                catch (Exception ex)
658
                {
659
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
660
                }
661
            }
662

    
663
            return dt;
664
        }
665

    
666
        public static DataTable SelectFluidCode()
667
        {
668
            DataTable dt = null;
669
            ID2Info id2Info = ID2Info.GetInstance();
670

    
671
            using (IAbstractDatabase connection = id2Info.CreateConnection())
672
            {
673
                try
674
                {
675
                    var query = "SELECT * FROM FluidCode";
676
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
677
                    {
678
                        dt = ds.Tables[0].Copy();
679
                    }
680
                }
681
                catch (Exception ex)
682
                {
683
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
684
                }
685
            }
686

    
687
            return dt;
688
        }
689

    
690
        public static DataTable SelectPipingMaterialsClass()
691
        {
692
            DataTable dt = null;
693
            ID2Info id2Info = ID2Info.GetInstance();
694

    
695
            using (IAbstractDatabase connection = id2Info.CreateConnection())
696
            {
697
                try
698
                {
699
                    var query = "SELECT * FROM PipingMaterialsClass";
700
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
701
                    {
702
                        dt = ds.Tables[0].Copy();
703
                    }
704
                }
705
                catch (Exception ex)
706
                {
707
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
708
                }
709
            }
710

    
711
            return dt;
712
        }
713

    
714
        public static DataTable SelectPSNPIPINGMATLCLASS()
715
        {
716
            DataTable dt = null;
717
            ID2Info id2Info = ID2Info.GetInstance();
718

    
719
            using (IAbstractDatabase connection = id2Info.CreateConnection())
720
            {
721
                try
722
                {
723
                    var query = $"SELECT * FROM {PSN_PIPINGMATLCLASS}";
724
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
725
                    {
726
                        dt = ds.Tables[0].Copy();
727
                    }
728
                }
729
                catch (Exception ex)
730
                {
731
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
732
                }
733
            }
734

    
735
            return dt;
736
        }
737
        
738
        public static DataTable SelectInsulationPurpose()
739
        {
740
            DataTable dt = null;
741
            ID2Info id2Info = ID2Info.GetInstance();
742

    
743
            using (IAbstractDatabase connection = id2Info.CreateConnection())
744
            {
745
                try
746
                {
747
                    var query = "SELECT * FROM InsulationPurpose";
748
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
749
                    {
750
                        dt = ds.Tables[0].Copy();
751
                    }
752
                }
753
                catch (Exception ex)
754
                {
755
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
756
                }
757
            }
758

    
759
            return dt;
760
        }
761

    
762
        public static DataTable SelectPSNINSULATIONPURPOSE()
763
        {
764
            DataTable dt = null;
765
            ID2Info id2Info = ID2Info.GetInstance();
766

    
767
            using (IAbstractDatabase connection = id2Info.CreateConnection())
768
            {
769
                try
770
                {
771
                    var query = $"SELECT * FROM {PSN_INSULATIONPURPOSE}";
772
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
773
                    {
774
                        dt = ds.Tables[0].Copy();
775
                    }
776
                }
777
                catch (Exception ex)
778
                {
779
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
780
                }
781
            }
782

    
783
            return dt;
784
        }
785

    
786
        public static DataTable SelectNominalDiameter()
787
        {
788
            DataTable dt = null;
789
            ID2Info id2Info = ID2Info.GetInstance();
790

    
791
            using (IAbstractDatabase connection = id2Info.CreateConnection())
792
            {
793
                try
794
                {
795
                    var query = "SELECT * FROM NominalDiameter ORDER BY Metric DESC";
796
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
797
                    {
798
                        dt = ds.Tables[0].Copy();
799
                    }
800
                }
801
                catch (Exception ex)
802
                {
803
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
804
                }
805
            }
806

    
807
            ///TODO: need to check below code
808
            dt.Rows.RemoveAt(0);
809
            dt.Rows.RemoveAt(0);
810
            dt.Rows.RemoveAt(0);
811
            dt.Rows.RemoveAt(0);
812

    
813
            return dt;
814
        }
815

    
816
        public static DataTable SelectAllSymbolAttribute()
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 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; ";
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
            return dt;
838
        }
839

    
840
        public static DataTable SelectSymbolAttribute()
841
        {
842
            DataTable dt = null;
843
            ID2Info id2Info = ID2Info.GetInstance();
844

    
845
            using (IAbstractDatabase connection = id2Info.CreateConnection())
846
            {
847
                try
848
                {
849
                   // var query = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
850
                    var query = "SELECT DISTINCT Attribute FROM SymbolAttribute;";
851
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
852
                    {
853
                        dt = ds.Tables[0].Copy();
854
                    }
855
                }
856
                catch (Exception ex)
857
                {
858
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
859
                }
860
            }
861

    
862
            return dt;
863
        }
864

    
865
        public static DataTable SelectSymbolName()
866
        {
867
            DataTable dt = null;
868
            ID2Info id2Info = ID2Info.GetInstance();
869

    
870
            using (IAbstractDatabase connection = id2Info.CreateConnection())
871
            {
872
                try
873
                {
874
                    var query = "SELECT * FROM SymbolName;";
875
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
876
                    {
877
                        dt = ds.Tables[0].Copy();
878
                    }
879
                }
880
                catch (Exception ex)
881
                {
882
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
883
                }
884
            }
885

    
886
            return dt;
887
        }
888

    
889
        public static double[] GetDrawingSize()
890
        {
891
            double[] result = null;
892

    
893
            ID2Info id2Info = ID2Info.GetInstance();
894
            using (IAbstractDatabase connection = id2Info.CreateConnection())
895
            {
896
                try
897
                {
898
                    var query = "SELECT value FROM Configuration WHERE Section = 'Area' AND [Key] = 'Drawing';";
899
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
900
                    {
901
                        if (ds.Tables[0].Rows.Count == 1)
902
                        {
903
                            string value = ds.Tables[0].Rows[0][0].ToString();
904
                            string[] split = value.Split(new char[] { ',' });
905
                            result = new double[] {
906
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", string.Empty)),
907
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", string.Empty)),
908
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", string.Empty)),
909
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", string.Empty))
910
                                };
911
                            result = new double[] {
912
                                Math.Min(result[0], result[2]),
913
                                Math.Min(result[1], result[3]),
914
                                Math.Max(result[0], result[2]),
915
                                Math.Max(result[1], result[3])
916
                                };
917
                        }
918
                    }
919
                }
920
                catch (Exception ex)
921
                {
922
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
923
                }
924
            }
925

    
926
            return result;
927
        }
928

    
929
        public static DataTable GetEquipmentType()
930
        {
931
            DataTable dt = null;
932
            ID2Info id2Info = ID2Info.GetInstance();
933

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

    
950
            return dt;
951
        }
952

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

    
964
            bool result = true;
965

    
966
            using (IAbstractDatabase connection = id2Info.CreateConnection())
967
            {
968
                try
969
                {
970
                    using (var txn = connection.BeginTransaction())
971
                    {
972
                        try
973
                        {
974
                            var query = $"DELETE FROM {PSN_VIEW}";
975
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
976

    
977
                            foreach (string value in values)
978
                            {
979
                                query = $"INSERT INTO {PSN_VIEW} (OID) VALUES (@OID)";
980
                                var cmd = connection.GetSqlStringCommand(query);
981
                                AddWithValue(cmd, "@OID", value);
982
                                connection.ExecuteNonQuery(cmd, txn);
983
                            }
984
                            txn.Commit();
985
                        }
986
                        catch (Exception ex)
987
                        {
988
                            txn.Rollback();
989
                            result = false;
990
                        }
991
                    }
992
                }
993
                catch (Exception ex)
994
                {
995
                    System.Windows.Forms.MessageBox.Show(ex.Message);
996
                    result = false;
997
                }
998
            }
999

    
1000
            return result;
1001
        }
1002

    
1003
        public static bool DeleteView()
1004
        {
1005
            ID2Info id2Info = ID2Info.GetInstance();
1006

    
1007
            bool result = true;
1008
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1009
            {
1010
                try
1011
                {
1012
                    using (var txn = connection.BeginTransaction())
1013
                    {
1014
                        try
1015
                        {
1016
                            var query = $"DELETE FROM {PSN_VIEW}";
1017
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1018
                            txn.Commit();
1019
                        }
1020
                        catch (Exception ex)
1021
                        {
1022
                            txn.Rollback();
1023
                            result = false;
1024
                        }
1025
                    }
1026
                }
1027
                catch (Exception ex)
1028
                {
1029
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1030
                    result = false;
1031
                }
1032
            }
1033

    
1034
            return result;
1035
        }
1036

    
1037
        //PSN Sqlite 
1038
        public static DataTable SelectHeaderSetting()
1039
        {
1040
            DataTable dt = null;
1041
            ID2Info id2Info = ID2Info.GetInstance();
1042

    
1043
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1044
            {
1045
                try
1046
                {
1047
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_HEADER_SETTING};";
1048
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1049
                    {
1050
                        dt = ds.Tables[0].Copy();
1051
                    }
1052
                }
1053
                catch (Exception ex)
1054
                {
1055
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1056
                }
1057
            }
1058

    
1059
            return dt;
1060
        }
1061

    
1062
        public static DataTable SelectVentDrainSetting()
1063
        {
1064
            DataTable dt = null;
1065
            ID2Info id2Info = ID2Info.GetInstance();
1066

    
1067
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1068
            {
1069
                try
1070
                {
1071
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_VENTDRAIN_SETTING};";
1072
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1073
                    {
1074
                        dt = ds.Tables[0].Copy();
1075
                    }
1076
                }
1077
                catch (Exception ex)
1078
                {
1079
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1080
                }
1081
            }
1082

    
1083
            return dt;
1084
        }
1085

    
1086
        public static DataTable SelectKeywordsSetting()
1087
        {
1088
            DataTable dt = null;
1089
            ID2Info id2Info = ID2Info.GetInstance();
1090

    
1091
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1092
            {
1093
                try
1094
                {
1095
                    var query = $@"SELECT [KEYWORD], [INDEX], [NAME] FROM {PSN_TRANSFORMKEYWORD_SETTING};";
1096
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1097
                    {
1098
                        dt = ds.Tables[0].Copy();
1099
                    }
1100
                }
1101
                catch (Exception ex)
1102
                {
1103
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1104
                }
1105
            }
1106

    
1107
            return dt;
1108
        }
1109

    
1110
        public static DataTable SelectValveGroupItemsSetting()
1111
        {
1112
            DataTable dt = null;
1113
            ID2Info id2Info = ID2Info.GetInstance();
1114

    
1115
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1116
            {
1117
                try
1118
                {
1119
                    var query = $@"SELECT OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName FROM {PSN_VALVEGROUP_SETTING};";
1120
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1121
                    {
1122
                        dt = ds.Tables[0].Copy();
1123
                    }
1124
                }
1125
                catch (Exception ex)
1126
                {
1127
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1128
                }
1129
            }
1130

    
1131
            return dt;
1132
        }
1133

    
1134
  
1135
        public static DataTable SelectEquipmentNoPocketSetting()
1136
        {
1137
            DataTable dt = null;
1138
            ID2Info id2Info = ID2Info.GetInstance();
1139

    
1140
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1141
            {
1142
                try
1143
                {
1144
                    var query = $@"SELECT [INDEX], [TYPE], [NAME] FROM {PSN_NOPOCKETSETTING};";
1145
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1146
                    {
1147
                        dt = ds.Tables[0].Copy();
1148
                    }
1149
                }
1150
                catch (Exception ex)
1151
                {
1152
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1153
                }
1154
            }
1155

    
1156
            return dt;
1157
        }
1158

    
1159
        public static DataTable SelectAirFinCoolerSetting()
1160
        {
1161
            DataTable dt = null;
1162
            ID2Info id2Info = ID2Info.GetInstance();
1163

    
1164
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1165
            {
1166
                try
1167
                {
1168
                    var query = $@"SELECT [INDEX], [TYPE], [NAME] FROM {PSN_AIRFINCOOLERSETTING};";
1169
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1170
                    {
1171
                        dt = ds.Tables[0].Copy();
1172
                    }
1173
                }
1174
                catch (Exception ex)
1175
                {
1176
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1177
                }
1178
            }
1179

    
1180
            return dt;            
1181
        }
1182

    
1183
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
1184
        {
1185
            ID2Info id2Info = ID2Info.GetInstance();
1186
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1187
            {
1188
                try
1189
                {
1190
                    using (var txn = connection.BeginTransaction())
1191
                    {
1192
                        var query = $"DELETE FROM {PSN_HEADER_SETTING}";
1193
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1194

    
1195
                        foreach (HeaderInfo headerInfo in headerInfos)
1196
                        {
1197
                            foreach (HeaderItem item in headerInfo.HeaderItems)
1198
                            {
1199
                                query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
1200
                                var cmd = connection.GetSqlStringCommand(query);
1201
                                AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
1202
                                AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
1203
                                AddWithValue(cmd, "@INDEX", item.Index);
1204
                                AddWithValue(cmd, "@NAME", item.Name);
1205
                                connection.ExecuteNonQuery(cmd, txn);
1206
                            }
1207
                        }
1208
                        txn.Commit();
1209
                    }
1210
                    
1211
                }
1212
                catch (Exception ex)
1213
                {
1214
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1215
                    return false;
1216
                }
1217
            }
1218
            return true;
1219
        }
1220

    
1221
        public static bool SaveVentDrainSetting(List<VentDrainInfo> ventDrainInfos)
1222
        {
1223
            ID2Info id2Info = ID2Info.GetInstance();
1224
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1225
            {
1226
                using (var txn = connection.BeginTransaction())
1227
                {
1228
                    try
1229
                    {
1230
                        var query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
1231
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1232

    
1233
                        foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
1234
                        {
1235
                            foreach (VentDrainItem item in ventDrainInfo.VentDrainItems)
1236
                            {
1237
                                query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
1238
                                var cmd = connection.GetSqlStringCommand(query);
1239
                                AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
1240
                                AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
1241
                                AddWithValue(cmd, "@INDEX", item.Index);
1242
                                AddWithValue(cmd, "@NAME", item.Name);
1243
                                connection.ExecuteNonQuery(cmd, txn);
1244
                            }
1245
                        }
1246

    
1247
                        txn.Commit();
1248
                    }
1249
                    catch (Exception ex)
1250
                    {
1251
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1252
                        return false;
1253
                    }
1254
                }
1255
            }
1256

    
1257
            return true;
1258
        }
1259

    
1260
        public static bool SaveValveGroupItemsSetting(List<ValveGroupItem> valveGroupItems)
1261
        {
1262
            ID2Info id2Info = ID2Info.GetInstance();
1263
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1264
            {
1265
                using (var txn = connection.BeginTransaction())
1266
                {
1267
                    try
1268
                    {
1269
                        var query = $"DELETE FROM {PSN_VALVEGROUP_SETTING}";
1270
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1271

    
1272
                        foreach (ValveGroupItem item in valveGroupItems)
1273
                        {
1274
                            query = $"INSERT INTO {PSN_VALVEGROUP_SETTING} (OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName) " +
1275
                                $"VALUES (@OID, @GroupType, @TagIdentifier, @AttributeName, @SppidSymbolName)";
1276
                            var cmd = connection.GetSqlStringCommand(query);
1277
                            AddWithValue(cmd, "@OID", item.OID);
1278
                            AddWithValue(cmd, "@GroupType", item.GroupType);
1279
                            AddWithValue(cmd, "@TagIdentifier", item.TagIdentifier);
1280
                            AddWithValue(cmd, "@AttributeName", item.AttributeName);
1281
                            AddWithValue(cmd, "@SppidSymbolName", item.SppidSymbolName);
1282
                            connection.ExecuteNonQuery(cmd, txn);
1283
                        }
1284

    
1285
                        txn.Commit();
1286
                    }
1287
                    catch (Exception ex)
1288
                    {
1289
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1290
                        return false;
1291
                    }
1292
                }
1293
            }
1294

    
1295
            return true;
1296
        }
1297

    
1298
        public static bool SaveKeywordsSetting(List<KeywordItem> keywordItems)
1299
        {
1300
            ID2Info id2Info = ID2Info.GetInstance();
1301
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1302
            {
1303
                using (var txn = connection.BeginTransaction())
1304
                {
1305
                    try
1306
                    {
1307
                        var query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
1308
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1309
                        
1310
                        foreach (KeywordItem item in keywordItems)
1311
                        {
1312
                            query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
1313
                            var cmd = connection.GetSqlStringCommand(query);
1314
                            AddWithValue(cmd, "@INDEX", item.Index);
1315
                            AddWithValue(cmd, "@NAME", item.Name);
1316
                            AddWithValue(cmd, "@KEYWORD", item.Keyword);                            
1317
                            connection.ExecuteNonQuery(cmd, txn);
1318
                        }
1319
                        
1320
                        txn.Commit();
1321
                    }
1322
                    catch (Exception ex)
1323
                    {
1324
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1325
                        return false;
1326
                    }
1327
                }
1328
            }
1329

    
1330
            return true;
1331
        }
1332

    
1333
        public static bool SaveEquipmentNopocketSetting(List<EquipmentNoPocketItem> keywordItems)
1334
        {
1335
            ID2Info id2Info = ID2Info.GetInstance();
1336
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1337
            {
1338
                using (var txn = connection.BeginTransaction())
1339
                {
1340
                    try
1341
                    {
1342
                        var query = $"DELETE FROM {PSN_NOPOCKETSETTING}";
1343
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1344

    
1345
                        foreach (EquipmentNoPocketItem item in keywordItems)
1346
                        {
1347
                            query = $"INSERT INTO {PSN_NOPOCKETSETTING} ([INDEX], [TYPE], [NAME]) VALUES (@INDEX, @TYPE, @NAME)";
1348
                            var cmd = connection.GetSqlStringCommand(query);
1349
                            AddWithValue(cmd, "@INDEX", item.Index);
1350
                            AddWithValue(cmd, "@TYPE", item.Type);
1351
                            AddWithValue(cmd, "@NAME", item.Name);
1352
                            connection.ExecuteNonQuery(cmd, txn);
1353
                        }
1354

    
1355
                        txn.Commit();
1356
                    }
1357
                    catch (Exception ex)
1358
                    {
1359
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1360
                        return false;
1361
                    }
1362
                }
1363
            }
1364

    
1365
            return true;
1366
        }
1367

    
1368
        public static bool SaveAirFinCoolerSetting(List<EquipmentAirFinCoolerItem> keywordItems)
1369
        {
1370
            ID2Info id2Info = ID2Info.GetInstance();
1371
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1372
            {
1373
                using (var txn = connection.BeginTransaction())
1374
                {
1375
                    try
1376
                    {
1377
                        var query = $"DELETE FROM {PSN_AIRFINCOOLERSETTING}";
1378
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1379

    
1380
                        foreach (EquipmentAirFinCoolerItem item in keywordItems)
1381
                        {
1382
                            query = $"INSERT INTO {PSN_AIRFINCOOLERSETTING} ([INDEX], [TYPE], [NAME]) VALUES (@INDEX, @TYPE, @NAME)";
1383
                            var cmd = connection.GetSqlStringCommand(query);
1384
                            AddWithValue(cmd, "@INDEX", item.Index);
1385
                            AddWithValue(cmd, "@TYPE", item.Type);
1386
                            AddWithValue(cmd, "@NAME", item.Name);
1387
                            connection.ExecuteNonQuery(cmd, txn);
1388
                        }
1389

    
1390
                        txn.Commit();
1391
                    }
1392
                    catch (Exception ex)
1393
                    {
1394
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1395
                        return false;
1396
                    }
1397
                }
1398
            }
1399

    
1400
            return true;
1401
        }
1402

    
1403
        public static bool SaveTopologyRule(DataTable dt)
1404
        {
1405
            ID2Info id2Info = ID2Info.GetInstance();
1406
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1407
            {
1408
                using (var txn = connection.BeginTransaction())
1409
                {
1410
                    try
1411
                    {
1412
                        var query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
1413
                        var cmd = connection.GetSqlStringCommand(query);
1414
                        //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1415
                        connection.ExecuteNonQuery(cmd, txn);
1416

    
1417
                        foreach (DataRow row in dt.Rows)
1418
                        {
1419
                            query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
1420
                            cmd = connection.GetSqlStringCommand(query);
1421
                            AddWithValue(cmd, "@UID", row["NAME"].ToString());
1422
                            connection.ExecuteNonQuery(cmd, txn);
1423
                        }
1424

    
1425
                        txn.Commit();
1426
                    }
1427
                    catch (Exception ex)
1428
                    {
1429
                        txn.Rollback();
1430
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1431
                        return false;
1432
                    }
1433
                }
1434
            }
1435

    
1436
            return true;
1437
        }
1438

    
1439
        public static DataTable SelectTopologyRule()
1440
        {
1441
            DataTable dt = null;
1442

    
1443
            ID2Info id2Info = ID2Info.GetInstance();
1444
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1445
            {
1446
                try
1447
                {
1448
                    var query = $"SELECT * FROM {PSN_TOPOLOGY_RULE}";
1449
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1450
                    {
1451
                        dt = ds.Tables[0].Copy();
1452
                    }
1453
                }
1454
                catch (Exception ex)
1455
                {
1456
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1457
                }
1458
            }
1459

    
1460
            return dt;
1461
        }
1462

    
1463
        private static void AddWithValue(DbCommand cmd, string PropName, object Value)
1464
        {
1465
            var param = cmd.CreateParameter();
1466
            param.ParameterName = PropName;
1467
            param.Value = Value;
1468
            cmd.Parameters.Add(param);
1469
        }
1470

    
1471
        public static DataTable SelectRevisionTable()
1472
        {
1473
            DataTable dt = null;
1474
            ID2Info id2Info = ID2Info.GetInstance();
1475

    
1476
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1477
            {
1478
                try
1479
                {
1480
                    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'";
1481
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1482
                    {
1483
                        dt = ds.Tables[0].Copy();
1484
                    }
1485
                }
1486
                catch (Exception ex)
1487
                {
1488
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1489
                }
1490
            }
1491

    
1492
            return dt;
1493
        }
1494

    
1495
        public static DataTable SelectRevision()
1496
        {
1497
            DataTable dt = null;
1498
            ID2Info id2Info = ID2Info.GetInstance();
1499

    
1500
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1501
            {
1502
                try
1503
                {
1504
                    var query = $"SELECT * FROM ARS_COMMON.dbo.PSNREVISION";
1505

    
1506
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1507
                    {
1508
                        dt = ds.Tables[0].Copy();
1509
                    }
1510
                }
1511
                catch (Exception ex)
1512
                {
1513
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1514
                }
1515
            }
1516

    
1517
            return dt;
1518
        }
1519

    
1520
        public static bool ConnCreateRevisionTable(Dictionary<string, string> dicColCheck, List<string> names)
1521
        {
1522
            bool result = false;
1523
            ID2Info id2Info = ID2Info.GetInstance();
1524

    
1525
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1526
            {
1527
                try
1528
                {
1529
                    if (names.Count == 0)
1530
                    {
1531
                        
1532
                        var query = $"CREATE TABLE ARS_COMMON.dbo.PSNRevision ([OID] [uniqueidentifier] ,[Project_OID] [uniqueidentifier] ," +
1533
                            $"[ProjectCode] nvarchar(255), [RevNumber] int ,[UserName] nvarchar(255) ,[TimeData] nvarchar(255) ," +
1534
                            $"[PSNDatabasePath] nvarchar(255) ,	[PsnByPBSFilter] varchar(20) ,[PsnByNPDFilter] varchar(20) ,[PidDrawings] int ,	" +
1535
                            $"[PipeSystems] int ,[PipeLines] int ,[Topologies] int ,[PipeSystemNetworks] int ,[InValidPSNs] int ,[E2E] int ," +
1536
                            $"[E2B] int ,[B2E] int ,[HDE] int ,[HD2] int ,[HDB] int ,[B2B] int, [LastModificationdate] nvarchar(255))";
1537
                        using (var cmd = connection.GetSqlStringCommand(query))
1538
                        {
1539
                            cmd.ExecuteNonQuery();
1540
                        }
1541
                    }
1542
                    else
1543
                    {
1544
                        AddColumn(PSN_REVISION, dicColCheck, names);
1545
                    }
1546

    
1547
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
1548
                    {
1549
                        bool check = false;
1550
                        if (colnames != null)
1551
                        {
1552
                            foreach (KeyValuePair<string, string> col in dicCol)
1553
                            {
1554
                                check = false;
1555
                                foreach (string c in colnames)
1556
                                {
1557
                                    if (col.Key.Contains(c))
1558
                                    {
1559
                                        check = true;
1560
                                        break;
1561
                                    }
1562
                                }
1563

    
1564
                                if (!check) //없으면 추가
1565
                                {
1566
                                    string i = string.Empty;
1567
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
1568
                                        i = "DEFAULT 0";
1569

    
1570
                                    var queryalter = $"ALTER TABLE ARS_COMMON.dbo.PSNRevision ADD [" + col.Key + "] " + col.Value + " " + i + ";";
1571
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
1572
                                    {
1573
                                        cmd.ExecuteNonQuery();
1574
                                    }
1575
                                }
1576
                            }
1577
                        }
1578
                    }
1579

    
1580
                    result = true;
1581
                }
1582
                catch (Exception ex)
1583
                {
1584
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1585
                }
1586
            }
1587

    
1588
            return result;
1589
        }
1590

    
1591
        public static bool UpdateRevision(string TimeData, string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
1592
           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)
1593
        {
1594
            ID2Info id2Info = ID2Info.GetInstance();
1595
            bool result = true;
1596

    
1597
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1598
            {
1599
                try
1600
                {
1601
                    using (var txn = connection.BeginTransaction())
1602
                    {
1603
                        try
1604
                        {
1605
                            string where = string.Format("DELETE FROM ARS_COMMON.dbo.PSNRevision WHERE ProjectCode = '{0}' AND PSNDatabasePath = '{1}' AND RevNumber = {2}", ProjectCode, PSNDatabasePath, RevNumber);
1606
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(where), txn);
1607
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
1608
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
1609
                                $"HD2, HDB, B2B, LastModificationdate) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
1610
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
1611
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B, @LastModificationdate)";
1612
                            var cmd = connection.GetSqlStringCommand(query);
1613
                      
1614
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
1615
                            AddWithValue(cmd, "@RevNumber", RevNumber);
1616
                            AddWithValue(cmd, "@UserName", UserName);
1617
                            AddWithValue(cmd, "@TimeData", TimeData); //String.Format("{0:G}", dt) // DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
1618

    
1619
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
1620

    
1621
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
1622
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
1623
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
1624
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
1625
                            AddWithValue(cmd, "@PipeLines", PipeLines);
1626

    
1627
                            AddWithValue(cmd, "@Topologies", Topologies);
1628
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
1629
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
1630
                            AddWithValue(cmd, "@E2E", E2E);
1631
                            AddWithValue(cmd, "@E2B", E2B);
1632
                            AddWithValue(cmd, "@B2E", B2E);
1633
                            AddWithValue(cmd, "@HDE", HDE);
1634
                            AddWithValue(cmd, "@HD2", HD2);
1635
                            AddWithValue(cmd, "@HDB", HDB);
1636
                            AddWithValue(cmd, "@B2B", B2B);
1637
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
1638
                            DateTime oDateTime = DateTime.Now;
1639
                            AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
1640
                            connection.ExecuteNonQuery(cmd, txn);
1641

    
1642
                            txn.Commit();
1643
                        }
1644
                        catch (Exception ex)
1645
                        {
1646
                            txn.Rollback();
1647
                            result = false;
1648
                        }
1649
                    }
1650
                }
1651
                catch (Exception ex)
1652
                {
1653
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1654
                    result = false;
1655
                }
1656
            }
1657

    
1658
            return result;
1659
        }
1660

    
1661

    
1662
        public static bool SaveRevision(string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
1663
            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)
1664
        {
1665
            ID2Info id2Info = ID2Info.GetInstance();
1666

    
1667
            bool result = true;
1668

    
1669
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1670
            {
1671
                try
1672
                {
1673
                    using (var txn = connection.BeginTransaction())
1674
                    {
1675
                        try
1676
                        {                           
1677
                         
1678
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
1679
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
1680
                                $"HD2, HDB, B2B, LastModificationdate) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
1681
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
1682
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B, @LastModificationdate)";
1683
                            var cmd = connection.GetSqlStringCommand(query);
1684
                            AddWithValue(cmd, "@OID", Guid.NewGuid().ToString());
1685
                            AddWithValue(cmd, "@Project_OID", Guid.NewGuid().ToString());
1686
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
1687
                            AddWithValue(cmd, "@RevNumber", RevNumber);
1688
                            AddWithValue(cmd, "@UserName", UserName);
1689

    
1690
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
1691
                            DateTime oDateTime = DateTime.Now;
1692

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

    
1695
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
1696

    
1697
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
1698
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
1699
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
1700
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
1701
                            AddWithValue(cmd, "@PipeLines", PipeLines);
1702

    
1703
                            AddWithValue(cmd, "@Topologies", Topologies);
1704
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
1705
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
1706
                            AddWithValue(cmd, "@E2E", E2E);
1707
                            AddWithValue(cmd, "@E2B", E2B);
1708
                            AddWithValue(cmd, "@B2E", B2E);
1709
                            AddWithValue(cmd, "@HDE", HDE);
1710
                            AddWithValue(cmd, "@HD2", HD2);
1711
                            AddWithValue(cmd, "@HDB", HDB);
1712
                            AddWithValue(cmd, "@B2B", B2B);
1713
                            // AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
1714
                            AddWithValue(cmd, "@LastModificationdate", "");
1715
                            connection.ExecuteNonQuery(cmd, txn);
1716
                            
1717
                            txn.Commit();
1718
                        }
1719
                        catch (Exception ex)
1720
                        {
1721
                            txn.Rollback();
1722
                            result = false;
1723
                        }
1724
                    }
1725
                }
1726
                catch (Exception ex)
1727
                {
1728
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1729
                    result = false;
1730
                }
1731
            }
1732

    
1733
            return result;
1734
        }
1735

    
1736
        public static bool SavePSNData(PSN item)
1737
        {
1738
            ID2Info id2Info = ID2Info.GetInstance();
1739

    
1740
            bool result = true;            
1741

    
1742
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1743
            {
1744
                try
1745
                {
1746
                    using (var txn = connection.BeginTransaction())
1747
                    {
1748
                        try
1749
                        {
1750
                            // Path Items
1751
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
1752
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1753
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
1754
                            {
1755
                                DataRow row = item.PathItems.Rows[i];
1756
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
1757
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, " +
1758
                                    $"PIDNAME, Equipment_OID, NPD, GROUPTAG, PipeSystemNetwork_OID, PipeRun_OID, ViewPipeSystemNetwork_OID) VALUES " +
1759
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME, " +
1760
                                    $"@Equipment_OID, @NPD, @GROUPTAG, @PipeSystemNetwork_OID,@PipeRun_OID, @ViewPipeSystemNetwork_OID)";
1761
                                var cmd = connection.GetSqlStringCommand(query);
1762
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1763
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
1764
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
1765
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
1766
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
1767
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
1768
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1769
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString());
1770
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
1771
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
1772
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
1773
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
1774
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1775
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1776
                                AddWithValue(cmd, "@GROUPTAG", string.IsNullOrEmpty(row["GROUPTAG"].ToString()) ? "" : row["GROUPTAG"].ToString());
1777
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
1778
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
1779
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
1780
                                connection.ExecuteNonQuery(cmd, txn);
1781
                            }
1782

    
1783
                            // Sequence
1784
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
1785
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1786
                            foreach (DataRow row in item.SequenceData.Rows)
1787
                            {
1788
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
1789
                                var cmd = connection.GetSqlStringCommand(query);
1790
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1791
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
1792
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
1793
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1794
                                connection.ExecuteNonQuery(cmd, txn);
1795
                            }
1796

    
1797
                            // Nozzle
1798
                            query = $"DELETE FROM {PSN_NOZZLE}";
1799
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1800
                            foreach (DataRow row in item.Nozzle.Rows)
1801
                            {
1802
                                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)";
1803
                                var cmd = connection.GetSqlStringCommand(query);
1804
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1805
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1806

    
1807
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1808
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1809
                                else
1810
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1811

    
1812
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1813
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1814
                                else
1815
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1816

    
1817
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1818
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
1819
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1820
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
1821

    
1822
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
1823
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
1824
                                else
1825
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
1826

    
1827
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
1828
                                connection.ExecuteNonQuery(cmd, txn);
1829
                            }
1830

    
1831
                            //Equipment
1832
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
1833
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1834
                            foreach (DataRow row in item.Equipment.Rows)
1835
                            {
1836
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
1837
                                var cmd = connection.GetSqlStringCommand(query);
1838
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1839
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1840

    
1841
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1842
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1843
                                else
1844
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1845

    
1846
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1847
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1848
                                else
1849
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1850

    
1851
                                connection.ExecuteNonQuery(cmd, txn);
1852
                            }
1853

    
1854
                            // TopologySet
1855
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
1856
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1857
                            foreach (DataRow row in item.TopologySet.Rows)
1858
                            {
1859
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
1860
                                var cmd = connection.GetSqlStringCommand(query);
1861
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1862
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1863
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
1864
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
1865
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
1866
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
1867
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
1868
                                connection.ExecuteNonQuery(cmd, txn);
1869
                            }
1870

    
1871
                            // PSN
1872
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
1873
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1874
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
1875
                            {
1876
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
1877
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy, Pocket) VALUES " +
1878
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy, @Pocket)";
1879
                                var cmd = connection.GetSqlStringCommand(query);
1880
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1881
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1882
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
1883
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
1884
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
1885
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
1886
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1887
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
1888

    
1889
                                int IsValid = 0;
1890
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
1891
                                    IsValid = 0;
1892
                                else if (row["IsValid"].ToString() == "InValid")
1893
                                    IsValid = 1;
1894
                                else if (row["IsValid"].ToString() == "Error")
1895
                                    IsValid = -1;
1896

    
1897
                                AddWithValue(cmd, "@IsValid", IsValid);
1898
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
1899

    
1900
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
1901
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
1902

    
1903
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
1904
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
1905
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
1906
                                else
1907
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString().Replace("%", ""));
1908

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

    
1911
                                connection.ExecuteNonQuery(cmd, txn);
1912
                            }
1913

    
1914
                            //Pipeline
1915
                            query = $"DELETE FROM {PSN_PIPELINE}";
1916
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1917
                            foreach (DataRow row in item.PipeLine.Rows)
1918
                            {
1919
                                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)";
1920
                                var cmd = connection.GetSqlStringCommand(query);
1921
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
1922
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
1923
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
1924
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
1925
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
1926
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
1927
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
1928
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
1929
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
1930
                                connection.ExecuteNonQuery(cmd, txn);
1931
                            }
1932

    
1933
                            //PipeSystem
1934
                            query = $"DELETE FROM {PSN_PIPESYSTEM}";
1935
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1936
                            foreach (DataRow row in item.PipeSystem.Rows)
1937
                            {
1938
                                query = $"INSERT INTO {PSN_PIPESYSTEM} (OID, DESCRIPTION, FLUID, PMC, PipeLineQty, GroundLevel) VALUES (@OID, @DESCRIPTION, @FLUID, @PMC, @PipeLineQty, @GroundLevel)";
1939
                                var cmd = connection.GetSqlStringCommand(query);
1940
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
1941
                                AddWithValue(cmd, "@DESCRIPTION", row["DESCRIPTION"].ToString());
1942
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
1943
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
1944
                                AddWithValue(cmd, "@PipeLineQty", row["PipeLineQty"].ToString());
1945
                                AddWithValue(cmd, "@GroundLevel", row["GroundLevel"].ToString());
1946
                                connection.ExecuteNonQuery(cmd, txn);
1947
                            }
1948

    
1949
                            if(id2Info.ID2DBType == ID2DB_Type.MSSQL)
1950
                            {
1951
                                query = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
1952
                                connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1953
                            }
1954
                          
1955

    
1956
                            txn.Commit();
1957
                        }
1958
                        catch (Exception ex)
1959
                        {
1960
                            txn.Rollback();
1961
                            result = false;
1962
                        }
1963

    
1964
                    }
1965
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
1966
                    {                        
1967
                        bool check = false;
1968
                        if (colnames != null)
1969
                        {
1970
                            foreach (KeyValuePair<string, string> col in dicCol)
1971
                            {
1972
                                check = false;
1973
                                foreach (string c in colnames)
1974
                                {
1975
                                    if (col.Key.Contains(c))
1976
                                    {
1977
                                        check = true;
1978
                                        break;
1979
                                    }
1980
                                }
1981

    
1982
                                if (!check) //없으면 추가
1983
                                {
1984
                                    string i = string.Empty;
1985
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
1986
                                        i = "DEFAULT 0";
1987

    
1988
                                    var queryalter = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
1989
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
1990
                                    {
1991
                                        cmd.ExecuteNonQuery();
1992
                                    }
1993
                                }
1994
                            }
1995
                        }
1996
                    }
1997
                }
1998
                catch (Exception ex)
1999
                {
2000
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2001
                    result = false;
2002
                }
2003
            }
2004

    
2005
            return result;
2006
        }
2007

    
2008
        public static bool SavePSNFluidCode(DataTable dt)
2009
        {
2010
            ID2Info id2Info = ID2Info.GetInstance();
2011

    
2012
            bool result = true;
2013
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2014
            {
2015
                try
2016
                {
2017
                    using (var txn = connection.BeginTransaction())
2018
                    {
2019
                        try
2020
                        {
2021
                            var query = $"DELETE FROM {PSN_FLUIDCODE}";
2022
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2023

    
2024
                            foreach (DataRow row in dt.Rows)
2025
                            {
2026
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
2027
                                var cmd = connection.GetSqlStringCommand(query);
2028
                                cmd.Parameters.Clear();
2029

    
2030
                                {
2031
                                    var param = cmd.CreateParameter();
2032
                                    param.ParameterName = "@UID";
2033
                                    param.Value = row["UID"].ToString();
2034
                                    cmd.Parameters.Add(param);
2035
                                }
2036

    
2037
                                {
2038
                                    var param = cmd.CreateParameter();
2039
                                    param.ParameterName = "@Code";
2040
                                    param.Value = row["Code"].ToString();
2041
                                    cmd.Parameters.Add(param);
2042
                                }
2043

    
2044
                                {
2045
                                    var param = cmd.CreateParameter();
2046
                                    param.ParameterName = "@Description";
2047
                                    param.Value = row["Description"].ToString();
2048
                                    cmd.Parameters.Add(param);
2049
                                }
2050

    
2051
                                {
2052
                                    var param = cmd.CreateParameter();
2053
                                    param.ParameterName = "@Condition";
2054
                                    param.Value = row["Condition"].ToString();
2055
                                    cmd.Parameters.Add(param);
2056
                                }
2057

    
2058
                                {
2059
                                    var param = cmd.CreateParameter();
2060
                                    param.ParameterName = "@Remarks";
2061
                                    param.Value = row["Remarks"].ToString();
2062
                                    cmd.Parameters.Add(param);
2063
                                }
2064

    
2065
                                {
2066
                                    var param = cmd.CreateParameter();
2067
                                    param.ParameterName = "@GroundLevel";
2068
                                    param.Value = row["GroundLevel"].ToString();
2069
                                    cmd.Parameters.Add(param);
2070
                                }
2071

    
2072
                                connection.ExecuteNonQuery(cmd, txn);
2073
                            }
2074
                            txn.Commit();
2075
                        }
2076
                        catch (Exception ex)
2077
                        {
2078
                            txn.Rollback();
2079
                            result = false;
2080
                        }
2081
                    }
2082
                }
2083
                catch (Exception ex)
2084
                {
2085
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2086
                    result = false;
2087
                }
2088
            }
2089

    
2090
            return result;
2091
        }
2092

    
2093
        public static DataTable SelectPSNFluidCode()
2094
        {
2095
            DataTable dt = null;
2096
            ID2Info id2Info = ID2Info.GetInstance();
2097

    
2098
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2099
            {
2100
                try
2101
                {
2102
                    var query = $"SELECT * FROM {PSN_FLUIDCODE}";
2103
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2104
                    {
2105
                        dt = ds.Tables[0].Copy();
2106
                    }
2107
                }
2108
                catch (Exception ex)
2109
                {
2110
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2111
                }
2112
            }
2113

    
2114
            return dt;
2115
        }
2116

    
2117
        public static bool SavePSNPMC(DataTable dt)
2118
        {
2119
            ID2Info id2Info = ID2Info.GetInstance();
2120

    
2121
            bool result = true;
2122
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2123
            {
2124
                try
2125
                {
2126
                    using (var txn = connection.BeginTransaction())
2127
                    {
2128
                        try
2129
                        {
2130
                            var query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
2131
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2132

    
2133
                            foreach (DataRow row in dt.Rows)
2134
                            {
2135
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
2136
                                var cmd = connection.GetSqlStringCommand(query);
2137
                                cmd.Parameters.Clear();
2138

    
2139
                                {
2140
                                    var param = cmd.CreateParameter();
2141
                                    param.ParameterName = "@UID";
2142
                                    param.Value = row["UID"].ToString();
2143
                                    cmd.Parameters.Add(param);
2144
                                }
2145

    
2146
                                {
2147
                                    var param = cmd.CreateParameter();
2148
                                    param.ParameterName = "@Priority";
2149
                                    param.Value = row["Priority"].ToString();
2150
                                    cmd.Parameters.Add(param);
2151
                                }
2152

    
2153
                                {
2154
                                    var param = cmd.CreateParameter();
2155
                                    param.ParameterName = "@Code";
2156
                                    param.Value = row["Code"].ToString();
2157
                                    cmd.Parameters.Add(param);
2158
                                }
2159

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

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

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

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

    
2188
                                connection.ExecuteNonQuery(cmd, txn);
2189
                            }
2190

    
2191
                            txn.Commit();
2192
                        }
2193
                        catch (Exception ex)
2194
                        {
2195
                            txn.Rollback();
2196
                            result = false;
2197
                        }
2198
                    }
2199
                }
2200
                catch (Exception ex)
2201
                {
2202
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2203
                    result = false;
2204
                }
2205
            }
2206

    
2207
            return result;
2208
        }
2209

    
2210
        public static bool SavePSNInsulation(DataTable dt)
2211
        {
2212
            ID2Info id2Info = ID2Info.GetInstance();
2213

    
2214
            bool result = true;
2215
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2216
            {
2217
                try
2218
                {
2219
                    using (var txn = connection.BeginTransaction())
2220
                    {
2221
                        try
2222
                        {
2223
                            var query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
2224
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2225

    
2226
                            foreach (DataRow row in dt.Rows)
2227
                            {
2228
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
2229
                                var cmd = connection.GetSqlStringCommand(query);
2230
                                cmd.Parameters.Clear();
2231

    
2232
                                {
2233
                                    var param = cmd.CreateParameter();
2234
                                    param.ParameterName = "@UID";
2235
                                    param.Value = row["UID"].ToString();
2236
                                    cmd.Parameters.Add(param);
2237
                                }
2238
                                
2239
                                {
2240
                                    var param = cmd.CreateParameter();
2241
                                    param.ParameterName = "@Code";
2242
                                    param.Value = row["Code"].ToString();
2243
                                    cmd.Parameters.Add(param);
2244
                                }
2245

    
2246
                                {
2247
                                    var param = cmd.CreateParameter();
2248
                                    param.ParameterName = "@Description";
2249
                                    param.Value = row["Description"].ToString();
2250
                                    cmd.Parameters.Add(param);
2251
                                }
2252
                                
2253
                                {
2254
                                    var param = cmd.CreateParameter();
2255
                                    param.ParameterName = "@Remarks";
2256
                                    param.Value = row["Remarks"].ToString();
2257
                                    cmd.Parameters.Add(param);
2258
                                }
2259

    
2260
                                connection.ExecuteNonQuery(cmd, txn);
2261
                            }
2262

    
2263
                            txn.Commit();
2264
                        }
2265
                        catch (Exception ex)
2266
                        {
2267
                            txn.Rollback();
2268
                            result = false;
2269
                        }
2270
                    }
2271
                }
2272
                catch (Exception ex)
2273
                {
2274
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2275
                    result = false;
2276
                }
2277
            }
2278

    
2279
            return result;
2280
        }
2281

    
2282
        public static PSN GetDBPSN()
2283
        {
2284
            PSN result = new PSN();
2285
            ID2Info id2Info = ID2Info.GetInstance();
2286

    
2287
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2288
            {
2289
                try
2290
                {
2291
                    //query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK}";
2292
                    //using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2293
                    //{
2294
                    //    result.PipeSystemNetwork = ds.Tables[0].Copy();
2295
                    //}
2296

    
2297
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
2298
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2299
                    {
2300
                        result.PipeSystemNetwork = ds.Tables[0].Clone();
2301
                        result.PipeSystemNetwork.Columns["IsValid"].DataType = typeof(string);
2302

    
2303
                        foreach (DataRow row in ds.Tables[0].Rows)
2304
                        {
2305
                            DataRow newRow = result.PipeSystemNetwork.NewRow();
2306
                            newRow["OID"] = row["OID"].ToString();
2307
                            newRow["Type"] = row["Type"].ToString();
2308
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
2309
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
2310
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
2311
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
2312
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
2313
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
2314

    
2315
                            string IsValid = string.Empty;
2316

    
2317
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
2318
                                IsValid = string.Empty;//"OK";
2319
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
2320
                                IsValid = "InValid";
2321
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
2322
                                IsValid = "Error";
2323

    
2324
                            newRow["IsValid"] = IsValid;
2325

    
2326
                            newRow["Status"] = row["Status"].ToString();
2327
                            newRow["PBS"] = row["PBS"].ToString();
2328
                            newRow["Drawings"] = row["Drawings"].ToString();
2329

    
2330
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2331
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
2332

    
2333
                            newRow["Pocket"] = row["Pocket"].ToString();
2334
                            
2335
                            result.PipeSystemNetwork.Rows.Add(newRow);
2336
                        }
2337
                    }
2338

    
2339
                    query = $"SELECT * FROM {PSN_EQUIPMENT}";
2340
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2341
                    {
2342
                        result.Equipment = ds.Tables[0].Copy();
2343
                    }
2344

    
2345
                    query = $"SELECT * FROM {PSN_NOZZLE}";
2346
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2347
                    {
2348
                        result.Nozzle = ds.Tables[0].Copy();
2349
                    }
2350

    
2351
                    query = $"SELECT * FROM {PSN_PATHITEMS}";
2352
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2353
                    {
2354
                        result.PathItems = ds.Tables[0].Copy();
2355
                    }
2356

    
2357
                    query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
2358
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2359
                    {
2360
                        result.SequenceData = ds.Tables[0].Copy();
2361
                    }
2362

    
2363
                    query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
2364
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2365
                    {
2366
                        result.TopologySet = ds.Tables[0].Copy();
2367
                    }
2368

    
2369
                    query = $"SELECT * FROM {PSN_PIPELINE}";
2370
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2371
                    {
2372
                        result.PipeLine = ds.Tables[0].Copy();
2373
                    }
2374

    
2375
                    query = $"SELECT * FROM {PSN_PIPESYSTEM}";
2376
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2377
                    {
2378
                        result.PipeSystem = ds.Tables[0].Copy();
2379
                    }
2380

    
2381
                    result.Revision = GetRevision();
2382
                }
2383
                catch (Exception ex)
2384
                {
2385
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2386
                    result = null;
2387
                }
2388
            }
2389

    
2390
            return result;
2391
        }
2392

    
2393
        public static int GetRevision()
2394
        {
2395
            int result = 0;
2396
            ID2Info id2Info = ID2Info.GetInstance();
2397

    
2398
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2399
            {
2400
                try
2401
                {
2402
                    var query = $"SELECT DISTINCT PSNRevisionNumber FROM {PSN_PIPESYSTEMNETWORK};";
2403
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2404
                    {
2405
                        foreach (DataRow row in ds.Tables[0].Rows)
2406
                        {
2407
                            string value = row["PSNRevisionNumber"].ToString();
2408
                            if (value.StartsWith("V"))
2409
                                value = value.Remove(0, 1);
2410
                            int revisionNumber = Convert.ToInt32(value);
2411
                            if (result < revisionNumber)
2412
                                result = revisionNumber;
2413
                        }
2414
                    }
2415
                }
2416
                catch (Exception ex)
2417
                {
2418
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2419
                    result = -1;
2420
                }
2421
            }
2422

    
2423
            return result;
2424
        }
2425

    
2426
        public static DataTable GetPathItem()
2427
        {
2428
            DataTable dt = null;
2429

    
2430
            ID2Info id2Info = ID2Info.GetInstance();
2431
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2432
            {
2433
                try
2434
                {
2435
                    var query = $"SELECT * FROM {PSN_PATHITEMS}";
2436
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2437
                    {
2438
                        dt = ds.Tables[0].Copy();
2439
                    }
2440
                }
2441
                catch (Exception ex)
2442
                {
2443
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2444
                }
2445
            }
2446

    
2447
            return dt;
2448
        }
2449

    
2450
        public static DataTable GetTopologySet()
2451
        {
2452
            DataTable dt = null;
2453

    
2454
            ID2Info id2Info = ID2Info.GetInstance();
2455
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2456
            {
2457
                try
2458
                {
2459
                    var query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
2460
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2461
                    {
2462
                        dt = ds.Tables[0].Clone();
2463
                        foreach (DataRow row in ds.Tables[0].Rows)
2464
                        {
2465
                            DataRow newRow = dt.NewRow();
2466
                            newRow["OID"] = row["OID"].ToString();
2467
                            newRow["Type"] = row["Type"].ToString();
2468
                            newRow["SubType"] = row["SubType"].ToString();
2469
                            newRow["HeadItemTag"] = row["HeadItemTag"].ToString();
2470
                            newRow["TailItemTag"] = row["TailItemTag"].ToString();
2471
                            newRow["HeadItemSPID"] = row["HeadItemSPID"].ToString();
2472
                            newRow["TailItemSPID"] = row["TailItemSPID"].ToString();
2473
                            dt.Rows.Add(newRow);
2474
                        }
2475
                    }
2476
                }
2477
                catch (Exception ex)
2478
                {
2479
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2480
                }
2481
            }
2482

    
2483
            return dt;
2484
        }
2485

    
2486
        public static DataTable GetPipeSystemNetwork()
2487
        {
2488
            DataTable dt = null;
2489

    
2490
            ID2Info id2Info = ID2Info.GetInstance();
2491
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2492
            {
2493
                try
2494
                {
2495
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}' ORDER BY OID, OrderNumber";
2496
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2497
                    {
2498
                        dt = ds.Tables[0].Clone();
2499
                        dt.Columns["IsValid"].DataType = typeof(string);
2500
                        foreach (DataRow row in ds.Tables[0].Rows)
2501
                        {
2502
                            DataRow newRow = dt.NewRow();
2503
                            newRow["OID"] = row["OID"].ToString();
2504
                            newRow["Type"] = row["Type"].ToString();
2505
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
2506
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
2507
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
2508
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
2509
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
2510
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
2511

    
2512
                            string IsValid = string.Empty;
2513

    
2514
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
2515
                                IsValid = string.Empty;//"OK";
2516
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
2517
                                IsValid = "InValid";
2518
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
2519
                                IsValid = "Error";
2520

    
2521
                            newRow["IsValid"] = IsValid;
2522
                            newRow["Status"] = row["Status"].ToString();
2523

    
2524
                            newRow["PBS"] = row["PBS"].ToString();
2525
                            newRow["Drawings"] = row["Drawings"].ToString();
2526
                            
2527
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2528
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
2529

    
2530
                            newRow["Pocket"] = row["Pocket"].ToString();
2531
                            
2532
                            dt.Rows.Add(newRow);
2533
                        }
2534
                    }
2535
                }
2536
                catch (Exception ex)
2537
                {
2538
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2539
                }
2540
            }
2541

    
2542
            return dt;
2543
        }
2544

    
2545
        public static DataTable GetSequenceData()
2546
        {
2547
            DataTable dt = null;
2548

    
2549
            ID2Info id2Info = ID2Info.GetInstance();
2550
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2551
            {
2552
                try
2553
                {
2554
                    var query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
2555
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2556
                    {
2557
                        dt = ds.Tables[0].Copy();
2558
                    }
2559
                }
2560
                catch (Exception ex)
2561
                {
2562
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2563
                }
2564
            }
2565

    
2566
            return dt;
2567
        }
2568

    
2569
       
2570
        //Anohter DB
2571
        public static bool ConnTestAndCreateAnotherTable()
2572
        {
2573
            bool result = false;
2574
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
2575

    
2576
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2577
            {
2578
                try
2579
                {
2580
                    var names = connection.GetTableNames();
2581
                    var matched = names.FirstOrDefault(param => param == PSN_HEADER_SETTING);
2582
                    Dictionary<string, string> dicColCheck = new Dictionary<string, string>();
2583
                    dicColCheck.Add("GROUP_ID", "TEXT");
2584
                    dicColCheck.Add("DESCRIPTION", "TEXT");
2585
                    dicColCheck.Add("INDEX", "INTEGER");
2586
                    dicColCheck.Add("NAME", "TEXT");
2587

    
2588
                    if (matched == null)
2589
                    {
2590
                        var query = $"CREATE TABLE {PSN_HEADER_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
2591
                        using (var cmd = connection.GetSqlStringCommand(query))
2592
                        {
2593
                            cmd.ExecuteNonQuery();
2594
                        }
2595
                    }
2596
                    else
2597
                    {
2598
                        AddColumn(PSN_HEADER_SETTING, dicColCheck);
2599
                    }
2600

    
2601
                    matched = names.FirstOrDefault(param => param == PSN_VENTDRAIN_SETTING);
2602
                    dicColCheck.Clear();
2603
                    dicColCheck.Add("GROUP_ID", "TEXT");
2604
                    dicColCheck.Add("DESCRIPTION", "TEXT");
2605
                    dicColCheck.Add("INDEX", "INTEGER");
2606
                    dicColCheck.Add("NAME", "TEXT");
2607
                    if (matched == null)
2608
                    {
2609
                        var query = $"CREATE TABLE {PSN_VENTDRAIN_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
2610
                        using (var cmd = connection.GetSqlStringCommand(query))
2611
                        {
2612
                            cmd.ExecuteNonQuery();
2613
                        }
2614
                    }
2615
                    else
2616
                    {
2617
                        AddColumn(PSN_VENTDRAIN_SETTING, dicColCheck);
2618
                    }
2619

    
2620
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGY_RULE);
2621
                    dicColCheck.Clear();
2622
                    dicColCheck.Add("UID", "TEXT");
2623
                    if (matched == null)
2624
                    {
2625
                        var query = $"CREATE TABLE {PSN_TOPOLOGY_RULE} (UID TEXT)";
2626
                        using (var cmd = connection.GetSqlStringCommand(query))
2627
                        {
2628
                            cmd.ExecuteNonQuery();
2629
                        }
2630

    
2631
                        DataTable topologyRule = new DataTable();
2632
                        topologyRule.Columns.Add("NAME", typeof(string));
2633

    
2634
                        topologyRule.Rows.Add("FluidCode");
2635
                        topologyRule.Rows.Add("-");
2636
                        topologyRule.Rows.Add("PipingMaterialsClass");
2637
                        topologyRule.Rows.Add("-");
2638
                        topologyRule.Rows.Add("Tag Seq No");
2639

    
2640
                        SaveTopologyRule(topologyRule);
2641
                    }
2642
                    //else
2643
                    //{
2644
                    //    AddColumn(PSN_TOPOLOGY_RULE, dicColCheck);
2645
                    //}
2646

    
2647
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
2648
                    dicColCheck.Clear();
2649
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2650
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2651
                    dicColCheck.Add("OrderNumber", "NVARCHAR(255)");
2652
                    dicColCheck.Add("Pipeline_OID", "NVARCHAR(255)");
2653
                    dicColCheck.Add("From_Data", "NVARCHAR(255)");
2654
                    dicColCheck.Add("To_Data", "NVARCHAR(255)");
2655
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(125)");
2656
                    dicColCheck.Add("PSNRevisionNumber", "NVARCHAR(255)");
2657
                    dicColCheck.Add("IsValid", "INT");
2658
                    dicColCheck.Add("Status", "NVARCHAR(255)");
2659
                    dicColCheck.Add("PBS", "NVARCHAR(255)");
2660
                    dicColCheck.Add("Drawings", "NVARCHAR(255)");
2661
                    dicColCheck.Add("IncludingVirtualData", "NVARCHAR(10)");
2662
                    dicColCheck.Add("PSNAccuracy", "REAL");
2663
                    dicColCheck.Add("Pocket", "NVARCHAR(10)");
2664
                    
2665
                    if (matched == null)
2666
                    {
2667
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(255), Type NVARCHAR(255), OrderNumber NVARCHAR(255), Pipeline_OID NVARCHAR(255), From_Data NVARCHAR(255), " +
2668
                            "To_Data NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
2669
                            "IncludingVirtualData NVARCHAR(10),  PSNAccuracy REAL, Pocket NVARCHAR(10))";
2670
                        using (var cmd = connection.GetSqlStringCommand(query))
2671
                        {
2672
                            cmd.ExecuteNonQuery();
2673
                        }
2674
                    }
2675
                    else
2676
                    {
2677
                        AddColumn(PSN_PIPESYSTEMNETWORK, dicColCheck);
2678
                    }
2679

    
2680
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
2681
                    dicColCheck.Clear();
2682
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2683
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2684
                    dicColCheck.Add("Xcoords", "REAL");
2685
                    dicColCheck.Add("Ycoords", "REAL");
2686
                    if (matched == null)
2687
                    {
2688
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL)";
2689
                        using (var cmd = connection.GetSqlStringCommand(query))
2690
                        {
2691
                            cmd.ExecuteNonQuery();
2692
                        }
2693
                    }
2694
                    else
2695
                    {
2696
                        AddColumn(PSN_EQUIPMENT, dicColCheck);
2697
                    }
2698

    
2699
                    dicColCheck.Clear();
2700
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2701
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2702
                    dicColCheck.Add("Xcoords", "REAL");
2703
                    dicColCheck.Add("Ycoords", "REAL");
2704
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2705
                    dicColCheck.Add("Fluid", "NVARCHAR(255)");
2706
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2707
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2708
                    dicColCheck.Add("Rotation", "REAL");
2709
                    dicColCheck.Add("FlowDirection", "NVARCHAR(255)");
2710

    
2711
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
2712
                    if (matched == null)
2713
                    {
2714
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL, Equipment_OID NVARCHAR(255), " +
2715
                            "Fluid NVARCHAR(255), NPD NVARCHAR(255), PMC NVARCHAR(255), Rotation REAL, FlowDirection NVARCHAR(255))";
2716
                        using (var cmd = connection.GetSqlStringCommand(query))
2717
                        {
2718
                            cmd.ExecuteNonQuery();
2719
                        }
2720
                    }
2721
                    else
2722
                    {
2723
                        AddColumn(PSN_NOZZLE, dicColCheck);
2724
                    }
2725

    
2726
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
2727
                    dicColCheck.Clear();
2728
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2729
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2730
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2731
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2732
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2733
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2734
                    if (matched == null)
2735
                    {
2736
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
2737
                        using (var cmd = connection.GetSqlStringCommand(query))
2738
                        {
2739
                            cmd.ExecuteNonQuery();
2740
                        }
2741
                    }
2742
                    else
2743
                    {
2744
                        AddColumn(PSN_FLUIDCODE, dicColCheck);
2745
                    }
2746

    
2747
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
2748
                    dicColCheck.Clear();
2749
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2750
                    dicColCheck.Add("Priority", "INTEGER");
2751
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2752
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2753
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2754
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2755
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2756
                    if (matched == null)
2757
                    {
2758
                        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))";
2759
                        using (var cmd = connection.GetSqlStringCommand(query))
2760
                        {
2761
                            cmd.ExecuteNonQuery();
2762
                        }
2763
                    }
2764
                    else
2765
                    {
2766
                        AddColumn(PSN_PIPINGMATLCLASS, dicColCheck);
2767
                    }
2768

    
2769
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
2770
                    dicColCheck.Clear();
2771
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2772
                    dicColCheck.Add("SERIALNUMBER", "NVARCHAR(255)");
2773
                    dicColCheck.Add("PathItem_OID", "NVARCHAR(255)");
2774
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(255)");
2775
                    if (matched == null)
2776
                    {
2777
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(255), SERIALNUMBER NVARCHAR(255), PathItem_OID NVARCHAR(255), TopologySet_OID_Key NVARCHAR(255))";
2778
                        using (var cmd = connection.GetSqlStringCommand(query))
2779
                        {
2780
                            cmd.ExecuteNonQuery();
2781
                        }
2782
                    }
2783
                    else
2784
                    {
2785
                        AddColumn(PSN_SEQUENCEDATA, dicColCheck);
2786
                    }
2787

    
2788
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
2789
                    dicColCheck.Clear();
2790
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2791
                    dicColCheck.Add("SequenceData_OID", "NVARCHAR(255)");
2792
                    dicColCheck.Add("TopologySet_OID", "NVARCHAR(255)");
2793
                    dicColCheck.Add("BranchTopologySet_OID", "NVARCHAR(255)");
2794
                    dicColCheck.Add("PipeLine_OID", "NVARCHAR(255)");
2795
                    dicColCheck.Add("ItemName", "NVARCHAR(255)");
2796
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2797
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
2798
                    dicColCheck.Add("CLASS", "NVARCHAR(255)");
2799
                    dicColCheck.Add("SUBCLASS", "NVARCHAR(255)");
2800
                    dicColCheck.Add("TYPE", "NVARCHAR(255)");
2801
                    dicColCheck.Add("PIDNAME", "NVARCHAR(255)");
2802
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2803
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2804
                    dicColCheck.Add("GROUPTAG", "NVARCHAR(255)");
2805
                    dicColCheck.Add("PipeSystemNetwork_OID", "NVARCHAR(255)");
2806
                    dicColCheck.Add("PipeRun_OID", "NVARCHAR(255)");
2807
                    dicColCheck.Add("ViewPipeSystemNetwork_OID", "NVARCHAR(255)");
2808
                    if (matched == null)
2809
                    {
2810
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
2811
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
2812
                            "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), " +
2813
                            "ViewPipeSystemNetwork_OID NVARCHAR(255))";
2814
                        using (var cmd = connection.GetSqlStringCommand(query))
2815
                        {
2816
                            cmd.ExecuteNonQuery();
2817
                        }
2818
                    }
2819
                    else
2820
                    {
2821
                        AddColumn(PSN_PATHITEMS, dicColCheck);
2822
                    }
2823

    
2824
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
2825
                    dicColCheck.Clear();
2826
                    dicColCheck.Add("OID", "TEXT");
2827
                    if (matched == null)
2828
                    {
2829
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
2830
                        using (var cmd = connection.GetSqlStringCommand(query))
2831
                        {
2832
                            cmd.ExecuteNonQuery();
2833
                        }
2834
                    }
2835

    
2836
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
2837
                    dicColCheck.Clear();
2838
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2839
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2840
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
2841
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
2842
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
2843
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
2844
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
2845

    
2846
                    if (matched == null)
2847
                    {
2848
                        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))";
2849
                        using (var cmd = connection.GetSqlStringCommand(query))
2850
                        {
2851
                            cmd.ExecuteNonQuery();
2852
                        }
2853
                    }
2854
                    else
2855
                    {
2856
                        AddColumn(PSN_TOPOLOGYSET, dicColCheck);
2857
                    }
2858

    
2859
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
2860
                    dicColCheck.Clear();
2861
                    dicColCheck.Add("INDEX", "INTEGER");
2862
                    dicColCheck.Add("NAME", "TEXT");
2863
                    dicColCheck.Add("KEYWORD", "TEXT");
2864
                    if (matched == null)
2865
                    {
2866
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
2867
                        using (var cmd = connection.GetSqlStringCommand(query))
2868
                        {
2869
                            cmd.ExecuteNonQuery();
2870
                        }
2871
                    }
2872
                    else
2873
                    {
2874
                        AddColumn(PSN_TRANSFORMKEYWORD_SETTING, dicColCheck);
2875
                    }
2876

    
2877
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
2878
                    dicColCheck.Clear();
2879
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2880
                    dicColCheck.Add("PipeSystem_OID", "NVARCHAR(255)");
2881
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
2882
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2883
                    dicColCheck.Add("SEQNUMBER", "NVARCHAR(255)");
2884
                    dicColCheck.Add("INSULATION", "NVARCHAR(255)");
2885
                    dicColCheck.Add("FROM_DATA", "NVARCHAR(255)");
2886
                    dicColCheck.Add("TO_DATA", "NVARCHAR(255)");
2887
                    dicColCheck.Add("Unit", "NVARCHAR(100)");
2888
                    if (matched == null)
2889
                    {
2890
                        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), " +
2891
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
2892
                        using (var cmd = connection.GetSqlStringCommand(query))
2893
                        {
2894
                            cmd.ExecuteNonQuery();
2895
                        }
2896
                    }
2897
                    else
2898
                    {
2899
                        AddColumn(PSN_PIPELINE, dicColCheck);
2900
                    }
2901

    
2902
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
2903
                    dicColCheck.Clear();
2904
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2905
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2906
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2907
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2908
                    if (matched == null)
2909
                    {
2910
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
2911
                        using (var cmd = connection.GetSqlStringCommand(query))
2912
                        {
2913
                            cmd.ExecuteNonQuery();
2914
                        }
2915
                    }
2916
                    else
2917
                    {
2918
                        AddColumn(PSN_INSULATIONPURPOSE, dicColCheck);
2919
                    }
2920

    
2921
                    matched = names.FirstOrDefault(param => param == PSN_VALVEGROUP_SETTING);
2922
                    dicColCheck.Clear();
2923
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2924
                    dicColCheck.Add("GroupType", "NVARCHAR(255)");
2925
                    dicColCheck.Add("TagIdentifier", "NVARCHAR(50)");
2926
                    dicColCheck.Add("AttributeName", "NVARCHAR(255)");
2927
                    dicColCheck.Add("SppidSymbolName", "NVARCHAR(255)");
2928
                    if (matched == null)
2929
                    {
2930
                        var query = $"CREATE TABLE {PSN_VALVEGROUP_SETTING} (OID NVARCHAR(50), GroupType NVARCHAR(255), TagIdentifier NVARCHAR(50), AttributeName NVARCHAR(255), SppidSymbolName NVARCHAR(255))";
2931
                        using (var cmd = connection.GetSqlStringCommand(query))
2932
                        {
2933
                            cmd.ExecuteNonQuery();
2934
                        }
2935
                    }
2936
                    else
2937
                    {
2938
                        AddColumn(PSN_VALVEGROUP_SETTING, dicColCheck);
2939
                    }
2940

    
2941
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEM);
2942
                    dicColCheck.Clear();
2943
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2944
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
2945
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
2946
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2947
                    dicColCheck.Add("PipeLineQty", "NVARCHAR(255)");
2948
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2949
                    if (matched == null)
2950
                    {
2951
                        var query = $"CREATE TABLE {PSN_PIPESYSTEM} (OID NVARCHAR(255), DESCRIPTION NVARCHAR(255), FLUID NVARCHAR(255), PMC NVARCHAR(255), PipeLineQty NVARCHAR(255), GroundLevel NVARCHAR(20))";
2952
                        using (var cmd = connection.GetSqlStringCommand(query))
2953
                        {
2954
                            cmd.ExecuteNonQuery();
2955
                        }
2956
                    }
2957
                    else
2958
                    {
2959
                        AddColumn(PSN_PIPESYSTEM, dicColCheck);
2960
                    }
2961

    
2962
                    matched = names.FirstOrDefault(param => param == PSN_NOPOCKETSETTING);
2963
                    dicColCheck.Clear();
2964
                    dicColCheck.Add("[INDEX]", "INTEGER");
2965
                    dicColCheck.Add("[TYPE]", "TEXT");
2966
                    dicColCheck.Add("[NAME]", "TEXT");
2967
                    if (matched == null)
2968
                    {
2969
                        var query = $"CREATE TABLE {PSN_NOPOCKETSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [NAME] TEXT)";
2970
                        using (var cmd = connection.GetSqlStringCommand(query))
2971
                        {
2972
                            cmd.ExecuteNonQuery();
2973
                        }
2974
                    }
2975
                    else
2976
                    {
2977
                        AddColumn(PSN_NOPOCKETSETTING, dicColCheck);
2978
                    }
2979

    
2980
                    matched = names.FirstOrDefault(param => param == PSN_AIRFINCOOLERSETTING);
2981
                    dicColCheck.Clear();
2982
                    dicColCheck.Add("[INDEX]", "INTEGER");
2983
                    dicColCheck.Add("[TYPE]", "TEXT");
2984
                    dicColCheck.Add("[NAME]", "TEXT");
2985
                    if (matched == null)
2986
                    {
2987
                        var query = $"CREATE TABLE {PSN_AIRFINCOOLERSETTING}  ([INDEX] INTEGER, [TYPE] TEXT, [NAME] TEXT)";
2988
                        using (var cmd = connection.GetSqlStringCommand(query))
2989
                        {
2990
                            cmd.ExecuteNonQuery();
2991
                        }
2992
                    }
2993
                    else
2994
                    {
2995
                        AddColumn(PSN_AIRFINCOOLERSETTING, dicColCheck);
2996
                    }
2997
                    
2998

    
2999
                    var query2 = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
3000
                    if (id2Info.ID2DBType == AnotherID2DB_Type.MSSQL)
3001
                    {
3002
                        //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3003
                        using (var cmd = connection.GetSqlStringCommand(query2))
3004
                        {
3005
                            cmd.ExecuteNonQuery();
3006
                        }
3007
                    }
3008

    
3009
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
3010
                    {
3011
                        var colnames = connection.GetColumnNames(TableName);
3012
                        bool check = false;
3013
                        if (colnames != null)
3014
                        {
3015
                            foreach (KeyValuePair<string, string> col in dicCol)
3016
                            {
3017
                                check = false;
3018
                                foreach (string c in colnames)
3019
                                {
3020
                                    if (col.Key.ToUpper().Equals(c.ToUpper()))
3021
                                    {
3022
                                        check = true;
3023
                                        break;
3024
                                    }
3025
                                }
3026

    
3027
                                if (!check) //없으면 추가
3028
                                {
3029
                                    string i = string.Empty;
3030
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
3031
                                        i = "DEFAULT 0";
3032

    
3033
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
3034
                                    using (var cmd = connection.GetSqlStringCommand(query))
3035
                                    {
3036
                                        cmd.ExecuteNonQuery();
3037
                                    }
3038
                                }
3039
                            }
3040
                        }
3041
                    }
3042

    
3043
                    result = true;
3044
                }
3045
                catch (Exception ex)
3046
                {
3047
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3048
                }
3049
            }
3050

    
3051
            return result;
3052
        }
3053

    
3054
        public static bool SaveAnotherRevision(string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
3055
         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)
3056
        {
3057
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3058

    
3059
            bool result = true;
3060

    
3061
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3062
            {
3063
                try
3064
                {
3065
                    using (var txn = connection.BeginTransaction())
3066
                    {
3067
                        try
3068
                        {
3069

    
3070
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
3071
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
3072
                                $"HD2, HDB, B2B, LastModificationdate) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
3073
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
3074
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B, @LastModificationdate)";
3075
                            var cmd = connection.GetSqlStringCommand(query);
3076
                            AddWithValue(cmd, "@OID", Guid.NewGuid().ToString());
3077
                            AddWithValue(cmd, "@Project_OID", Guid.NewGuid().ToString());
3078
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
3079
                            AddWithValue(cmd, "@RevNumber", RevNumber);
3080
                            AddWithValue(cmd, "@UserName", UserName);
3081

    
3082
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
3083
                            DateTime oDateTime = DateTime.Now;                            
3084

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

    
3089
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
3090
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
3091
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
3092
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
3093
                            AddWithValue(cmd, "@PipeLines", PipeLines);
3094

    
3095
                            AddWithValue(cmd, "@Topologies", Topologies);
3096
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
3097
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
3098
                            AddWithValue(cmd, "@E2E", E2E);
3099
                            AddWithValue(cmd, "@E2B", E2B);
3100
                            AddWithValue(cmd, "@B2E", B2E);
3101
                            AddWithValue(cmd, "@HDE", HDE);
3102
                            AddWithValue(cmd, "@HD2", HD2);
3103
                            AddWithValue(cmd, "@HDB", HDB);
3104
                            AddWithValue(cmd, "@B2B", B2B);
3105

    
3106
                            AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
3107
                            connection.ExecuteNonQuery(cmd, txn);
3108

    
3109
                            txn.Commit();
3110
                        }
3111
                        catch (Exception ex)
3112
                        {
3113
                            txn.Rollback();
3114
                            result = false;
3115
                        }
3116
                    }
3117
                }
3118
                catch (Exception ex)
3119
                {
3120
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3121
                    result = false;
3122
                }
3123
            }
3124

    
3125
            return result;
3126
        }
3127

    
3128
        public static bool ConnAnotherCreateRevisionTable(Dictionary<string, string> dicColCheck, List<string> names)
3129
        {
3130
            bool result = false;
3131
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3132

    
3133
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3134
            {
3135
                try
3136
                {
3137
                    if (names.Count == 0)
3138
                    {
3139

    
3140
                        var query = $"CREATE TABLE ARS_COMMON.dbo.PSNRevision ([OID] [uniqueidentifier] ,[Project_OID] [uniqueidentifier] ," +
3141
                            $"[ProjectCode] nvarchar(255), [RevNumber] int ,[UserName] nvarchar(255) ,[TimeData] nvarchar(255) ," +
3142
                            $"[PSNDatabasePath] nvarchar(255) ,	[PsnByPBSFilter] varchar(20) ,[PsnByNPDFilter] varchar(20) ,[PidDrawings] int ,	" +
3143
                            $"[PipeSystems] int ,[PipeLines] int ,[Topologies] int ,[PipeSystemNetworks] int ,[InValidPSNs] int ,[E2E] int ," +
3144
                            $"[E2B] int ,[B2E] int ,[HDE] int ,[HD2] int ,[HDB] int ,[B2B] int , [LastModificationdate] nvarchar(255))";
3145
                        using (var cmd = connection.GetSqlStringCommand(query))
3146
                        {
3147
                            cmd.ExecuteNonQuery();
3148
                        }
3149
                    }
3150
                    else
3151
                    {
3152
                        AddColumn(PSN_REVISION, dicColCheck, names);
3153
                    }
3154

    
3155
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
3156
                    {
3157
                        bool check = false;
3158
                        if (colnames != null)
3159
                        {
3160
                            foreach (KeyValuePair<string, string> col in dicCol)
3161
                            {
3162
                                check = false;
3163
                                foreach (string c in colnames)
3164
                                {
3165
                                    if (col.Key.Contains(c))
3166
                                    {
3167
                                        check = true;
3168
                                        break;
3169
                                    }
3170
                                }
3171

    
3172
                                if (!check) //없으면 추가
3173
                                {
3174
                                    string i = string.Empty;
3175
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
3176
                                        i = "DEFAULT 0";
3177

    
3178
                                    var queryalter = $"ALTER TABLE ARS_COMMON.dbo.PSNRevision ADD [" + col.Key + "] " + col.Value + " " + i + ";";
3179
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
3180
                                    {
3181
                                        cmd.ExecuteNonQuery();
3182
                                    }
3183
                                }
3184
                            }
3185
                        }
3186
                    }
3187

    
3188
                    result = true;
3189
                }
3190
                catch (Exception ex)
3191
                {
3192
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3193
                }
3194
            }
3195

    
3196
            return result;
3197
        }
3198

    
3199
        public static DataTable SelectAnotherRevision()
3200
        {
3201
            DataTable dt = null;
3202
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3203

    
3204
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3205
            {
3206
                try
3207
                {
3208
                    var query = $"SELECT * FROM ARS_COMMON.dbo.PSNREVISION";
3209

    
3210
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
3211
                    {
3212
                        dt = ds.Tables[0].Copy();
3213
                    }
3214
                }
3215
                catch (Exception ex)
3216
                {
3217
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3218
                }
3219
            }
3220

    
3221
            return dt;
3222
        }
3223

    
3224
        public static DataTable SelectAnotherRevisionTable()
3225
        {
3226
            DataTable dt = null;
3227
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3228

    
3229
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3230
            {
3231
                try
3232
                {
3233
                    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'";
3234
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
3235
                    {
3236
                        dt = ds.Tables[0].Copy();
3237
                    }
3238
                }
3239
                catch (Exception ex)
3240
                {
3241
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3242
                }
3243
            }
3244

    
3245
            return dt;
3246
        }
3247

    
3248
        public static bool SaveAnotherPSNData(PSN item, List<HeaderInfo> headerInfos, List<VentDrainInfo> ventDrainInfos,
3249
          List<KeywordItem> keywordItems, DataTable dtTopologyRule, DataTable dtFluidCode, DataTable dtPMC, DataTable dtInsulation, DataTable dtvalvegrouping, DataTable dtnopocket)
3250
        {
3251
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3252

    
3253
            bool result = true;
3254
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3255
            {
3256
                try
3257
                {
3258
                    using (var txn = connection.BeginTransaction())
3259
                    {
3260
                        try
3261
                        {
3262
                            // Path Items
3263
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
3264
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3265
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
3266
                            {
3267
                                DataRow row = item.PathItems.Rows[i];
3268
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
3269
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, " +
3270
                                    $"PIDNAME, Equipment_OID, NPD, GROUPTAG, PipeSystemNetwork_OID, PipeRun_OID, ViewPipeSystemNetwork_OID) VALUES " +
3271
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME," +
3272
                                    $" @Equipment_OID, @NPD, @GROUPTAG, @PipeSystemNetwork_OID,@PipeRun_OID, @ViewPipeSystemNetwork_OID)";
3273
                                var cmd = connection.GetSqlStringCommand(query);
3274
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3275
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
3276
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
3277
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
3278
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
3279
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
3280
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
3281
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString());
3282
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
3283
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
3284
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
3285
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
3286
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
3287
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
3288
                                AddWithValue(cmd, "@GROUPTAG", string.IsNullOrEmpty(row["GROUPTAG"].ToString()) ? "" : row["GROUPTAG"].ToString());
3289
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
3290
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
3291
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
3292
                                connection.ExecuteNonQuery(cmd, txn);
3293
                            }
3294

    
3295
                            // Sequence
3296
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
3297
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3298
                            foreach (DataRow row in item.SequenceData.Rows)
3299
                            {
3300
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
3301
                                var cmd = connection.GetSqlStringCommand(query);
3302
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3303
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
3304
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
3305
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
3306
                                connection.ExecuteNonQuery(cmd, txn);
3307
                            }
3308

    
3309
                            // Nozzle
3310
                            query = $"DELETE FROM {PSN_NOZZLE}";
3311
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3312
                            foreach (DataRow row in item.Nozzle.Rows)
3313
                            {
3314
                                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)";
3315
                                var cmd = connection.GetSqlStringCommand(query);
3316
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3317
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
3318

    
3319
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
3320
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
3321
                                else
3322
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
3323

    
3324
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
3325
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
3326
                                else
3327
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
3328

    
3329
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
3330
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
3331
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
3332
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
3333

    
3334
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
3335
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
3336
                                else
3337
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
3338

    
3339
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
3340
                                connection.ExecuteNonQuery(cmd, txn);
3341
                            }
3342

    
3343
                            //Equipment
3344
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
3345
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3346
                            foreach (DataRow row in item.Equipment.Rows)
3347
                            {
3348
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
3349
                                var cmd = connection.GetSqlStringCommand(query);
3350
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3351
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
3352

    
3353
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
3354
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
3355
                                else
3356
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
3357

    
3358
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
3359
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
3360
                                else
3361
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
3362

    
3363
                                connection.ExecuteNonQuery(cmd, txn);
3364
                            }
3365

    
3366
                            // TopologySet
3367
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
3368
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3369
                            foreach (DataRow row in item.TopologySet.Rows)
3370
                            {
3371
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
3372
                                var cmd = connection.GetSqlStringCommand(query);
3373
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3374
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
3375
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
3376
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
3377
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
3378
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
3379
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
3380
                                connection.ExecuteNonQuery(cmd, txn);
3381
                            }
3382

    
3383
                            // PSN
3384
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
3385
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3386
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
3387
                            {
3388
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
3389
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy, Pocket) VALUES " +
3390
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy, @Pocket)";
3391
                                var cmd = connection.GetSqlStringCommand(query);
3392
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3393
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
3394
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
3395
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
3396
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
3397
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
3398
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
3399
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
3400

    
3401
                                int IsValid = 0;
3402
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
3403
                                    IsValid = 0;
3404
                                else if (row["IsValid"].ToString() == "InValid")
3405
                                    IsValid = 1;
3406
                                else if (row["IsValid"].ToString() == "Error")
3407
                                    IsValid = -1;
3408

    
3409
                                AddWithValue(cmd, "@IsValid", IsValid);
3410
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
3411

    
3412
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
3413
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
3414

    
3415
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
3416
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
3417
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
3418
                                else
3419
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString().Replace("%", ""));
3420

    
3421
                                AddWithValue(cmd, "@Pocket", string.IsNullOrEmpty(row["Pocket"].ToString()) ? "Yes" : row["Pocket"].ToString());
3422
                                connection.ExecuteNonQuery(cmd, txn);
3423
                            }
3424

    
3425
                            //Pipeline
3426
                            query = $"DELETE FROM {PSN_PIPELINE}";
3427
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3428
                            foreach (DataRow row in item.PipeLine.Rows)
3429
                            {
3430
                                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)";
3431
                                var cmd = connection.GetSqlStringCommand(query);
3432
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3433
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
3434
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
3435
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
3436
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
3437
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
3438
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
3439
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
3440
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
3441
                                connection.ExecuteNonQuery(cmd, txn);
3442
                            }
3443

    
3444
                            //PipeSystem
3445
                            query = $"DELETE FROM {PSN_PIPESYSTEM}";
3446
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3447
                            foreach (DataRow row in item.PipeSystem.Rows)
3448
                            {
3449
                                query = $"INSERT INTO {PSN_PIPESYSTEM} (OID, DESCRIPTION, FLUID, PMC, PipeLineQty, GroundLevel) VALUES (@OID, @DESCRIPTION, @FLUID, @PMC, @PipeLineQty, @GroundLevel)";
3450
                                var cmd = connection.GetSqlStringCommand(query);
3451
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3452
                                AddWithValue(cmd, "@DESCRIPTION", row["DESCRIPTION"].ToString());
3453
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
3454
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
3455
                                AddWithValue(cmd, "@PipeLineQty", row["PipeLineQty"].ToString());
3456
                                AddWithValue(cmd, "@GroundLevel", row["GroundLevel"].ToString());
3457
                                connection.ExecuteNonQuery(cmd, txn);
3458
                            }
3459

    
3460
                            //Header Setting
3461
                            query = $"DELETE FROM {PSN_HEADER_SETTING}";
3462
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3463

    
3464
                            foreach (HeaderInfo headerInfo in headerInfos)
3465
                            {
3466
                                foreach (HeaderItem itemheader in headerInfo.HeaderItems)
3467
                                {
3468
                                    query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
3469
                                    var cmd = connection.GetSqlStringCommand(query);
3470
                                    AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
3471
                                    AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
3472
                                    AddWithValue(cmd, "@INDEX", itemheader.Index);
3473
                                    AddWithValue(cmd, "@NAME", itemheader.Name);
3474
                                    connection.ExecuteNonQuery(cmd, txn);
3475
                                }
3476
                            }
3477

    
3478
                            //Vent/Drain Setting
3479
                            query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
3480
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3481

    
3482
                            foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
3483
                            {
3484
                                foreach (VentDrainItem itemventdrain in ventDrainInfo.VentDrainItems)
3485
                                {
3486
                                    query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
3487
                                    var cmd = connection.GetSqlStringCommand(query);
3488
                                    AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
3489
                                    AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
3490
                                    AddWithValue(cmd, "@INDEX", itemventdrain.Index);
3491
                                    AddWithValue(cmd, "@NAME", itemventdrain.Name);
3492
                                    connection.ExecuteNonQuery(cmd, txn);
3493
                                }
3494
                            }
3495

    
3496
                            //Keyword Setting
3497
                            query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
3498
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3499

    
3500
                            foreach (KeywordItem itemKeyword in keywordItems)
3501
                            {
3502
                                query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
3503
                                var cmd = connection.GetSqlStringCommand(query);
3504
                                AddWithValue(cmd, "@INDEX", itemKeyword.Index);
3505
                                AddWithValue(cmd, "@NAME", itemKeyword.Name);
3506
                                AddWithValue(cmd, "@KEYWORD", itemKeyword.Keyword);
3507
                                connection.ExecuteNonQuery(cmd, txn);
3508
                            }
3509

    
3510
                            //FulidCode
3511
                            query = $"DELETE FROM {PSN_FLUIDCODE}";
3512
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3513

    
3514
                            foreach (DataRow row in dtFluidCode.Rows)
3515
                            {
3516
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
3517
                                var cmd = connection.GetSqlStringCommand(query);
3518
                                cmd.Parameters.Clear();
3519

    
3520
                                {
3521
                                    var param = cmd.CreateParameter();
3522
                                    param.ParameterName = "@UID";
3523
                                    param.Value = row["UID"].ToString();
3524
                                    cmd.Parameters.Add(param);
3525
                                }
3526

    
3527
                                {
3528
                                    var param = cmd.CreateParameter();
3529
                                    param.ParameterName = "@Code";
3530
                                    param.Value = row["Code"].ToString();
3531
                                    cmd.Parameters.Add(param);
3532
                                }
3533

    
3534
                                {
3535
                                    var param = cmd.CreateParameter();
3536
                                    param.ParameterName = "@Description";
3537
                                    param.Value = row["Description"].ToString();
3538
                                    cmd.Parameters.Add(param);
3539
                                }
3540

    
3541
                                {
3542
                                    var param = cmd.CreateParameter();
3543
                                    param.ParameterName = "@Condition";
3544
                                    param.Value = row["Condition"].ToString();
3545
                                    cmd.Parameters.Add(param);
3546
                                }
3547

    
3548
                                {
3549
                                    var param = cmd.CreateParameter();
3550
                                    param.ParameterName = "@Remarks";
3551
                                    param.Value = row["Remarks"].ToString();
3552
                                    cmd.Parameters.Add(param);
3553
                                }
3554

    
3555
                                {
3556
                                    var param = cmd.CreateParameter();
3557
                                    param.ParameterName = "@GroundLevel";
3558
                                    param.Value = row["GroundLevel"].ToString();
3559
                                    cmd.Parameters.Add(param);
3560
                                }
3561

    
3562
                                connection.ExecuteNonQuery(cmd, txn);
3563
                            }
3564

    
3565
                            //PMC
3566
                            query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
3567
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3568

    
3569
                            foreach (DataRow row in dtPMC.Rows)
3570
                            {
3571
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
3572
                                var cmd = connection.GetSqlStringCommand(query);
3573
                                cmd.Parameters.Clear();
3574

    
3575
                                {
3576
                                    var param = cmd.CreateParameter();
3577
                                    param.ParameterName = "@UID";
3578
                                    param.Value = row["UID"].ToString();
3579
                                    cmd.Parameters.Add(param);
3580
                                }
3581

    
3582
                                {
3583
                                    var param = cmd.CreateParameter();
3584
                                    param.ParameterName = "@Priority";
3585
                                    param.Value = row["Priority"].ToString();
3586
                                    cmd.Parameters.Add(param);
3587
                                }
3588

    
3589
                                {
3590
                                    var param = cmd.CreateParameter();
3591
                                    param.ParameterName = "@Code";
3592
                                    param.Value = row["Code"].ToString();
3593
                                    cmd.Parameters.Add(param);
3594
                                }
3595

    
3596
                                {
3597
                                    var param = cmd.CreateParameter();
3598
                                    param.ParameterName = "@Description";
3599
                                    param.Value = row["Description"].ToString();
3600
                                    cmd.Parameters.Add(param);
3601
                                }
3602

    
3603
                                {
3604
                                    var param = cmd.CreateParameter();
3605
                                    param.ParameterName = "@Condition";
3606
                                    param.Value = row["Condition"].ToString();
3607
                                    cmd.Parameters.Add(param);
3608
                                }
3609

    
3610
                                {
3611
                                    var param = cmd.CreateParameter();
3612
                                    param.ParameterName = "@Remarks";
3613
                                    param.Value = row["Remarks"].ToString();
3614
                                    cmd.Parameters.Add(param);
3615
                                }
3616

    
3617
                                {
3618
                                    var param = cmd.CreateParameter();
3619
                                    param.ParameterName = "@GroundLevel";
3620
                                    param.Value = row["GroundLevel"].ToString();
3621
                                    cmd.Parameters.Add(param);
3622
                                }
3623

    
3624
                                connection.ExecuteNonQuery(cmd, txn);
3625
                            }
3626

    
3627
                            //Insulation
3628
                            query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
3629
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3630

    
3631
                            foreach (DataRow row in dtInsulation.Rows)
3632
                            {
3633
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
3634
                                var cmd = connection.GetSqlStringCommand(query);
3635
                                cmd.Parameters.Clear();
3636

    
3637
                                {
3638
                                    var param = cmd.CreateParameter();
3639
                                    param.ParameterName = "@UID";
3640
                                    param.Value = row["UID"].ToString();
3641
                                    cmd.Parameters.Add(param);
3642
                                }
3643

    
3644
                                {
3645
                                    var param = cmd.CreateParameter();
3646
                                    param.ParameterName = "@Code";
3647
                                    param.Value = row["Code"].ToString();
3648
                                    cmd.Parameters.Add(param);
3649
                                }
3650

    
3651
                                {
3652
                                    var param = cmd.CreateParameter();
3653
                                    param.ParameterName = "@Description";
3654
                                    param.Value = row["Description"].ToString();
3655
                                    cmd.Parameters.Add(param);
3656
                                }
3657

    
3658
                                {
3659
                                    var param = cmd.CreateParameter();
3660
                                    param.ParameterName = "@Remarks";
3661
                                    param.Value = row["Remarks"].ToString();
3662
                                    cmd.Parameters.Add(param);
3663
                                }
3664

    
3665
                                connection.ExecuteNonQuery(cmd, txn);
3666
                            }
3667

    
3668
                            //Topology Rule
3669
                            query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
3670
                            var cmdtopology = connection.GetSqlStringCommand(query);
3671
                            //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3672
                            connection.ExecuteNonQuery(cmdtopology, txn);
3673

    
3674
                            foreach (DataRow row in dtTopologyRule.Rows)
3675
                            {
3676
                                query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
3677
                                cmdtopology = connection.GetSqlStringCommand(query);
3678
                                AddWithValue(cmdtopology, "@UID", row["UID"].ToString());
3679
                                connection.ExecuteNonQuery(cmdtopology, txn);
3680
                            }
3681

    
3682
                            //valve grouping
3683
                            query = $"DELETE FROM {PSN_VALVEGROUP_SETTING}";
3684
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3685

    
3686
                            foreach (DataRow row in dtvalvegrouping.Rows)
3687
                            {
3688
                                query = $"INSERT INTO {PSN_VALVEGROUP_SETTING} (OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName) " +
3689
                                    $"VALUES (@OID, @GroupType, @TagIdentifier, @AttributeName, @SppidSymbolName)";
3690
                                var cmd = connection.GetSqlStringCommand(query);
3691
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3692
                                AddWithValue(cmd, "@GroupType", row["GroupType"].ToString());
3693
                                AddWithValue(cmd, "@TagIdentifier", row["TagIdentifier"].ToString());
3694
                                AddWithValue(cmd, "@AttributeName", row["AttributeName"].ToString());
3695
                                AddWithValue(cmd, "@SppidSymbolName", row["SppidSymbolName"].ToString());
3696
                                connection.ExecuteNonQuery(cmd, txn);
3697
                            }
3698

    
3699
                            //no pocket Setting
3700
                            query = $"DELETE FROM {PSN_NOPOCKETSETTING}";
3701
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3702

    
3703
                            foreach (DataRow row in dtnopocket.Rows)
3704
                            {
3705
                                query = $"INSERT INTO {PSN_NOPOCKETSETTING} ([INDEX], [TYPE], [NAME]) VALUES (@INDEX, @TYPE, @NAME)";
3706
                                var cmd = connection.GetSqlStringCommand(query);
3707
                                AddWithValue(cmd, "@INDEX", row["INDEX"].ToString());
3708
                                AddWithValue(cmd, "@TYPE", row["TYPE"].ToString());
3709
                                AddWithValue(cmd, "@NAME", row["NAME"].ToString());
3710
                                connection.ExecuteNonQuery(cmd, txn);
3711
                            }
3712

    
3713
                            //air fin cooler Setting
3714
                            query = $"DELETE FROM {PSN_AIRFINCOOLERSETTING}";
3715
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3716

    
3717
                            foreach (DataRow row in dtnopocket.Rows)
3718
                            {
3719
                                query = $"INSERT INTO {PSN_AIRFINCOOLERSETTING} ([INDEX], [TYPE], [NAME]) VALUES (@INDEX, @TYPE, @NAME)";
3720
                                var cmd = connection.GetSqlStringCommand(query);
3721
                                AddWithValue(cmd, "@INDEX", row["INDEX"].ToString());
3722
                                AddWithValue(cmd, "@TYPE", row["TYPE"].ToString());
3723
                                AddWithValue(cmd, "@NAME", row["NAME"].ToString());
3724
                                connection.ExecuteNonQuery(cmd, txn);
3725
                            }
3726
                            
3727

    
3728
                            txn.Commit();
3729
                        }
3730
                        catch (Exception ex)
3731
                        {
3732
                            txn.Rollback();
3733
                            result = false;
3734
                        }
3735
                    }
3736
                }
3737
                catch (Exception ex)
3738
                {
3739
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3740
                    result = false;
3741
                }
3742
            }
3743

    
3744
            return result;
3745
        }
3746

    
3747
        public static bool CreatePSN_COMMON()
3748
        {
3749
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3750

    
3751
            bool result = true;
3752
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3753
            {
3754
                try
3755
                {
3756
                    using (var txn = connection.BeginTransaction())
3757
                    {
3758
                        try
3759
                        {
3760
                            
3761
                            var query = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
3762
                            if (id2Info.ID2DBType == AnotherID2DB_Type.MSSQL)
3763
                            {                                
3764
                                connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3765
                            }
3766

    
3767
                            txn.Commit();
3768
                        }
3769
                        catch (Exception ex)
3770
                        {
3771
                            txn.Rollback();
3772
                            result = false;
3773
                        }
3774
                    }
3775
                }
3776
                catch (Exception ex)
3777
                {
3778
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3779
                    result = false;
3780
                }
3781
            }
3782

    
3783
            return result;
3784
        }
3785
    }
3786
}
3787

    
3788

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