프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ 531fb158

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

    
62
                    }
63
                    connection.Close();
64
                }
65
            }
66
            catch (Exception ex)
67
            {
68
                System.Windows.Forms.MessageBox.Show(ex.Message);
69
            }
70

    
71
            dt.AcceptChanges();
72
            dt.DefaultView.Sort = "Name";
73
            dt = dt.DefaultView.ToTable();
74

    
75
            return dt;
76
        }
77

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

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

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

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

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

    
143
                        DataTable topologyRule = new DataTable();
144
                        topologyRule.Columns.Add("NAME", typeof(string));
145

    
146
                        topologyRule.Rows.Add("FluidCode");
147
                        topologyRule.Rows.Add("-");
148
                        topologyRule.Rows.Add("PipingMaterialsClass");
149
                        topologyRule.Rows.Add("-");
150
                        topologyRule.Rows.Add("Tag Seq No");
151

    
152
                        SaveTopologyRule(topologyRule);
153
                    }
154
                    //else
155
                    //{
156
                    //    AddColumn(PSN_TOPOLOGY_RULE, dicColCheck);
157
                    //}
158

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

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

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

    
210
                    dicColCheck.Clear();
211
                    dicColCheck.Add("OID", "NVARCHAR(255)");
212
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
213
                    dicColCheck.Add("Xcoords", "REAL");
214
                    dicColCheck.Add("Ycoords", "REAL");
215
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
216
                    dicColCheck.Add("Fluid", "NVARCHAR(255)");
217
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
218
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
219
                    dicColCheck.Add("Rotation", "REAL");
220
                    dicColCheck.Add("FlowDirection", "NVARCHAR(255)");
221

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

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

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

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

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

    
335
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
336
                    dicColCheck.Clear();
337
                    dicColCheck.Add("OID", "TEXT");
338
                    if (matched == null)
339
                    {
340
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
341
                        using (var cmd = connection.GetSqlStringCommand(query))
342
                        {
343
                            cmd.ExecuteNonQuery();
344
                        }
345
                    }
346

    
347
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
348
                    dicColCheck.Clear();
349
                    dicColCheck.Add("OID", "NVARCHAR(255)");
350
                    dicColCheck.Add("Type", "NVARCHAR(255)");
351
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
352
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
353
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
354
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
355
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
356

    
357
                    if (matched == null)
358
                    {
359
                        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))";
360
                        using (var cmd = connection.GetSqlStringCommand(query))
361
                        {
362
                            cmd.ExecuteNonQuery();
363
                        }
364
                    }
365
                    else
366
                    {
367
                        AddColumn(PSN_TOPOLOGYSET, dicColCheck);
368
                    }
369

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

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

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

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

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

    
474
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
475
                    {
476
                        var colnames = connection.GetColumnNames(TableName);
477
                        bool check = false;
478
                        if (colnames != null)
479
                        {
480
                            foreach (KeyValuePair<string, string> col in dicCol)
481
                            {
482
                                check = false;
483
                                foreach (string c in colnames)
484
                                {
485
                                    if (col.Key.Contains(c))
486
                                    {
487
                                        check = true;
488
                                        break;
489
                                    }
490
                                }
491

    
492
                                if (!check) //없으면 추가
493
                                {
494
                                    string i = string.Empty;
495
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
496
                                        i = "DEFAULT 0";
497

    
498
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
499
                                    using (var cmd = connection.GetSqlStringCommand(query))
500
                                    {
501
                                        cmd.ExecuteNonQuery();
502
                                    }
503
                                }
504
                            }
505
                        }
506
                    }
507

    
508
                    result = true;
509
                }
510
                catch (Exception ex)
511
                {
512
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
513
                }
514
            }
515

    
516
            return result;
517
        }
518

    
519
        // ID2 DB 데이터
520
        /// <summary>
521
        /// ID2 데이타베이스에서 OPC 데이터를 조회
522
        /// </summary>
523
        /// <returns></returns>
524
        public static DataTable SelectOPCRelations()
525
        {
526
            DataTable dt = null;
527
            ID2Info id2Info = ID2Info.GetInstance();
528

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

    
545
            return dt;
546
        }
547

    
548
        /// <summary>
549
        /// ID2 데이타베이스에서 도면 데이터를 조회
550
        /// </summary>
551
        /// <returns></returns>
552
        public static DataTable SelectDrawings()
553
        {
554
            DataTable dt = null;
555
            ID2Info id2Info = ID2Info.GetInstance();
556

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

    
573
            return dt;
574
        }
575

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

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

    
597
            return dt;
598
        }
599

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

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

    
621
            return dt;
622
        }
623

    
624
        public static DataTable SelectPipingMaterialsClass()
625
        {
626
            DataTable dt = null;
627
            ID2Info id2Info = ID2Info.GetInstance();
628

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

    
645
            return dt;
646
        }
647

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

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

    
669
            return dt;
670
        }
671
        
672
        public static DataTable SelectInsulationPurpose()
673
        {
674
            DataTable dt = null;
675
            ID2Info id2Info = ID2Info.GetInstance();
676

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

    
693
            return dt;
694
        }
695

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

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

    
717
            return dt;
718
        }
719

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

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

    
741
            ///TODO: need to check below code
742
            dt.Rows.RemoveAt(0);
743
            dt.Rows.RemoveAt(0);
744
            dt.Rows.RemoveAt(0);
745
            dt.Rows.RemoveAt(0);
746

    
747
            return dt;
748
        }
749

    
750
        public static DataTable SelectAllSymbolAttribute()
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 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; ";
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 SelectSymbolAttribute()
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 DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
784
                    var query = "SELECT DISTINCT Attribute FROM SymbolAttribute;";
785
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
786
                    {
787
                        dt = ds.Tables[0].Copy();
788
                    }
789
                }
790
                catch (Exception ex)
791
                {
792
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
793
                }
794
            }
795

    
796
            return dt;
797
        }
798

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

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

    
820
            return dt;
821
        }
822

    
823
        public static double[] GetDrawingSize()
824
        {
825
            double[] result = null;
826

    
827
            ID2Info id2Info = ID2Info.GetInstance();
828
            using (IAbstractDatabase connection = id2Info.CreateConnection())
829
            {
830
                try
831
                {
832
                    var query = "SELECT value FROM Configuration WHERE Section = 'Area' AND [Key] = 'Drawing';";
833
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
834
                    {
835
                        if (ds.Tables[0].Rows.Count == 1)
836
                        {
837
                            string value = ds.Tables[0].Rows[0][0].ToString();
838
                            string[] split = value.Split(new char[] { ',' });
839
                            result = new double[] {
840
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", string.Empty)),
841
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", string.Empty)),
842
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", string.Empty)),
843
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", string.Empty))
844
                                };
845
                            result = new double[] {
846
                                Math.Min(result[0], result[2]),
847
                                Math.Min(result[1], result[3]),
848
                                Math.Max(result[0], result[2]),
849
                                Math.Max(result[1], result[3])
850
                                };
851
                        }
852
                    }
853
                }
854
                catch (Exception ex)
855
                {
856
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
857
                }
858
            }
859

    
860
            return result;
861
        }
862

    
863
        public static DataTable GetEquipmentType()
864
        {
865
            DataTable dt = null;
866
            ID2Info id2Info = ID2Info.GetInstance();
867

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

    
884
            return dt;
885
        }
886

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

    
898
            bool result = true;
899

    
900
            using (IAbstractDatabase connection = id2Info.CreateConnection())
901
            {
902
                try
903
                {
904
                    using (var txn = connection.BeginTransaction())
905
                    {
906
                        try
907
                        {
908
                            var query = $"DELETE FROM {PSN_VIEW}";
909
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
910

    
911
                            foreach (string value in values)
912
                            {
913
                                query = $"INSERT INTO {PSN_VIEW} (OID) VALUES (@OID)";
914
                                var cmd = connection.GetSqlStringCommand(query);
915
                                AddWithValue(cmd, "@OID", value);
916
                                connection.ExecuteNonQuery(cmd, txn);
917
                            }
918
                            txn.Commit();
919
                        }
920
                        catch (Exception ex)
921
                        {
922
                            txn.Rollback();
923
                            result = false;
924
                        }
925
                    }
926
                }
927
                catch (Exception ex)
928
                {
929
                    System.Windows.Forms.MessageBox.Show(ex.Message);
930
                    result = false;
931
                }
932
            }
933

    
934
            return result;
935
        }
936

    
937
        public static bool DeleteView()
938
        {
939
            ID2Info id2Info = ID2Info.GetInstance();
940

    
941
            bool result = true;
942
            using (IAbstractDatabase connection = id2Info.CreateConnection())
943
            {
944
                try
945
                {
946
                    using (var txn = connection.BeginTransaction())
947
                    {
948
                        try
949
                        {
950
                            var query = $"DELETE FROM {PSN_VIEW}";
951
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
952
                            txn.Commit();
953
                        }
954
                        catch (Exception ex)
955
                        {
956
                            txn.Rollback();
957
                            result = false;
958
                        }
959
                    }
960
                }
961
                catch (Exception ex)
962
                {
963
                    System.Windows.Forms.MessageBox.Show(ex.Message);
964
                    result = false;
965
                }
966
            }
967

    
968
            return result;
969
        }
970

    
971
        //PSN Sqlite 
972
        public static DataTable SelectHeaderSetting()
973
        {
974
            DataTable dt = null;
975
            ID2Info id2Info = ID2Info.GetInstance();
976

    
977
            using (IAbstractDatabase connection = id2Info.CreateConnection())
978
            {
979
                try
980
                {
981
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_HEADER_SETTING};";
982
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
983
                    {
984
                        dt = ds.Tables[0].Copy();
985
                    }
986
                }
987
                catch (Exception ex)
988
                {
989
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
990
                }
991
            }
992

    
993
            return dt;
994
        }
995

    
996
        public static DataTable SelectVentDrainSetting()
997
        {
998
            DataTable dt = null;
999
            ID2Info id2Info = ID2Info.GetInstance();
1000

    
1001
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1002
            {
1003
                try
1004
                {
1005
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_VENTDRAIN_SETTING};";
1006
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1007
                    {
1008
                        dt = ds.Tables[0].Copy();
1009
                    }
1010
                }
1011
                catch (Exception ex)
1012
                {
1013
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1014
                }
1015
            }
1016

    
1017
            return dt;
1018
        }
1019

    
1020
        public static DataTable SelectKeywordsSetting()
