프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ 839708c6

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

1 0dae5645 gaqhf
using System;
2
using System.Collections.Generic;
3 6b9e7a56 gaqhf
using System.Data;
4 757ab2f2 LJIYEON
using System.Data.Common;
5 45529c16 LJIYEON
using System.Data.SQLite;
6
using System.Linq;
7
using System.Text.RegularExpressions;
8 0dae5645 gaqhf
9
namespace ID2PSN
10
{
11 45529c16 LJIYEON
    public class DB
12 0dae5645 gaqhf
    {
13 6b9e7a56 gaqhf
        const string PSN_TOPOLOGY_RULE = "T_PSN_TOPOLOGY_RULE";
14
        const string PSN_HEADER_SETTING = "T_PSN_HEADER_SETTING";
15 36a45f13 gaqhf
        const string PSN_VENTDRAIN_SETTING = "T_PSN_VENTDRAIN_SETTING";
16 5dfc785c LJIYEON
        const string PSN_VIEW = "T_PSN_VIEW";
17 ddc1c369 LJIYEON
        const string PSN_TRANSFORMKEYWORD_SETTING = "T_PSN_TRANSFORMKEYWORD_SETTING"; 
18 5dfc785c LJIYEON
19 8f24b438 gaqhf
        const string PSN_PATHITEMS = "SPPIDPathItem";
20
        const string PSN_SEQUENCEDATA = "SPPIDSequenceData";
21
        const string PSN_PIPESYSTEMNETWORK = "SPPIDPipeSystemNetwork";
22
        const string PSN_EQUIPMENT = "SPPIDEquipment";
23
        const string PSN_NOZZLE = "SPPIDNozzle";
24
        const string PSN_FLUIDCODE = "SPPIDFluidCode";
25 67638be0 LJIYEON
        const string PSN_PIPINGMATLCLASS = "SPPIDPipingMatlClass";
26 36a45f13 gaqhf
        const string PSN_TOPOLOGYSET = "SPPIDTopologySet";
27 a36541fb LJIYEON
        //2021.11.17 추가 
28
        const string PSN_PIPELINE = "SPPIDPipeLine";
29 839708c6 LJIYEON
        //2021.11.26 추가
30
        const string PSN_INSULATIONPURPOSE = "SPPIDInsulationPurpose";
31 1ae1a1c6 LJIYEON
        /// <summary>
32
        ///  ID2 Project.db 데이터를 가져온다. 
33
        ///  DB 접속 정보 및 DBType (Sqlite, Mssql) 정보를 가져옴
34
        ///  - JY
35
        /// </summary>
36
        /// <returns></returns>
37
        public static DataTable GetProject()
38 757ab2f2 LJIYEON
        {
39 1ae1a1c6 LJIYEON
            DataTable dt = new DataTable();
40
            ID2Info id2Info = ID2Info.GetInstance();
41
            try
42
            {
43
                using (SQLiteConnection connection = new SQLiteConnection(@"Data Source = C:\ProgramData\Digital PID\Project.db", true))
44
                {
45
                    connection.Open();
46
                    if (connection.State.Equals(ConnectionState.Open))
47
                    {
48
                        using (SQLiteCommand cmd = connection.CreateCommand())
49
                        {
50
                            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]";
51
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
52
                                dt.Load(dr);
53
                        }
54
55
                    }
56
                    connection.Close();
57
                }
58
            }
59
            catch (Exception ex)
60
            {
61
                System.Windows.Forms.MessageBox.Show(ex.Message);
62
            }
63
64
            dt.AcceptChanges();
65
            dt.DefaultView.Sort = "Name";
66
            dt = dt.DefaultView.ToTable();
67 757ab2f2 LJIYEON
68 1ae1a1c6 LJIYEON
            return dt;
69
        }
70 757ab2f2 LJIYEON
71 1ae1a1c6 LJIYEON
        /// <summary>
72 c4a35107 humkyung
        ///  SQLite에 초기 DB 생성
73 1ae1a1c6 LJIYEON
        ///  - JY
74
        /// </summary>
75
        /// <returns></returns>
76 6b9e7a56 gaqhf
        public static bool ConnTestAndCreateTable()
77
        {
78
            bool result = false;
79
            ID2Info id2Info = ID2Info.GetInstance();
80 1ae1a1c6 LJIYEON
81 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
82 6b9e7a56 gaqhf
            {
83 0fe04b33 LJIYEON
                try
84 6b9e7a56 gaqhf
                {
85 23a96301 humkyung
                    var names = connection.GetTableNames();
86
                    var matched = names.FirstOrDefault(param => param == PSN_HEADER_SETTING);
87
                    if (matched == null)
88 6b9e7a56 gaqhf
                    {
89 23a96301 humkyung
                        var query = $"CREATE TABLE {PSN_HEADER_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
90
                        using (var cmd = connection.GetSqlStringCommand(query))
91 6b9e7a56 gaqhf
                        {
92 23a96301 humkyung
                            cmd.ExecuteNonQuery();
93
                        }
94
                    }
95 757ab2f2 LJIYEON
96 23a96301 humkyung
                    matched = names.FirstOrDefault(param => param == PSN_VENTDRAIN_SETTING);
97
                    if (matched == null)
98
                    {
99
                        var query = $"CREATE TABLE {PSN_VENTDRAIN_SETTING} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)";
100
                        using (var cmd = connection.GetSqlStringCommand(query))
101
                        {
102
                            cmd.ExecuteNonQuery();
103
                        }
104
                    }
105 757ab2f2 LJIYEON
106 23a96301 humkyung
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGY_RULE);
107
                    if (matched == null)
108
                    {
109
                        var query = $"CREATE TABLE {PSN_TOPOLOGY_RULE} (UID TEXT)";
110
                        using (var cmd = connection.GetSqlStringCommand(query))
111
                        {
112
                            cmd.ExecuteNonQuery();
113
                        }
114 757ab2f2 LJIYEON
115 23a96301 humkyung
                        DataTable topologyRule = new DataTable();
116
                        topologyRule.Columns.Add("NAME", typeof(string));
117 757ab2f2 LJIYEON
118 23a96301 humkyung
                        topologyRule.Rows.Add("FluidCode");
119
                        topologyRule.Rows.Add("-");
120
                        topologyRule.Rows.Add("PipingMaterialsClass");
121
                        topologyRule.Rows.Add("-");
122
                        topologyRule.Rows.Add("Tag Seq No");
123
124
                        SaveTopologyRule(topologyRule);
125
                    }
126
127 72775f2e LJIYEON
128
                    matched = names.FirstOrDefault(param => param == PSN_PIPESYSTEMNETWORK);
129 23a96301 humkyung
                    if (matched == null)
130
                    {
131 f9f2787b LJIYEON
                        var query = $"CREATE TABLE {PSN_PIPESYSTEMNETWORK} (OID NVARCHAR(255), Type NVARCHAR(255), OrderNumber NVARCHAR(255), Pipeline_OID NVARCHAR(255), From_Data NVARCHAR(255), " +
132
                            "To_Data NVARCHAR(255), TopologySet_OID_Key NVARCHAR(125), PSNRevisionNumber NVARCHAR(255), IsValid INTEGER, Status NVARCHAR(255), PBS NVARCHAR(255), Drawings NVARCHAR(255), " +
133 72775f2e LJIYEON
                            "IncludingVirtualData NVARCHAR(10),  PSNAccuracy REAL)";
134 23a96301 humkyung
                        using (var cmd = connection.GetSqlStringCommand(query))
135
                        {
136
                            cmd.ExecuteNonQuery();
137 757ab2f2 LJIYEON
                        }
138
                    }
139 23a96301 humkyung
140 72775f2e LJIYEON
                    matched = names.FirstOrDefault(param => param == PSN_EQUIPMENT);
141 23a96301 humkyung
                    if (matched == null)
142
                    {
143 f9f2787b LJIYEON
                        var query = $"CREATE TABLE {PSN_EQUIPMENT} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL)";
144 23a96301 humkyung
                        using (var cmd = connection.GetSqlStringCommand(query))
145
                        {
146
                            cmd.ExecuteNonQuery();
147
                        }
148
                    }
149
150 72775f2e LJIYEON
                    matched = names.FirstOrDefault(param => param == PSN_NOZZLE);
151 23a96301 humkyung
                    if (matched == null)
152
                    {
153 f9f2787b LJIYEON
                        var query = $"CREATE TABLE {PSN_NOZZLE} (OID NVARCHAR(255), ItemTag NVARCHAR(255), Xcoords REAL, Ycoords REAL, Equipment_OID NVARCHAR(255), " +
154
                            "Fluid NVARCHAR(255), NPD NVARCHAR(255), Rotation REAL, FlowDirection NVARCHAR(255))";
155 23a96301 humkyung
                        using (var cmd = connection.GetSqlStringCommand(query))
156
                        {
157
                            cmd.ExecuteNonQuery();
158
                        }
159
                    }
160
161 72775f2e LJIYEON
                    matched = names.FirstOrDefault(param => param == PSN_FLUIDCODE);
162 23a96301 humkyung
                    if (matched == null)
163
                    {
164 72775f2e LJIYEON
                        var query = $"CREATE TABLE {PSN_FLUIDCODE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Condition NVARCHAR(255), Remarks NVARCHAR(255), GroundLevel NVARCHAR(20))";
165 23a96301 humkyung
                        using (var cmd = connection.GetSqlStringCommand(query))
166
                        {
167
                            cmd.ExecuteNonQuery();
168
                        }
169
                    }
170
171 72775f2e LJIYEON
172
                    matched = names.FirstOrDefault(param => param == PSN_PIPINGMATLCLASS);
173 23a96301 humkyung
                    if (matched == null)
174
                    {
175 72775f2e LJIYEON
                        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))";
176 23a96301 humkyung
                        using (var cmd = connection.GetSqlStringCommand(query))
177
                        {
178
                            cmd.ExecuteNonQuery();
179
                        }
180
                    }
181
182 72775f2e LJIYEON
                    matched = names.FirstOrDefault(param => param == PSN_SEQUENCEDATA);
183 23a96301 humkyung
                    if (matched == null)
184
                    {
185 f9f2787b LJIYEON
                        var query = $"CREATE TABLE {PSN_SEQUENCEDATA} (OID NVARCHAR(255), SERIALNUMBER NVARCHAR(255), PathItem_OID NVARCHAR(255), TopologySet_OID_Key NVARCHAR(255))";
186 23a96301 humkyung
                        using (var cmd = connection.GetSqlStringCommand(query))
187
                        {
188
                            cmd.ExecuteNonQuery();
189
                        }
190
                    }
191
192 72775f2e LJIYEON
                    matched = names.FirstOrDefault(param => param == PSN_PATHITEMS);
193 23a96301 humkyung
                    if (matched == null)
194
                    {
195 f9f2787b LJIYEON
                        var query = $"CREATE TABLE {PSN_PATHITEMS} (OID NVARCHAR(255), SequenceData_OID NVARCHAR(255), " +
196
                            "TopologySet_OID NVARCHAR(255), BranchTopologySet_OID NVARCHAR(255), PipeLine_OID NVARCHAR(255), ItemName NVARCHAR(255), ItemTag NVARCHAR(255), DESCRIPTION NVARCHAR(255), " +
197
                            "CLASS NVARCHAR(255), SUBCLASS NVARCHAR(255), TYPE NVARCHAR(255), PIDNAME NVARCHAR(255), Equipment_OID NVARCHAR(255), NPD NVARCHAR(255), PipeSystemNetwork_OID NVARCHAR(255), PipeRun_OID NVARCHAR(255), " +
198 72775f2e LJIYEON
                            "ViewPipeSystemNetwork_OID NVARCHAR(255))";
199 23a96301 humkyung
                        using (var cmd = connection.GetSqlStringCommand(query))
200
                        {
201
                            cmd.ExecuteNonQuery();
202
                        }
203
                    }
204 72775f2e LJIYEON
                    
205 23a96301 humkyung
                    matched = names.FirstOrDefault(param => param == PSN_VIEW);
206
                    if (matched == null)
207
                    {
208
                        var query = $"CREATE TABLE {PSN_VIEW} (OID TEXT)";
209
                        using (var cmd = connection.GetSqlStringCommand(query))
210
                        {
211
                            cmd.ExecuteNonQuery();
212
                        }
213
                    }
214
215
                    matched = names.FirstOrDefault(param => param == PSN_TOPOLOGYSET);
216
                    if (matched == null)
217
                    {
218 f9f2787b LJIYEON
                        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))";
