프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ 284fa2c9

이력 | 보기 | 이력해설 | 다운로드 (190 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
                    dicColCheck.Add("EGTag", "NVARCHAR(255)");
181
                    dicColCheck.Add("HasMLTags", "NVARCHAR(50)");
182

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

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

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

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

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

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

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

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

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

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

    
333
                    if (matched == null)
334
                    {
335
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
336
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
337
                            "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), " +
338
                            "ViewPipeSystemNetwork_OID NVARCHAR(255), EqpGroupTag NVARCHAR(255), MainLineTag NVARCHAR(255), EGTConnectedPoint INT, EGFlowDirection NVARCHAR(10))";
339
                        using (var cmd = connection.GetSqlStringCommand(query))
340
                        {
341
                            cmd.ExecuteNonQuery();
342
                        }
343
                    }
344
                    else
345
                    {
346
                        AddColumn(PSN_PATHITEMS, dicColCheck);
347
                    }
348

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

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

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

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

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

    
427
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
428
                    dicColCheck.Clear();
429
                    dicColCheck.Add("UID", "NVARCHAR(50)");
430
                    dicColCheck.Add("Code", "NVARCHAR(255)");
431
                    dicColCheck.Add("Description", "NVARCHAR(255)");
432
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
433
                    if (matched == null)
434
                    {
435
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
436
                        using (var cmd = connection.GetSqlStringCommand(query))
437
                        {
438
                            cmd.ExecuteNonQuery();
439
                        }
440
                    }
441
                    else
442
                    {
443
                        AddColumn(PSN_INSULATIONPURPOSE, dicColCheck);
444
                    }
445

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

    
466
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEM);
467
                    dicColCheck.Clear();
468
                    dicColCheck.Add("OID", "NVARCHAR(255)");
469
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
470
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
471
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
472
                    dicColCheck.Add("PipeLineQty", "NVARCHAR(255)");
473
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
474
                    if (matched == null)
475
                    {
476
                        var query = $"CREATE TABLE {PSN_PIPESYSTEM} (OID NVARCHAR(255), DESCRIPTION NVARCHAR(255), FLUID NVARCHAR(255), PMC NVARCHAR(255), PipeLineQty NVARCHAR(255), GroundLevel NVARCHAR(20))";
477
                        using (var cmd = connection.GetSqlStringCommand(query))
478
                        {
479
                            cmd.ExecuteNonQuery();
480
                        }
481
                    }
482
                    else
483
                    {
484
                        AddColumn(PSN_PIPESYSTEM, dicColCheck);
485
                    }
486

    
487
                    matched = names.FirstOrDefault(param => param == PSN_NOPOCKETSETTING);
488
                    dicColCheck.Clear();
489
                    dicColCheck.Add("INDEX", "INTEGER");
490
                    dicColCheck.Add("TYPE", "TEXT");
491
                    dicColCheck.Add("NAME", "TEXT");
492
                    if (matched == null)
493
                    {
494
                        var query = $"CREATE TABLE {PSN_NOPOCKETSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [NAME] TEXT)";
495
                        using (var cmd = connection.GetSqlStringCommand(query))
496
                        {
497
                            cmd.ExecuteNonQuery();
498
                        }
499
                    }
500
                    else
501
                    {
502
                        AddColumn(PSN_NOPOCKETSETTING, dicColCheck);
503
                    }
504

    
505
                    matched = names.FirstOrDefault(param => param == PSN_AIRFINCOOLERSETTING);
506

    
507
                    dicColCheck.Clear();
508
                    dicColCheck.Add("INDEX", "INTEGER");
509
                    dicColCheck.Add("TYPE", "TEXT");
510
                    dicColCheck.Add("TagIdentifier", "TEXT");
511
                    dicColCheck.Add("AttributeName", "TEXT");
512
                    dicColCheck.Add("NAME", "TEXT");
513

    
514
                    if (matched == null)
515
                    {
516
                        var query = $"CREATE TABLE {PSN_AIRFINCOOLERSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [TagIdentifier] TEXT, [AttributeName] TEXT, [NAME] TEXT)";
517
                        using (var cmd = connection.GetSqlStringCommand(query))
518
                        {
519
                            cmd.ExecuteNonQuery();
520
                        }
521
                    }
522
                    else
523
                    {
524
                        AddColumn(PSN_AIRFINCOOLERSETTING, dicColCheck);
525
                    }
526

    
527

    
528
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
529
                    {
530
                        var colnames = connection.GetColumnNames(TableName);
531
                        bool check = false;
532
                        if (colnames != null)
533
                        {
534
                            foreach (KeyValuePair<string, string> col in dicCol)
535
                            {
536
                                check = false;
537
                                foreach (string c in colnames)
538
                                {
539
                                    if (col.Key.Contains(c))
540
                                    {
541
                                        check = true;
542
                                        break;
543
                                    }
544
                                }
545

    
546
                                if (!check) //없으면 추가
547
                                {
548
                                    string i = string.Empty;
549
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
550
                                        i = "DEFAULT 0";
551

    
552
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
553
                                    using (var cmd = connection.GetSqlStringCommand(query))
554
                                    {
555
                                        cmd.ExecuteNonQuery();
556
                                    }
557
                                }
558
                            }
559
                        }
560
                    }
561

    
562
                    result = true;
563
                }
564
                catch (Exception ex)
565
                {
566
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
567
                }
568
            }
569

    
570
            return result;
571
        }
572

    
573
        // ID2 DB 데이터
574
        /// <summary>
575
        /// ID2 데이타베이스에서 OPC 데이터를 조회
576
        /// </summary>
577
        /// <returns></returns>
578
        public static DataTable SelectOPCRelations()
579
        {
580
            DataTable dt = null;
581
            ID2Info id2Info = ID2Info.GetInstance();
582

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

    
599
            return dt;
600
        }
601

    
602
        /// <summary>
603
        /// ID2 데이타베이스에서 도면 데이터를 조회
604
        /// </summary>
605
        /// <returns></returns>
606
        public static DataTable SelectDrawings()
607
        {
608
            DataTable dt = null;
609
            ID2Info id2Info = ID2Info.GetInstance();
610

    
611
            using (IAbstractDatabase connection = id2Info.CreateConnection())
612
            {
613
                try
614
                {
615
                    var query = "SELECT DISTINCT NAME FROM [Drawings] order by Name";
616
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
617
                    {
618
                        dt = ds.Tables[0].Copy();
619
                    }
620
                }
621
                catch (Exception ex)
622
                {
623
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
624
                }
625
            }
626

    
627
            return dt;
628
        }
629

    
630
        public static DataTable AllDrawings()
631
        {
632
            DataTable dt = null;
633
            ID2Info id2Info = ID2Info.GetInstance();
634

    
635
            using (IAbstractDatabase connection = id2Info.CreateConnection())
636
            {
637
                try
638
                {
639
                    var query = "SELECT * FROM [Drawings]";
640
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
641
                    {
642
                        dt = ds.Tables[0].Copy();
643
                    }
644
                }
645
                catch (Exception ex)
646
                {
647
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
648
                }
649
            }
650

    
651
            return dt;
652
        }
653

    
654
        public static DataTable SelectLineProperties()
655
        {
656
            DataTable dt = null;
657
            ID2Info id2Info = ID2Info.GetInstance();
658

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

    
675
            return dt;
676
        }
677

    
678
        public static DataTable SelectFluidCode()
679
        {
680
            DataTable dt = null;
681
            ID2Info id2Info = ID2Info.GetInstance();
682

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

    
699
            return dt;
700
        }
701

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

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

    
723
            return dt;
724
        }
725

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

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

    
747
            return dt;
748
        }
749

    
750
        public static DataTable SelectInsulationPurpose()
751
        {
752
            DataTable dt = null;
753
            ID2Info id2Info = ID2Info.GetInstance();
754

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

    
771
            return dt;
772
        }
773

    
774
        public static DataTable SelectPSNINSULATIONPURPOSE()
775
        {
776
            DataTable dt = null;
777
            ID2Info id2Info = ID2Info.GetInstance();
778

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

    
795
            return dt;
796
        }
797

    
798
        public static DataTable SelectNominalDiameter()
799
        {
800
            DataTable dt = null;
801
            ID2Info id2Info = ID2Info.GetInstance();
802

    
803
            using (IAbstractDatabase connection = id2Info.CreateConnection())
804
            {
805
                try
806
                {
807
                    var query = "SELECT * FROM NominalDiameter ORDER BY Metric DESC";
808
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
809
                    {
810
                        dt = ds.Tables[0].Copy();
811
                    }
812
                }
813
                catch (Exception ex)
814
                {
815
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
816
                }
817
            }
818

    
819
            ///TODO: need to check below code
820
            dt.Rows.RemoveAt(0);
821
            dt.Rows.RemoveAt(0);
822
            dt.Rows.RemoveAt(0);
823
            dt.Rows.RemoveAt(0);
824

    
825
            return dt;
826
        }
827

    
828
        public static DataTable SelectAllSymbolAttribute()
829
        {
830
            DataTable dt = null;
831
            ID2Info id2Info = ID2Info.GetInstance();
832

    
833
            using (IAbstractDatabase connection = id2Info.CreateConnection())
834
            {
835
                try
836
                {
837
                    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; ";
838
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
839
                    {
840
                        dt = ds.Tables[0].Copy();
841
                    }
842
                }
843
                catch (Exception ex)
844
                {
845
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
846
                }
847
            }
848

    
849
            return dt;
850
        }
851

    
852
        public static DataTable SelectSymbolAttribute()
853
        {
854
            DataTable dt = null;
855
            ID2Info id2Info = ID2Info.GetInstance();
856

    
857
            using (IAbstractDatabase connection = id2Info.CreateConnection())
858
            {
859
                try
860
                {
861
                    // var query = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
862
                    var query = "SELECT DISTINCT Attribute FROM SymbolAttribute;";
863
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
864
                    {
865
                        dt = ds.Tables[0].Copy();
866
                    }
867
                }
868
                catch (Exception ex)
869
                {
870
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
871
                }
872
            }
873

    
874
            return dt;
875
        }
876

    
877
        public static DataTable SelectSymbolName()
878
        {
879
            DataTable dt = null;
880
            ID2Info id2Info = ID2Info.GetInstance();
881

    
882
            using (IAbstractDatabase connection = id2Info.CreateConnection())
883
            {
884
                try
885
                {
886
                    var query = "SELECT * FROM SymbolName;";
887
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
888
                    {
889
                        dt = ds.Tables[0].Copy();
890
                    }
891
                }
892
                catch (Exception ex)
893
                {
894
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
895
                }
896
            }
897

    
898
            return dt;
899
        }
900

    
901
        public static double[] GetDrawingSize()
902
        {
903
            double[] result = null;
904

    
905
            ID2Info id2Info = ID2Info.GetInstance();
906
            using (IAbstractDatabase connection = id2Info.CreateConnection())
907
            {
908
                try
909
                {
910
                    var query = "SELECT value FROM Configuration WHERE Section = 'Area' AND [Key] = 'Drawing';";
911
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
912
                    {
913
                        if (ds.Tables[0].Rows.Count == 1)
914
                        {
915
                            string value = ds.Tables[0].Rows[0][0].ToString();
916
                            string[] split = value.Split(new char[] { ',' });
917
                            result = new double[] {
918
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", string.Empty)),
919
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", string.Empty)),
920
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", string.Empty)),
921
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", string.Empty))
922
                                };
923
                            result = new double[] {
924
                                Math.Min(result[0], result[2]),
925
                                Math.Min(result[1], result[3]),
926
                                Math.Max(result[0], result[2]),
927
                                Math.Max(result[1], result[3])
928
                                };
929
                        }
930
                    }
931
                }
932
                catch (Exception ex)
933
                {
934
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
935
                }
936
            }
937

    
938
            return result;
939
        }
940

    
941
        public static DataTable GetEquipmentType()
942
        {
943
            DataTable dt = null;
944
            ID2Info id2Info = ID2Info.GetInstance();
945

    
946
            using (IAbstractDatabase connection = id2Info.CreateConnection())
947
            {
948
                try
949
                {
950
                    var query = "SELECT Type FROM SymbolType WHERE Category = 'Equipment';";
951
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
952
                    {
953
                        dt = ds.Tables[0].Copy();
954
                    }
955
                }
956
                catch (Exception ex)
957
                {
958
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
959
                }
960
            }
961

    
962
            return dt;
963
        }