1021
        {
1022
            DataTable dt = null;
1023
            ID2Info id2Info = ID2Info.GetInstance();
1024

    
1025
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1026
            {
1027
                try
1028
                {
1029
                    var query = $@"SELECT [KEYWORD], [INDEX], [NAME] FROM {PSN_TRANSFORMKEYWORD_SETTING};";
1030
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1031
                    {
1032
                        dt = ds.Tables[0].Copy();
1033
                    }
1034
                }
1035
                catch (Exception ex)
1036
                {
1037
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1038
                }
1039
            }
1040

    
1041
            return dt;
1042
        }
1043

    
1044
        public static DataTable SelectValveGroupItemsSetting()
1045
        {
1046
            DataTable dt = null;
1047
            ID2Info id2Info = ID2Info.GetInstance();
1048

    
1049
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1050
            {
1051
                try
1052
                {
1053
                    var query = $@"SELECT OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName FROM {PSN_VALVEGROUP_SETTING};";
1054
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1055
                    {
1056
                        dt = ds.Tables[0].Copy();
1057
                    }
1058
                }
1059
                catch (Exception ex)
1060
                {
1061
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1062
                }
1063
            }
1064

    
1065
            return dt;
1066
        }
1067

    
1068
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
1069
        {
1070
            ID2Info id2Info = ID2Info.GetInstance();
1071
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1072
            {
1073
                try
1074
                {
1075
                    using (var txn = connection.BeginTransaction())
1076
                    {
1077
                        var query = $"DELETE FROM {PSN_HEADER_SETTING}";
1078
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1079

    
1080
                        foreach (HeaderInfo headerInfo in headerInfos)
1081
                        {
1082
                            foreach (HeaderItem item in headerInfo.HeaderItems)
1083
                            {
1084
                                query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
1085
                                var cmd = connection.GetSqlStringCommand(query);
1086
                                AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
1087
                                AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
1088
                                AddWithValue(cmd, "@INDEX", item.Index);
1089
                                AddWithValue(cmd, "@NAME", item.Name);
1090
                                connection.ExecuteNonQuery(cmd, txn);
1091
                            }
1092
                        }
1093
                        txn.Commit();
1094
                    }
1095
                    
1096
                }
1097
                catch (Exception ex)
1098
                {
1099
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1100
                    return false;
1101
                }
1102
            }
1103
            return true;
1104
        }
1105

    
1106
        public static bool SaveVentDrainSetting(List<VentDrainInfo> ventDrainInfos)
1107
        {
1108
            ID2Info id2Info = ID2Info.GetInstance();
1109
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1110
            {
1111
                using (var txn = connection.BeginTransaction())
1112
                {
1113
                    try
1114
                    {
1115
                        var query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
1116
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1117

    
1118
                        foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
1119
                        {
1120
                            foreach (VentDrainItem item in ventDrainInfo.VentDrainItems)
1121
                            {
1122
                                query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
1123
                                var cmd = connection.GetSqlStringCommand(query);
1124
                                AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
1125
                                AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
1126
                                AddWithValue(cmd, "@INDEX", item.Index);
1127
                                AddWithValue(cmd, "@NAME", item.Name);
1128
                                connection.ExecuteNonQuery(cmd, txn);
1129
                            }
1130
                        }
1131

    
1132
                        txn.Commit();
1133
                    }
1134
                    catch (Exception ex)
1135
                    {
1136
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1137
                        return false;
1138
                    }
1139
                }
1140
            }
1141

    
1142
            return true;
1143
        }
1144

    
1145
        public static bool SaveValveGroupItemsSetting(List<ValveGroupItem> valveGroupItems)
1146
        {
1147
            ID2Info id2Info = ID2Info.GetInstance();
1148
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1149
            {
1150
                using (var txn = connection.BeginTransaction())
1151
                {
1152
                    try
1153
                    {
1154
                        var query = $"DELETE FROM {PSN_VALVEGROUP_SETTING}";
1155
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1156

    
1157
                        foreach (ValveGroupItem item in valveGroupItems)
1158
                        {
1159
                            query = $"INSERT INTO {PSN_VALVEGROUP_SETTING} (OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName) " +
1160
                                $"VALUES (@OID, @GroupType, @TagIdentifier, @AttributeName, @SppidSymbolName)";
1161
                            var cmd = connection.GetSqlStringCommand(query);
1162
                            AddWithValue(cmd, "@OID", item.OID);
1163
                            AddWithValue(cmd, "@GroupType", item.GroupType);
1164
                            AddWithValue(cmd, "@TagIdentifier", item.TagIdentifier);
1165
                            AddWithValue(cmd, "@AttributeName", item.AttributeName);
1166
                            AddWithValue(cmd, "@SppidSymbolName", item.SppidSymbolName);
1167
                            connection.ExecuteNonQuery(cmd, txn);
1168
                        }
1169

    
1170
                        txn.Commit();
1171
                    }
1172
                    catch (Exception ex)
1173
                    {
1174
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1175
                        return false;
1176
                    }
1177
                }
1178
            }
1179

    
1180
            return true;
1181
        }
1182

    
1183
        public static bool SaveKeywordsSetting(List<KeywordItem> keywordItems)
1184
        {
1185
            ID2Info id2Info = ID2Info.GetInstance();
1186
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1187
            {
1188
                using (var txn = connection.BeginTransaction())
1189
                {
1190
                    try
1191
                    {
1192
                        var query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
1193
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1194
                        
1195
                        foreach (KeywordItem item in keywordItems)
1196
                        {
1197
                            query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
1198
                            var cmd = connection.GetSqlStringCommand(query);
1199
                            AddWithValue(cmd, "@INDEX", item.Index);
1200
                            AddWithValue(cmd, "@NAME", item.Name);
1201
                            AddWithValue(cmd, "@KEYWORD", item.Keyword);                            
1202
                            connection.ExecuteNonQuery(cmd, txn);
1203
                        }
1204
                        
1205
                        txn.Commit();
1206
                    }
1207
                    catch (Exception ex)
1208
                    {
1209
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1210
                        return false;
1211
                    }
1212
                }
1213
            }
1214

    
1215
            return true;
1216
        }
1217

    
1218
        public static bool SaveTopologyRule(DataTable dt)
1219
        {
1220
            ID2Info id2Info = ID2Info.GetInstance();
1221
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1222
            {
1223
                using (var txn = connection.BeginTransaction())
1224
                {
1225
                    try
1226
                    {
1227
                        var query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
1228
                        var cmd = connection.GetSqlStringCommand(query);
1229
                        //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1230
                        connection.ExecuteNonQuery(cmd, txn);
1231

    
1232
                        foreach (DataRow row in dt.Rows)
1233
                        {
1234
                            query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
1235
                            cmd = connection.GetSqlStringCommand(query);
1236
                            AddWithValue(cmd, "@UID", row["NAME"].ToString());
1237
                            connection.ExecuteNonQuery(cmd, txn);
1238
                        }
1239

    
1240
                        txn.Commit();
1241
                    }
1242
                    catch (Exception ex)
1243
                    {
1244
                        txn.Rollback();
1245
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1246
                        return false;
1247
                    }
1248
                }
1249
            }
1250

    
1251
            return true;
1252
        }
1253

    
1254
        public static DataTable SelectTopologyRule()
1255
        {
1256
            DataTable dt = null;
1257

    
1258
            ID2Info id2Info = ID2Info.GetInstance();
1259
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1260
            {
1261
                try
1262
                {
1263
                    var query = $"SELECT * FROM {PSN_TOPOLOGY_RULE}";
1264
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1265
                    {
1266
                        dt = ds.Tables[0].Copy();
1267
                    }
1268
                }
1269
                catch (Exception ex)
1270
                {
1271
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1272
                }
1273
            }
1274

    
1275
            return dt;
1276
        }
1277

    
1278
        private static void AddWithValue(DbCommand cmd, string PropName, object Value)
1279
        {
1280
            var param = cmd.CreateParameter();
1281
            param.ParameterName = PropName;
1282
            param.Value = Value;
1283
            cmd.Parameters.Add(param);
1284
        }
1285

    
1286
        public static DataTable SelectRevisionTable()
1287
        {
1288
            DataTable dt = null;
1289
            ID2Info id2Info = ID2Info.GetInstance();
1290

    
1291
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1292
            {
1293
                try
1294
                {
1295
                    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 = 'PSN_REVISION'";
1296
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1297
                    {
1298
                        dt = ds.Tables[0].Copy();
1299
                    }
1300
                }
1301
                catch (Exception ex)
1302
                {
1303
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1304
                }
1305
            }
1306

    
1307
            return dt;
1308
        }
1309
        public static bool ConnCreateRevisionTable(Dictionary<string, string> dicColCheck, List<string> names)
1310
        {
1311
            bool result = false;
1312
            ID2Info id2Info = ID2Info.GetInstance();
1313

    
1314
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1315
            {
1316
                try
1317
                {
1318
                    if (names.Count == 0)
1319
                    {
1320
                        
1321
                        var query = $"CREATE TABLE ARS_COMMON.dbo.PSNRevision ([OID] [uniqueidentifier] ,[Project_OID] [uniqueidentifier] ," +
1322
                            $"[ProjectCode] nvarchar(255), [RevNumber] int ,[UserName] nvarchar(255) ,[TimeData] nvarchar(255) ," +
1323
                            $"[PSNDatabasePath] nvarchar(255) ,	[PsnByPBSFilter] varchar(20) ,[PsnByNPDFilter] varchar(20) ,[PidDrawings] int ,	" +
1324
                            $"[PipeSystems] int ,[PipeLines] int ,[Topologies] int ,[PipeSystemNetworks] int ,[InValidPSNs] int ,[E2E] int ," +
1325
                            $"[E2B] int ,[B2E] int ,[HDE] int ,[HD2] int ,[HDB] int ,[B2B] int )";
1326
                        using (var cmd = connection.GetSqlStringCommand(query))
1327
                        {
1328
                            cmd.ExecuteNonQuery();
1329
                        }
1330
                    }
1331
                    else
1332
                    {
1333
                        AddColumn(PSN_REVISION, dicColCheck, names);
1334
                    }
1335

    
1336
                    void AddColumn(string TableName, Dictionary<string, string> dicCol, List<string> colnames)
1337
                    {
1338
                        bool check = false;
1339
                        if (colnames != null)
1340
                        {
1341
                            foreach (KeyValuePair<string, string> col in dicCol)
1342
                            {
1343
                                check = false;
1344
                                foreach (string c in colnames)
1345
                                {
1346
                                    if (col.Key.Contains(c))
1347
                                    {
1348
                                        check = true;
1349
                                        break;
1350
                                    }
1351
                                }
1352

    
1353
                                if (!check) //없으면 추가
1354
                                {
1355
                                    string i = string.Empty;
1356
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
1357
                                        i = "DEFAULT 0";
1358

    
1359
                                    var queryalter = $"ALTER TABLE ARS_COMMON.dbo.PSNRevision ADD [" + col.Key + "] " + col.Value + " " + i + ";";
1360
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
1361
                                    {
1362
                                        cmd.ExecuteNonQuery();
1363
                                    }
1364
                                }
1365
                            }
1366
                        }
1367
                    }
1368

    
1369
                    result = true;
1370
                }
1371
                catch (Exception ex)
1372
                {
1373
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1374
                }
1375
            }
1376

    
1377
            return result;
1378
        }