219 23a96301 humkyung
                        using (var cmd = connection.GetSqlStringCommand(query))
220
                        {
221
                            cmd.ExecuteNonQuery();
222
                        }
223
                    }
224 ddc1c369 LJIYEON
                   
225
                    matched = names.FirstOrDefault(param => param == PSN_TRANSFORMKEYWORD_SETTING);
226
                    if (matched == null)
227
                    {
228 d4b1ab29 LJIYEON
                        var query = $"CREATE TABLE {PSN_TRANSFORMKEYWORD_SETTING} ([INDEX] INTEGER, [NAME] TEXT, [KEYWORD] TEXT)";
229 ddc1c369 LJIYEON
                        using (var cmd = connection.GetSqlStringCommand(query))
230
                        {
231
                            cmd.ExecuteNonQuery();
232
                        }
233
                    }
234
235 a36541fb LJIYEON
                    matched = names.FirstOrDefault(param => param == PSN_PIPELINE);
236
                    if (matched == null)
237
                    {
238
                        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), " +
239
                            $"FROM_DATA NVARCHAR(255), TO_DATA NVARCHAR(255), Unit NVARCHAR(100))";
240
                        using (var cmd = connection.GetSqlStringCommand(query))
241
                        {
242
                            cmd.ExecuteNonQuery();
243
                        }
244
                    }
245
246 839708c6 LJIYEON
                    matched = names.FirstOrDefault(param => param == PSN_INSULATIONPURPOSE);
247
                    if (matched == null)
248
                    {
249
                        var query = $"CREATE TABLE {PSN_INSULATIONPURPOSE} (UID NVARCHAR(50), Code NVARCHAR(255), Description NVARCHAR(255), Remarks NVARCHAR(255))";
250
                        using (var cmd = connection.GetSqlStringCommand(query))
251
                        {
252
                            cmd.ExecuteNonQuery();
253
                        }
254
                    }
255
256 23a96301 humkyung
                    result = true;
257 0fe04b33 LJIYEON
                }
258
                catch (Exception ex)
259
                {
260
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
261
                }
262 6b9e7a56 gaqhf
            }
263 45529c16 LJIYEON
264 6b9e7a56 gaqhf
            return result;
265
        }
266
267 1ae1a1c6 LJIYEON
        // ID2 DB 데이터
268 c4a35107 humkyung
        /// <summary>
269
        /// ID2 데이타베이스에서 OPC 데이터를 조회
270
        /// </summary>
271
        /// <returns></returns>
272 1ae1a1c6 LJIYEON
        public static DataTable SelectOPCRelations()
273 6b9e7a56 gaqhf
        {
274 23a96301 humkyung
            DataTable dt = null;
275 6b9e7a56 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
276 0fe04b33 LJIYEON
277 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
278 6b9e7a56 gaqhf
            {
279 0fe04b33 LJIYEON
                try
280 6b9e7a56 gaqhf
                {
281 23a96301 humkyung
                    var query = "SELECT * FROM OPCRelations;";
282
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
283 757ab2f2 LJIYEON
                    {
284 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
285 6b9e7a56 gaqhf
                    }
286
                }
287 0fe04b33 LJIYEON
                catch (Exception ex)
288 36a45f13 gaqhf
                {
289 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
290
                }
291 36a45f13 gaqhf
            }
292 1ae1a1c6 LJIYEON
293 36a45f13 gaqhf
            return dt;
294
        }
295 6b9e7a56 gaqhf
296 c4a35107 humkyung
        /// <summary>
297
        /// ID2 데이타베이스에서 도면 데이터를 조회
298
        /// </summary>
299
        /// <returns></returns>
300 1ae1a1c6 LJIYEON
        public static DataTable SelectDrawings()
301 6b9e7a56 gaqhf
        {
302 23a96301 humkyung
            DataTable dt = null;
303 6b9e7a56 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
304 0fe04b33 LJIYEON
305 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
306 6b9e7a56 gaqhf
            {
307 0fe04b33 LJIYEON
                try
308 6b9e7a56 gaqhf
                {
309 23a96301 humkyung
                    var query = "SELECT * FROM Drawings";
310
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
311 1ae1a1c6 LJIYEON
                    {
312 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
313 6b9e7a56 gaqhf
                    }
314
                }
315 0fe04b33 LJIYEON
                catch (Exception ex)
316 36a45f13 gaqhf
                {
317 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
318
                }
319 36a45f13 gaqhf
            }
320 1ae1a1c6 LJIYEON
321
            return dt;
322 36a45f13 gaqhf
        }
323 abee404a LJIYEON
324 1ae1a1c6 LJIYEON
        public static DataTable SelectLineProperties()
325 6b9e7a56 gaqhf
        {
326 23a96301 humkyung
            DataTable dt = null;
327 6b9e7a56 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
328 23a96301 humkyung
329
            using (IAbstractDatabase connection = id2Info.CreateConnection())
330 6b9e7a56 gaqhf
            {
331 0fe04b33 LJIYEON
                try
332 6b9e7a56 gaqhf
                {
333 23a96301 humkyung
                    var query = "SELECT * FROM LineProperties";
334
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
335 1ae1a1c6 LJIYEON
                    {
336 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
337 6b9e7a56 gaqhf
                    }
338
                }
339 0fe04b33 LJIYEON
                catch (Exception ex)
340 6b9e7a56 gaqhf
                {
341 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
342
                }
343 6b9e7a56 gaqhf
            }
344 0fe04b33 LJIYEON
345 6b9e7a56 gaqhf
            return dt;
346
        }
347 45529c16 LJIYEON
348 1ae1a1c6 LJIYEON
        public static DataTable SelectFluidCode()
349 6b9e7a56 gaqhf
        {
350 23a96301 humkyung
            DataTable dt = null;
351 6b9e7a56 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
352 0fe04b33 LJIYEON
353 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
354 6b9e7a56 gaqhf
            {
355 0fe04b33 LJIYEON
                try
356 6b9e7a56 gaqhf
                {
357 23a96301 humkyung
                    var query = "SELECT * FROM FluidCode";
358 45529c16 LJIYEON
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
359 1ae1a1c6 LJIYEON
                    {
360 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
361 6b9e7a56 gaqhf
                    }
362
                }
363 0fe04b33 LJIYEON
                catch (Exception ex)
364 6b9e7a56 gaqhf
                {
365 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
366
                }
367 1ae1a1c6 LJIYEON
            }
368
369
            return dt;
370
        }
371
372
        public static DataTable SelectPipingMaterialsClass()
373
        {
374 23a96301 humkyung
            DataTable dt = null;
375 1ae1a1c6 LJIYEON
            ID2Info id2Info = ID2Info.GetInstance();
376 0fe04b33 LJIYEON
377 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
378 1ae1a1c6 LJIYEON
            {
379 0fe04b33 LJIYEON
                try
380 6b9e7a56 gaqhf
                {
381 23a96301 humkyung
                    var query = "SELECT * FROM PipingMaterialsClass";
382
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
383 1ae1a1c6 LJIYEON
                    {
384 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
385 1ae1a1c6 LJIYEON
                    }
386
                }
387 0fe04b33 LJIYEON
                catch (Exception ex)
388 1ae1a1c6 LJIYEON
                {
389 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
390
                }
391 6b9e7a56 gaqhf
            }
392 1ae1a1c6 LJIYEON
393 6b9e7a56 gaqhf
            return dt;
394
        }