964

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

    
976
            bool result = true;
977

    
978
            using (IAbstractDatabase connection = id2Info.CreateConnection())
979
            {
980
                try
981
                {
982
                    using (var txn = connection.BeginTransaction())
983
                    {
984
                        try
985
                        {
986
                            var query = $"DELETE FROM {PSN_VIEW}";
987
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
988

    
989
                            foreach (string value in values)
990
                            {
991
                                query = $"INSERT INTO {PSN_VIEW} (OID) VALUES (@OID)";
992
                                var cmd = connection.GetSqlStringCommand(query);
993
                                AddWithValue(cmd, "@OID", value);
994
                                connection.ExecuteNonQuery(cmd, txn);
995
                            }
996
                            txn.Commit();
997
                        }
998
                        catch (Exception ex)
999
                        {
1000
                            txn.Rollback();
1001
                            result = false;
1002
                        }
1003
                    }
1004
                }
1005
                catch (Exception ex)
1006
                {
1007
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1008
                    result = false;
1009
                }
1010
            }
1011

    
1012
            return result;
1013
        }
1014

    
1015
        public static bool DeleteView()
1016
        {
1017
            ID2Info id2Info = ID2Info.GetInstance();
1018

    
1019
            bool result = true;
1020
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1021
            {
1022
                try
1023
                {
1024
                    using (var txn = connection.BeginTransaction())
1025
                    {
1026
                        try
1027
                        {
1028
                            var query = $"DELETE FROM {PSN_VIEW}";
1029
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1030
                            txn.Commit();
1031
                        }
1032
                        catch (Exception ex)
1033
                        {
1034
                            txn.Rollback();
1035
                            result = false;
1036
                        }
1037
                    }
1038
                }
1039
                catch (Exception ex)
1040
                {
1041
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1042
                    result = false;
1043
                }
1044
            }
1045

    
1046
            return result;
1047
        }
1048

    
1049
        //PSN Sqlite 
1050
        public static DataTable SelectHeaderSetting()
1051
        {
1052
            DataTable dt = null;
1053
            ID2Info id2Info = ID2Info.GetInstance();
1054

    
1055
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1056
            {
1057
                try
1058
                {
1059
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_HEADER_SETTING};";
1060
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1061
                    {
1062
                        dt = ds.Tables[0].Copy();
1063
                    }
1064
                }
1065
                catch (Exception ex)
1066
                {
1067
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1068
                }
1069
            }
1070

    
1071
            return dt;
1072
        }
1073

    
1074
        public static DataTable SelectVentDrainSetting()
1075
        {
1076
            DataTable dt = null;
1077
            ID2Info id2Info = ID2Info.GetInstance();
1078

    
1079
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1080
            {
1081
                try
1082
                {
1083
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_VENTDRAIN_SETTING};";
1084
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1085
                    {
1086
                        dt = ds.Tables[0].Copy();
1087
                    }
1088
                }
1089
                catch (Exception ex)
1090
                {
1091
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1092
                }
1093
            }
1094

    
1095
            return dt;
1096
        }
1097

    
1098
        public static DataTable SelectKeywordsSetting()
1099
        {
1100
            DataTable dt = null;
1101
            ID2Info id2Info = ID2Info.GetInstance();
1102

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

    
1119
            return dt;
1120
        }
1121

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

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

    
1143
            return dt;
1144
        }
1145

    
1146

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

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

    
1168
            return dt;
1169
        }
1170

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

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

    
1192
            return dt;
1193
        }
1194

    
1195
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
1196
        {
1197
            ID2Info id2Info = ID2Info.GetInstance();
1198
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1199
            {
1200
                try
1201
                {
1202
                    using (var txn = connection.BeginTransaction())
1203
                    {
1204
                        var query = $"DELETE FROM {PSN_HEADER_SETTING}";
1205
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1206

    
1207
                        foreach (HeaderInfo headerInfo in headerInfos)
1208
                        {
1209
                            foreach (HeaderItem item in headerInfo.HeaderItems)
1210
                            {
1211
                                query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
1212
                                var cmd = connection.GetSqlStringCommand(query);
1213
                                AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
1214
                                AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
1215
                                AddWithValue(cmd, "@INDEX", item.Index);
1216
                                AddWithValue(cmd, "@NAME", item.Name);
1217
                                connection.ExecuteNonQuery(cmd, txn);
1218
                            }
1219
                        }
1220
                        txn.Commit();
1221
                    }
1222

    
1223
                }
1224
                catch (Exception ex)
1225
                {
1226
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1227
                    return false;
1228
                }
1229
            }
1230
            return true;
1231
        }
1232

    
1233
        public static bool SaveVentDrainSetting(List<VentDrainInfo> ventDrainInfos)
1234
        {
1235
            ID2Info id2Info = ID2Info.GetInstance();
1236
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1237
            {
1238
                using (var txn = connection.BeginTransaction())
1239
                {
1240
                    try
1241
                    {
1242
                        var query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
1243
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1244

    
1245
                        foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
1246
                        {
1247
                            foreach (VentDrainItem item in ventDrainInfo.VentDrainItems)
1248
                            {
1249
                                query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
1250
                                var cmd = connection.GetSqlStringCommand(query);
1251
                                AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
1252
                                AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
1253
                                AddWithValue(cmd, "@INDEX", item.Index);
1254
                                AddWithValue(cmd, "@NAME", item.Name);
1255
                                connection.ExecuteNonQuery(cmd, txn);
1256
                            }
1257
                        }
1258

    
1259
                        txn.Commit();
1260
                    }
1261
                    catch (Exception ex)
1262
                    {
1263
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1264
                        return false;
1265
                    }
1266
                }
1267
            }
1268

    
1269
            return true;
1270
        }
1271

    
1272
        public static bool SaveValveGroupItemsSetting(List<ValveGroupItem> valveGroupItems)