1379

    
1380
        public static bool SaveRevision(string ProjectCode, int RevNumber, string UserName, string PSNDatabasePath, string PsnByPBSFilter, string PsnByNPDFilter,
1381
            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)
1382
        {
1383
            ID2Info id2Info = ID2Info.GetInstance();
1384

    
1385
            bool result = true;
1386

    
1387
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1388
            {
1389
                try
1390
                {
1391
                    using (var txn = connection.BeginTransaction())
1392
                    {
1393
                        try
1394
                        {                           
1395
                         
1396
                            var query = $"INSERT INTO ARS_COMMON.dbo.PSNRevision (OID, Project_OID, ProjectCode, RevNumber, UserName, TimeData, PSNDatabasePath, " +
1397
                                $"PsnByPBSFilter, PsnByNPDFilter, PidDrawings, PipeSystems, PipeLines, Topologies, PipeSystemNetworks, InValidPSNs, E2E, E2B, B2E, HDE, " +
1398
                                $"HD2, HDB, B2B) VALUES (NEWID(), NEWID(), @ProjectCode, @RevNumber, @UserName, @TimeData, @PSNDatabasePath" +
1399
                                $", @PsnByPBSFilter, @PsnByNPDFilter, @PidDrawings, @PipeSystems, @PipeLines, @Topologies, @PipeSystemNetworks, @InValidPSNs, " +
1400
                                $"@E2E, @E2B, @B2E, @HDE, @HD2, @HDB, @B2B)";
1401
                            var cmd = connection.GetSqlStringCommand(query);
1402
                            AddWithValue(cmd, "@OID", Guid.NewGuid().ToString());
1403
                            AddWithValue(cmd, "@Project_OID", Guid.NewGuid().ToString());
1404
                            AddWithValue(cmd, "@ProjectCode", ProjectCode);
1405
                            AddWithValue(cmd, "@RevNumber", RevNumber);
1406
                            AddWithValue(cmd, "@UserName", UserName);
1407
                            AddWithValue(cmd, "@TimeData", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
1408
                            AddWithValue(cmd, "@PSNDatabasePath", PSNDatabasePath);
1409

    
1410
                            AddWithValue(cmd, "@PsnByPBSFilter", PsnByPBSFilter);
1411
                            AddWithValue(cmd, "@PsnByNPDFilter", PsnByNPDFilter);
1412
                            AddWithValue(cmd, "@PidDrawings", PidDrawings);
1413
                            AddWithValue(cmd, "@PipeSystems", PipeSystems);
1414
                            AddWithValue(cmd, "@PipeLines", PipeLines);
1415

    
1416
                            AddWithValue(cmd, "@Topologies", Topologies);
1417
                            AddWithValue(cmd, "@PipeSystemNetworks", PipeSystemNetworks);
1418
                            AddWithValue(cmd, "@InValidPSNs", InValidPSNs);
1419
                            AddWithValue(cmd, "@E2E", E2E);
1420
                            AddWithValue(cmd, "@E2B", E2B);
1421
                            AddWithValue(cmd, "@B2E", B2E);
1422
                            AddWithValue(cmd, "@HDE", HDE);
1423
                            AddWithValue(cmd, "@HD2", HD2);
1424
                            AddWithValue(cmd, "@HDB", HDB);
1425
                            AddWithValue(cmd, "@B2B", B2B);
1426
                            connection.ExecuteNonQuery(cmd, txn);
1427
                            
1428
                            txn.Commit();
1429
                        }
1430
                        catch (Exception ex)
1431
                        {
1432
                            txn.Rollback();
1433
                            result = false;
1434
                        }
1435
                    }
1436
                }
1437
                catch (Exception ex)
1438
                {
1439
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1440
                    result = false;
1441
                }
1442
            }
1443

    
1444
            return result;
1445
        }
1446

    
1447
        public static bool SavePSNData(PSN item)
1448
        {
1449
            ID2Info id2Info = ID2Info.GetInstance();
1450

    
1451
            bool result = true;            
1452

    
1453
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1454
            {
1455
                try
1456
                {
1457
                    using (var txn = connection.BeginTransaction())
1458
                    {
1459
                        try
1460
                        {
1461
                            // Path Items
1462
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
1463
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1464
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
1465
                            {
1466
                                DataRow row = item.PathItems.Rows[i];
1467
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
1468
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, " +
1469
                                    $"PIDNAME, Equipment_OID, NPD, GROUPTAG, PipeSystemNetwork_OID, PipeRun_OID, ViewPipeSystemNetwork_OID) VALUES " +
1470
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME, " +
1471
                                    $"@Equipment_OID, @NPD, @GROUPTAG, @PipeSystemNetwork_OID,@PipeRun_OID, @ViewPipeSystemNetwork_OID)";
1472
                                var cmd = connection.GetSqlStringCommand(query);
1473
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1474
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
1475
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
1476
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
1477
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
1478
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
1479
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1480
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString());
1481
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
1482
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
1483
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
1484
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
1485
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1486
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1487
                                AddWithValue(cmd, "@GROUPTAG", string.IsNullOrEmpty(row["GROUPTAG"].ToString()) ? "" : row["GROUPTAG"].ToString());
1488
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
1489
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
1490
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
1491
                                connection.ExecuteNonQuery(cmd, txn);
1492
                            }
1493

    
1494
                            // Sequence
1495
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
1496
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1497
                            foreach (DataRow row in item.SequenceData.Rows)
1498
                            {
1499
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
1500
                                var cmd = connection.GetSqlStringCommand(query);
1501
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1502
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
1503
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
1504
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1505
                                connection.ExecuteNonQuery(cmd, txn);
1506
                            }
1507

    
1508
                            // Nozzle
1509
                            query = $"DELETE FROM {PSN_NOZZLE}";
1510
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1511
                            foreach (DataRow row in item.Nozzle.Rows)
1512
                            {
1513
                                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)";
1514
                                var cmd = connection.GetSqlStringCommand(query);
1515
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1516
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1517

    
1518
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1519
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1520
                                else
1521
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1522

    
1523
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1524
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1525
                                else
1526
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1527

    
1528
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1529
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
1530
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1531
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
1532

    
1533
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
1534
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
1535
                                else
1536
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
1537

    
1538
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
1539
                                connection.ExecuteNonQuery(cmd, txn);
1540
                            }
1541

    
1542
                            //Equipment
1543
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
1544
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1545
                            foreach (DataRow row in item.Equipment.Rows)
1546
                            {
1547
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
1548
                                var cmd = connection.GetSqlStringCommand(query);
1549
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1550
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1551

    
1552
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1553
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1554
                                else
1555
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1556

    
1557
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1558
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1559
                                else
1560
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1561

    
1562
                                connection.ExecuteNonQuery(cmd, txn);
1563
                            }
1564

    
1565
                            // TopologySet
1566
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
1567
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1568
                            foreach (DataRow row in item.TopologySet.Rows)
1569
                            {
1570
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
1571
                                var cmd = connection.GetSqlStringCommand(query);
1572
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1573
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1574
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
1575
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
1576
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
1577
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
1578
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
1579
                                connection.ExecuteNonQuery(cmd, txn);
1580
                            }
1581

    
1582
                            // PSN
1583
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
1584
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1585
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
1586
                            {
1587
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
1588
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy) VALUES " +
1589
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy)";
1590
                                var cmd = connection.GetSqlStringCommand(query);
1591
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1592
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1593
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
1594
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
1595
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
1596
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
1597
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1598
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
1599

    
1600
                                int IsValid = 0;
1601
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
1602
                                    IsValid = 0;
1603
                                else if (row["IsValid"].ToString() == "InValid")
1604
                                    IsValid = 1;
1605
                                else if (row["IsValid"].ToString() == "Error")
1606
                                    IsValid = -1;
1607

    
1608
                                AddWithValue(cmd, "@IsValid", IsValid);
1609
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
1610

    
1611
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
1612
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
1613

    
1614
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
1615
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
1616
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
1617
                                else
1618
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString());
1619

    
1620
                                connection.ExecuteNonQuery(cmd, txn);
1621
                            }
1622

    
1623
                            //Pipeline
1624
                            query = $"DELETE FROM {PSN_PIPELINE}";
1625
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1626
                            foreach (DataRow row in item.PipeLine.Rows)
1627
                            {
1628
                                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)";
1629
                                var cmd = connection.GetSqlStringCommand(query);
1630
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
1631
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
1632
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
1633
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
1634
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
1635
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
1636
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
1637
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
1638
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
1639
                                connection.ExecuteNonQuery(cmd, txn);
1640
                            }
1641

    
1642
                            //PipeSystem
1643
                            query = $"DELETE FROM {PSN_PIPESYSTEM}";
1644
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1645
                            foreach (DataRow row in item.PipeSystem.Rows)
1646
                            {
1647
                                query = $"INSERT INTO {PSN_PIPESYSTEM} (OID, DESCRIPTION, FLUID, PMC, PipeLineQty, GroundLevel) VALUES (@OID, @DESCRIPTION, @FLUID, @PMC, @PipeLineQty, @GroundLevel)";
1648
                                var cmd = connection.GetSqlStringCommand(query);
1649
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
1650
                                AddWithValue(cmd, "@DESCRIPTION", row["DESCRIPTION"].ToString());
1651
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
1652
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
1653
                                AddWithValue(cmd, "@PipeLineQty", row["PipeLineQty"].ToString());
1654
                                AddWithValue(cmd, "@GroundLevel", row["GroundLevel"].ToString());
1655
                                connection.ExecuteNonQuery(cmd, txn);
1656
                            }
1657

    
1658
                            query = $"If(db_id(N'" + PSN_COMMON + "') IS NULL) CREATE DATABASE [" + PSN_COMMON + "]";
1659
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1660
                          
1661

    
1662
                            txn.Commit();
1663
                        }
1664
                        catch (Exception ex)
1665
                        {
1666
                            txn.Rollback();
1667
                            result = false;
1668
                        }
1669

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

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

    
1694
                                    var queryalter = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