395
396 1ae1a1c6 LJIYEON
        public static DataTable SelectPSNPIPINGMATLCLASS()
397 6b9e7a56 gaqhf
        {
398 23a96301 humkyung
            DataTable dt = null;
399 6b9e7a56 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
400 0fe04b33 LJIYEON
401 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
402 1ae1a1c6 LJIYEON
            {
403 0fe04b33 LJIYEON
                try
404
                {
405 23a96301 humkyung
                    var query = $"SELECT * FROM {PSN_PIPINGMATLCLASS}";
406
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
407 1ae1a1c6 LJIYEON
                    {
408 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
409 1ae1a1c6 LJIYEON
                    }
410 0fe04b33 LJIYEON
                }
411
                catch (Exception ex)
412
                {
413
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
414
                }
415 1ae1a1c6 LJIYEON
            }
416
417
            return dt;
418
        }
419
420 839708c6 LJIYEON
421
        public static DataTable SelectInsulationPurpose()
422
        {
423
            DataTable dt = null;
424
            ID2Info id2Info = ID2Info.GetInstance();
425
426
            using (IAbstractDatabase connection = id2Info.CreateConnection())
427
            {
428
                try
429
                {
430
                    var query = "SELECT * FROM InsulationPurpose";
431
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
432
                    {
433
                        dt = ds.Tables[0].Copy();
434
                    }
435
                }
436
                catch (Exception ex)
437
                {
438
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
439
                }
440
            }
441
442
            return dt;
443
        }
444
445
        public static DataTable SelectPSNINSULATIONPURPOSE()
446
        {
447
            DataTable dt = null;
448
            ID2Info id2Info = ID2Info.GetInstance();
449
450
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
451
            {
452
                try
453
                {
454
                    var query = $"SELECT * FROM {PSN_INSULATIONPURPOSE}";
455
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
456
                    {
457
                        dt = ds.Tables[0].Copy();
458
                    }
459
                }
460
                catch (Exception ex)
461
                {
462
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
463
                }
464
            }
465
466
            return dt;
467
        }
468
469 1ae1a1c6 LJIYEON
        public static DataTable SelectNominalDiameter()
470
        {
471 23a96301 humkyung
            DataTable dt = null;
472 1ae1a1c6 LJIYEON
            ID2Info id2Info = ID2Info.GetInstance();
473 0fe04b33 LJIYEON
474 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
475 1ae1a1c6 LJIYEON
            {
476 0fe04b33 LJIYEON
                try
477 6b9e7a56 gaqhf
                {
478 23a96301 humkyung
                    var query = "SELECT * FROM NominalDiameter ORDER BY Metric DESC";
479
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
480 1ae1a1c6 LJIYEON
                    {
481 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
482 1ae1a1c6 LJIYEON
                    }
483 6b9e7a56 gaqhf
                }
484 0fe04b33 LJIYEON
                catch (Exception ex)
485 1ae1a1c6 LJIYEON
                {
486 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
487
                }
488 1ae1a1c6 LJIYEON
            }
489
490 c4a35107 humkyung
            ///TODO: need to check below code
491 1ae1a1c6 LJIYEON
            dt.Rows.RemoveAt(0);
492
            dt.Rows.RemoveAt(0);
493
            dt.Rows.RemoveAt(0);
494
            dt.Rows.RemoveAt(0);
495
496 6b9e7a56 gaqhf
            return dt;
497
        }
498 abee404a LJIYEON
499 1ae1a1c6 LJIYEON
        public static DataTable SelectSymbolAttribute()
500 6b9e7a56 gaqhf
        {
501 23b86f8b LJIYEON
            DataTable dt = null;
502 6b9e7a56 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
503 0fe04b33 LJIYEON
504 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
505 6b9e7a56 gaqhf
            {
506 0fe04b33 LJIYEON
                try
507 6b9e7a56 gaqhf
                {
508 23b86f8b LJIYEON
                   // var query = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
509 502d1d50 LJIYEON
                    var query = "SELECT DISTINCT Attribute FROM SymbolAttribute;";
510 23a96301 humkyung
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
511 1ae1a1c6 LJIYEON
                    {
512 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
513 1ae1a1c6 LJIYEON
                    }
514
                }
515 0fe04b33 LJIYEON
                catch (Exception ex)
516 1ae1a1c6 LJIYEON
                {
517 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
518
                }
519 1ae1a1c6 LJIYEON
            }
520 0fe04b33 LJIYEON
521 1ae1a1c6 LJIYEON
            return dt;
522
        }
523
524
        public static DataTable SelectSymbolName()
525
        {
526 23a96301 humkyung
            DataTable dt = null;
527 1ae1a1c6 LJIYEON
            ID2Info id2Info = ID2Info.GetInstance();
528 0fe04b33 LJIYEON
529 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
530 1ae1a1c6 LJIYEON
            {
531 0fe04b33 LJIYEON
                try
532 1ae1a1c6 LJIYEON
                {
533 23a96301 humkyung
                    var query = "SELECT * FROM SymbolName;";
534
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
535 1ae1a1c6 LJIYEON
                    {
536 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
537 1ae1a1c6 LJIYEON
                    }
538
                }
539 0fe04b33 LJIYEON
                catch (Exception ex)
540 1ae1a1c6 LJIYEON
                {
541 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
542
                }
543 1ae1a1c6 LJIYEON
            }
544 0fe04b33 LJIYEON
545 1ae1a1c6 LJIYEON
            return dt;
546 45529c16 LJIYEON
        }
547 1ae1a1c6 LJIYEON
548
        public static double[] GetDrawingSize()
549
        {
550
            double[] result = null;
551
552
            ID2Info id2Info = ID2Info.GetInstance();
553 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
554 1ae1a1c6 LJIYEON
            {
555 0fe04b33 LJIYEON
                try
556 1ae1a1c6 LJIYEON
                {
557 d36e2fe0 esham21
                    var query = "SELECT value FROM Configuration WHERE Section = 'Area' AND [Key] = 'Drawing';";
558 23a96301 humkyung
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
559 1ae1a1c6 LJIYEON
                    {
560 23a96301 humkyung
                        if (ds.Tables[0].Rows.Count == 1)
561 0fe04b33 LJIYEON
                        {
562 23a96301 humkyung
                            string value = ds.Tables[0].Rows[0][0].ToString();
563 0fe04b33 LJIYEON
                            string[] split = value.Split(new char[] { ',' });
564
                            result = new double[] {
565 45529c16 LJIYEON
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", string.Empty)),
566
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", string.Empty)),
567
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", string.Empty)),
568
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", string.Empty))
569 1ae1a1c6 LJIYEON
                                };
570 0fe04b33 LJIYEON
                            result = new double[] {
571 1ae1a1c6 LJIYEON
                                Math.Min(result[0], result[2]),
572
                                Math.Min(result[1], result[3]),
573
                                Math.Max(result[0], result[2]),
574
                                Math.Max(result[1], result[3])
575
                                };
576
                        }
577
                    }
578
                }
579 0fe04b33 LJIYEON
                catch (Exception ex)
580 1ae1a1c6 LJIYEON
                {
581 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
582
                }
583 1ae1a1c6 LJIYEON
            }
584
585
            return result;
586
        }
587
588
        public static DataTable GetEquipmentType()
589
        {
590 23a96301 humkyung
            DataTable dt = null;
591 1ae1a1c6 LJIYEON
            ID2Info id2Info = ID2Info.GetInstance();
592 0fe04b33 LJIYEON
593 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
594 1ae1a1c6 LJIYEON
            {
595 0fe04b33 LJIYEON
                try
596 1ae1a1c6 LJIYEON
                {
597 23a96301 humkyung
                    var query = "SELECT Type FROM SymbolType WHERE Category = 'Equipment';";
598
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
599 1ae1a1c6 LJIYEON
                    {
600 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
601 1ae1a1c6 LJIYEON
                    }
602
                }
603 0fe04b33 LJIYEON
                catch (Exception ex)
604 1ae1a1c6 LJIYEON
                {
605 0fe04b33 LJIYEON
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
606
                }
607 6b9e7a56 gaqhf
            }
608 0fe04b33 LJIYEON
609 6b9e7a56 gaqhf
            return dt;
610
        }
611 abee404a LJIYEON
612 1ae1a1c6 LJIYEON
        /// <summary>
613
        ///  Zoom 을 위하여 저장 T_PSN_VIEW 데이터로 PSN 데이터지만 Msqql 과 Sqlite 둘다 저장되어 있음
614
        ///  => ID2 가 DB 타입에 따라서 바라보는 DB가 mssql이냐 sqlite냐로 지정되기 때문에 zoom 기능을 그대로 사용하려면 해당 데이터는 psn도 id2 dbtype에 맞춰서 저장 및 로드해야함
615 c4a35107 humkyung
        ///  => 더 이상 사용하지 않음
616 1ae1a1c6 LJIYEON
        /// </summary>
617
        /// <param name="values"></param>
618
        /// <returns></returns>
619
        public static bool SaveView(List<string> values)