1273
        {
1274
            ID2Info id2Info = ID2Info.GetInstance();
1275
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1276
            {
1277
                using (var txn = connection.BeginTransaction())
1278
                {
1279
                    try
1280
                    {
1281
                        var query = $"DELETE FROM {PSN_VALVEGROUP_SETTING}";
1282
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1283

    
1284
                        foreach (ValveGroupItem item in valveGroupItems)
1285
                        {
1286
                            query = $"INSERT INTO {PSN_VALVEGROUP_SETTING} (OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName) " +
1287
                                $"VALUES (@OID, @GroupType, @TagIdentifier, @AttributeName, @SppidSymbolName)";
1288
                            var cmd = connection.GetSqlStringCommand(query);
1289
                            AddWithValue(cmd, "@OID", item.OID);
1290
                            AddWithValue(cmd, "@GroupType", item.GroupType);
1291
                            AddWithValue(cmd, "@TagIdentifier", item.TagIdentifier);
1292
                            AddWithValue(cmd, "@AttributeName", item.AttributeName);
1293
                            AddWithValue(cmd, "@SppidSymbolName", item.SppidSymbolName);
1294
                            connection.ExecuteNonQuery(cmd, txn);
1295
                        }
1296

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

    
1307
            return true;
1308
        }
1309

    
1310
        public static bool SaveKeywordsSetting(List<KeywordItem> keywordItems)
1311
        {
1312
            ID2Info id2Info = ID2Info.GetInstance();
1313
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1314
            {
1315
                using (var txn = connection.BeginTransaction())
1316
                {
1317
                    try
1318
                    {
1319
                        var query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
1320
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1321

    
1322
                        foreach (KeywordItem item in keywordItems)
1323
                        {
1324
                            query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
1325
                            var cmd = connection.GetSqlStringCommand(query);
1326
                            AddWithValue(cmd, "@INDEX", item.Index);
1327
                            AddWithValue(cmd, "@NAME", item.Name);
1328
                            AddWithValue(cmd, "@KEYWORD", item.Keyword);
1329
                            connection.ExecuteNonQuery(cmd, txn);
1330
                        }
1331

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

    
1342
            return true;
1343
        }
1344

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

    
1357
                        foreach (EquipmentNoPocketItem item in keywordItems)
1358
                        {
1359
                            query = $"INSERT INTO {PSN_NOPOCKETSETTING} ([INDEX], [TYPE], [NAME]) VALUES (@INDEX, @TYPE, @NAME)";
1360
                            var cmd = connection.GetSqlStringCommand(query);
1361
                            AddWithValue(cmd, "@INDEX", item.Index);
1362
                            AddWithValue(cmd, "@TYPE", item.Type);
1363
                            AddWithValue(cmd, "@NAME", item.Name);
1364
                            connection.ExecuteNonQuery(cmd, txn);
1365
                        }
1366

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

    
1377
            return true;
1378
        }
1379

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

    
1392
                        foreach (EquipmentAirFinCoolerItem item in keywordItems)
1393
                        {
1394
                            query = $"INSERT INTO {PSN_AIRFINCOOLERSETTING} ([INDEX], [TYPE], [TagIdentifier], [AttributeName], [NAME]) VALUES (@INDEX, @TYPE, @TagIdentifier, @AttributeName, @NAME)";
1395
                            var cmd = connection.GetSqlStringCommand(query);
1396
                            AddWithValue(cmd, "@INDEX", item.Index);
1397
                            AddWithValue(cmd, "@TYPE", item.Type);
1398
                            AddWithValue(cmd, "@TagIdentifier", item.TagIdentifier);
1399
                            AddWithValue(cmd, "@AttributeName", item.AttributeName);
1400
                            AddWithValue(cmd, "@NAME", item.Name);
1401
                            connection.ExecuteNonQuery(cmd, txn);
1402

    
1403
                        }
1404

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

    
1415
            return true;
1416
        }
1417

    
1418
        public static bool SaveTopologyRule(DataTable dt)
1419
        {
1420
            ID2Info id2Info = ID2Info.GetInstance();
1421
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1422
            {
1423
                using (var txn = connection.BeginTransaction())
1424
                {
1425
                    try
1426
                    {
1427
                        var query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
1428
                        var cmd = connection.GetSqlStringCommand(query);
1429
                        //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1430
                        connection.ExecuteNonQuery(cmd, txn);
1431

    
1432
                        foreach (DataRow row in dt.Rows)
1433
                        {
1434
                            query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
1435
                            cmd = connection.GetSqlStringCommand(query);
1436
                            AddWithValue(cmd, "@UID", row["NAME"].ToString());
1437
                            connection.ExecuteNonQuery(cmd, txn);
1438
                        }
1439

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

    
1451
            return true;
1452
        }
1453

    
1454
        public static DataTable SelectTopologyRule()
1455
        {
1456
            DataTable dt = null;
1457

    
1458
            ID2Info id2Info = ID2Info.GetInstance();
1459
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1460
            {
1461
                try
1462
                {
1463
                    var query = $"SELECT * FROM {PSN_TOPOLOGY_RULE}";
1464
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1465
                    {
1466
                        dt = ds.Tables[0].Copy();
1467
                    }
1468
                }
1469
                catch (Exception ex)
1470
                {
1471
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1472
                }
1473
            }
1474

    
1475
            return dt;
1476
        }
1477

    
1478
        private static void AddWithValue(DbCommand cmd, string PropName, object Value)
1479
        {
1480
            var param = cmd.CreateParameter();
1481
            param.ParameterName = PropName;
1482
            param.Value = Value;
1483
            cmd.Parameters.Add(param);
1484
        }
1485

    
1486
        public static DataTable SelectRevisionTable()
1487
        {
1488
            DataTable dt = null;
1489
            ID2Info id2Info = ID2Info.GetInstance();
1490

    
1491
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1492
            {
1493
                try
1494
                {
1495
                    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'";
1496
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1497
                    {
1498
                        dt = ds.Tables[0].Copy();
1499
                    }
1500
                }
1501
                catch (Exception ex)
1502
                {
1503
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1504
                }
1505
            }
1506

    
1507
            return dt;
1508
        }
1509

    
1510
        public static DataTable SelectRevision()
1511
        {
1512
            DataTable dt = null;
1513
            ID2Info id2Info = ID2Info.GetInstance();
1514

    
1515
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1516
            {
1517
                try
1518
                {
1519
                    var query = $"SELECT * FROM ARS_COMMON.dbo.PSNREVISION";
1520

    
1521
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1522
                    {
1523
                        dt = ds.Tables[0].Copy();
1524
                    }
1525
                }
1526
                catch (Exception ex)
1527
                {
1528
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1529
                }
1530
            }
1531

    
1532
            return dt;
1533
        }
1534

    
1535
        public static bool ConnCreateRevisionTable(Dictionary<string, string> dicColCheck, List<string> names)
1536
        {
1537
            bool result = false;
1538
            ID2Info id2Info = ID2Info.GetInstance();
1539

    
1540
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1541
            {
1542
                try
1543
                {
1544
                    if (names.Count == 0)
1545
                    {
1546

    
1547
                        var query = $"CREATE TABLE ARS_COMMON.dbo.PSNRevision ([OID] [uniqueidentifier] ,[Project_OID] [uniqueidentifier] ," +
1548
                            $"[ProjectCode] nvarchar(255), [RevNumber] int ,[UserName] nvarchar(255) ,[TimeData] nvarchar(255) ," +
1549
                            $"[PSNDatabasePath] nvarchar(255) ,	[PsnByPBSFilter] varchar(20) ,[PsnByNPDFilter] varchar(20) ,[PidDrawings] int ,	" +
1550
                            $"[PipeSystems] int ,[PipeLines] int ,[Topologies] int ,[PipeSystemNetworks] int ,[InValidPSNs] int ,[E2E] int ," +
1551
                            $"[E2B] int ,[B2E] int ,[HDE] int ,[HD2] int ,[HDB] int ,[B2B] int, [LastModificationdate] nvarchar(255), [ID2_PSN] nvarchar(10))";
1552
                        using (var cmd = connection.GetSqlStringCommand(query))
1553
                        {
1554
                            cmd.ExecuteNonQuery();
1555
                        }
1556
                    }
1557
                    else
1558
                    {
1559
                        AddColumn(PSN_REVISION, dicColCheck, names);
1560
                    }
1561

    
1562
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
1563
                    {
1564
                        bool check = false;
1565
                        if (colnames != null)
1566
                        {
1567
                            foreach (KeyValuePair<string, string> col in dicCol)
1568
                            {
1569
                                check = false;
1570
                                foreach (string c in colnames)
1571
                                {
1572
                                    if (col.Key.Contains(c))
1573
                                    {
1574
                                        check = true;
1575
                                        break;
1576
                                    }
1577
                                }
1578

    
1579
                                if (!check) //없으면 추가
1580
                                {
1581
                                    string i = string.Empty;
1582
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
1583
                                        i = "DEFAULT 0";
1584

    
1585
                                    var queryalter = $"ALTER TABLE ARS_COMMON.dbo.PSNRevision ADD [" + col.Key + "] " + col.Value + " " + i + ";";
1586
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
1587
                                    {
1588
                                        cmd.ExecuteNonQuery();
1589
                                    }
1590
                                }
1591
                            }
1592
                        }
1593
                    }
1594

    
1595
                    result = true;
1596
                }
1597
                catch (Exception ex)
1598
                {
1599
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1600
                }
1601
            }
1602

    
1603
            return result;
1604
        }
1605

    
1606
        public static bool UpdateRevision(string TimeData, string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
1607
           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)
1608
        {
1609
            ID2Info id2Info = ID2Info.GetInstance();
1610
            bool result = true;
1611

    
1612
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1613
            {
1614
                try
1615
                {
1616
                    using (var txn = connection.BeginTransaction())
1617
                    {
1618
                        try
1619
                        {
1620
                            string where = string.Format("DELETE FROM ARS_COMMON.dbo.PSNRevision WHERE ProjectCode = '{0}' AND PSNDatabasePath = '{1}' AND RevNumber = {2}", ProjectCode, PSNDatabasePath, RevNumber);
1621
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(where), txn);
1622
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
1623
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
1624
                                $"HD2, HDB, B2B, LastModificationdate, ID2_PSN) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
1625
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
1626
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B, @LastModificationdate, @ID2_PSN)";
1627
                            var cmd = connection.GetSqlStringCommand(query);
1628

    
1629
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
1630
                            AddWithValue(cmd, "@RevNumber", RevNumber);
1631
                            AddWithValue(cmd, "@UserName", UserName);
1632
                            AddWithValue(cmd, "@TimeData", TimeData); //String.Format("{0:G}", dt) // DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
1633

    
1634
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
1635

    
1636
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
1637
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
1638
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
1639
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
1640
                            AddWithValue(cmd, "@PipeLines", PipeLines);
1641

    
1642
                            AddWithValue(cmd, "@Topologies", Topologies);
1643
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
1644
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
1645
                            AddWithValue(cmd, "@E2E", E2E);
1646
                            AddWithValue(cmd, "@E2B", E2B);
1647
                            AddWithValue(cmd, "@B2E", B2E);
1648
                            AddWithValue(cmd, "@HDE", HDE);
1649
                            AddWithValue(cmd, "@HD2", HD2);
1650
                            AddWithValue(cmd, "@HDB", HDB);
1651
                            AddWithValue(cmd, "@B2B", B2B);
1652
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
1653
                            DateTime oDateTime = DateTime.Now;
1654
                            AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
1655
                            AddWithValue(cmd, "@ID2_PSN", "Y");
1656
                            connection.ExecuteNonQuery(cmd, txn);
1657

    
1658
                            txn.Commit();
1659
                        }
1660
                        catch (Exception ex)
1661
                        {
1662
                            txn.Rollback();
1663
                            result = false;
1664
                        }
1665
                    }
1666
                }
1667
                catch (Exception ex)
1668
                {
1669
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1670
                    result = false;
1671
                }
1672
            }
1673

    
1674
            return result;
1675
        }
1676

    
1677

    
1678
        public static bool SaveRevision(string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
1679
            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)
1680
        {
1681
            ID2Info id2Info = ID2Info.GetInstance();
1682

    
1683
            bool result = true;
1684

    
1685
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1686
            {
1687
                try
1688
                {
1689
                    using (var txn = connection.BeginTransaction())
1690
                    {
1691
                        try
1692
                        {
1693

    
1694
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
1695
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
1696
                                $"HD2, HDB, B2B, LastModificationdate, ID2_PSN) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
1697
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
1698
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B, @LastModificationdate, @ID2_PSN)";
1699
                            var cmd = connection.GetSqlStringCommand(query);
1700
                            AddWithValue(cmd, "@OID", Guid.NewGuid().ToString());
1701
                            AddWithValue(cmd, "@Project_OID", Guid.NewGuid().ToString());
1702
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
1703
                            AddWithValue(cmd, "@RevNumber", RevNumber);
1704
                            AddWithValue(cmd, "@UserName", UserName);
1705

    
1706
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
1707
                            DateTime oDateTime = DateTime.Now;
1708

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

    
1711
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
1712

    
1713
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
1714
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
1715
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
1716
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
1717
                            AddWithValue(cmd, "@PipeLines", PipeLines);
1718

    
1719
                            AddWithValue(cmd, "@Topologies", Topologies);
1720
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
1721
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
1722
                            AddWithValue(cmd, "@E2E", E2E);
1723
                            AddWithValue(cmd, "@E2B", E2B);
1724
                            AddWithValue(cmd, "@B2E", B2E);
1725
                            AddWithValue(cmd, "@HDE", HDE);
1726
                            AddWithValue(cmd, "@HD2", HD2);
1727
                            AddWithValue(cmd, "@HDB", HDB);
1728
                            AddWithValue(cmd, "@B2B", B2B);
1729
                            // AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
1730
                            AddWithValue(cmd, "@LastModificationdate", "");
1731
                            AddWithValue(cmd, "@ID2_PSN", "Y");
1732

    
1733
                            connection.ExecuteNonQuery(cmd, txn);
1734

    
1735
                            txn.Commit();
1736
                        }
1737
                        catch (Exception ex)
1738
                        {
1739
                            txn.Rollback();
1740
                            result = false;
1741
                        }
1742
                    }
1743
                }
1744
                catch (Exception ex)
1745
                {
1746
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1747
                    result = false;
1748
                }
1749
            }
1750

    
1751
            return result;
1752
        }
1753

    
1754
        public static bool SavePSNData(PSN item)
1755
        {
1756
            ID2Info id2Info = ID2Info.GetInstance();
1757

    
1758
            bool result = true;
1759

    
1760
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1761
            {
1762
                try
1763
                {
1764
                    using (var txn = connection.BeginTransaction())
1765
                    {
1766
                        try
1767
                        {
1768
                            // Path Items
1769
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
1770
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1771
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
1772
                            {
1773
                                DataRow row = item.PathItems.Rows[i];
1774
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
1775
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, " +
1776
                                    $"PIDNAME, Equipment_OID, NPD, GROUPTAG, PipeSystemNetwork_OID, PipeRun_OID, ViewPipeSystemNetwork_OID, EqpGroupTag, MainLineTag, EGTConnectedPoint, EGFlowDirection) VALUES " +
1777
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME, " +
1778
                                    $"@Equipment_OID, @NPD, @GROUPTAG, @PipeSystemNetwork_OID,@PipeRun_OID, @ViewPipeSystemNetwork_OID, @EqpGroupTag , @MainLineTag, @EGTConnectedPoint, @EGFlowDirection)";
1779
                                var cmd = connection.GetSqlStringCommand(query);
1780
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1781
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
1782
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
1783
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
1784
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
1785
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
1786
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1787
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString());
1788
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
1789
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
1790
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
1791
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
1792
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1793
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1794
                                AddWithValue(cmd, "@GROUPTAG", string.IsNullOrEmpty(row["GROUPTAG"].ToString()) ? "" : row["GROUPTAG"].ToString());
1795
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
1796
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
1797
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
1798
                                AddWithValue(cmd, "@EqpGroupTag", string.IsNullOrEmpty(row["EqpGroupTag"].ToString()) ? "" : row["EqpGroupTag"].ToString());
1799
                                AddWithValue(cmd, "@MainLineTag", string.IsNullOrEmpty(row["MainLineTag"].ToString()) ? "" : row["MainLineTag"].ToString());
1800

    
1801
                                AddWithValue(cmd, "@EGTConnectedPoint", string.IsNullOrEmpty(row["EGTConnectedPoint"].ToString()) ? "0" : row["EGTConnectedPoint"].ToString());
1802
                                AddWithValue(cmd, "@EGFlowDirection", string.IsNullOrEmpty(row["EGFlowDirection"].ToString()) ? "" : row["EGFlowDirection"].ToString());
1803
                                connection.ExecuteNonQuery(cmd, txn);
1804
                            }
1805

    
1806
                            // Sequence
1807
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
1808
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1809
                            foreach (DataRow row in item.SequenceData.Rows)
1810
                            {
1811
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
1812
                                var cmd = connection.GetSqlStringCommand(query);
1813
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1814
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
1815
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
1816
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1817
                                connection.ExecuteNonQuery(cmd, txn);
1818
                            }
1819

    
1820
                            // Nozzle
1821
                            query = $"DELETE FROM {PSN_NOZZLE}";
1822
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1823
                            foreach (DataRow row in item.Nozzle.Rows)
1824
                            {
1825
                                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)";
1826
                                var cmd = connection.GetSqlStringCommand(query);
1827
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1828
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1829

    
1830
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1831
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1832
                                else
1833
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1834

    
1835
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1836
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1837
                                else
1838
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1839

    
1840
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1841
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
1842
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1843
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
1844

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

    
1850
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
1851
                                connection.ExecuteNonQuery(cmd, txn);
1852
                            }
1853

    
1854
                            //Equipment
1855
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
1856
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1857
                            foreach (DataRow row in item.Equipment.Rows)
1858
                            {
1859
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
1860
                                var cmd = connection.GetSqlStringCommand(query);
1861
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1862
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1863

    
1864
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1865
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1866
                                else
1867
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1868

    
1869
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1870
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1871
                                else
1872
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1873

    
1874
                                connection.ExecuteNonQuery(cmd, txn);
1875
                            }
1876

    
1877
                            // TopologySet
1878
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
1879
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1880
                            foreach (DataRow row in item.TopologySet.Rows)
1881
                            {
1882
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
1883
                                var cmd = connection.GetSqlStringCommand(query);
1884
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1885
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1886
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
1887
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
1888
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
1889
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
1890
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
1891
                                connection.ExecuteNonQuery(cmd, txn);
1892
                            }
1893

    
1894
                            // PSN
1895
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
1896
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1897
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
1898
                            {
1899
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
1900
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy, Pocket, EGTag, HasMLTags) VALUES " +
1901
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy, @Pocket, @EGTag, @HasMLTags)";
1902
                                var cmd = connection.GetSqlStringCommand(query);
1903
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1904
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1905
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
1906
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
1907
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
1908
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
1909
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1910
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
1911

    
1912
                                int IsValid = 0;
1913
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
1914
                                    IsValid = 0;
1915
                                else if (row["IsValid"].ToString() == "InValid")
1916
                                    IsValid = 1;
1917
                                else if (row["IsValid"].ToString() == "Error")
1918
                                    IsValid = -1;
1919

    
1920
                                AddWithValue(cmd, "@IsValid", IsValid);
1921
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
1922

    
1923
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
1924
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
1925

    
1926
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
1927
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
1928
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
1929
                                else
1930
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString().Replace("%", ""));
1931

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

    
1934
                                AddWithValue(cmd, "@EGTag", string.IsNullOrEmpty(row["EGTag"].ToString()) ? "" : row["EGTag"].ToString());
1935
                                AddWithValue(cmd, "@HasMLTags", string.IsNullOrEmpty(row["HasMLTags"].ToString()) ? "False" : row["HasMLTags"].ToString());
1936
                                connection.ExecuteNonQuery(cmd, txn);
1937
                            }
1938

    
1939
                            //Pipeline
1940
                            query = $"DELETE FROM {PSN_PIPELINE}";
1941
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1942
                            foreach (DataRow row in item.PipeLine.Rows)
1943
                            {
1944
                                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)";
1945
                                var cmd = connection.GetSqlStringCommand(query);
1946
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
1947
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
1948
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
1949
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
1950
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
1951
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
1952
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
1953
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
1954
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
1955
                                connection.ExecuteNonQuery(cmd, txn);
1956
                            }
1957

    
1958
                            //PipeSystem
1959
                            query = $"DELETE FROM {PSN_PIPESYSTEM}";
1960
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1961
                            foreach (DataRow row in item.PipeSystem.Rows)
1962
                            {
1963
                                query = $"INSERT INTO {PSN_PIPESYSTEM} (OID, DESCRIPTION, FLUID, PMC, PipeLineQty, GroundLevel) VALUES (@OID, @DESCRIPTION, @FLUID, @PMC, @PipeLineQty, @GroundLevel)";
1964
                                var cmd = connection.GetSqlStringCommand(query);
1965
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
1966
                                AddWithValue(cmd, "@DESCRIPTION", row["DESCRIPTION"].ToString());
1967
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
1968
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
1969
                                AddWithValue(cmd, "@PipeLineQty", row["PipeLineQty"].ToString());
1970
                                AddWithValue(cmd, "@GroundLevel", row["GroundLevel"].ToString());
1971
                                connection.ExecuteNonQuery(cmd, txn);
1972
                            }
1973

    
1974
                            if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
1975
                            {
1976
                                query = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
1977
                                connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1978
                            }
1979

    
1980

    
1981
                            txn.Commit();
1982
                        }
1983
                        catch (Exception ex)
1984
                        {
1985
                            txn.Rollback();
1986
                            result = false;
1987
                        }
1988

    
1989
                    }
1990
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
1991
                    {
1992
                        bool check = false;
1993
                        if (colnames != null)
1994
                        {
1995
                            foreach (KeyValuePair<string, string> col in dicCol)
1996
                            {
1997
                                check = false;
1998
                                foreach (string c in colnames)
1999
                                {
2000
                                    if (col.Key.Contains(c))
2001
                                    {
2002
                                        check = true;
2003
                                        break;
2004
                                    }
2005
                                }
2006

    
2007
                                if (!check) //없으면 추가
2008
                                {
2009
                                    string i = string.Empty;
2010
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
2011
                                        i = "DEFAULT 0";
2012

    
2013
                                    var queryalter = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
2014
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
2015
                                    {
2016
                                        cmd.ExecuteNonQuery();
2017
                                    }
2018
                                }
2019
                            }
2020
                        }
2021
                    }
2022
                }