1695
                                    using (var cmd = connection.GetSqlStringCommand(queryalter))
1696
                                    {
1697
                                        cmd.ExecuteNonQuery();
1698
                                    }
1699
                                }
1700
                            }
1701
                        }
1702
                    }
1703
                }
1704
                catch (Exception ex)
1705
                {
1706
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1707
                    result = false;
1708
                }
1709
            }
1710

    
1711
            return result;
1712
        }
1713

    
1714
        public static bool SavePSNFluidCode(DataTable dt)
1715
        {
1716
            ID2Info id2Info = ID2Info.GetInstance();
1717

    
1718
            bool result = true;
1719
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1720
            {
1721
                try
1722
                {
1723
                    using (var txn = connection.BeginTransaction())
1724
                    {
1725
                        try
1726
                        {
1727
                            var query = $"DELETE FROM {PSN_FLUIDCODE}";
1728
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1729

    
1730
                            foreach (DataRow row in dt.Rows)
1731
                            {
1732
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1733
                                var cmd = connection.GetSqlStringCommand(query);
1734
                                cmd.Parameters.Clear();
1735

    
1736
                                {
1737
                                    var param = cmd.CreateParameter();
1738
                                    param.ParameterName = "@UID";
1739
                                    param.Value = row["UID"].ToString();
1740
                                    cmd.Parameters.Add(param);
1741
                                }
1742

    
1743
                                {
1744
                                    var param = cmd.CreateParameter();
1745
                                    param.ParameterName = "@Code";
1746
                                    param.Value = row["Code"].ToString();
1747
                                    cmd.Parameters.Add(param);
1748
                                }
1749

    
1750
                                {
1751
                                    var param = cmd.CreateParameter();
1752
                                    param.ParameterName = "@Description";
1753
                                    param.Value = row["Description"].ToString();
1754
                                    cmd.Parameters.Add(param);
1755
                                }
1756

    
1757
                                {
1758
                                    var param = cmd.CreateParameter();
1759
                                    param.ParameterName = "@Condition";
1760
                                    param.Value = row["Condition"].ToString();
1761
                                    cmd.Parameters.Add(param);
1762
                                }
1763

    
1764
                                {
1765
                                    var param = cmd.CreateParameter();
1766
                                    param.ParameterName = "@Remarks";
1767
                                    param.Value = row["Remarks"].ToString();
1768
                                    cmd.Parameters.Add(param);
1769
                                }
1770

    
1771
                                {
1772
                                    var param = cmd.CreateParameter();
1773
                                    param.ParameterName = "@GroundLevel";
1774
                                    param.Value = row["GroundLevel"].ToString();
1775
                                    cmd.Parameters.Add(param);
1776
                                }
1777

    
1778
                                connection.ExecuteNonQuery(cmd, txn);
1779
                            }
1780
                            txn.Commit();
1781
                        }
1782
                        catch (Exception ex)
1783
                        {
1784
                            txn.Rollback();
1785
                            result = false;
1786
                        }
1787
                    }
1788
                }
1789
                catch (Exception ex)
1790
                {
1791
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1792
                    result = false;
1793
                }
1794
            }
1795

    
1796
            return result;
1797
        }
1798

    
1799
        public static DataTable SelectPSNFluidCode()
1800
        {
1801
            DataTable dt = null;
1802
            ID2Info id2Info = ID2Info.GetInstance();
1803

    
1804
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1805
            {
1806
                try
1807
                {
1808
                    var query = $"SELECT * FROM {PSN_FLUIDCODE}";
1809
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1810
                    {
1811
                        dt = ds.Tables[0].Copy();
1812
                    }
1813
                }
1814
                catch (Exception ex)
1815
                {
1816
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1817
                }
1818
            }
1819

    
1820
            return dt;
1821
        }
1822

    
1823
        public static bool SavePSNPMC(DataTable dt)
1824
        {
1825
            ID2Info id2Info = ID2Info.GetInstance();
1826

    
1827
            bool result = true;
1828
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1829
            {
1830
                try
1831
                {
1832
                    using (var txn = connection.BeginTransaction())
1833
                    {
1834
                        try
1835
                        {
1836
                            var query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
1837
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1838

    
1839
                            foreach (DataRow row in dt.Rows)
1840
                            {
1841
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1842
                                var cmd = connection.GetSqlStringCommand(query);
1843
                                cmd.Parameters.Clear();
1844

    
1845
                                {
1846
                                    var param = cmd.CreateParameter();
1847
                                    param.ParameterName = "@UID";
1848
                                    param.Value = row["UID"].ToString();
1849
                                    cmd.Parameters.Add(param);
1850
                                }
1851

    
1852
                                {
1853
                                    var param = cmd.CreateParameter();
1854
                                    param.ParameterName = "@Priority";
1855
                                    param.Value = row["Priority"].ToString();
1856
                                    cmd.Parameters.Add(param);
1857
                                }
1858

    
1859
                                {
1860
                                    var param = cmd.CreateParameter();
1861
                                    param.ParameterName = "@Code";
1862
                                    param.Value = row["Code"].ToString();
1863
                                    cmd.Parameters.Add(param);
1864
                                }
1865

    
1866
                                {
1867
                                    var param = cmd.CreateParameter();
1868
                                    param.ParameterName = "@Description";
1869
                                    param.Value = row["Description"].ToString();
1870
                                    cmd.Parameters.Add(param);
1871
                                }
1872

    
1873
                                {
1874
                                    var param = cmd.CreateParameter();
1875
                                    param.ParameterName = "@Condition";
1876
                                    param.Value = row["Condition"].ToString();
1877
                                    cmd.Parameters.Add(param);
1878
                                }
1879

    
1880
                                {
1881
                                    var param = cmd.CreateParameter();
1882
                                    param.ParameterName = "@Remarks";
1883
                                    param.Value = row["Remarks"].ToString();
1884
                                    cmd.Parameters.Add(param);
1885
                                }
1886

    
1887
                                {
1888
                                    var param = cmd.CreateParameter();
1889
                                    param.ParameterName = "@GroundLevel";
1890
                                    param.Value = row["GroundLevel"].ToString();
1891
                                    cmd.Parameters.Add(param);
1892
                                }
1893

    
1894
                                connection.ExecuteNonQuery(cmd, txn);
1895
                            }
1896

    
1897
                            txn.Commit();
1898
                        }
1899
                        catch (Exception ex)
1900
                        {
1901
                            txn.Rollback();
1902
                            result = false;
1903
                        }
1904
                    }
1905
                }
1906
                catch (Exception ex)
1907
                {
1908
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1909
                    result = false;
1910
                }
1911
            }
1912

    
1913
            return result;
1914
        }
1915

    
1916
        public static bool SavePSNInsulation(DataTable dt)
1917
        {
1918
            ID2Info id2Info = ID2Info.GetInstance();
1919

    
1920
            bool result = true;
1921
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1922
            {
1923
                try
1924
                {
1925
                    using (var txn = connection.BeginTransaction())
1926
                    {
1927
                        try
1928
                        {
1929
                            var query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
1930
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1931

    
1932
                            foreach (DataRow row in dt.Rows)
1933
                            {
1934
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
1935
                                var cmd = connection.GetSqlStringCommand(query);
1936
                                cmd.Parameters.Clear();
1937

    
1938
                                {
1939
                                    var param = cmd.CreateParameter();
1940
                                    param.ParameterName = "@UID";
1941
                                    param.Value = row["UID"].ToString();
1942
                                    cmd.Parameters.Add(param);
1943
                                }
1944
                                
1945
                                {
1946
                                    var param = cmd.CreateParameter();
1947
                                    param.ParameterName = "@Code";
1948
                                    param.Value = row["Code"].ToString();
1949
                                    cmd.Parameters.Add(param);
1950
                                }
1951

    
1952
                                {
1953
                                    var param = cmd.CreateParameter();
1954
                                    param.ParameterName = "@Description";
1955
                                    param.Value = row["Description"].ToString();
1956
                                    cmd.Parameters.Add(param);
1957
                                }
1958
                                
1959
                                {
1960
                                    var param = cmd.CreateParameter();
1961
                                    param.ParameterName = "@Remarks";
1962
                                    param.Value = row["Remarks"].ToString();
1963
                                    cmd.Parameters.Add(param);
1964
                                }
1965

    
1966
                                connection.ExecuteNonQuery(cmd, txn);
1967
                            }
1968

    
1969
                            txn.Commit();
1970
                        }
1971
                        catch (Exception ex)
1972
                        {
1973
                            txn.Rollback();
1974
                            result = false;
1975
                        }
1976
                    }
1977
                }
1978
                catch (Exception ex)
1979
                {
1980
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1981
                    result = false;
1982
                }
1983
            }
1984

    
1985
            return result;
1986
        }
1987

    
1988
        public static PSN GetDBPSN()
1989
        {
1990
            PSN result = new PSN();
1991
            ID2Info id2Info = ID2Info.GetInstance();
1992

    
1993
            using (IAbstractDatabase connection = id2Info.CreateConnection())
1994
            {
1995
                try
1996
                {
1997
                    //query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK}";
1998
                    //using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1999
                    //{
2000
                    //    result.PipeSystemNetwork = ds.Tables[0].Copy();
2001
                    //}
2002

    
2003
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
2004
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2005
                    {
2006
                        result.PipeSystemNetwork = ds.Tables[0].Clone();
2007
                        result.PipeSystemNetwork.Columns["IsValid"].DataType = typeof(string);
2008

    
2009
                        foreach (DataRow row in ds.Tables[0].Rows)
2010
                        {
2011
                            DataRow newRow = result.PipeSystemNetwork.NewRow();
2012
                            newRow["OID"] = row["OID"].ToString();
2013
                            newRow["Type"] = row["Type"].ToString();
2014
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
2015
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
2016
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
2017
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
2018
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
2019
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
2020

    
2021
                            string IsValid = string.Empty;
2022

    
2023
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
2024
                                IsValid = string.Empty;//"OK";
2025
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
2026
                                IsValid = "InValid";
2027
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
2028
                                IsValid = "Error";
2029

    
2030
                            newRow["IsValid"] = IsValid;
2031

    
2032
                            newRow["Status"] = row["Status"].ToString();
2033
                            newRow["PBS"] = row["PBS"].ToString();
2034
                            newRow["Drawings"] = row["Drawings"].ToString();
2035

    
2036
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2037
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
2038

    
2039
                            result.PipeSystemNetwork.Rows.Add(newRow);
2040
                        }
2041
                    }
2042

    
2043
                    query = $"SELECT * FROM {PSN_EQUIPMENT}";
2044
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2045
                    {
2046
                        result.Equipment = ds.Tables[0].Copy();
2047
                    }
2048

    
2049
                    query = $"SELECT * FROM {PSN_NOZZLE}";
2050
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2051
                    {
2052
                        result.Nozzle = ds.Tables[0].Copy();
2053
                    }
2054

    
2055
                    query = $"SELECT * FROM {PSN_PATHITEMS}";
2056
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2057
                    {
2058
                        result.PathItems = ds.Tables[0].Copy();
2059
                    }
2060

    
2061
                    query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
2062
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2063
                    {
2064
                        result.SequenceData = ds.Tables[0].Copy();
2065
                    }
2066

    
2067
                    query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
2068
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2069
                    {
2070
                        result.TopologySet = ds.Tables[0].Copy();
2071
                    }
2072

    
2073
                    query = $"SELECT * FROM {PSN_PIPELINE}";
2074
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2075
                    {
2076
                        result.PipeLine = ds.Tables[0].Copy();
2077
                    }
2078

    
2079
                    query = $"SELECT * FROM {PSN_PIPESYSTEM}";
2080
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2081
                    {
2082
                        result.PipeSystem = ds.Tables[0].Copy();
2083
                    }
2084

    
2085
                    result.Revision = GetRevision();
2086
                }
2087
                catch (Exception ex)
2088
                {
2089
                    System.Windows.Forms.MessageBox.Show(ex.Message);
2090
                    result = null;
2091
                }
2092
            }
2093

    
2094
            return result;
2095
        }