620 6b9e7a56 gaqhf
        {
621
            ID2Info id2Info = ID2Info.GetInstance();
622 1ae1a1c6 LJIYEON
623
            bool result = true;
624
625 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
626
            {
627
                try
628 6b9e7a56 gaqhf
                {
629 23a96301 humkyung
                    using (var txn = connection.BeginTransaction())
630 6b9e7a56 gaqhf
                    {
631 23a96301 humkyung
                        try
632 6b9e7a56 gaqhf
                        {
633 23a96301 humkyung
                            var query = $"DELETE FROM {PSN_VIEW}";
634
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
635
636
                            foreach (string value in values)
637 1ae1a1c6 LJIYEON
                            {
638 23a96301 humkyung
                                query = $"INSERT INTO {PSN_VIEW} VALUES (@OID)";
639
                                var cmd = connection.GetSqlStringCommand(query);
640
                                AddWithValue(cmd, "@OID", value);
641
                                connection.ExecuteNonQuery(cmd, txn);
642 1ae1a1c6 LJIYEON
                            }
643 23a96301 humkyung
                            txn.Commit();
644
                        }
645
                        catch (Exception ex)
646
                        {
647
                            txn.Rollback();
648
                            result = false;
649 6b9e7a56 gaqhf
                        }
650 1ae1a1c6 LJIYEON
                    }
651 6b9e7a56 gaqhf
                }
652 23a96301 humkyung
                catch (Exception ex)
653
                {
654
                    System.Windows.Forms.MessageBox.Show(ex.Message);
655
                    result = false;
656
                }
657
            }
658
659 1ae1a1c6 LJIYEON
            return result;
660 6b9e7a56 gaqhf
        }
661
662 1ae1a1c6 LJIYEON
        public static bool DeleteView()
663 6b9e7a56 gaqhf
        {
664
            ID2Info id2Info = ID2Info.GetInstance();
665 1ae1a1c6 LJIYEON
666
            bool result = true;
667 23a96301 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection())
668
            {
669
                try
670 6b9e7a56 gaqhf
                {
671 23a96301 humkyung
                    using (var txn = connection.BeginTransaction())
672 6b9e7a56 gaqhf
                    {
673 23a96301 humkyung
                        try
674 1ae1a1c6 LJIYEON
                        {
675 23a96301 humkyung
                            var query = $"DELETE FROM {PSN_VIEW}";
676
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
677
                            txn.Commit();
678
                        }
679
                        catch (Exception ex)
680
                        {
681
                            txn.Rollback();
682
                            result = false;
683 1ae1a1c6 LJIYEON
                        }
684 6b9e7a56 gaqhf
                    }
685
                }
686 23a96301 humkyung
                catch (Exception ex)
687
                {
688
                    System.Windows.Forms.MessageBox.Show(ex.Message);
689
                    result = false;
690
                }
691
            }
692 1ae1a1c6 LJIYEON
693
            return result;
694 6b9e7a56 gaqhf
        }
695 abee404a LJIYEON
696 1ae1a1c6 LJIYEON
        //PSN Sqlite 
697
        public static DataTable SelectHeaderSetting()
698 6b9e7a56 gaqhf
        {
699 23a96301 humkyung
            DataTable dt = null;
700 6b9e7a56 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
701 1ae1a1c6 LJIYEON
702 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
703 6b9e7a56 gaqhf
            {
704
                try
705
                {
706 23a96301 humkyung
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_HEADER_SETTING};";
707
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
708 6b9e7a56 gaqhf
                    {
709 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
710 6b9e7a56 gaqhf
                    }
711
                }
712
                catch (Exception ex)
713
                {
714
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
715
                }
716
            }
717 1ae1a1c6 LJIYEON
718 6b9e7a56 gaqhf
            return dt;
719
        }
720
721 1ae1a1c6 LJIYEON
        public static DataTable SelectVentDrainSetting()
722 6b9e7a56 gaqhf
        {
723 23a96301 humkyung
            DataTable dt = null;
724 6b9e7a56 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
725 23a96301 humkyung
726 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
727 6b9e7a56 gaqhf
            {
728
                try
729
                {
730 23a96301 humkyung
                    var query = $@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {PSN_VENTDRAIN_SETTING};";
731
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
732 6b9e7a56 gaqhf
                    {
733 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
734 6b9e7a56 gaqhf
                    }
735
                }
736
                catch (Exception ex)
737
                {
738
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
739
                }
740
            }
741 23a96301 humkyung
742 6b9e7a56 gaqhf
            return dt;
743
        }
744 abee404a LJIYEON
745 ddc1c369 LJIYEON
        public static DataTable SelectKeywordsSetting()
746
        {
747
            DataTable dt = null;
748
            ID2Info id2Info = ID2Info.GetInstance();
749
750
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
751
            {
752
                try
753
                {
754 d4b1ab29 LJIYEON
                    var query = $@"SELECT [KEYWORD], [INDEX], [NAME] FROM {PSN_TRANSFORMKEYWORD_SETTING};";
755 ddc1c369 LJIYEON
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
756
                    {
757
                        dt = ds.Tables[0].Copy();
758
                    }
759
                }
760
                catch (Exception ex)
761
                {
762
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
763
                }
764
            }
765
766
            return dt;
767
        }
768
769 1ae1a1c6 LJIYEON
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
770 6b9e7a56 gaqhf
        {
771
            ID2Info id2Info = ID2Info.GetInstance();
772 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
773 6b9e7a56 gaqhf
            {
774
                try
775
                {
776 23a96301 humkyung
                    using (var txn = connection.BeginTransaction())
777 6b9e7a56 gaqhf
                    {
778 23a96301 humkyung
                        var query = $"DELETE FROM {PSN_HEADER_SETTING}";
779
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
780 1ae1a1c6 LJIYEON
781
                        foreach (HeaderInfo headerInfo in headerInfos)
782
                        {
783
                            foreach (HeaderItem item in headerInfo.HeaderItems)
784
                            {
785 23a96301 humkyung
                                query = $"INSERT INTO {PSN_HEADER_SETTING} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
786
                                var cmd = connection.GetSqlStringCommand(query);
787
                                AddWithValue(cmd, "@GROUP_ID", headerInfo.UID);
788
                                AddWithValue(cmd, "@DESCRIPTION", headerInfo.Description);
789
                                AddWithValue(cmd, "@INDEX", item.Index);
790
                                AddWithValue(cmd, "@NAME", item.Name);
791
                                connection.ExecuteNonQuery(cmd, txn);
792 1ae1a1c6 LJIYEON
                            }
793
                        }
794 81bdaeed LJIYEON
                        txn.Commit();
795 6b9e7a56 gaqhf
                    }
796 81bdaeed LJIYEON
                    
797 6b9e7a56 gaqhf
                }
798
                catch (Exception ex)
799
                {
800
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
801 1ae1a1c6 LJIYEON
                    return false;
802 6b9e7a56 gaqhf
                }
803
            }
804 1ae1a1c6 LJIYEON
            return true;
805 6b9e7a56 gaqhf
        }
806 abee404a LJIYEON
807 1ae1a1c6 LJIYEON
        public static bool SaveVentDrainSetting(List<VentDrainInfo> ventDrainInfos)
808 6b9e7a56 gaqhf
        {
809
            ID2Info id2Info = ID2Info.GetInstance();
810 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
811 6b9e7a56 gaqhf
            {
812 23a96301 humkyung
                using (var txn = connection.BeginTransaction())
813 6b9e7a56 gaqhf
                {
814 23a96301 humkyung
                    try
815 6b9e7a56 gaqhf
                    {
816 23a96301 humkyung
                        var query = $"DELETE FROM {PSN_VENTDRAIN_SETTING}";
817
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
818 1ae1a1c6 LJIYEON
819
                        foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
820
                        {
821
                            foreach (VentDrainItem item in ventDrainInfo.VentDrainItems)
822
                            {
823 23a96301 humkyung
                                query = $"INSERT INTO {PSN_VENTDRAIN_SETTING} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)";
824
                                var cmd = connection.GetSqlStringCommand(query);
825
                                AddWithValue(cmd, "@GROUP_ID", ventDrainInfo.UID);
826
                                AddWithValue(cmd, "@DESCRIPTION", ventDrainInfo.Description);
827
                                AddWithValue(cmd, "@INDEX", item.Index);
828
                                AddWithValue(cmd, "@NAME", item.Name);
829
                                connection.ExecuteNonQuery(cmd, txn);
830 1ae1a1c6 LJIYEON
                            }
831
                        }
832 81bdaeed LJIYEON
833
                        txn.Commit();
834 6b9e7a56 gaqhf
                    }
835 23a96301 humkyung
                    catch (Exception ex)
836
                    {
837
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
838
                        return false;
839
                    }
840 6b9e7a56 gaqhf
                }
841
            }
842 23a96301 humkyung
843 1ae1a1c6 LJIYEON
            return true;
844 6b9e7a56 gaqhf
        }
845 abee404a LJIYEON
846 8ab98ea3 LJIYEON
        public static bool SaveKeywordsSetting(List<KeywordItem> keywordItems)
847 ddc1c369 LJIYEON
        {
848
            ID2Info id2Info = ID2Info.GetInstance();
849
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
850
            {
851
                using (var txn = connection.BeginTransaction())
852
                {
853
                    try
854
                    {
855 81bdaeed LJIYEON
                        var query = $"DELETE FROM {PSN_TRANSFORMKEYWORD_SETTING}";
856 ddc1c369 LJIYEON
                        connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
857 8ab98ea3 LJIYEON
                        
858
                        foreach (KeywordItem item in keywordItems)
859 ddc1c369 LJIYEON
                        {
860 8ab98ea3 LJIYEON
                            query = $"INSERT INTO {PSN_TRANSFORMKEYWORD_SETTING} VALUES (@INDEX, @NAME, @KEYWORD)";
861
                            var cmd = connection.GetSqlStringCommand(query);
862
                            AddWithValue(cmd, "@INDEX", item.Index);
863
                            AddWithValue(cmd, "@NAME", item.Name);
864
                            AddWithValue(cmd, "@KEYWORD", item.Keyword);                            
865
                            connection.ExecuteNonQuery(cmd, txn);
866 ddc1c369 LJIYEON
                        }
867 8ab98ea3 LJIYEON
                        
868 81bdaeed LJIYEON
                        txn.Commit();
869 ddc1c369 LJIYEON
                    }
870
                    catch (Exception ex)
871
                    {
872
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
873
                        return false;
874
                    }
875
                }
876
            }
877
878
            return true;
879
        }