2023
                catch (Exception ex)
2024
                {
2025
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2026
                    result = false;
2027
                }
2028
            }
2029

    
2030
            return result;
2031
        }
2032

    
2033
        public static bool SavePSNFluidCode(DataTable dt)
2034
        {
2035
            ID2Info id2Info = ID2Info.GetInstance();
2036

    
2037
            bool result = true;
2038
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2039
            {
2040
                try
2041
                {
2042
                    using (var txn = connection.BeginTransaction())
2043
                    {
2044
                        try
2045
                        {
2046
                            var query = $"DELETE FROM {PSN_FLUIDCODE}";
2047
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2048

    
2049
                            foreach (DataRow row in dt.Rows)
2050
                            {
2051
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
2052
                                var cmd = connection.GetSqlStringCommand(query);
2053
                                cmd.Parameters.Clear();
2054

    
2055
                                {
2056
                                    var param = cmd.CreateParameter();
2057
                                    param.ParameterName = "@UID";
2058
                                    param.Value = row["UID"].ToString();
2059
                                    cmd.Parameters.Add(param);
2060
                                }
2061

    
2062
                                {
2063
                                    var param = cmd.CreateParameter();
2064
                                    param.ParameterName = "@Code";
2065
                                    param.Value = row["Code"].ToString();
2066
                                    cmd.Parameters.Add(param);
2067
                                }
2068

    
2069
                                {
2070
                                    var param = cmd.CreateParameter();
2071
                                    param.ParameterName = "@Description";
2072
                                    param.Value = row["Description"].ToString();
2073
                                    cmd.Parameters.Add(param);
2074
                                }
2075

    
2076
                                {
2077
                                    var param = cmd.CreateParameter();
2078
                                    param.ParameterName = "@Condition";
2079
                                    param.Value = row["Condition"].ToString();
2080
                                    cmd.Parameters.Add(param);
2081
                                }
2082

    
2083
                                {
2084
                                    var param = cmd.CreateParameter();
2085
                                    param.ParameterName = "@Remarks";
2086
                                    param.Value = row["Remarks"].ToString();
2087
                                    cmd.Parameters.Add(param);
2088
                                }
2089

    
2090
                                {
2091
                                    var param = cmd.CreateParameter();
2092
                                    param.ParameterName = "@GroundLevel";
2093
                                    param.Value = row["GroundLevel"].ToString();
2094
                                    cmd.Parameters.Add(param);
2095
                                }
2096

    
2097
                                connection.ExecuteNonQuery(cmd, txn);
2098
                            }
2099
                            txn.Commit();
2100
                        }
2101
                        catch (Exception ex)
2102
                        {
2103
                            txn.Rollback();
2104
                            result = false;
2105
                        }
2106
                    }
2107
                }
2108
                catch (Exception ex)
2109
                {
2110
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2111
                    result = false;
2112
                }
2113
            }
2114

    
2115
            return result;
2116
        }
2117

    
2118
        public static DataTable SelectPSNFluidCode()
2119
        {
2120
            DataTable dt = null;
2121
            ID2Info id2Info = ID2Info.GetInstance();
2122

    
2123
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2124
            {
2125
                try
2126
                {
2127
                    var query = $"SELECT * FROM {PSN_FLUIDCODE}";
2128
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2129
                    {
2130
                        dt = ds.Tables[0].Copy();
2131
                    }
2132
                }
2133
                catch (Exception ex)
2134
                {
2135
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2136
                }
2137
            }
2138

    
2139
            return dt;
2140
        }
2141

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

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

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

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

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

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

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

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

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

    
2206
                                {
2207
                                    var param = cmd.CreateParameter();
2208
                                    param.ParameterName = "@GroundLevel";
2209
                                    param.Value = row["GroundLevel"].ToString();
2210
                                    cmd.Parameters.Add(param);
2211
                                }
2212

    
2213
                                connection.ExecuteNonQuery(cmd, txn);
2214
                            }
2215

    
2216
                            txn.Commit();
2217
                        }
2218
                        catch (Exception ex)
2219
                        {
2220
                            txn.Rollback();
2221
                            result = false;
2222
                        }
2223
                    }
2224
                }
2225
                catch (Exception ex)
2226
                {
2227
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2228
                    result = false;
2229
                }
2230
            }
2231

    
2232
            return result;
2233
        }
2234

    
2235
        public static bool SavePSNInsulation(DataTable dt)
2236
        {
2237
            ID2Info id2Info = ID2Info.GetInstance();
2238

    
2239
            bool result = true;
2240
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2241
            {
2242
                try
2243
                {
2244
                    using (var txn = connection.BeginTransaction())
2245
                    {
2246
                        try
2247
                        {
2248
                            var query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
2249
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2250

    
2251
                            foreach (DataRow row in dt.Rows)
2252
                            {
2253
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
2254
                                var cmd = connection.GetSqlStringCommand(query);
2255
                                cmd.Parameters.Clear();
2256

    
2257
                                {
2258
                                    var param = cmd.CreateParameter();
2259
                                    param.ParameterName = "@UID";
2260
                                    param.Value = row["UID"].ToString();
2261
                                    cmd.Parameters.Add(param);
2262
                                }
2263

    
2264
                                {
2265
                                    var param = cmd.CreateParameter();
2266
                                    param.ParameterName = "@Code";
2267
                                    param.Value = row["Code"].ToString();
2268
                                    cmd.Parameters.Add(param);
2269
                                }
2270

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

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

    
2285
                                connection.ExecuteNonQuery(cmd, txn);
2286
                            }
2287

    
2288
                            txn.Commit();
2289
                        }
2290
                        catch (Exception ex)
2291
                        {
2292
                            txn.Rollback();
2293
                            result = false;
2294
                        }
2295
                    }
2296
                }
2297
                catch (Exception ex)
2298
                {
2299
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2300
                    result = false;
2301
                }
2302
            }
2303

    
2304
            return result;
2305
        }
2306

    
2307
        public static PSN GetDBPSN()
2308
        {
2309
            PSN result = new PSN();
2310
            ID2Info id2Info = ID2Info.GetInstance();
2311

    
2312
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2313
            {
2314
                try
2315
                {
2316
                    //query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK}";
2317
                    //using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2318
                    //{
2319
                    //    result.PipeSystemNetwork = ds.Tables[0].Copy();
2320
                    //}
2321

    
2322
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
2323
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2324
                    {
2325
                        result.PipeSystemNetwork = ds.Tables[0].Clone();
2326
                        result.PipeSystemNetwork.Columns["IsValid"].DataType = typeof(string);
2327

    
2328
                        foreach (DataRow row in ds.Tables[0].Rows)
2329
                        {
2330
                            DataRow newRow = result.PipeSystemNetwork.NewRow();
2331
                            newRow["OID"] = row["OID"].ToString();
2332
                            newRow["Type"] = row["Type"].ToString();
2333
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
2334
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
2335
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
2336
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
2337
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
2338
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
2339

    
2340
                            string IsValid = string.Empty;
2341

    
2342
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
2343
                                IsValid = string.Empty;//"OK";
2344
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
2345
                                IsValid = "InValid";
2346
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
2347
                                IsValid = "Error";
2348

    
2349
                            newRow["IsValid"] = IsValid;
2350

    
2351
                            newRow["Status"] = row["Status"].ToString();
2352
                            newRow["PBS"] = row["PBS"].ToString();
2353
                            newRow["Drawings"] = row["Drawings"].ToString();
2354

    
2355
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2356
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
2357

    
2358
                            newRow["Pocket"] = row["Pocket"].ToString();
2359

    
2360
                            newRow["EGTag"] = row["EGTag"].ToString();
2361
                            newRow["HasMLTags"] = row["HasMLTags"].ToString();
2362
                            result.PipeSystemNetwork.Rows.Add(newRow);
2363
                        }
2364
                    }
2365

    
2366
                    query = $"SELECT * FROM {PSN_EQUIPMENT}";
2367
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2368
                    {
2369
                        result.Equipment = ds.Tables[0].Copy();
2370
                    }
2371

    
2372
                    query = $"SELECT * FROM {PSN_NOZZLE}";
2373
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2374
                    {
2375
                        result.Nozzle = ds.Tables[0].Copy();
2376
                    }
2377

    
2378
                    query = $"SELECT * FROM {PSN_PATHITEMS}";
2379
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2380
                    {
2381
                        result.PathItems = ds.Tables[0].Copy();
2382
                    }
2383

    
2384
                    query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
2385
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2386
                    {
2387
                        result.SequenceData = ds.Tables[0].Copy();
2388
                    }
2389

    
2390
                    query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
2391
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2392
                    {
2393
                        result.TopologySet = ds.Tables[0].Copy();
2394
                    }
2395

    
2396
                    query = $"SELECT * FROM {PSN_PIPELINE}";
2397
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2398
                    {
2399
                        result.PipeLine = ds.Tables[0].Copy();
2400
                    }
2401

    
2402
                    query = $"SELECT * FROM {PSN_PIPESYSTEM}";
2403
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2404
                    {
2405
                        result.PipeSystem = ds.Tables[0].Copy();
2406
                    }
2407

    
2408
                    result.Revision = GetRevision();
2409
                }
2410
                catch (Exception ex)
2411
                {
2412
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2413
                    result = null;
2414
                }
2415
            }
2416

    
2417
            return result;
2418
        }