2096

    
2097
        public static int GetRevision()
2098
        {
2099
            int result = 0;
2100
            ID2Info id2Info = ID2Info.GetInstance();
2101

    
2102
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2103
            {
2104
                try
2105
                {
2106
                    var query = $"SELECT DISTINCT PSNRevisionNumber FROM {PSN_PIPESYSTEMNETWORK};";
2107
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2108
                    {
2109
                        foreach (DataRow row in ds.Tables[0].Rows)
2110
                        {
2111
                            string value = row["PSNRevisionNumber"].ToString();
2112
                            if (value.StartsWith("V"))
2113
                                value = value.Remove(0, 1);
2114
                            int revisionNumber = Convert.ToInt32(value);
2115
                            if (result < revisionNumber)
2116
                                result = revisionNumber;
2117
                        }
2118
                    }
2119
                }
2120
                catch (Exception ex)
2121
                {
2122
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2123
                    result = -1;
2124
                }
2125
            }
2126

    
2127
            return result;
2128
        }
2129

    
2130
        public static DataTable GetPathItem()
2131
        {
2132
            DataTable dt = null;
2133

    
2134
            ID2Info id2Info = ID2Info.GetInstance();
2135
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2136
            {
2137
                try
2138
                {
2139
                    var query = $"SELECT * FROM {PSN_PATHITEMS}";
2140
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2141
                    {
2142
                        dt = ds.Tables[0].Copy();
2143
                    }
2144
                }
2145
                catch (Exception ex)
2146
                {
2147
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2148
                }
2149
            }
2150

    
2151
            return dt;
2152
        }
2153

    
2154
        public static DataTable GetTopologySet()
2155
        {
2156
            DataTable dt = null;
2157

    
2158
            ID2Info id2Info = ID2Info.GetInstance();
2159
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2160
            {
2161
                try
2162
                {
2163
                    var query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
2164
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2165
                    {
2166
                        dt = ds.Tables[0].Clone();
2167
                        foreach (DataRow row in ds.Tables[0].Rows)
2168
                        {
2169
                            DataRow newRow = dt.NewRow();
2170
                            newRow["OID"] = row["OID"].ToString();
2171
                            newRow["Type"] = row["Type"].ToString();
2172
                            newRow["SubType"] = row["SubType"].ToString();
2173
                            newRow["HeadItemTag"] = row["HeadItemTag"].ToString();
2174
                            newRow["TailItemTag"] = row["TailItemTag"].ToString();
2175
                            newRow["HeadItemSPID"] = row["HeadItemSPID"].ToString();
2176
                            newRow["TailItemSPID"] = row["TailItemSPID"].ToString();
2177
                            dt.Rows.Add(newRow);
2178
                        }
2179
                    }
2180
                }
2181
                catch (Exception ex)
2182
                {
2183
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2184
                }
2185
            }
2186

    
2187
            return dt;
2188
        }
2189

    
2190
        public static DataTable GetPipeSystemNetwork()
2191
        {
2192
            DataTable dt = null;
2193

    
2194
            ID2Info id2Info = ID2Info.GetInstance();
2195
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2196
            {
2197
                try
2198
                {
2199
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}' ORDER BY OID, OrderNumber";
2200
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2201
                    {
2202
                        dt = ds.Tables[0].Clone();
2203
                        dt.Columns["IsValid"].DataType = typeof(string);
2204
                        foreach (DataRow row in ds.Tables[0].Rows)
2205
                        {
2206
                            DataRow newRow = dt.NewRow();
2207
                            newRow["OID"] = row["OID"].ToString();
2208
                            newRow["Type"] = row["Type"].ToString();
2209
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
2210
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
2211
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
2212
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
2213
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
2214
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
2215

    
2216
                            string IsValid = string.Empty;
2217

    
2218
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
2219
                                IsValid = string.Empty;//"OK";
2220
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
2221
                                IsValid = "InValid";
2222
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
2223
                                IsValid = "Error";
2224

    
2225
                            newRow["IsValid"] = IsValid;
2226
                            newRow["Status"] = row["Status"].ToString();
2227

    
2228
                            newRow["PBS"] = row["PBS"].ToString();
2229
                            newRow["Drawings"] = row["Drawings"].ToString();
2230
                            
2231
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
2232
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();                                           
2233
                                                        
2234
                            dt.Rows.Add(newRow);
2235
                        }
2236
                    }
2237
                }
2238
                catch (Exception ex)
2239
                {
2240
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2241
                }
2242
            }
2243

    
2244
            return dt;
2245
        }
2246

    
2247
        public static DataTable GetSequenceData()
2248
        {
2249
            DataTable dt = null;
2250

    
2251
            ID2Info id2Info = ID2Info.GetInstance();
2252
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2253
            {
2254
                try
2255
                {
2256
                    var query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
2257
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
2258
                    {
2259
                        dt = ds.Tables[0].Copy();
2260
                    }
2261
                }
2262
                catch (Exception ex)
2263
                {
2264
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2265
                }
2266
            }
2267

    
2268
            return dt;
2269
        }
2270

    
2271
       
2272
        //Anohter DB
2273
        public static bool ConnTestAndCreateAnotherTable()
2274
        {
2275
            bool result = false;
2276
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
2277

    
2278
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2279
            {
2280
                try
2281
                {
2282
                    var names = connection.GetTableNames();
2283
                    var matched = names.FirstOrDefault(param => param == PSN_HEADER_SETTING);
2284
                    Dictionary<string, string> dicColCheck = new Dictionary<string, string>();
2285
                    dicColCheck.Add("GROUP_ID", "TEXT");
2286
                    dicColCheck.Add("DESCRIPTION", "TEXT");
2287
                    dicColCheck.Add("INDEX", "INTEGER");
2288
                    dicColCheck.Add("NAME", "TEXT");
2289

    
2290
                    if (matched == null)
2291
                    {
2292
                        var query = $"CREATE TABLE {PSN_HEADER_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
2293
                        using (var cmd = connection.GetSqlStringCommand(query))
2294
                        {
2295
                            cmd.ExecuteNonQuery();
2296
                        }
2297
                    }
2298
                    else
2299
                    {
2300
                        AddColumn(PSN_HEADER_SETTING, dicColCheck);
2301
                    }
2302

    
2303
                    matched = names.FirstOrDefault(param => param == PSN_VENTDRAIN_SETTING);
2304
                    dicColCheck.Clear();
2305
                    dicColCheck.Add("GROUP_ID", "TEXT");
2306
                    dicColCheck.Add("DESCRIPTION", "TEXT");
2307
                    dicColCheck.Add("INDEX", "INTEGER");
2308
                    dicColCheck.Add("NAME", "TEXT");
2309
                    if (matched == null)
2310
                    {
2311
                        var query = $"CREATE TABLE {PSN_VENTDRAIN_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
2312
                        using (var cmd = connection.GetSqlStringCommand(query))
2313
                        {
2314
                            cmd.ExecuteNonQuery();
2315
                        }
2316
                    }
2317
                    else
2318
                    {
2319
                        AddColumn(PSN_VENTDRAIN_SETTING, dicColCheck);
2320
                    }
2321

    
2322
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGY_RULE);
2323
                    dicColCheck.Clear();
2324
                    dicColCheck.Add("UID", "TEXT");
2325
                    if (matched == null)
2326
                    {
2327
                        var query = $"CREATE TABLE {PSN_TOPOLOGY_RULE} (UID TEXT)";
2328
                        using (var cmd = connection.GetSqlStringCommand(query))
2329
                        {
2330
                            cmd.ExecuteNonQuery();
2331
                        }
2332

    
2333
                        DataTable topologyRule = new DataTable();
2334
                        topologyRule.Columns.Add("NAME", typeof(string));
2335

    
2336
                        topologyRule.Rows.Add("FluidCode");
2337
                        topologyRule.Rows.Add("-");
2338
                        topologyRule.Rows.Add("PipingMaterialsClass");
2339
                        topologyRule.Rows.Add("-");
2340
                        topologyRule.Rows.Add("Tag Seq No");
2341

    
2342
                        SaveTopologyRule(topologyRule);
2343
                    }
2344
                    //else
2345
                    //{
2346
                    //    AddColumn(PSN_TOPOLOGY_RULE, dicColCheck);
2347
                    //}
2348

    
2349
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
2350
                    dicColCheck.Clear();
2351
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2352
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2353
                    dicColCheck.Add("OrderNumber", "NVARCHAR(255)");
2354
                    dicColCheck.Add("Pipeline_OID", "NVARCHAR(255)");
2355
                    dicColCheck.Add("From_Data", "NVARCHAR(255)");
2356
                    dicColCheck.Add("To_Data", "NVARCHAR(255)");
2357
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(125)");
2358
                    dicColCheck.Add("PSNRevisionNumber", "NVARCHAR(255)");
2359
                    dicColCheck.Add("IsValid", "INT");
2360
                    dicColCheck.Add("Status", "NVARCHAR(255)");
2361
                    dicColCheck.Add("PBS", "NVARCHAR(255)");
2362
                    dicColCheck.Add("Drawings", "NVARCHAR(255)");
2363
                    dicColCheck.Add("IncludingVirtualData", "NVARCHAR(10)");
2364
                    dicColCheck.Add("PSNAccuracy", "REAL");
2365

    
2366
                    if (matched == null)
2367
                    {
2368
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(255), Type NVARCHAR(255), OrderNumber NVARCHAR(255), Pipeline_OID NVARCHAR(255), From_Data NVARCHAR(255), " +
2369
                            "To_Data NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
2370
                            "IncludingVirtualData NVARCHAR(10),  PSNAccuracy REAL)";
2371
                        using (var cmd = connection.GetSqlStringCommand(query))
2372
                        {
2373
                            cmd.ExecuteNonQuery();
2374
                        }
2375
                    }
2376
                    else
2377
                    {
2378
                        AddColumn(PSN_PIPESYSTEMNETWORK, dicColCheck);
2379
                    }
2380

    
2381
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
2382
                    dicColCheck.Clear();
2383
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2384
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2385
                    dicColCheck.Add("Xcoords", "REAL");
2386
                    dicColCheck.Add("Ycoords", "REAL");
2387
                    if (matched == null)
2388
                    {
2389
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL)";
2390
                        using (var cmd = connection.GetSqlStringCommand(query))
2391
                        {
2392
                            cmd.ExecuteNonQuery();
2393
                        }
2394
                    }
2395
                    else
2396
                    {
2397
                        AddColumn(PSN_EQUIPMENT, dicColCheck);
2398
                    }
2399

    
2400
                    dicColCheck.Clear();
2401
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2402
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2403
                    dicColCheck.Add("Xcoords", "REAL");
2404
                    dicColCheck.Add("Ycoords", "REAL");
2405
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2406
                    dicColCheck.Add("Fluid", "NVARCHAR(255)");
2407
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2408
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2409
                    dicColCheck.Add("Rotation", "REAL");
2410
                    dicColCheck.Add("FlowDirection", "NVARCHAR(255)");
2411

    
2412
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
2413
                    if (matched == null)
2414
                    {
2415
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL, Equipment_OID NVARCHAR(255), " +
2416
                            "Fluid NVARCHAR(255), NPD NVARCHAR(255), PMC NVARCHAR(255), Rotation REAL, FlowDirection NVARCHAR(255))";
2417
                        using (var cmd = connection.GetSqlStringCommand(query))
2418
                        {
2419
                            cmd.ExecuteNonQuery();
2420
                        }
2421
                    }
2422
                    else
2423
                    {
2424
                        AddColumn(PSN_NOZZLE, dicColCheck);
2425
                    }
2426

    
2427
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
2428
                    dicColCheck.Clear();
2429
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2430
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2431
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2432
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2433
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2434
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2435
                    if (matched == null)
2436
                    {
2437
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
2438
                        using (var cmd = connection.GetSqlStringCommand(query))
2439
                        {
2440
                            cmd.ExecuteNonQuery();
2441
                        }
2442
                    }
2443
                    else
2444
                    {
2445
                        AddColumn(PSN_FLUIDCODE, dicColCheck);
2446
                    }
2447

    
2448
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
2449
                    dicColCheck.Clear();
2450
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2451
                    dicColCheck.Add("Priority", "INTEGER");
2452
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2453
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2454
                    dicColCheck.Add("Condition", "NVARCHAR(255)");
2455
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2456
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2457
                    if (matched == null)
2458
                    {
2459
                        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))";