880
881 1ae1a1c6 LJIYEON
        public static bool SaveTopologyRule(DataTable dt)
882 c6503eaa gaqhf
        {
883
            ID2Info id2Info = ID2Info.GetInstance();
884 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
885 c6503eaa gaqhf
            {
886 23a96301 humkyung
                using (var txn = connection.BeginTransaction())
887 c6503eaa gaqhf
                {
888 23a96301 humkyung
                    try
889 c6503eaa gaqhf
                    {
890 23a96301 humkyung
                        var query = $"DELETE FROM {PSN_TOPOLOGY_RULE}";
891
                        var cmd = connection.GetSqlStringCommand(query);
892 1ae1a1c6 LJIYEON
                        cmd.ExecuteNonQuery();
893
894
                        foreach (DataRow row in dt.Rows)
895
                        {
896 23a96301 humkyung
                            query = $"INSERT INTO {PSN_TOPOLOGY_RULE} VALUES (@UID)";
897
                            cmd = connection.GetSqlStringCommand(query);
898
                            AddWithValue(cmd, "@UID", row["NAME"].ToString());
899
                            connection.ExecuteNonQuery(cmd, txn);
900 1ae1a1c6 LJIYEON
                        }
901 23a96301 humkyung
902
                        txn.Commit();
903
                    }
904
                    catch (Exception ex)
905
                    {
906
                        txn.Rollback();
907
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
908
                        return false;
909 c6503eaa gaqhf
                    }
910
                }
911
            }
912 23a96301 humkyung
913 1ae1a1c6 LJIYEON
            return true;
914 c6503eaa gaqhf
        }
915 abee404a LJIYEON
916 1ae1a1c6 LJIYEON
        public static DataTable SelectTopologyRule()
917 7881ec8f gaqhf
        {
918 23a96301 humkyung
            DataTable dt = null;
919
920 7881ec8f gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
921 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
922 7881ec8f gaqhf
            {
923
                try
924
                {
925 23a96301 humkyung
                    var query = $"SELECT * FROM {PSN_TOPOLOGY_RULE}";
926
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
927 7881ec8f gaqhf
                    {
928 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
929 7881ec8f gaqhf
                    }
930
                }
931
                catch (Exception ex)
932
                {
933
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
934
                }
935
            }
936 23a96301 humkyung
937 7881ec8f gaqhf
            return dt;
938
        }
939 45529c16 LJIYEON
940 23a96301 humkyung
        private static void AddWithValue(DbCommand cmd, string PropName, object Value)
941
        {
942
            var param = cmd.CreateParameter();
943
            param.ParameterName = PropName;
944
            param.Value = Value;
945
            cmd.Parameters.Add(param);
946
        }
947
948 5c248ee3 gaqhf
        public static bool SavePSNData(PSN item)