2419

    
2420
        public static int GetRevision()
2421
        {
2422
            int result = 0;
2423
            ID2Info id2Info = ID2Info.GetInstance();
2424

    
2425
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2426
            {
2427
                try
2428
                {
2429
                    var query = $"SELECT DISTINCT PSNRevisionNumber FROM {PSN_PIPESYSTEMNETWORK};";
2430
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2431
                    {
2432
                        foreach (DataRow row in ds.Tables[0].Rows)
2433
                        {
2434
                            string value = row["PSNRevisionNumber"].ToString();
2435
                            if (value.StartsWith("V"))
2436
                                value = value.Remove(0, 1);
2437
                            int revisionNumber = Convert.ToInt32(value);
2438
                            if (result < revisionNumber)
2439
                                result = revisionNumber;
2440
                        }
2441
                    }
2442
                }
2443
                catch (Exception ex)
2444
                {
2445
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2446
                    result = -1;
2447
                }
2448
            }
2449

    
2450
            return result;
2451
        }
2452

    
2453
        public static DataTable GetPathItem()
2454
        {
2455
            DataTable dt = null;
2456

    
2457
            ID2Info id2Info = ID2Info.GetInstance();
2458
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2459
            {
2460
                try
2461
                {
2462
                    var query = $"SELECT * FROM {PSN_PATHITEMS}";
2463
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2464
                    {
2465
                        dt = ds.Tables[0].Copy();
2466
                    }
2467
                }
2468
                catch (Exception ex)
2469
                {
2470
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2471
                }
2472
            }
2473

    
2474
            return dt;
2475
        }
2476

    
2477
        public static DataTable GetTopologySet()
2478
        {
2479
            DataTable dt = null;
2480

    
2481
            ID2Info id2Info = ID2Info.GetInstance();
2482
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2483
            {
2484
                try
2485
                {
2486
                    var query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
2487
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2488
                    {
2489
                        dt = ds.Tables[0].Clone();
2490
                        foreach (DataRow row in ds.Tables[0].Rows)
2491
                        {
2492
                            DataRow newRow = dt.NewRow();
2493
                            newRow["OID"] = row["OID"].ToString();
2494
                            newRow["Type"] = row["Type"].ToString();
2495
                            newRow["SubType"] = row["SubType"].ToString();
2496
                            newRow["HeadItemTag"] = row["HeadItemTag"].ToString();
2497
                            newRow["TailItemTag"] = row["TailItemTag"].ToString();
2498
                            newRow["HeadItemSPID"] = row["HeadItemSPID"].ToString();
2499
                            newRow["TailItemSPID"] = row["TailItemSPID"].ToString();
2500
                            dt.Rows.Add(newRow);
2501
                        }
2502
                    }
2503
                }
2504
                catch (Exception ex)
2505
                {
2506
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2507
                }
2508
            }
2509

    
2510
            return dt;
2511
        }
2512

    
2513
        public static DataTable GetPipeSystemNetwork()
2514
        {
2515
            DataTable dt = null;
2516

    
2517
            ID2Info id2Info = ID2Info.GetInstance();
2518
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2519
            {
2520
                try
2521
                {
2522
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}' ORDER BY OID, OrderNumber";
2523
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2524
                    {
2525
                        dt = ds.Tables[0].Clone();
2526
                        dt.Columns["IsValid"].DataType = typeof(string);
2527
                        foreach (DataRow row in ds.Tables[0].Rows)
2528
                        {
2529
                            DataRow newRow = dt.NewRow();
2530
                            newRow["OID"] = row["OID"].ToString();
2531
                            newRow["Type"] = row["Type"].ToString();
2532
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
2533
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
2534
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
2535
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
2536
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
2537
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
2538

    
2539
                            string IsValid = string.Empty;
2540

    
2541
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
2542
                                IsValid = string.Empty;//"OK";
2543
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
2544
                                IsValid = "InValid";
2545
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
2546
                                IsValid = "Error";
2547

    
2548
                            newRow["IsValid"] = IsValid;
2549
                            newRow["Status"] = row["Status"].ToString();
2550

    
2551
                            newRow["PBS"] = row["PBS"].ToString();
2552
                            newRow["Drawings"] = row["Drawings"].ToString();
2553

    
2554
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2555
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
2556

    
2557
                            newRow["Pocket"] = row["Pocket"].ToString();
2558
                            newRow["EGTag"] = row["EGTag"].ToString();
2559
                            newRow["HasMLTags"] = row["HasMLTags"].ToString();
2560

    
2561
                            dt.Rows.Add(newRow);
2562
                        }
2563
                    }
2564
                }
2565
                catch (Exception ex)
2566
                {
2567
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2568
                }
2569
            }
2570

    
2571
            return dt;
2572
        }
2573

    
2574
        public static DataTable GetSequenceData()
2575
        {
2576
            DataTable dt = null;
2577

    
2578
            ID2Info id2Info = ID2Info.GetInstance();
2579
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2580
            {
2581
                try
2582
                {
2583
                    var query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
2584
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2585
                    {
2586
                        dt = ds.Tables[0].Copy();
2587
                    }
2588
                }
2589
                catch (Exception ex)
2590
                {
2591
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2592
                }
2593
            }
2594

    
2595
            return dt;
2596
        }
2597

    
2598

    
2599
        //Anohter DB
2600
        public static bool ConnTestAndCreateAnotherTable()
2601
        {
2602
            bool result = false;
2603
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
2604

    
2605
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2606
            {
2607
                try
2608
                {
2609
                    var names = connection.GetTableNames();
2610
                    var matched = names.FirstOrDefault(param => param == PSN_HEADER_SETTING);
2611
                    Dictionary<string, string> dicColCheck = new Dictionary<string, string>();
2612
                    dicColCheck.Add("GROUP_ID", "TEXT");
2613
                    dicColCheck.Add("DESCRIPTION", "TEXT");
2614
                    dicColCheck.Add("INDEX", "INTEGER");
2615
                    dicColCheck.Add("NAME", "TEXT");
2616

    
2617
                    if (matched == null)
2618
                    {
2619
                        var query = $"CREATE TABLE {PSN_HEADER_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
2620
                        using (var cmd = connection.GetSqlStringCommand(query))
2621
                        {
2622
                            cmd.ExecuteNonQuery();
2623
                        }
2624
                    }
2625
                    else
2626
                    {
2627
                        AddColumn(PSN_HEADER_SETTING, dicColCheck);
2628
                    }
2629

    
2630
                    matched = names.FirstOrDefault(param => param == PSN_VENTDRAIN_SETTING);
2631
                    dicColCheck.Clear();
2632
                    dicColCheck.Add("GROUP_ID", "TEXT");
2633
                    dicColCheck.Add("DESCRIPTION", "TEXT");
2634
                    dicColCheck.Add("INDEX", "INTEGER");
2635
                    dicColCheck.Add("NAME", "TEXT");
2636
                    if (matched == null)
2637
                    {
2638
                        var query = $"CREATE TABLE {PSN_VENTDRAIN_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
2639
                        using (var cmd = connection.GetSqlStringCommand(query))
2640
                        {
2641
                            cmd.ExecuteNonQuery();
2642
                        }
2643
                    }
2644
                    else
2645
                    {
2646
                        AddColumn(PSN_VENTDRAIN_SETTING, dicColCheck);
2647
                    }
2648

    
2649
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGY_RULE);
2650
                    dicColCheck.Clear();
2651
                    dicColCheck.Add("UID", "TEXT");
2652
                    if (matched == null)
2653
                    {
2654
                        var query = $"CREATE TABLE {PSN_TOPOLOGY_RULE} (UID TEXT)";
2655
                        using (var cmd = connection.GetSqlStringCommand(query))
2656
                        {
2657
                            cmd.ExecuteNonQuery();
2658
                        }
2659

    
2660
                        DataTable topologyRule = new DataTable();
2661
                        topologyRule.Columns.Add("NAME", typeof(string));
2662

    
2663
                        topologyRule.Rows.Add("FluidCode");
2664
                        topologyRule.Rows.Add("-");
2665
                        topologyRule.Rows.Add("PipingMaterialsClass");
2666
                        topologyRule.Rows.Add("-");
2667
                        topologyRule.Rows.Add("Tag Seq No");
2668

    
2669
                        SaveTopologyRule(topologyRule);
2670
                    }
2671
                    //else
2672
                    //{
2673
                    //    AddColumn(PSN_TOPOLOGY_RULE, dicColCheck);
2674
                    //}
2675

    
2676
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
2677
                    dicColCheck.Clear();
2678
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2679
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2680
                    dicColCheck.Add("OrderNumber", "NVARCHAR(255)");
2681
                    dicColCheck.Add("Pipeline_OID", "NVARCHAR(255)");
2682
                    dicColCheck.Add("From_Data", "NVARCHAR(255)");
2683
                    dicColCheck.Add("To_Data", "NVARCHAR(255)");
2684
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(125)");
2685
                    dicColCheck.Add("PSNRevisionNumber", "NVARCHAR(255)");
2686
                    dicColCheck.Add("IsValid", "INT");
2687
                    dicColCheck.Add("Status", "NVARCHAR(255)");
2688
                    dicColCheck.Add("PBS", "NVARCHAR(255)");
2689
                    dicColCheck.Add("Drawings", "NVARCHAR(255)");
2690
                    dicColCheck.Add("IncludingVirtualData", "NVARCHAR(10)");
2691
                    dicColCheck.Add("PSNAccuracy", "REAL");
2692
                    dicColCheck.Add("Pocket", "NVARCHAR(10)");
2693
                    dicColCheck.Add("EGTag", "NVARCHAR(255)");
2694
                    dicColCheck.Add("HasMLTags", "NVARCHAR(50)");
2695

    
2696
                    if (matched == null)
2697
                    {
2698
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(255), Type NVARCHAR(255), OrderNumber NVARCHAR(255), Pipeline_OID NVARCHAR(255), From_Data NVARCHAR(255), " +
2699
                            "To_Data NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
2700
                            "IncludingVirtualData NVARCHAR(10),  PSNAccuracy REAL, Pocket NVARCHAR(10), EGTag NVARCHAR(255), HasMLTags NVARCHAR(50))";
2701
                        using (var cmd = connection.GetSqlStringCommand(query))
2702
                        {
2703
                            cmd.ExecuteNonQuery();
2704
                        }
2705
                    }
2706
                    else
2707
                    {
2708
                        AddColumn(PSN_PIPESYSTEMNETWORK, dicColCheck);
2709
                    }
2710

    
2711
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
2712
                    dicColCheck.Clear();
2713
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2714
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2715
                    dicColCheck.Add("Xcoords", "REAL");
2716
                    dicColCheck.Add("Ycoords", "REAL");
2717
                    if (matched == null)
2718
                    {
2719
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL)";
2720
                        using (var cmd = connection.GetSqlStringCommand(query))
2721
                        {
2722
                            cmd.ExecuteNonQuery();
2723
                        }
2724
                    }
2725
                    else
2726
                    {
2727
                        AddColumn(PSN_EQUIPMENT, dicColCheck);
2728
                    }
2729

    
2730
                    dicColCheck.Clear();
2731
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2732
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2733
                    dicColCheck.Add("Xcoords", "REAL");
2734
                    dicColCheck.Add("Ycoords", "REAL");
2735
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2736
                    dicColCheck.Add("Fluid", "NVARCHAR(255)");
2737
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2738
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2739
                    dicColCheck.Add("Rotation", "REAL");
2740
                    dicColCheck.Add("FlowDirection", "NVARCHAR(255)");
2741

    
2742
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
2743
                    if (matched == null)
2744
                    {
2745
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL, Equipment_OID NVARCHAR(255), " +
2746
                            "Fluid NVARCHAR(255), NPD NVARCHAR(255), PMC NVARCHAR(255), Rotation REAL, FlowDirection NVARCHAR(255))";
2747
                        using (var cmd = connection.GetSqlStringCommand(query))
2748
                        {
2749
                            cmd.ExecuteNonQuery();
2750
                        }
2751
                    }
2752
                    else
2753
                    {
2754
                        AddColumn(PSN_NOZZLE, dicColCheck);
2755
                    }
2756

    
2757
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
2758
                    dicColCheck.Clear();
2759
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2760
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2761
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2762
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2763
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2764
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2765
                    if (matched == null)
2766
                    {
2767
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
2768
                        using (var cmd = connection.GetSqlStringCommand(query))
2769
                        {
2770
                            cmd.ExecuteNonQuery();
2771
                        }
2772
                    }
2773
                    else
2774
                    {
2775
                        AddColumn(PSN_FLUIDCODE, dicColCheck);
2776
                    }
2777

    
2778
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
2779
                    dicColCheck.Clear();
2780
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2781
                    dicColCheck.Add("Priority", "INTEGER");
2782
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2783
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2784
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2785
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2786
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2787
                    if (matched == null)
2788
                    {
2789
                        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))";