2460
                        using (var cmd = connection.GetSqlStringCommand(query))
2461
                        {
2462
                            cmd.ExecuteNonQuery();
2463
                        }
2464
                    }
2465
                    else
2466
                    {
2467
                        AddColumn(PSN_PIPINGMATLCLASS, dicColCheck);
2468
                    }
2469

    
2470
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
2471
                    dicColCheck.Clear();
2472
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2473
                    dicColCheck.Add("SERIALNUMBER", "NVARCHAR(255)");
2474
                    dicColCheck.Add("PathItem_OID", "NVARCHAR(255)");
2475
                    dicColCheck.Add("TopologySet_OID_Key", "NVARCHAR(255)");
2476
                    if (matched == null)
2477
                    {
2478
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(255), SERIALNUMBER NVARCHAR(255), PathItem_OID NVARCHAR(255), TopologySet_OID_Key NVARCHAR(255))";
2479
                        using (var cmd = connection.GetSqlStringCommand(query))
2480
                        {
2481
                            cmd.ExecuteNonQuery();
2482
                        }
2483
                    }
2484
                    else
2485
                    {
2486
                        AddColumn(PSN_SEQUENCEDATA, dicColCheck);
2487
                    }
2488

    
2489
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
2490
                    dicColCheck.Clear();
2491
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2492
                    dicColCheck.Add("SequenceData_OID", "NVARCHAR(255)");
2493
                    dicColCheck.Add("TopologySet_OID", "NVARCHAR(255)");
2494
                    dicColCheck.Add("BranchTopologySet_OID", "NVARCHAR(255)");
2495
                    dicColCheck.Add("PipeLine_OID", "NVARCHAR(255)");
2496
                    dicColCheck.Add("ItemName", "NVARCHAR(255)");
2497
                    dicColCheck.Add("ItemTag", "NVARCHAR(255)");
2498
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
2499
                    dicColCheck.Add("CLASS", "NVARCHAR(255)");
2500
                    dicColCheck.Add("SUBCLASS", "NVARCHAR(255)");
2501
                    dicColCheck.Add("TYPE", "NVARCHAR(255)");
2502
                    dicColCheck.Add("PIDNAME", "NVARCHAR(255)");
2503
                    dicColCheck.Add("Equipment_OID", "NVARCHAR(255)");
2504
                    dicColCheck.Add("NPD", "NVARCHAR(255)");
2505
                    dicColCheck.Add("GROUPTAG", "NVARCHAR(255)");
2506
                    dicColCheck.Add("PipeSystemNetwork_OID", "NVARCHAR(255)");
2507
                    dicColCheck.Add("PipeRun_OID", "NVARCHAR(255)");
2508
                    dicColCheck.Add("ViewPipeSystemNetwork_OID", "NVARCHAR(255)");
2509
                    if (matched == null)
2510
                    {
2511
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
2512
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
2513
                            "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), " +
2514
                            "ViewPipeSystemNetwork_OID NVARCHAR(255))";
2515
                        using (var cmd = connection.GetSqlStringCommand(query))
2516
                        {
2517
                            cmd.ExecuteNonQuery();
2518
                        }
2519
                    }
2520
                    else
2521
                    {
2522
                        AddColumn(PSN_PATHITEMS, dicColCheck);
2523
                    }
2524

    
2525
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
2526
                    dicColCheck.Clear();
2527
                    dicColCheck.Add("OID", "TEXT");
2528
                    if (matched == null)
2529
                    {
2530
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
2531
                        using (var cmd = connection.GetSqlStringCommand(query))
2532
                        {
2533
                            cmd.ExecuteNonQuery();
2534
                        }
2535
                    }
2536

    
2537
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
2538
                    dicColCheck.Clear();
2539
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2540
                    dicColCheck.Add("Type", "NVARCHAR(255)");
2541
                    dicColCheck.Add("SubType", "NVARCHAR(255)");
2542
                    dicColCheck.Add("HeadItemTag", "NVARCHAR(255)");
2543
                    dicColCheck.Add("TailItemTag", "NVARCHAR(255)");
2544
                    dicColCheck.Add("HeadItemSPID", "NVARCHAR(255)");
2545
                    dicColCheck.Add("TailItemSPID", "NVARCHAR(255)");
2546

    
2547
                    if (matched == null)
2548
                    {
2549
                        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))";
2550
                        using (var cmd = connection.GetSqlStringCommand(query))
2551
                        {
2552
                            cmd.ExecuteNonQuery();
2553
                        }
2554
                    }
2555
                    else
2556
                    {
2557
                        AddColumn(PSN_TOPOLOGYSET, dicColCheck);
2558
                    }
2559

    
2560
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
2561
                    dicColCheck.Clear();
2562
                    dicColCheck.Add("INDEX", "INTEGER");
2563
                    dicColCheck.Add("NAME", "TEXT");
2564
                    dicColCheck.Add("KEYWORD", "TEXT");
2565
                    if (matched == null)
2566
                    {
2567
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
2568
                        using (var cmd = connection.GetSqlStringCommand(query))
2569
                        {
2570
                            cmd.ExecuteNonQuery();
2571
                        }
2572
                    }
2573
                    else
2574
                    {
2575
                        AddColumn(PSN_TRANSFORMKEYWORD_SETTING, dicColCheck);
2576
                    }
2577

    
2578
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
2579
                    dicColCheck.Clear();
2580
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2581
                    dicColCheck.Add("PipeSystem_OID", "NVARCHAR(255)");
2582
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
2583
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2584
                    dicColCheck.Add("SEQNUMBER", "NVARCHAR(255)");
2585
                    dicColCheck.Add("INSULATION", "NVARCHAR(255)");
2586
                    dicColCheck.Add("FROM_DATA", "NVARCHAR(255)");
2587
                    dicColCheck.Add("TO_DATA", "NVARCHAR(255)");
2588
                    dicColCheck.Add("Unit", "NVARCHAR(100)");
2589
                    if (matched == null)
2590
                    {
2591
                        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), " +
2592
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
2593
                        using (var cmd = connection.GetSqlStringCommand(query))
2594
                        {
2595
                            cmd.ExecuteNonQuery();
2596
                        }
2597
                    }
2598
                    else
2599
                    {
2600
                        AddColumn(PSN_PIPELINE, dicColCheck);
2601
                    }
2602

    
2603
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
2604
                    dicColCheck.Clear();
2605
                    dicColCheck.Add("UID", "NVARCHAR(50)");
2606
                    dicColCheck.Add("Code", "NVARCHAR(255)");
2607
                    dicColCheck.Add("Description", "NVARCHAR(255)");
2608
                    dicColCheck.Add("Remarks", "NVARCHAR(255)");
2609
                    if (matched == null)
2610
                    {
2611
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
2612
                        using (var cmd = connection.GetSqlStringCommand(query))
2613
                        {
2614
                            cmd.ExecuteNonQuery();
2615
                        }
2616
                    }
2617
                    else
2618
                    {
2619
                        AddColumn(PSN_INSULATIONPURPOSE, dicColCheck);
2620
                    }
2621

    
2622
                    matched = names.FirstOrDefault(param => param == PSN_VALVEGROUP_SETTING);
2623
                    dicColCheck.Clear();
2624
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2625
                    dicColCheck.Add("GroupType", "NVARCHAR(255)");