949 6b9e7a56 gaqhf
        {
950
            ID2Info id2Info = ID2Info.GetInstance();
951
952
            bool result = true;
953 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
954 6b9e7a56 gaqhf
            {
955
                try
956
                {
957 23a96301 humkyung
                    using (var txn = connection.BeginTransaction())
958 6b9e7a56 gaqhf
                    {
959
                        try
960
                        {
961 23a96301 humkyung
                            // Path Items
962
                            var query = $"DELETE FROM {PSN_PATHITEMS}";
963
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
964 9c151350 gaqhf
                            for (int i = 0; i < item.PathItems.Rows.Count; i++)
965 6b9e7a56 gaqhf
                            {
966 9c151350 gaqhf
                                DataRow row = item.PathItems.Rows[i];
967 a36541fb LJIYEON
                                query = $"INSERT INTO {PSN_PATHITEMS} VALUES (@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @DESCRIPTION, @Class, @SubClass, @TYPE, @PIDNAME, @Equipment_OID, @NPD, @PipeSystemNetwork_OID, @ViewPipeSystemNetwork_OID, @PipeRun_OID)";
968 23a96301 humkyung
                                var cmd = connection.GetSqlStringCommand(query);
969 f9f2787b LJIYEON
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
970
                                AddWithValue(cmd, "@SequenceData_OID", string.IsNullOrEmpty(row["SequenceData_OID"].ToString()) ? "" : row["SequenceData_OID"].ToString());
971
                                AddWithValue(cmd, "@TopologySet_OID", string.IsNullOrEmpty(row["TopologySet_OID"].ToString()) ? "" : row["TopologySet_OID"].ToString());
972
                                AddWithValue(cmd, "@BranchTopologySet_OID", string.IsNullOrEmpty(row["BranchTopologySet_OID"].ToString()) ? "" : row["BranchTopologySet_OID"].ToString());
973
                                AddWithValue(cmd, "@PipeLine_OID", string.IsNullOrEmpty(row["PipeLine_OID"].ToString()) ? "" : row["PipeLine_OID"].ToString());
974
                                AddWithValue(cmd, "@ITEMNAME", string.IsNullOrEmpty(row["ITEMNAME"].ToString()) ? "" : row["ITEMNAME"].ToString());
975
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
976
                                AddWithValue(cmd, "@DESCRIPTION", string.IsNullOrEmpty(row["DESCRIPTION"].ToString()) ? "" : row["DESCRIPTION"].ToString()); 
977
                                AddWithValue(cmd, "@Class", string.IsNullOrEmpty(row["Class"].ToString()) ? "" : row["Class"].ToString());
978
                                AddWithValue(cmd, "@SubClass", string.IsNullOrEmpty(row["SubClass"].ToString()) ? "" : row["SubClass"].ToString());
979
                                AddWithValue(cmd, "@TYPE", string.IsNullOrEmpty(row["TYPE"].ToString()) ? "" : row["TYPE"].ToString());
980
                                AddWithValue(cmd, "@PIDNAME", string.IsNullOrEmpty(row["PIDNAME"].ToString()) ? "" : row["PIDNAME"].ToString());
981
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString()); 
982
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
983
                                AddWithValue(cmd, "@PipeSystemNetwork_OID", string.IsNullOrEmpty(row["PipeSystemNetwork_OID"].ToString()) ? "" : row["PipeSystemNetwork_OID"].ToString());
984
                                AddWithValue(cmd, "@ViewPipeSystemNetwork_OID", string.IsNullOrEmpty(row["ViewPipeSystemNetwork_OID"].ToString()) ? "" : row["ViewPipeSystemNetwork_OID"].ToString());
985
                                AddWithValue(cmd, "@PipeRun_OID", string.IsNullOrEmpty(row["PipeRun_OID"].ToString()) ? "" : row["PipeRun_OID"].ToString());
986 23a96301 humkyung
                                connection.ExecuteNonQuery(cmd, txn);
987
                            }
988 6b9e7a56 gaqhf
989 23a96301 humkyung
                            // Sequence
990
                            query = $"DELETE FROM {PSN_SEQUENCEDATA}";
991
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
992
                            foreach (DataRow row in item.SequenceData.Rows)
993
                            {
994
                                query = $"INSERT INTO {PSN_SEQUENCEDATA} VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)";
995
                                var cmd = connection.GetSqlStringCommand(query);
996 f9f2787b LJIYEON
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
997
                                AddWithValue(cmd, "@SERIALNUMBER", string.IsNullOrEmpty(row["SERIALNUMBER"].ToString()) ? "" : row["SERIALNUMBER"].ToString());
998
                                AddWithValue(cmd, "@PathItem_OID", string.IsNullOrEmpty(row["PathItem_OID"].ToString()) ? "" : row["PathItem_OID"].ToString());
999
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1000 23a96301 humkyung
                                connection.ExecuteNonQuery(cmd, txn);
1001
                            }
1002 5c248ee3 gaqhf
1003 23a96301 humkyung
                            // Nozzle
1004
                            query = $"DELETE FROM {PSN_NOZZLE}";
1005
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1006
                            foreach (DataRow row in item.Nozzle.Rows)
1007
                            {
1008
                                query = $"INSERT INTO {PSN_NOZZLE} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS, @Equipment_OID, @FLUID, @NPD, @ROTATION, @FlowDirection)";
1009
                                var cmd = connection.GetSqlStringCommand(query);
1010 f9f2787b LJIYEON
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1011
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1012
1013
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1014
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1015
                                else
1016
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1017
1018
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1019
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1020
                                else
1021
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1022
1023
                                AddWithValue(cmd, "@Equipment_OID", string.IsNullOrEmpty(row["Equipment_OID"].ToString()) ? "" : row["Equipment_OID"].ToString());
1024
                                AddWithValue(cmd, "@FLUID", string.IsNullOrEmpty(row["FLUID"].ToString()) ? "" : row["FLUID"].ToString());
1025
                                AddWithValue(cmd, "@NPD", string.IsNullOrEmpty(row["NPD"].ToString()) ? "" : row["NPD"].ToString());
1026
1027
                                if (string.IsNullOrEmpty(row["ROTATION"].ToString()))
1028
                                    AddWithValue(cmd, "@ROTATION", DBNull.Value);
1029
                                else
1030
                                    AddWithValue(cmd, "@ROTATION", row["ROTATION"].ToString());
1031
                                
1032
                                AddWithValue(cmd, "@FlowDirection", string.IsNullOrEmpty(row["FlowDirection"].ToString()) ? "" : row["FlowDirection"].ToString());
1033 23a96301 humkyung
                                connection.ExecuteNonQuery(cmd, txn);
1034
                            }
1035 5c248ee3 gaqhf
1036 23a96301 humkyung
                            //Equipment
1037
                            query = $"DELETE FROM {PSN_EQUIPMENT}";
1038
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1039
                            foreach (DataRow row in item.Equipment.Rows)
1040
                            {
1041
                                query = $"INSERT INTO {PSN_EQUIPMENT} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)";
1042
                                var cmd = connection.GetSqlStringCommand(query);
1043 f9f2787b LJIYEON
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1044
                                AddWithValue(cmd, "@ITEMTAG", string.IsNullOrEmpty(row["ITEMTAG"].ToString()) ? "" : row["ITEMTAG"].ToString());
1045
1046
                                if (string.IsNullOrEmpty(row["XCOORDS"].ToString()))
1047
                                    AddWithValue(cmd, "@XCOORDS", DBNull.Value);
1048
                                else
1049
                                    AddWithValue(cmd, "@XCOORDS", row["XCOORDS"].ToString());
1050
1051
                                if (string.IsNullOrEmpty(row["YCOORDS"].ToString()))
1052
                                    AddWithValue(cmd, "@YCOORDS", DBNull.Value);
1053
                                else
1054
                                    AddWithValue(cmd, "@YCOORDS", row["YCOORDS"].ToString());
1055
1056 23a96301 humkyung
                                connection.ExecuteNonQuery(cmd, txn);
1057
                            }
1058 5c248ee3 gaqhf
1059 23a96301 humkyung
                            // TopologySet
1060
                            query = $"DELETE FROM {PSN_TOPOLOGYSET}";
1061
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1062
                            foreach (DataRow row in item.TopologySet.Rows)
1063
                            {
1064 72775f2e LJIYEON
                                query = $"INSERT INTO {PSN_TOPOLOGYSET} VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemSPID, @TailItemSPID)";
1065 23a96301 humkyung
                                var cmd = connection.GetSqlStringCommand(query);
1066 f9f2787b LJIYEON
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1067
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1068
                                AddWithValue(cmd, "@SubType", string.IsNullOrEmpty(row["SubType"].ToString()) ? "" : row["SubType"].ToString());
1069
                                AddWithValue(cmd, "@HeadItemTag", string.IsNullOrEmpty(row["HeadItemTag"].ToString()) ? "" : row["HeadItemTag"].ToString());
1070
                                AddWithValue(cmd, "@TailItemTag", string.IsNullOrEmpty(row["TailItemTag"].ToString()) ? "" : row["TailItemTag"].ToString());
1071
                                AddWithValue(cmd, "@HeadItemSPID", string.IsNullOrEmpty(row["HeadItemSPID"].ToString()) ? "" : row["HeadItemSPID"].ToString());
1072
                                AddWithValue(cmd, "@TailItemSPID", string.IsNullOrEmpty(row["TailItemSPID"].ToString()) ? "" : row["TailItemSPID"].ToString());
1073 23a96301 humkyung
                                connection.ExecuteNonQuery(cmd, txn);
1074
                            }
1075 36a45f13 gaqhf
1076 23a96301 humkyung
                            // PSN
1077
                            query = $"DELETE FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", item.Revision)}'";
1078
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1079
                            foreach (DataRow row in item.PipeSystemNetwork.Rows)
1080
                            {
1081 eb44d82c LJIYEON
                                query = $"INSERT INTO {PSN_PIPESYSTEMNETWORK} " +
1082
                                    $"(OID, Type, OrderNumber, Pipeline_OID, FROM_DATA, TO_DATA, TopologySet_OID_Key, PSNRevisionNumber, PBS, Drawings, IsValid, Status, IncludingVirtualData, PSNAccuracy) VALUES " +
1083
                                    $"(@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber,  @PBS, @Drawings, @IsValid, @Status, @IncludingVirtualData, @PSNAccuracy)";
1084 23a96301 humkyung
                                var cmd = connection.GetSqlStringCommand(query);
1085 f9f2787b LJIYEON
                                AddWithValue(cmd, "@OID", string.IsNullOrEmpty(row["OID"].ToString()) ? "" : row["OID"].ToString());
1086
                                AddWithValue(cmd, "@Type", string.IsNullOrEmpty(row["Type"].ToString()) ? "" : row["Type"].ToString());
1087
                                AddWithValue(cmd, "@OrderNumber", string.IsNullOrEmpty(row["OrderNumber"].ToString()) ? "" : row["OrderNumber"].ToString());
1088
                                AddWithValue(cmd, "@Pipeline_OID", string.IsNullOrEmpty(row["Pipeline_OID"].ToString()) ? "" : row["Pipeline_OID"].ToString());
1089
                                AddWithValue(cmd, "@FROM_DATA", string.IsNullOrEmpty(row["FROM_DATA"].ToString()) ? "" : row["FROM_DATA"].ToString());
1090
                                AddWithValue(cmd, "@TO_DATA", string.IsNullOrEmpty(row["TO_DATA"].ToString()) ? "" : row["TO_DATA"].ToString());
1091
                                AddWithValue(cmd, "@TopologySet_OID_Key", string.IsNullOrEmpty(row["TopologySet_OID_Key"].ToString()) ? "" : row["TopologySet_OID_Key"].ToString());
1092
                                AddWithValue(cmd, "@PSNRevisionNumber", string.IsNullOrEmpty(row["PSNRevisionNumber"].ToString()) ? "" : row["PSNRevisionNumber"].ToString());
1093 eb44d82c LJIYEON
1094 72775f2e LJIYEON
                                int IsValid = 0;
1095 eb44d82c LJIYEON
                                if (row["IsValid"].ToString() == string.Empty || row["IsValid"].ToString() == "OK")
1096 72775f2e LJIYEON
                                    IsValid = 0;
1097
                                else if (row["IsValid"].ToString() == "InValid")
1098
                                    IsValid = 1;
1099
                                else if (row["IsValid"].ToString() == "Error")
1100
                                    IsValid = -1;
1101 f9f2787b LJIYEON
1102 72775f2e LJIYEON
                                AddWithValue(cmd, "@IsValid", IsValid);
1103 f9f2787b LJIYEON
                                AddWithValue(cmd, "@Status", string.IsNullOrEmpty(row["Status"].ToString()) ? "" : row["Status"].ToString());
1104
1105
                                AddWithValue(cmd, "@PBS", string.IsNullOrEmpty(row["PBS"].ToString()) ? "" : row["PBS"].ToString());
1106
                                AddWithValue(cmd, "@Drawings", string.IsNullOrEmpty(row["Drawings"].ToString()) ? "" : row["Drawings"].ToString());
1107
1108
                                AddWithValue(cmd, "@IncludingVirtualData", string.IsNullOrEmpty(row["IncludingVirtualData"].ToString()) ? "" : row["IncludingVirtualData"].ToString());
1109
                                if (string.IsNullOrEmpty(row["PSNAccuracy"].ToString()))
1110
                                    AddWithValue(cmd, "@PSNAccuracy", DBNull.Value);
1111
                                else
1112
                                    AddWithValue(cmd, "@PSNAccuracy", row["PSNAccuracy"].ToString());
1113 eb44d82c LJIYEON
1114 23a96301 humkyung
                                connection.ExecuteNonQuery(cmd, txn);
1115 6b9e7a56 gaqhf
                            }
1116 23a96301 humkyung
1117 f9f2787b LJIYEON
                            //Pipeline
1118
                            query = $"DELETE FROM {PSN_PIPELINE}";
1119
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1120
                            foreach (DataRow row in item.PipeLine.Rows)
1121
                            {
1122
                                query = $"INSERT INTO {PSN_PIPELINE} VALUES (@OID, @PipeSystem_OID, @FLUID, @PMC, @SEQNUMBER, @INSULATION, @FROM_DATA, @TO_DATA, @Unit)";
1123
                                var cmd = connection.GetSqlStringCommand(query);
1124
                                AddWithValue(cmd, "@OID", row["OID"].ToString());
1125
                                AddWithValue(cmd, "@PipeSystem_OID", row["PipeSystem_OID"].ToString());
1126
                                AddWithValue(cmd, "@FLUID", row["FLUID"].ToString());
1127
                                AddWithValue(cmd, "@PMC", row["PMC"].ToString());
1128
                                AddWithValue(cmd, "@SEQNUMBER", row["SEQNUMBER"].ToString());
1129
                                AddWithValue(cmd, "@INSULATION", row["INSULATION"].ToString());
1130
                                AddWithValue(cmd, "@FROM_DATA", row["FROM_DATA"].ToString());
1131
                                AddWithValue(cmd, "@TO_DATA", row["TO_DATA"].ToString());
1132
                                AddWithValue(cmd, "@Unit", row["Unit"].ToString());
1133
                                connection.ExecuteNonQuery(cmd, txn);
1134
                            }
1135 a36541fb LJIYEON
1136 23a96301 humkyung
                            txn.Commit();
1137 6b9e7a56 gaqhf
                        }
1138
                        catch (Exception ex)
1139
                        {
1140 23a96301 humkyung
                            txn.Rollback();
1141 6b9e7a56 gaqhf
                            result = false;
1142
                        }
1143
                    }
1144
                }
1145
                catch (Exception ex)
1146
                {
1147
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1148
                    result = false;
1149
                }
1150
            }
1151
1152
            return result;
1153
        }
1154
1155 5c248ee3 gaqhf
        public static bool SavePSNFluidCode(DataTable dt)