2790
                        using (var cmd = connection.GetSqlStringCommand(query))
2791
                        {
2792
                            cmd.ExecuteNonQuery();
2793
                        }
2794
                    }
2795
                    else
2796
                    {
2797
                        AddColumn(PSN_PIPINGMATLCLASS, dicColCheck);
2798
                    }
2799

    
2800
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
2801
                    dicColCheck.Clear();
2802
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2803
                    dicColCheck.Add("SERIALNUMBER", "NVARCHAR(255)");
2804
                    dicColCheck.Add("PathItem_OID", "NVARCHAR(255)");
2805
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(255)");
2806
                    if (matched == null)
2807
                    {
2808
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(255), SERIALNUMBER NVARCHAR(255), PathItem_OID NVARCHAR(255), TopologySet_OID_Key NVARCHAR(255))";
2809
                        using (var cmd = connection.GetSqlStringCommand(query))
2810
                        {
2811
                            cmd.ExecuteNonQuery();
2812
                        }
2813
                    }
2814
                    else
2815
                    {
2816
                        AddColumn(PSN_SEQUENCEDATA, dicColCheck);
2817
                    }
2818

    
2819
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
2820
                    dicColCheck.Clear();
2821
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2822
                    dicColCheck.Add("SequenceData_OID", "NVARCHAR(255)");
2823
                    dicColCheck.Add("TopologySet_OID", "NVARCHAR(255)");
2824
                    dicColCheck.Add("BranchTopologySet_OID", "NVARCHAR(255)");
2825
                    dicColCheck.Add("PipeLine_OID", "NVARCHAR(255)");
2826
                    dicColCheck.Add("ItemName", "NVARCHAR(255)");
2827
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2828
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
2829
                    dicColCheck.Add("CLASS", "NVARCHAR(255)");
2830
                    dicColCheck.Add("SUBCLASS", "NVARCHAR(255)");
2831
                    dicColCheck.Add("TYPE", "NVARCHAR(255)");
2832
                    dicColCheck.Add("PIDNAME", "NVARCHAR(255)");
2833
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2834
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2835
                    dicColCheck.Add("GROUPTAG", "NVARCHAR(255)");
2836
                    dicColCheck.Add("PipeSystemNetwork_OID", "NVARCHAR(255)");
2837
                    dicColCheck.Add("PipeRun_OID", "NVARCHAR(255)");
2838
                    dicColCheck.Add("ViewPipeSystemNetwork_OID", "NVARCHAR(255)");
2839
                    dicColCheck.Add("EqpGroupTag", "NVARCHAR(255)");
2840
                    dicColCheck.Add("MainLineTag", "NVARCHAR(255)");
2841
                    dicColCheck.Add("EGTConnectedPoint", "INT");
2842
                    dicColCheck.Add("EGFlowDirection", "NVARCHAR(10)");
2843
                    if (matched == null)
2844
                    {
2845
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
2846
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
2847
                            "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), " +
2848
                            "ViewPipeSystemNetwork_OID NVARCHAR(255), EqpGroupTag NVARCHAR(255), MainLineTag NVARCHAR(255), EGTConnectedPoint INT, EGFlowDirection NVARCHAR(10))";
2849
                        using (var cmd = connection.GetSqlStringCommand(query))
2850
                        {
2851
                            cmd.ExecuteNonQuery();
2852
                        }
2853
                    }
2854
                    else
2855
                    {
2856
                        AddColumn(PSN_PATHITEMS, dicColCheck);
2857
                    }
2858

    
2859
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
2860
                    dicColCheck.Clear();
2861
                    dicColCheck.Add("OID", "TEXT");
2862
                    if (matched == null)
2863
                    {
2864
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
2865
                        using (var cmd = connection.GetSqlStringCommand(query))
2866
                        {
2867
                            cmd.ExecuteNonQuery();
2868
                        }
2869
                    }
2870

    
2871
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
2872
                    dicColCheck.Clear();
2873
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2874
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2875
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
2876
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
2877
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
2878
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
2879
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
2880

    
2881
                    if (matched == null)
2882
                    {
2883
                        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))";
2884
                        using (var cmd = connection.GetSqlStringCommand(query))
2885
                        {
2886
                            cmd.ExecuteNonQuery();
2887
                        }
2888
                    }
2889
                    else
2890
                    {
2891
                        AddColumn(PSN_TOPOLOGYSET, dicColCheck);
2892
                    }
2893

    
2894
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
2895
                    dicColCheck.Clear();
2896
                    dicColCheck.Add("INDEX", "INTEGER");
2897
                    dicColCheck.Add("NAME", "TEXT");
2898
                    dicColCheck.Add("KEYWORD", "TEXT");
2899
                    if (matched == null)
2900
                    {
2901
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
2902
                        using (var cmd = connection.GetSqlStringCommand(query))
2903
                        {
2904
                            cmd.ExecuteNonQuery();
2905
                        }
2906
                    }
2907
                    else
2908
                    {
2909
                        AddColumn(PSN_TRANSFORMKEYWORD_SETTING, dicColCheck);
2910
                    }
2911

    
2912
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
2913
                    dicColCheck.Clear();
2914
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2915
                    dicColCheck.Add("PipeSystem_OID", "NVARCHAR(255)");
2916
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
2917
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2918
                    dicColCheck.Add("SEQNUMBER", "NVARCHAR(255)");
2919
                    dicColCheck.Add("INSULATION", "NVARCHAR(255)");
2920
                    dicColCheck.Add("FROM_DATA", "NVARCHAR(255)");
2921
                    dicColCheck.Add("TO_DATA", "NVARCHAR(255)");
2922
                    dicColCheck.Add("Unit", "NVARCHAR(100)");
2923
                    if (matched == null)
2924
                    {
2925
                        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), " +
2926
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
2927
                        using (var cmd = connection.GetSqlStringCommand(query))
2928
                        {
2929
                            cmd.ExecuteNonQuery();
2930
                        }
2931
                    }
2932
                    else
2933
                    {
2934
                        AddColumn(PSN_PIPELINE, dicColCheck);
2935
                    }
2936

    
2937
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
2938
                    dicColCheck.Clear();
2939
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2940
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2941
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2942
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2943
                    if (matched == null)
2944
                    {
2945
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
2946
                        using (var cmd = connection.GetSqlStringCommand(query))
2947
                        {
2948
                            cmd.ExecuteNonQuery();
2949
                        }
2950
                    }
2951
                    else
2952
                    {
2953
                        AddColumn(PSN_INSULATIONPURPOSE, dicColCheck);
2954
                    }
2955

    
2956
                    matched = names.FirstOrDefault(param => param == PSN_VALVEGROUP_SETTING);
2957
                    dicColCheck.Clear();
2958
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2959
                    dicColCheck.Add("GroupType", "NVARCHAR(255)");
2960
                    dicColCheck.Add("TagIdentifier", "NVARCHAR(50)");
2961
                    dicColCheck.Add("AttributeName", "NVARCHAR(255)");
2962
                    dicColCheck.Add("SppidSymbolName", "NVARCHAR(255)");
2963
                    if (matched == null)
2964
                    {
2965
                        var query = $"CREATE TABLE {PSN_VALVEGROUP_SETTING} (OID NVARCHAR(50), GroupType NVARCHAR(255), TagIdentifier NVARCHAR(50), AttributeName NVARCHAR(255), SppidSymbolName NVARCHAR(255))";
2966
                        using (var cmd = connection.GetSqlStringCommand(query))
2967
                        {
2968
                            cmd.ExecuteNonQuery();
2969
                        }
2970
                    }
2971
                    else
2972
                    {
2973
                        AddColumn(PSN_VALVEGROUP_SETTING, dicColCheck);
2974
                    }
2975

    
2976
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEM);
2977
                    dicColCheck.Clear();
2978
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2979
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
2980
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
2981
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2982
                    dicColCheck.Add("PipeLineQty", "NVARCHAR(255)");
2983
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2984
                    if (matched == null)
2985
                    {
2986
                        var query = $"CREATE TABLE {PSN_PIPESYSTEM} (OID NVARCHAR(255), DESCRIPTION NVARCHAR(255), FLUID NVARCHAR(255), PMC NVARCHAR(255), PipeLineQty NVARCHAR(255), GroundLevel NVARCHAR(20))";
2987
                        using (var cmd = connection.GetSqlStringCommand(query))
2988
                        {
2989
                            cmd.ExecuteNonQuery();
2990
                        }
2991
                    }
2992
                    else
2993
                    {
2994
                        AddColumn(PSN_PIPESYSTEM, dicColCheck);
2995
                    }
2996

    
2997
                    matched = names.FirstOrDefault(param => param == PSN_NOPOCKETSETTING);
2998
                    dicColCheck.Clear();
2999
                    dicColCheck.Add("INDEX", "INTEGER");
3000
                    dicColCheck.Add("TYPE", "TEXT");
3001
                    dicColCheck.Add("NAME", "TEXT");
3002
                    if (matched == null)
3003
                    {
3004
                        var query = $"CREATE TABLE {PSN_NOPOCKETSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [NAME] TEXT)";
3005
                        using (var cmd = connection.GetSqlStringCommand(query))
3006
                        {
3007
                            cmd.ExecuteNonQuery();
3008
                        }
3009
                    }
3010
                    else
3011
                    {
3012
                        AddColumn(PSN_NOPOCKETSETTING, dicColCheck);
3013
                    }
3014

    
3015
                    matched = names.FirstOrDefault(param => param == PSN_AIRFINCOOLERSETTING);
3016
                    dicColCheck.Clear();
3017
                    dicColCheck.Add("INDEX", "INTEGER");
3018
                    dicColCheck.Add("TYPE", "TEXT");
3019
                    dicColCheck.Add("TagIdentifier", "NVARCHAR(50)");
3020
                    dicColCheck.Add("AttributeName", "NVARCHAR(255)");
3021
                    dicColCheck.Add("NAME", "TEXT");
3022

    
3023
                    if (matched == null)
3024
                    {
3025
                        var query = $"CREATE TABLE {PSN_AIRFINCOOLERSETTING} ([INDEX] INTEGER, [TYPE] TEXT, [TagIdentifier] TEXT, [AttributeName] TEXT, [NAME] TEXT)";
3026
                       
3027
                        using (var cmd = connection.GetSqlStringCommand(query))
3028
                        {
3029
                            cmd.ExecuteNonQuery();
3030
                        }
3031
                    }
3032
                    else
3033
                    {
3034
                        AddColumn(PSN_AIRFINCOOLERSETTING, dicColCheck);
3035
                    }
3036

    
3037

    
3038
                    var query2 = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
3039
                    if (id2Info.ID2DBType == AnotherID2DB_Type.MSSQL)
3040
                    {
3041
                        //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3042
                        using (var cmd = connection.GetSqlStringCommand(query2))
3043
                        {
3044
                            cmd.ExecuteNonQuery();
3045
                        }
3046
                    }