2626
                    dicColCheck.Add("TagIdentifier", "NVARCHAR(50)");
2627
                    dicColCheck.Add("AttributeName", "NVARCHAR(255)");
2628
                    dicColCheck.Add("SppidSymbolName", "NVARCHAR(255)");
2629
                    if (matched == null)
2630
                    {
2631
                        var query = $"CREATE TABLE {PSN_VALVEGROUP_SETTING} (OID NVARCHAR(50), GroupType NVARCHAR(255), TagIdentifier NVARCHAR(50), AttributeName NVARCHAR(255), SppidSymbolName NVARCHAR(255))";
2632
                        using (var cmd = connection.GetSqlStringCommand(query))
2633
                        {
2634
                            cmd.ExecuteNonQuery();
2635
                        }
2636
                    }
2637
                    else
2638
                    {
2639
                        AddColumn(PSN_VALVEGROUP_SETTING, dicColCheck);
2640
                    }
2641

    
2642
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEM);
2643
                    dicColCheck.Clear();
2644
                    dicColCheck.Add("OID", "NVARCHAR(255)");
2645
                    dicColCheck.Add("DESCRIPTION", "NVARCHAR(255)");
2646
                    dicColCheck.Add("FLUID", "NVARCHAR(255)");
2647
                    dicColCheck.Add("PMC", "NVARCHAR(255)");
2648
                    dicColCheck.Add("PipeLineQty", "NVARCHAR(255)");
2649
                    dicColCheck.Add("GroundLevel", "NVARCHAR(20)");
2650
                    if (matched == null)
2651
                    {
2652
                        var query = $"CREATE TABLE {PSN_PIPESYSTEM} (OID NVARCHAR(255), DESCRIPTION NVARCHAR(255), FLUID NVARCHAR(255), PMC NVARCHAR(255), PipeLineQty NVARCHAR(255), GroundLevel NVARCHAR(20))";
2653
                        using (var cmd = connection.GetSqlStringCommand(query))
2654
                        {
2655
                            cmd.ExecuteNonQuery();
2656
                        }
2657
                    }
2658
                    else
2659
                    {
2660
                        AddColumn(PSN_PIPESYSTEM, dicColCheck);
2661
                    }
2662

    
2663
                    void AddColumn(string TableName, Dictionary<string, string> dicCol)
2664
                    {
2665
                        var colnames = connection.GetColumnNames(TableName);
2666
                        bool check = false;
2667
                        if (colnames != null)
2668
                        {
2669
                            foreach (KeyValuePair<string, string> col in dicCol)
2670
                            {
2671
                                check = false;
2672
                                foreach (string c in colnames)
2673
                                {
2674
                                    if (col.Key.ToUpper().Equals(c.ToUpper()))
2675
                                    {
2676
                                        check = true;
2677
                                        break;
2678
                                    }
2679
                                }
2680

    
2681
                                if (!check) //없으면 추가
2682
                                {
2683
                                    string i = string.Empty;
2684
                                    if (col.Value.ToString().ToUpper().Contains("INT") || col.Value.ToString().ToUpper().Contains("REAL"))
2685
                                        i = "DEFAULT 0";
2686

    
2687
                                    var query = $"ALTER TABLE " + TableName + " ADD [" + col.Key + "] " + col.Value + " " + i + ";";
2688
                                    using (var cmd = connection.GetSqlStringCommand(query))
2689
                                    {
2690
                                        cmd.ExecuteNonQuery();
2691
                                    }
2692
                                }
2693
                            }
2694
                        }
2695
                    }
2696

    
2697
                    result = true;
2698
                }
2699
                catch (Exception ex)
2700
                {
2701
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2702
                }
2703
            }
2704

    
2705
            return result;
2706
        }
2707

    
2708
        public static bool SaveAnotherPSNData(PSN item, List<HeaderInfo> headerInfos, List<VentDrainInfo> ventDrainInfos,
2709
          List<KeywordItem> keywordItems, DataTable dtTopologyRule, DataTable dtFluidCode, DataTable dtPMC, DataTable dtInsulation, DataTable dtvalvegrouping)
2710
        {
2711
            AnotherID2Info id2Info = AnotherID2Info.GetInstance();
2712

    
2713
            bool result = true;
2714
            using (IAbstractDatabase connection = id2Info.CreateConnection())
2715
            {
2716
                try
2717
                {
2718
                    using (var txn = connection.BeginTransaction())
2719
                    {
2720
                        try
2721
                        {
2722
                            // Path Items
2723
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
2724
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2725
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
2726
                            {
2727
                                DataRow row = item.PathItems.Rows[i];
2728
                                query = $"INSERT INTO {PSN_PATHITEMS} " +
2729
                                    $"(OID, SequenceData_OID, TopologySet_OID, BranchTopologySet_OID, PipeLine_OID, ItemName, ItemTag, DESCRIPTION, CLASS, SUBCLASS, TYPE, " +
2730
                                    $"PIDNAME, Equipment_OID, NPD, GROUPTAG, PipeSystemNetwork_OID, PipeRun_OID, ViewPipeSystemNetwork_OID) VALUES " +
2731
                                    $"(@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME," +
2732
                                    $" @Equipment_OID, @NPD, @GROUPTAG, @PipeSystemNetwork_OID,@PipeRun_OID, @ViewPipeSystemNetwork_OID)";
2733
                                var cmd = connection.GetSqlStringCommand(query);
2734
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2735
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
2736
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
2737
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
2738
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
2739
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
2740
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
2741
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString());
2742
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
2743
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
2744
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
2745
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
2746
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
2747
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
2748
                                AddWithValue(cmd, "@GROUPTAG", string.IsNullOrEmpty(row["GROUPTAG"].ToString()) ? "" : row["GROUPTAG"].ToString());
2749
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
2750
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
2751
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
2752
                                connection.ExecuteNonQuery(cmd, txn);
2753
                            }
2754

    
2755
                            // Sequence
2756
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
2757
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2758
                            foreach (DataRow row in item.SequenceData.Rows)
2759
                            {
2760
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} (OID, SERIALNUMBER, PathItem_OID, TopologySet_OID_Key) VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
2761
                                var cmd = connection.GetSqlStringCommand(query);
2762
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2763
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
2764
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
2765
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
2766
                                connection.ExecuteNonQuery(cmd, txn);
2767
                            }
2768

    
2769
                            // Nozzle
2770
                            query = $"DELETE FROM {PSN_NOZZLE}";
2771
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2772
                            foreach (DataRow row in item.Nozzle.Rows)
2773
                            {
2774
                                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)";
2775
                                var cmd = connection.GetSqlStringCommand(query);
2776
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2777
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
2778

    
2779
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
2780
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
2781
                                else
2782
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
2783

    
2784
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
2785
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
2786
                                else
2787
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
2788

    
2789
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
2790
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
2791
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
2792
                                AddWithValue(cmd, "@PMC", string.IsNullOrEmpty(row["PMC"].ToString()) ? "" : row["PMC"].ToString());
2793

    
2794
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
2795
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
2796
                                else
2797
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
2798

    
2799
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
2800
                                connection.ExecuteNonQuery(cmd, txn);
2801
                            }
2802

    
2803
                            //Equipment
2804
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
2805
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2806
                            foreach (DataRow row in item.Equipment.Rows)
2807
                            {
2808
                                query = $"INSERT INTO {PSN_EQUIPMENT} (OID, ItemTag, Xcoords, Ycoords) VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
2809
                                var cmd = connection.GetSqlStringCommand(query);
2810
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2811
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
2812

    
2813
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
2814
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
2815
                                else
2816
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
2817

    
2818
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
2819
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
2820
                                else
2821
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
2822

    
2823
                                connection.ExecuteNonQuery(cmd, txn);
2824
                            }
2825

    
2826
                            // TopologySet
2827
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
2828
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2829
                            foreach (DataRow row in item.TopologySet.Rows)
2830
                            {
2831
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} (OID, Type, SubType, HeadItemTag, TailItemTag, HeadItemSPID, TailItemSPID) VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
2832
                                var cmd = connection.GetSqlStringCommand(query);
2833
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2834
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
2835
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
2836
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
2837
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
2838
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
2839
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
2840
                                connection.ExecuteNonQuery(cmd, txn);
2841
                            }
2842

    
2843
                            // PSN
2844
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
2845
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2846
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
2847
                            {
2848
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
2849
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy) VALUES " +
2850
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy)";
2851
                                var cmd = connection.GetSqlStringCommand(query);
2852
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
2853
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
2854
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
2855
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
2856
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
2857
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
2858
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
2859
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
2860

    
2861
                                int IsValid = 0;
2862
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
2863
                                    IsValid = 0;
2864
                                else if (row["IsValid"].ToString() == "InValid")
2865
                                    IsValid = 1;
2866
                                else if (row["IsValid"].ToString() == "Error")
2867
                                    IsValid = -1;
2868

    
2869
                                AddWithValue(cmd, "@IsValid", IsValid);
2870
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
2871

    
2872
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
2873
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
2874

    
2875
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
2876
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
2877
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
2878
                                else
2879
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString());
2880

    
2881
                                connection.ExecuteNonQuery(cmd, txn);
2882
                            }
2883

    
2884
                            //Pipeline
2885
                            query = $"DELETE FROM {PSN_PIPELINE}";
2886
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2887
                            foreach (DataRow row in item.PipeLine.Rows)
2888
                            {
2889
                                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)";
2890
                                var cmd = connection.GetSqlStringCommand(query);
2891
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
2892
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
2893
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
2894
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
2895
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
2896
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
2897
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
2898
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
2899
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
2900
                                connection.ExecuteNonQuery(cmd, txn);
2901
                            }
2902

    
2903
                            //PipeSystem
2904
                            query = $"DELETE FROM {PSN_PIPESYSTEM}";
2905
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2906
                            foreach (DataRow row in item.PipeSystem.Rows)
2907
                            {
2908
                                query = $"INSERT INTO {PSN_PIPESYSTEM} (OID, DESCRIPTION, FLUID, PMC, PipeLineQty, GroundLevel) VALUES (@OID, @DESCRIPTION, @FLUID, @PMC, @PipeLineQty, @GroundLevel)";
2909
                                var cmd = connection.GetSqlStringCommand(query);
2910
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
2911
                                AddWithValue(cmd, "@DESCRIPTION", row["DESCRIPTION"].ToString());
2912
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
2913
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
2914
                                AddWithValue(cmd, "@PipeLineQty", row["PipeLineQty"].ToString());
2915
                                AddWithValue(cmd, "@GroundLevel", row["GroundLevel"].ToString());
2916
                                connection.ExecuteNonQuery(cmd, txn);
2917
                            }