1156 6b9e7a56 gaqhf
        {
1157
            ID2Info id2Info = ID2Info.GetInstance();
1158
1159
            bool result = true;
1160 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1161 6b9e7a56 gaqhf
            {
1162 1ae1a1c6 LJIYEON
                try
1163 6b9e7a56 gaqhf
                {
1164 23a96301 humkyung
                    using (var txn = connection.BeginTransaction())
1165 1ae1a1c6 LJIYEON
                    {
1166
                        try
1167 757ab2f2 LJIYEON
                        {
1168 23a96301 humkyung
                            var query = $"DELETE FROM {PSN_FLUIDCODE}";
1169
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1170
1171
                            foreach (DataRow row in dt.Rows)
1172 757ab2f2 LJIYEON
                            {
1173 23a96301 humkyung
                                query = $"INSERT INTO {PSN_FLUIDCODE} VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1174
                                var cmd = connection.GetSqlStringCommand(query);
1175
                                cmd.Parameters.Clear();
1176
1177
                                {
1178
                                    var param = cmd.CreateParameter();
1179
                                    param.ParameterName = "@UID";
1180
                                    param.Value = row["UID"].ToString();
1181
                                    cmd.Parameters.Add(param);
1182
                                }
1183
1184
                                {
1185
                                    var param = cmd.CreateParameter();
1186
                                    param.ParameterName = "@Code";
1187
                                    param.Value = row["Code"].ToString();
1188
                                    cmd.Parameters.Add(param);
1189
                                }
1190
1191
                                {
1192
                                    var param = cmd.CreateParameter();
1193
                                    param.ParameterName = "@Description";
1194
                                    param.Value = row["Description"].ToString();
1195
                                    cmd.Parameters.Add(param);
1196
                                }
1197
1198
                                {
1199
                                    var param = cmd.CreateParameter();
1200
                                    param.ParameterName = "@Condition";
1201
                                    param.Value = row["Condition"].ToString();
1202
                                    cmd.Parameters.Add(param);
1203
                                }
1204
1205
                                {
1206
                                    var param = cmd.CreateParameter();
1207
                                    param.ParameterName = "@Remarks";
1208
                                    param.Value = row["Remarks"].ToString();
1209
                                    cmd.Parameters.Add(param);
1210
                                }
1211 1ae1a1c6 LJIYEON
1212 757ab2f2 LJIYEON
                                {
1213 23a96301 humkyung
                                    var param = cmd.CreateParameter();
1214
                                    param.ParameterName = "@GroundLevel";
1215
                                    param.Value = row["GroundLevel"].ToString();
1216
                                    cmd.Parameters.Add(param);
1217 757ab2f2 LJIYEON
                                }
1218 23a96301 humkyung
1219
                                connection.ExecuteNonQuery(cmd, txn);
1220 757ab2f2 LJIYEON
                            }
1221 23a96301 humkyung
                            txn.Commit();
1222 757ab2f2 LJIYEON
                        }
1223 1ae1a1c6 LJIYEON
                        catch (Exception ex)
1224 6b9e7a56 gaqhf
                        {
1225 23a96301 humkyung
                            txn.Rollback();
1226 1ae1a1c6 LJIYEON
                            result = false;
1227 6b9e7a56 gaqhf
                        }
1228
                    }
1229 1ae1a1c6 LJIYEON
                }
1230
                catch (Exception ex)
1231
                {
1232
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1233
                    result = false;
1234
                }
1235 6b9e7a56 gaqhf
            }
1236
1237
            return result;
1238
        }
1239
1240 1ae1a1c6 LJIYEON
        public static DataTable SelectPSNFluidCode()
1241 31d37d58 LJIYEON
        {
1242 23a96301 humkyung
            DataTable dt = null;
1243 31d37d58 LJIYEON
            ID2Info id2Info = ID2Info.GetInstance();
1244 1ae1a1c6 LJIYEON
1245 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1246 31d37d58 LJIYEON
            {
1247 1ae1a1c6 LJIYEON
                try
1248 31d37d58 LJIYEON
                {
1249 23a96301 humkyung
                    var query = $"SELECT * FROM {PSN_FLUIDCODE}";
1250
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1251 31d37d58 LJIYEON
                    {
1252 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
1253 31d37d58 LJIYEON
                    }
1254
                }
1255 1ae1a1c6 LJIYEON
                catch (Exception ex)
1256
                {
1257
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1258
                }
1259 31d37d58 LJIYEON
            }
1260 23a96301 humkyung
1261 1ae1a1c6 LJIYEON
            return dt;
1262
        }
1263
1264
        public static bool SavePSNPMC(DataTable dt)
1265
        {
1266
            ID2Info id2Info = ID2Info.GetInstance();
1267
1268
            bool result = true;
1269 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1270 31d37d58 LJIYEON
            {
1271 1ae1a1c6 LJIYEON
                try
1272
                {
1273 23a96301 humkyung
                    using (var txn = connection.BeginTransaction())
1274 1ae1a1c6 LJIYEON
                    {
1275
                        try
1276
                        {
1277 23a96301 humkyung
                            var query = $"DELETE FROM {PSN_PIPINGMATLCLASS}";
1278
                            connection.ExecuteNonQuery(connection.GetSqlStringCommand(query), txn);
1279
1280
                            foreach (DataRow row in dt.Rows)
1281 1ae1a1c6 LJIYEON
                            {
1282 23a96301 humkyung
                                query = $"INSERT INTO {PSN_PIPINGMATLCLASS} VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)";
1283
                                var cmd = connection.GetSqlStringCommand(query);
1284
                                cmd.Parameters.Clear();
1285
1286
                                {
1287
                                    var param = cmd.CreateParameter();
1288
                                    param.ParameterName = "@UID";
1289
                                    param.Value = row["UID"].ToString();
1290
                                    cmd.Parameters.Add(param);
1291
                                }
1292
1293
                                {
1294
                                    var param = cmd.CreateParameter();
1295
                                    param.ParameterName = "@Priority";
1296
                                    param.Value = row["Priority"].ToString();
1297
                                    cmd.Parameters.Add(param);
1298
                                }
1299
1300
                                {
1301
                                    var param = cmd.CreateParameter();
1302
                                    param.ParameterName = "@Code";
1303
                                    param.Value = row["Code"].ToString();
1304
                                    cmd.Parameters.Add(param);
1305
                                }
1306
1307
                                {
1308
                                    var param = cmd.CreateParameter();
1309
                                    param.ParameterName = "@Description";
1310
                                    param.Value = row["Description"].ToString();
1311
                                    cmd.Parameters.Add(param);
1312
                                }
1313 31d37d58 LJIYEON
1314 1ae1a1c6 LJIYEON
                                {
1315 23a96301 humkyung
                                    var param = cmd.CreateParameter();
1316
                                    param.ParameterName = "@Condition";
1317
                                    param.Value = row["Condition"].ToString();
1318
                                    cmd.Parameters.Add(param);
1319 1ae1a1c6 LJIYEON
                                }
1320 23a96301 humkyung
1321
                                {
1322
                                    var param = cmd.CreateParameter();
1323
                                    param.ParameterName = "@Remarks";
1324
                                    param.Value = row["Remarks"].ToString();
1325
                                    cmd.Parameters.Add(param);
1326
                                }
1327
1328
                                {
1329
                                    var param = cmd.CreateParameter();
1330
                                    param.ParameterName = "@GroundLevel";
1331
                                    param.Value = row["GroundLevel"].ToString();
1332
                                    cmd.Parameters.Add(param);
1333
                                }
1334
1335
                                connection.ExecuteNonQuery(cmd, txn);
1336 1ae1a1c6 LJIYEON
                            }
1337 23a96301 humkyung
1338
                            txn.Commit();
1339 1ae1a1c6 LJIYEON
                        }
1340
                        catch (Exception ex)
1341
                        {
1342 23a96301 humkyung
                            txn.Rollback();
1343 1ae1a1c6 LJIYEON
                            result = false;
1344
                        }
1345
                    }
1346
                }
1347
                catch (Exception ex)
1348
                {
1349
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1350
                    result = false;
1351
                }
1352
            }
1353 31d37d58 LJIYEON
1354 1ae1a1c6 LJIYEON
            return result;
1355 31d37d58 LJIYEON
        }
1356
1357 5c248ee3 gaqhf
        public static PSN GetDBPSN()
1358 6b9e7a56 gaqhf
        {
1359 5c248ee3 gaqhf
            PSN result = new PSN();
1360 6b9e7a56 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
1361
1362 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1363 6b9e7a56 gaqhf
            {
1364
                try
1365
                {
1366 eb44d82c LJIYEON
                    //var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
1367
                    //using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1368
                    //{
1369
                    //    result.PipeSystemNetwork = ds.Tables[0].Clone();
1370
                    //    result.PipeSystemNetwork.Columns["IsValid"].DataType = typeof(string);
1371
1372
                    //    foreach (DataRow row in ds.Tables[0].Rows)
1373
                    //    {
1374
                    //        DataRow newRow = result.PipeSystemNetwork.NewRow();
1375
                    //        newRow["OID"] = row["OID"].ToString();
1376
                    //        newRow["Type"] = row["Type"].ToString();
1377
                    //        newRow["OrderNumber"] = row["OrderNumber"].ToString();
1378
                    //        newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
1379
                    //        newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
1380
                    //        newRow["TO_DATA"] = row["TO_DATA"].ToString();
1381
                    //        newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
1382
                    //        newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
1383
                           
1384
                    //        string IsValid = string.Empty;
1385
1386
                    //        if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
1387
                    //            IsValid = string.Empty;//"OK";
1388
                    //        else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
1389
                    //            IsValid = "InValid";
1390
                    //        else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
1391
                    //            IsValid = "Error";
1392
1393
                    //        newRow["IsValid"] = IsValid;
1394
1395
                    //        newRow["Status"] = row["Status"].ToString();
1396
                    //        newRow["PBS"] = row["PBS"].ToString();
1397
                    //        newRow["Drawings"] = row["Drawings"].ToString();
1398
1399
                    //        newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1400
                    //        newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();
1401
1402
                    //        result.PipeSystemNetwork.Rows.Add(newRow);
1403
                    //    }
1404
                    //}
1405
1406
                    var query = $"SELECT * FROM {PSN_EQUIPMENT}";
1407 23a96301 humkyung
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1408 6b9e7a56 gaqhf
                    {
1409 23a96301 humkyung
                        result.Equipment = ds.Tables[0].Copy();
1410
                    }
1411
1412
                    query = $"SELECT * FROM {PSN_NOZZLE}";
1413
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1414
                    {
1415
                        result.Nozzle = ds.Tables[0].Copy();
1416 6b9e7a56 gaqhf
                    }
1417 23a96301 humkyung
1418
                    query = $"SELECT * FROM {PSN_PATHITEMS}";
1419
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1420
                    {
1421
                        result.PathItems = ds.Tables[0].Copy();
1422
                    }
1423
1424
                    query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
1425
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1426
                    {
1427
                        result.SequenceData = ds.Tables[0].Copy();
1428
                    }
1429
1430 aadd8450 LJIYEON
                    query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
1431
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1432
                    {
1433
                        result.TopologySet = ds.Tables[0].Copy();
1434
                    }
1435
1436 a36541fb LJIYEON
                    query = $"SELECT * FROM {PSN_PIPELINE}";
1437
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1438
                    {
1439
                        result.PipeLine = ds.Tables[0].Copy();
1440
                    }                    
1441
1442 23a96301 humkyung
                    result.Revision = GetRevision();
1443 6b9e7a56 gaqhf
                }
1444
                catch (Exception ex)
1445
                {
1446
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1447 5c248ee3 gaqhf
                    result = null;
1448 6b9e7a56 gaqhf
                }
1449
            }
1450 23a96301 humkyung
1451 6b9e7a56 gaqhf
            return result;
1452
        }