3047

    
3048
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
3049
                    {
3050
                        var colnames = connection.GetColumnNames(TableName);
3051
                        bool check = false;
3052
                        if (colnames != null)
3053
                        {
3054
                            foreach (KeyValuePair<string, string> col in dicCol)
3055
                            {
3056
                                check = false;
3057
                                foreach (string c in colnames)
3058
                                {
3059
                                    if (col.Key.ToUpper().Equals(c.ToUpper()))
3060
                                    {
3061
                                        check = true;
3062
                                        break;
3063
                                    }
3064
                                }
3065

    
3066
                                if (!check) //없으면 추가
3067
                                {
3068
                                    string i = string.Empty;
3069
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
3070
                                        i = "DEFAULT 0";
3071

    
3072
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
3073
                                    using (var cmd = connection.GetSqlStringCommand(query))
3074
                                    {
3075
                                        cmd.ExecuteNonQuery();
3076
                                    }
3077
                                }
3078
                            }
3079
                        }
3080
                    }
3081

    
3082
                    result = true;
3083
                }
3084
                catch (Exception ex)
3085
                {
3086
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3087
                }
3088
            }
3089

    
3090
            return result;
3091
        }
3092

    
3093
        public static bool SaveAnotherRevision(string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
3094
         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)
3095
        {
3096
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3097

    
3098
            bool result = true;
3099

    
3100
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3101
            {
3102
                try
3103
                {
3104
                    using (var txn = connection.BeginTransaction())
3105
                    {
3106
                        try
3107
                        {
3108

    
3109
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
3110
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
3111
                                $"HD2, HDB, B2B, LastModificationdate, ID2_PSN) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
3112
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
3113
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B, @LastModificationdate, @ID2_PSN)";
3114
                            var cmd = connection.GetSqlStringCommand(query);
3115
                            AddWithValue(cmd, "@OID", Guid.NewGuid().ToString());
3116
                            AddWithValue(cmd, "@Project_OID", Guid.NewGuid().ToString());
3117
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
3118
                            AddWithValue(cmd, "@RevNumber", RevNumber);
3119
                            AddWithValue(cmd, "@UserName", UserName);
3120

    
3121
                            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
3122
                            DateTime oDateTime = DateTime.Now;
3123

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

    
3126
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
3127

    
3128
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
3129
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
3130
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
3131
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
3132
                            AddWithValue(cmd, "@PipeLines", PipeLines);
3133

    
3134
                            AddWithValue(cmd, "@Topologies", Topologies);
3135
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
3136
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
3137
                            AddWithValue(cmd, "@E2E", E2E);
3138
                            AddWithValue(cmd, "@E2B", E2B);
3139
                            AddWithValue(cmd, "@B2E", B2E);
3140
                            AddWithValue(cmd, "@HDE", HDE);
3141
                            AddWithValue(cmd, "@HD2", HD2);
3142
                            AddWithValue(cmd, "@HDB", HDB);
3143
                            AddWithValue(cmd, "@B2B", B2B);
3144

    
3145
                            AddWithValue(cmd, "@LastModificationdate", oDateTime.ToString(ci));
3146
                            AddWithValue(cmd, "@ID2_PSN", "Y");
3147

    
3148
                            connection.ExecuteNonQuery(cmd, txn);
3149

    
3150
                            txn.Commit();
3151
                        }
3152
                        catch (Exception ex)
3153
                        {
3154
                            txn.Rollback();
3155
                            result = false;
3156
                        }
3157
                    }
3158
                }
3159
                catch (Exception ex)
3160
                {
3161
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3162
                    result = false;
3163
                }
3164
            }
3165

    
3166
            return result;
3167
        }
3168

    
3169
        public static bool ConnAnotherCreateRevisionTable(Dictionary<string, string> dicColCheck, List<string> names)
3170
        {
3171
            bool result = false;
3172
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3173

    
3174
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3175
            {
3176
                try
3177
                {
3178
                    if (names.Count == 0)
3179
                    {
3180

    
3181
                        var query = $"CREATE TABLE ARS_COMMON.dbo.PSNRevision ([OID] [uniqueidentifier] ,[Project_OID] [uniqueidentifier] ," +
3182
                            $"[ProjectCode] nvarchar(255), [RevNumber] int ,[UserName] nvarchar(255) ,[TimeData] nvarchar(255) ," +
3183
                            $"[PSNDatabasePath] nvarchar(255) ,	[PsnByPBSFilter] varchar(20) ,[PsnByNPDFilter] varchar(20) ,[PidDrawings] int ,	" +
3184
                            $"[PipeSystems] int ,[PipeLines] int ,[Topologies] int ,[PipeSystemNetworks] int ,[InValidPSNs] int ,[E2E] int ," +
3185
                            $"[E2B] int ,[B2E] int ,[HDE] int ,[HD2] int ,[HDB] int ,[B2B] int , [LastModificationdate] nvarchar(255), [ID2_PSN] nvarchar(10))";
3186
                        using (var cmd = connection.GetSqlStringCommand(query))
3187
                        {
3188
                            cmd.ExecuteNonQuery();
3189
                        }
3190
                    }
3191
                    else
3192
                    {
3193
                        AddColumn(PSN_REVISION, dicColCheck, names);
3194
                    }
3195

    
3196
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
3197
                    {
3198
                        bool check = false;
3199
                        if (colnames != null)
3200
                        {
3201
                            foreach (KeyValuePair<string, string> col in dicCol)
3202
                            {
3203
                                check = false;
3204
                                foreach (string c in colnames)
3205
                                {
3206
                                    if (col.Key.Contains(c))
3207
                                    {
3208
                                        check = true;
3209
                                        break;
3210
                                    }
3211
                                }
3212

    
3213
                                if (!check) //없으면 추가
3214
                                {
3215
                                    string i = string.Empty;
3216
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
3217
                                        i = "DEFAULT 0";
3218

    
3219
                                    var queryalter = $"ALTER TABLE ARS_COMMON.dbo.PSNRevision ADD [" + col.Key + "] " + col.Value + " " + i + ";";
3220
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
3221
                                    {
3222
                                        cmd.ExecuteNonQuery();
3223
                                    }
3224
                                }
3225
                            }
3226
                        }
3227
                    }
3228

    
3229
                    result = true;
3230
                }
3231
                catch (Exception ex)
3232
                {
3233
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3234
                }
3235
            }
3236

    
3237
            return result;
3238
        }
3239

    
3240
        public static DataTable SelectAnotherRevision()
3241
        {
3242
            DataTable dt = null;
3243
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3244

    
3245
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3246
            {
3247
                try
3248
                {
3249
                    var query = $"SELECT * FROM ARS_COMMON.dbo.PSNREVISION";
3250

    
3251
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
3252
                    {
3253
                        dt = ds.Tables[0].Copy();
3254
                    }
3255
                }
3256
                catch (Exception ex)
3257
                {
3258
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3259
                }
3260
            }
3261

    
3262
            return dt;
3263
        }
3264

    
3265
        public static DataTable SelectAnotherRevisionTable()
3266
        {
3267
            DataTable dt = null;
3268
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3269

    
3270
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3271
            {
3272
                try
3273
                {
3274
                    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'";
3275
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
3276
                    {
3277
                        dt = ds.Tables[0].Copy();
3278
                    }
3279
                }
3280
                catch (Exception ex)
3281
                {
3282
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3283
                }
3284
            }
3285

    
3286
            return dt;
3287
        }
3288

    
3289
        public static bool SaveAnotherPSNData(PSN item, List<HeaderInfo> headerInfos, List<VentDrainInfo> ventDrainInfos,
3290
          List<KeywordItem> keywordItems, DataTable dtTopologyRule, DataTable dtFluidCode, DataTable dtPMC, DataTable dtInsulation, DataTable dtvalvegrouping, DataTable dtnopocket, DataTable dtafc)
3291
        {
3292
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3293

    
3294
            bool result = true;
3295
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3296
            {
3297
                try
3298
                {
3299
                    using (var txn = connection.BeginTransaction())
3300
                    {
3301
                        try
3302
                        {
3303
                            // Path Items
3304
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
3305
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3306
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
3307
                            {
3308
                                DataRow row = item.PathItems.Rows[i];
3309
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
3310
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, " +
3311
                                    $"PIDNAME, Equipment_OID, NPD, GROUPTAG, PipeSystemNetwork_OID, PipeRun_OID, ViewPipeSystemNetwork_OID, EqpGroupTag , MainLineTag, EGTConnectedPoint, EGFlowDirection ) VALUES " +
3312
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME," +
3313
                                    $" @Equipment_OID, @NPD, @GROUPTAG, @PipeSystemNetwork_OID,@PipeRun_OID, @ViewPipeSystemNetwork_OID, @EqpGroupTag , @MainLineTag, @EGTConnectedPoint, @EGFlowDirection )";
3314
                                var cmd = connection.GetSqlStringCommand(query);
3315
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3316
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
3317
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
3318
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
3319
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
3320
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
3321
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
3322
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString());
3323
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
3324
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
3325
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
3326
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
3327
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
3328
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
3329
                                AddWithValue(cmd, "@GROUPTAG", string.IsNullOrEmpty(row["GROUPTAG"].ToString()) ? "" : row["GROUPTAG"].ToString());
3330
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
3331
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
3332
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
3333
                                AddWithValue(cmd, "@EqpGroupTag", string.IsNullOrEmpty(row["EqpGroupTag"].ToString()) ? "" : row["EqpGroupTag"].ToString());
3334
                                AddWithValue(cmd, "@MainLineTag", string.IsNullOrEmpty(row["MainLineTag"].ToString()) ? "" : row["MainLineTag"].ToString());
3335
                                AddWithValue(cmd, "@EGTConnectedPoint", string.IsNullOrEmpty(row["EGTConnectedPoint"].ToString()) ? "0" : row["EGTConnectedPoint"].ToString());
3336
                                AddWithValue(cmd, "@EGFlowDirection", string.IsNullOrEmpty(row["EGFlowDirection"].ToString()) ? "" : row["EGFlowDirection"].ToString());
3337

    
3338
                                connection.ExecuteNonQuery(cmd, txn);
3339
                            }
3340

    
3341
                            // Sequence
3342
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
3343
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3344
                            foreach (DataRow row in item.SequenceData.Rows)
3345
                            {
3346
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
3347
                                var cmd = connection.GetSqlStringCommand(query);
3348
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3349
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
3350
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
3351
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
3352
                                connection.ExecuteNonQuery(cmd, txn);
3353
                            }
3354

    
3355
                            // Nozzle
3356
                            query = $"DELETE FROM {PSN_NOZZLE}";
3357
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3358
                            foreach (DataRow row in item.Nozzle.Rows)
3359
                            {
3360
                                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)";
3361
                                var cmd = connection.GetSqlStringCommand(query);
3362
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3363
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
3364

    
3365
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
3366
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
3367
                                else
3368
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
3369

    
3370
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
3371
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
3372
                                else
3373
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
3374

    
3375
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
3376
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
3377
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
3378
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
3379

    
3380
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
3381
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
3382
                                else
3383
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
3384

    
3385
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
3386
                                connection.ExecuteNonQuery(cmd, txn);
3387
                            }
3388

    
3389
                            //Equipment
3390
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
3391
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3392
                            foreach (DataRow row in item.Equipment.Rows)
3393
                            {
3394
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
3395
                                var cmd = connection.GetSqlStringCommand(query);
3396
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3397
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
3398

    
3399
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
3400
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
3401
                                else
3402
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
3403

    
3404
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
3405
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
3406
                                else
3407
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
3408

    
3409
                                connection.ExecuteNonQuery(cmd, txn);
3410
                            }
3411

    
3412
                            // TopologySet
3413
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
3414
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3415
                            foreach (DataRow row in item.TopologySet.Rows)
3416
                            {
3417
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
3418
                                var cmd = connection.GetSqlStringCommand(query);
3419
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3420
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
3421
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
3422
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
3423
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
3424
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
3425
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
3426
                                connection.ExecuteNonQuery(cmd, txn);
3427
                            }
3428

    
3429
                            // PSN
3430
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
3431
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3432
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
3433
                            {
3434
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
3435
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy, Pocket, EGTag, HasMLTags) VALUES " +
3436
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy, @Pocket, @EGTag, @HasMLTags)";
3437
                                var cmd = connection.GetSqlStringCommand(query);
3438
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
3439
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
3440
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
3441
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
3442
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
3443
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
3444
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
3445
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
3446

    
3447
                                int IsValid = 0;
3448
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
3449
                                    IsValid = 0;
3450
                                else if (row["IsValid"].ToString() == "InValid")
3451
                                    IsValid = 1;
3452
                                else if (row["IsValid"].ToString() == "Error")
3453
                                    IsValid = -1;
3454

    
3455
                                AddWithValue(cmd, "@IsValid", IsValid);
3456
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
3457

    
3458
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
3459
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
3460

    
3461
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
3462
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
3463
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
3464
                                else
3465
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString().Replace("%", ""));
3466

    
3467
                                AddWithValue(cmd, "@Pocket", string.IsNullOrEmpty(row["Pocket"].ToString()) ? "Yes" : row["Pocket"].ToString());
3468
                                AddWithValue(cmd, "@EGTag", string.IsNullOrEmpty(row["EGTag"].ToString()) ? "" : row["EGTag"].ToString());
3469
                                AddWithValue(cmd, "@HasMLTags", string.IsNullOrEmpty(row["HasMLTags"].ToString()) ? "False" : row["HasMLTags"].ToString());
3470

    
3471
                                connection.ExecuteNonQuery(cmd, txn);
3472
                            }