2918

    
2919
                            //Header Setting
2920
                            query = $"DELETE FROM {PSN_HEADER_SETTING}";
2921
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2922

    
2923
                            foreach (HeaderInfo headerInfo in headerInfos)
2924
                            {
2925
                                foreach (HeaderItem itemheader in headerInfo.HeaderItems)
2926
                                {
2927
                                    query = $"INSERT INTO {PSN_HEADER_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
2928
                                    var cmd = connection.GetSqlStringCommand(query);
2929
                                    AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
2930
                                    AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
2931
                                    AddWithValue(cmd, "@INDEX", itemheader.Index);
2932
                                    AddWithValue(cmd, "@NAME", itemheader.Name);
2933
                                    connection.ExecuteNonQuery(cmd, txn);
2934
                                }
2935
                            }
2936

    
2937
                            //Vent/Drain Setting
2938
                            query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
2939
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2940

    
2941
                            foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
2942
                            {
2943
                                foreach (VentDrainItem itemventdrain in ventDrainInfo.VentDrainItems)
2944
                                {
2945
                                    query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} (GROUP_ID, DESCRIPTION, [INDEX], NAME) VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
2946
                                    var cmd = connection.GetSqlStringCommand(query);
2947
                                    AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
2948
                                    AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
2949
                                    AddWithValue(cmd, "@INDEX", itemventdrain.Index);
2950
                                    AddWithValue(cmd, "@NAME", itemventdrain.Name);
2951
                                    connection.ExecuteNonQuery(cmd, txn);
2952
                                }
2953
                            }
2954

    
2955
                            //Keyword Setting
2956
                            query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
2957
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2958

    
2959
                            foreach (KeywordItem itemKeyword in keywordItems)
2960
                            {
2961
                                query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX], NAME, KEYWORD) VALUES (@INDEX, @NAME, @KEYWORD)";
2962
                                var cmd = connection.GetSqlStringCommand(query);
2963
                                AddWithValue(cmd, "@INDEX", itemKeyword.Index);
2964
                                AddWithValue(cmd, "@NAME", itemKeyword.Name);
2965
                                AddWithValue(cmd, "@KEYWORD", itemKeyword.Keyword);
2966
                                connection.ExecuteNonQuery(cmd, txn);
2967
                            }
2968

    
2969
                            //FulidCode
2970
                            query = $"DELETE FROM {PSN_FLUIDCODE}";
2971
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
2972

    
2973
                            foreach (DataRow row in dtFluidCode.Rows)
2974
                            {
2975
                                query = $"INSERT INTO {PSN_FLUIDCODE} (UID, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
2976
                                var cmd = connection.GetSqlStringCommand(query);
2977
                                cmd.Parameters.Clear();
2978

    
2979
                                {
2980
                                    var param = cmd.CreateParameter();
2981
                                    param.ParameterName = "@UID";
2982
                                    param.Value = row["UID"].ToString();
2983
                                    cmd.Parameters.Add(param);
2984
                                }
2985

    
2986
                                {
2987
                                    var param = cmd.CreateParameter();
2988
                                    param.ParameterName = "@Code";
2989
                                    param.Value = row["Code"].ToString();
2990
                                    cmd.Parameters.Add(param);
2991
                                }
2992

    
2993
                                {
2994
                                    var param = cmd.CreateParameter();
2995
                                    param.ParameterName = "@Description";
2996
                                    param.Value = row["Description"].ToString();
2997
                                    cmd.Parameters.Add(param);
2998
                                }
2999

    
3000
                                {
3001
                                    var param = cmd.CreateParameter();
3002
                                    param.ParameterName = "@Condition";
3003
                                    param.Value = row["Condition"].ToString();
3004
                                    cmd.Parameters.Add(param);
3005
                                }
3006

    
3007
                                {
3008
                                    var param = cmd.CreateParameter();
3009
                                    param.ParameterName = "@Remarks";
3010
                                    param.Value = row["Remarks"].ToString();
3011
                                    cmd.Parameters.Add(param);
3012
                                }
3013

    
3014
                                {
3015
                                    var param = cmd.CreateParameter();
3016
                                    param.ParameterName = "@GroundLevel";
3017
                                    param.Value = row["GroundLevel"].ToString();
3018
                                    cmd.Parameters.Add(param);
3019
                                }
3020

    
3021
                                connection.ExecuteNonQuery(cmd, txn);
3022
                            }
3023

    
3024
                            //PMC
3025
                            query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
3026
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3027

    
3028
                            foreach (DataRow row in dtPMC.Rows)
3029
                            {
3030
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} ( UID, Priority, Code, Description, Condition, Remarks, GroundLevel) VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
3031
                                var cmd = connection.GetSqlStringCommand(query);
3032
                                cmd.Parameters.Clear();
3033

    
3034
                                {
3035
                                    var param = cmd.CreateParameter();
3036
                                    param.ParameterName = "@UID";
3037
                                    param.Value = row["UID"].ToString();
3038
                                    cmd.Parameters.Add(param);
3039
                                }
3040

    
3041
                                {
3042
                                    var param = cmd.CreateParameter();
3043
                                    param.ParameterName = "@Priority";
3044
                                    param.Value = row["Priority"].ToString();
3045
                                    cmd.Parameters.Add(param);
3046
                                }
3047

    
3048
                                {
3049
                                    var param = cmd.CreateParameter();
3050
                                    param.ParameterName = "@Code";
3051
                                    param.Value = row["Code"].ToString();
3052
                                    cmd.Parameters.Add(param);
3053
                                }
3054

    
3055
                                {
3056
                                    var param = cmd.CreateParameter();
3057
                                    param.ParameterName = "@Description";
3058
                                    param.Value = row["Description"].ToString();
3059
                                    cmd.Parameters.Add(param);
3060
                                }
3061

    
3062
                                {
3063
                                    var param = cmd.CreateParameter();
3064
                                    param.ParameterName = "@Condition";
3065
                                    param.Value = row["Condition"].ToString();
3066
                                    cmd.Parameters.Add(param);
3067
                                }
3068

    
3069
                                {
3070
                                    var param = cmd.CreateParameter();
3071
                                    param.ParameterName = "@Remarks";
3072
                                    param.Value = row["Remarks"].ToString();
3073
                                    cmd.Parameters.Add(param);
3074
                                }
3075

    
3076
                                {
3077
                                    var param = cmd.CreateParameter();
3078
                                    param.ParameterName = "@GroundLevel";
3079
                                    param.Value = row["GroundLevel"].ToString();
3080
                                    cmd.Parameters.Add(param);
3081
                                }
3082

    
3083
                                connection.ExecuteNonQuery(cmd, txn);
3084
                            }
3085

    
3086
                            //Insulation
3087
                            query = $"DELETE FROM {PSN_INSULATIONPURPOSE}";
3088
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3089

    
3090
                            foreach (DataRow row in dtInsulation.Rows)
3091
                            {
3092
                                query = $"INSERT INTO {PSN_INSULATIONPURPOSE} (UID, Code, Description, Remarks) VALUES (@UID, @Code, @Description, @Remarks)";
3093
                                var cmd = connection.GetSqlStringCommand(query);
3094
                                cmd.Parameters.Clear();
3095

    
3096
                                {
3097
                                    var param = cmd.CreateParameter();
3098
                                    param.ParameterName = "@UID";
3099
                                    param.Value = row["UID"].ToString();
3100
                                    cmd.Parameters.Add(param);
3101
                                }
3102

    
3103
                                {
3104
                                    var param = cmd.CreateParameter();
3105
                                    param.ParameterName = "@Code";
3106
                                    param.Value = row["Code"].ToString();
3107
                                    cmd.Parameters.Add(param);
3108
                                }
3109

    
3110
                                {
3111
                                    var param = cmd.CreateParameter();
3112
                                    param.ParameterName = "@Description";
3113
                                    param.Value = row["Description"].ToString();
3114
                                    cmd.Parameters.Add(param);
3115
                                }
3116

    
3117
                                {
3118
                                    var param = cmd.CreateParameter();
3119
                                    param.ParameterName = "@Remarks";
3120
                                    param.Value = row["Remarks"].ToString();
3121
                                    cmd.Parameters.Add(param);
3122
                                }
3123

    
3124
                                connection.ExecuteNonQuery(cmd, txn);
3125
                            }
3126

    
3127
                            //Topology Rule
3128
                            query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
3129
                            var cmdtopology = connection.GetSqlStringCommand(query);
3130
                            //connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3131
                            connection.ExecuteNonQuery(cmdtopology, txn);
3132

    
3133
                            foreach (DataRow row in dtTopologyRule.Rows)
3134
                            {
3135
                                query = $"INSERT INTO {PSN_TOPOLOGY_RULE} (UID) VALUES (@UID)";
3136
                                cmdtopology = connection.GetSqlStringCommand(query);
3137
                                AddWithValue(cmdtopology, "@UID", row["UID"].ToString());
3138
                                connection.ExecuteNonQuery(cmdtopology, txn);
3139
                            }
3140

    
3141
                            //valve grouping
3142
                            query = $"DELETE FROM {PSN_VALVEGROUP_SETTING}";
3143
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
3144

    
3145
                            foreach (DataRow row in dtvalvegrouping.Rows)
3146
                            {
3147
                                query = $"INSERT INTO {PSN_VALVEGROUP_SETTING} (OID, GroupType, TagIdentifier, AttributeName, SppidSymbolName) " +
3148
                                    $"VALUES (@OID, @GroupType, @TagIdentifier, @AttributeName, @SppidSymbolName)";
3149
                                var cmd = connection.GetSqlStringCommand(query);
3150
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
3151
                                AddWithValue(cmd, "@GroupType", row["GroupType"].ToString());
3152
                                AddWithValue(cmd, "@TagIdentifier", row["TagIdentifier"].ToString());
3153
                                AddWithValue(cmd, "@AttributeName", row["AttributeName"].ToString());
3154
                                AddWithValue(cmd, "@SppidSymbolName", row["SppidSymbolName"].ToString());
3155
                                connection.ExecuteNonQuery(cmd, txn);
3156
                            }
3157

    
3158
                            txn.Commit();
3159
                        }
3160
                        catch (Exception ex)
3161
                        {
3162
                            txn.Rollback();
3163
                            result = false;
3164
                        }
3165
                    }
3166
                }
3167
                catch (Exception ex)
3168
                {
3169
                    System.Windows.Forms.MessageBox.Show(ex.Message);
3170
                    result = false;
3171
                }
3172
            }
3173

    
3174
            return result;
3175
        }
3176
    }
3177

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