1453 8f24b438 gaqhf
1454
        public static int GetRevision()
1455
        {
1456
            int result = 0;
1457
            ID2Info id2Info = ID2Info.GetInstance();
1458 23a96301 humkyung
1459 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1460 8f24b438 gaqhf
            {
1461
                try
1462
                {
1463 23a96301 humkyung
                    var query = $"SELECT DISTINCT PSNRevisionNumber FROM {PSN_PIPESYSTEMNETWORK};";
1464
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1465 8f24b438 gaqhf
                    {
1466 23a96301 humkyung
                        foreach (DataRow row in ds.Tables[0].Rows)
1467 8f24b438 gaqhf
                        {
1468 d5637426 gaqhf
                            string value = row["PSNRevisionNumber"].ToString();
1469 36a45f13 gaqhf
                            if (value.StartsWith("V"))
1470 d5637426 gaqhf
                                value = value.Remove(0, 1);
1471
                            int revisionNumber = Convert.ToInt32(value);
1472 8f24b438 gaqhf
                            if (result < revisionNumber)
1473
                                result = revisionNumber;
1474
                        }
1475
                    }
1476
                }
1477
                catch (Exception ex)
1478
                {
1479
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1480
                    result = -1;
1481
                }
1482
            }
1483
1484
            return result;
1485
        }
1486
1487 5c248ee3 gaqhf
        public static DataTable GetPathItem()
1488
        {
1489 23a96301 humkyung
            DataTable dt = null;
1490
1491 5c248ee3 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
1492 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1493 5c248ee3 gaqhf
            {
1494
                try
1495
                {
1496 23a96301 humkyung
                    var query = $"SELECT * FROM {PSN_PATHITEMS}";
1497
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1498 5c248ee3 gaqhf
                    {
1499 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
1500 5c248ee3 gaqhf
                    }
1501
                }
1502
                catch (Exception ex)
1503
                {
1504
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1505
                }
1506
            }
1507 23a96301 humkyung
1508 5c248ee3 gaqhf
            return dt;
1509
        }
1510 757ab2f2 LJIYEON
1511 36a45f13 gaqhf
        public static DataTable GetTopologySet()
1512 5c248ee3 gaqhf
        {
1513 23a96301 humkyung
            DataTable dt = null;
1514
1515 5c248ee3 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
1516 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1517 5c248ee3 gaqhf
            {
1518
                try
1519
                {
1520 23a96301 humkyung
                    var query = $"SELECT * FROM {PSN_TOPOLOGYSET}";
1521
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1522 5c248ee3 gaqhf
                    {
1523 aadd8450 LJIYEON
                        dt = ds.Tables[0].Clone();
1524
                        foreach (DataRow row in ds.Tables[0].Rows)
1525
                        {
1526
                            DataRow newRow = dt.NewRow();
1527
                            newRow["OID"] = row["OID"].ToString();
1528
                            newRow["Type"] = row["Type"].ToString();
1529
                            newRow["SubType"] = row["SubType"].ToString();
1530
                            newRow["HeadItemTag"] = row["HeadItemTag"].ToString();
1531
                            newRow["TailItemTag"] = row["TailItemTag"].ToString();
1532 72775f2e LJIYEON
                            newRow["HeadItemSPID"] = row["HeadItemSPID"].ToString();
1533
                            newRow["TailItemSPID"] = row["TailItemSPID"].ToString();
1534 aadd8450 LJIYEON
                            dt.Rows.Add(newRow);
1535
                        }
1536 5c248ee3 gaqhf
                    }
1537
                }
1538
                catch (Exception ex)
1539
                {
1540
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1541
                }
1542
            }
1543 23a96301 humkyung
1544 5c248ee3 gaqhf
            return dt;
1545
        }
1546 757ab2f2 LJIYEON
1547 5c248ee3 gaqhf
        public static DataTable GetPipeSystemNetwork()
1548
        {
1549 23a96301 humkyung
            DataTable dt = null;
1550
1551 5c248ee3 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
1552 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1553 5c248ee3 gaqhf
            {
1554
                try
1555
                {
1556 23a96301 humkyung
                    var query = $"SELECT * FROM {PSN_PIPESYSTEMNETWORK} WHERE PSNRevisionNumber = '{string.Format("V{0:D4}", GetRevision())}'";
1557
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1558 5c248ee3 gaqhf
                    {
1559 45529c16 LJIYEON
                        dt = ds.Tables[0].Clone();
1560 72775f2e LJIYEON
                        dt.Columns["IsValid"].DataType = typeof(string);
1561 45529c16 LJIYEON
                        foreach (DataRow row in ds.Tables[0].Rows)
1562
                        {
1563
                            DataRow newRow = dt.NewRow();
1564
                            newRow["OID"] = row["OID"].ToString();
1565
                            newRow["Type"] = row["Type"].ToString();
1566
                            newRow["OrderNumber"] = row["OrderNumber"].ToString();
1567
                            newRow["Pipeline_OID"] = row["Pipeline_OID"].ToString();
1568
                            newRow["FROM_DATA"] = row["FROM_DATA"].ToString();
1569
                            newRow["TO_DATA"] = row["TO_DATA"].ToString();
1570
                            newRow["TopologySet_OID_Key"] = row["TopologySet_OID_Key"].ToString();
1571
                            newRow["PSNRevisionNumber"] = row["PSNRevisionNumber"].ToString();
1572 eb44d82c LJIYEON
1573 72775f2e LJIYEON
                            string IsValid = string.Empty;
1574 45529c16 LJIYEON
1575 72775f2e LJIYEON
                            if (Convert.ToInt32(row["IsValid"].ToString()) == 0)
1576
                                IsValid = string.Empty;//"OK";
1577
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == 1)
1578
                                IsValid = "InValid";
1579
                            else if (Convert.ToInt32(row["IsValid"].ToString()) == -1)
1580
                                IsValid = "Error";
1581 45529c16 LJIYEON
1582 72775f2e LJIYEON
                            newRow["IsValid"] = IsValid;
1583 45529c16 LJIYEON
                            newRow["Status"] = row["Status"].ToString();
1584 eb44d82c LJIYEON
1585
                            newRow["PBS"] = row["PBS"].ToString();
1586
                            newRow["Drawings"] = row["Drawings"].ToString();
1587
                            
1588 ddc1c369 LJIYEON
                            newRow["IncludingVirtualData"] = row["IncludingVirtualData"].ToString();
1589 eb44d82c LJIYEON
                            newRow["PSNAccuracy"] = row["PSNAccuracy"].ToString();                                           
1590
                                                        
1591 45529c16 LJIYEON
                            dt.Rows.Add(newRow);
1592
                        }
1593 5c248ee3 gaqhf
                    }
1594
                }
1595
                catch (Exception ex)
1596
                {
1597
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1598
                }
1599
            }
1600 23a96301 humkyung
1601 5c248ee3 gaqhf
            return dt;
1602
        }
1603 757ab2f2 LJIYEON
1604 5c248ee3 gaqhf
        public static DataTable GetSequenceData()
1605
        {
1606 23a96301 humkyung
            DataTable dt = null;
1607
1608 5c248ee3 gaqhf
            ID2Info id2Info = ID2Info.GetInstance();
1609 c4a35107 humkyung
            using (IAbstractDatabase connection = id2Info.CreateConnection(ID2DB_Type.SQLite))
1610 5c248ee3 gaqhf
            {
1611
                try
1612
                {
1613 23a96301 humkyung
                    var query = $"SELECT * FROM {PSN_SEQUENCEDATA}";
1614
                    using (var ds = connection.ExecuteDataSet(connection.GetSqlStringCommand(query)))
1615 5c248ee3 gaqhf
                    {
1616 23a96301 humkyung
                        dt = ds.Tables[0].Copy();
1617 5c248ee3 gaqhf
                    }
1618
                }
1619
                catch (Exception ex)
1620
                {
1621
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1622
                }
1623
            }
1624 23a96301 humkyung
1625 5c248ee3 gaqhf
            return dt;
1626
        }
1627 0dae5645 gaqhf
    }
1628
}
클립보드 이미지 추가 (최대 크기: 500 MB)