3473

    
3474
                            //Pipeline
3475
                            query = $"DELETE FROM {PSN_PIPELINE}";
3476
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3477
                            foreach (DataRow row in item.PipeLine.Rows)
3478
                            {
3479
                                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)";
3480
                                var cmd = connection.GetSqlStringCommand(query);
3481
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3482
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
3483
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
3484
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
3485
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
3486
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
3487
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
3488
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
3489
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
3490
                                connection.ExecuteNonQuery(cmd, txn);
3491
                            }
3492

    
3493
                            //PipeSystem
3494
                            query = $"DELETE FROM {PSN_PIPESYSTEM}";
3495
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3496
                            foreach (DataRow row in item.PipeSystem.Rows)
3497
                            {
3498
                                query = $"INSERT INTO {PSN_PIPESYSTEM} (OID, DESCRIPTION, FLUID, PMC, PipeLineQty, GroundLevel) VALUES (@OID, @DESCRIPTION, @FLUID, @PMC, @PipeLineQty, @GroundLevel)";
3499
                                var cmd = connection.GetSqlStringCommand(query);
3500
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3501
                                AddWithValue(cmd, "@DESCRIPTION", row["DESCRIPTION"].ToString());
3502
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
3503
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
3504
                                AddWithValue(cmd, "@PipeLineQty", row["PipeLineQty"].ToString());
3505
                                AddWithValue(cmd, "@GroundLevel", row["GroundLevel"].ToString());
3506
                                connection.ExecuteNonQuery(cmd, txn);
3507
                            }
3508

    
3509
                            //Header Setting
3510
                            query = $"DELETE FROM {PSN_HEADER_SETTING}";
3511
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3512

    
3513
                            foreach (HeaderInfo headerInfo in headerInfos)
3514
                            {
3515
                                foreach (HeaderItem itemheader in headerInfo.HeaderItems)
3516
                                {
3517
                                    query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
3518
                                    var cmd = connection.GetSqlStringCommand(query);
3519
                                    AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
3520
                                    AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
3521
                                    AddWithValue(cmd, "@INDEX", itemheader.Index);
3522
                                    AddWithValue(cmd, "@NAME", itemheader.Name);
3523
                                    connection.ExecuteNonQuery(cmd, txn);
3524
                                }
3525
                            }
3526

    
3527
                            //Vent/Drain Setting
3528
                            query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
3529
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3530

    
3531
                            foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
3532
                            {
3533
                                foreach (VentDrainItem itemventdrain in ventDrainInfo.VentDrainItems)
3534
                                {
3535
                                    query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
3536
                                    var cmd = connection.GetSqlStringCommand(query);
3537
                                    AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
3538
                                    AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
3539
                                    AddWithValue(cmd, "@INDEX", itemventdrain.Index);
3540
                                    AddWithValue(cmd, "@NAME", itemventdrain.Name);
3541
                                    connection.ExecuteNonQuery(cmd, txn);
3542
                                }
3543
                            }
3544

    
3545
                            //Keyword Setting
3546
                            query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
3547
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3548

    
3549
                            foreach (KeywordItem itemKeyword in keywordItems)
3550
                            {
3551
                                query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
3552
                                var cmd = connection.GetSqlStringCommand(query);
3553
                                AddWithValue(cmd, "@INDEX", itemKeyword.Index);
3554
                                AddWithValue(cmd, "@NAME", itemKeyword.Name);
3555
                                AddWithValue(cmd, "@KEYWORD", itemKeyword.Keyword);
3556
                                connection.ExecuteNonQuery(cmd, txn);
3557
                            }
3558

    
3559
                            //FulidCode
3560
                            query = $"DELETE FROM {PSN_FLUIDCODE}";
3561
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3562

    
3563
                            foreach (DataRow row in dtFluidCode.Rows)
3564
                            {
3565
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
3566
                                var cmd = connection.GetSqlStringCommand(query);
3567
                                cmd.Parameters.Clear();
3568

    
3569
                                {
3570
                                    var param = cmd.CreateParameter();
3571
                                    param.ParameterName = "@UID";
3572
                                    param.Value = row["UID"].ToString();
3573
                                    cmd.Parameters.Add(param);
3574
                                }
3575

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

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

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

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

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

    
3611
                                connection.ExecuteNonQuery(cmd, txn);
3612
                            }
3613

    
3614
                            //PMC
3615
                            query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
3616
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3617

    
3618
                            foreach (DataRow row in dtPMC.Rows)
3619
                            {
3620
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
3621
                                var cmd = connection.GetSqlStringCommand(query);
3622
                                cmd.Parameters.Clear();
3623

    
3624
                                {
3625
                                    var param = cmd.CreateParameter();
3626
                                    param.ParameterName = "@UID";
3627
                                    param.Value = row["UID"].ToString();
3628
                                    cmd.Parameters.Add(param);
3629
                                }
3630

    
3631
                                {
3632
                                    var param = cmd.CreateParameter();
3633
                                    param.ParameterName = "@Priority";
3634
                                    param.Value = row["Priority"].ToString();
3635
                                    cmd.Parameters.Add(param);
3636
                                }
3637

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

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

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

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

    
3666
                                {
3667
                                    var param = cmd.CreateParameter();
3668
                                    param.ParameterName = "@GroundLevel";
3669
                                    param.Value = row["GroundLevel"].ToString();
3670
                                    cmd.Parameters.Add(param);
3671
                                }
3672

    
3673
                                connection.ExecuteNonQuery(cmd, txn);
3674
                            }
3675

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

    
3680
                            foreach (DataRow row in dtInsulation.Rows)
3681
                            {
3682
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
3683
                                var cmd = connection.GetSqlStringCommand(query);
3684
                                cmd.Parameters.Clear();
3685

    
3686
                                {
3687
                                    var param = cmd.CreateParameter();
3688
                                    param.ParameterName = "@UID";
3689
                                    param.Value = row["UID"].ToString();
3690
                                    cmd.Parameters.Add(param);
3691
                                }
3692

    
3693
                                {
3694
                                    var param = cmd.CreateParameter();
3695
                                    param.ParameterName = "@Code";
3696
                                    param.Value = row["Code"].ToString();
3697
                                    cmd.Parameters.Add(param);
3698
                                }
3699

    
3700
                                {
3701
                                    var param = cmd.CreateParameter();
3702
                                    param.ParameterName = "@Description";
3703
                                    param.Value = row["Description"].ToString();
3704
                                    cmd.Parameters.Add(param);
3705
                                }
3706

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

    
3714
                                connection.ExecuteNonQuery(cmd, txn);
3715
                            }
3716

    
3717
                            //Topology Rule
3718
                            query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
3719
                            var cmdtopology = connection.GetSqlStringCommand(query);
3720
                            //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3721
                            connection.ExecuteNonQuery(cmdtopology, txn);
3722

    
3723
                            foreach (DataRow row in dtTopologyRule.Rows)
3724
                            {
3725
                                query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
3726
                                cmdtopology = connection.GetSqlStringCommand(query);
3727
                                AddWithValue(cmdtopology, "@UID", row["UID"].ToString());
3728
                                connection.ExecuteNonQuery(cmdtopology, txn);
3729
                            }
3730

    
3731
                            //valve grouping
3732
                            query = $"DELETE FROM {PSN_VALVEGROUP_SETTING}";
3733
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3734

    
3735
                            foreach (DataRow row in dtvalvegrouping.Rows)
3736
                            {
3737
                                query = $"INSERT INTO {PSN_VALVEGROUP_SETTING} (OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName) " +
3738
                                    $"VALUES (@OID, @GroupType, @TagIdentifier, @AttributeName, @SppidSymbolName)";
3739
                                var cmd = connection.GetSqlStringCommand(query);
3740
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3741
                                AddWithValue(cmd, "@GroupType", row["GroupType"].ToString());
3742
                                AddWithValue(cmd, "@TagIdentifier", row["TagIdentifier"].ToString());
3743
                                AddWithValue(cmd, "@AttributeName", row["AttributeName"].ToString());
3744
                                AddWithValue(cmd, "@SppidSymbolName", row["SppidSymbolName"].ToString());
3745
                                connection.ExecuteNonQuery(cmd, txn);
3746
                            }
3747

    
3748
                            //no pocket Setting
3749
                            query = $"DELETE FROM {PSN_NOPOCKETSETTING}";
3750
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3751

    
3752
                            foreach (DataRow row in dtnopocket.Rows)
3753
                            {
3754
                                query = $"INSERT INTO {PSN_NOPOCKETSETTING} ([INDEX], [TYPE], [NAME]) VALUES (@INDEX, @TYPE, @NAME)";
3755
                                var cmd = connection.GetSqlStringCommand(query);
3756
                                AddWithValue(cmd, "@INDEX", row["INDEX"].ToString());
3757
                                AddWithValue(cmd, "@TYPE", row["TYPE"].ToString());
3758
                                AddWithValue(cmd, "@NAME", row["NAME"].ToString());
3759
                                connection.ExecuteNonQuery(cmd, txn);
3760
                            }
3761

    
3762
                            //air fin cooler Setting
3763
                            query = $"DELETE FROM {PSN_AIRFINCOOLERSETTING}";
3764
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3765
                             
3766
                            foreach (DataRow row in dtafc.Rows)
3767
                            {
3768
                                query = $"INSERT INTO {PSN_AIRFINCOOLERSETTING} ([INDEX], [TYPE], [TagIdentifier], [AttributeName], [NAME]) VALUES (@INDEX, @TYPE, @TagIdentifier, @AttributeName, @NAME)";
3769
                                var cmd = connection.GetSqlStringCommand(query);
3770
                                AddWithValue(cmd, "@INDEX", row["INDEX"].ToString());
3771
                                AddWithValue(cmd, "@TYPE", row["TYPE"].ToString());
3772
                                AddWithValue(cmd, "@TagIdentifier", row["TagIdentifier"].ToString());
3773
                                AddWithValue(cmd, "@AttributeName", row["AttributeName"].ToString());
3774
                                AddWithValue(cmd, "@NAME", row["NAME"].ToString());
3775
                                connection.ExecuteNonQuery(cmd, txn);
3776
                            }
3777

    
3778

    
3779
                            txn.Commit();
3780
                        }
3781
                        catch (Exception ex)
3782
                        {
3783
                            txn.Rollback();
3784
                            result = false;
3785
                        }
3786
                    }
3787
                }
3788
                catch (Exception ex)
3789
                {
3790
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3791
                    result = false;
3792
                }
3793
            }
3794

    
3795
            return result;
3796
        }
3797

    
3798
        public static bool CreatePSN_COMMON()
3799
        {
3800
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
3801

    
3802
            bool result = true;
3803
            using (IAbstractDatabase connection = id2Info.CreateConnection())
3804
            {
3805
                try
3806
                {
3807
                    using (var txn = connection.BeginTransaction())
3808
                    {
3809
                        try
3810
                        {
3811

    
3812
                            var query = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
3813
                            if (id2Info.ID2DBType == AnotherID2DB_Type.MSSQL)
3814
                            {
3815
                                connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3816
                            }
3817

    
3818
                            txn.Commit();
3819
                        }
3820
                        catch (Exception ex)
3821
                        {
3822
                            txn.Rollback();
3823
                            result = false;
3824
                        }
3825
                    }
3826
                }
3827
                catch (Exception ex)
3828
                {
3829
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3830
                    result = false;
3831
                }
3832
            }
3833

    
3834
            return result;
3835
        }
3836
    }
3837
}
3838

    
3839

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