프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / DB / Project_DB.cs @ 1b09fe17

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Globalization;
7
using System.Data.SQLite;
8
using System.Data;
9
using System.Data.SqlClient;
10
using Converter.SPPID;
11
using System.IO;
12
using Newtonsoft.Json;
13

    
14
namespace Converter.BaseModel
15
{
16
    public class Project_DB
17
    {
18
        const string SPPID_DB_INFO_TABLE = "T_SPPID_CONNECTION_INFO";
19
        const string SPPID_SYMBOL_MAPPING_TABLE = "T_SPPID_SYMBOL_MAPPING";
20
        const string SPPID_ATTRIBUTE_MAPPING_TABLE = "T_SPPID_ATTRIBUTE_MAPPING";
21
        const string SPPID_SETTING_TABLE = "T_SPPID_SETTING_TABLE";
22
        const string SPPID_LABEL_INFO_TABLE = "T_SPPID_LABEL_INFO";
23
        const string SPPID_DRAWING_INFO = "T_SPPID_DRAWING_INFO";
24
        const string SPPID_OPC_INFO = "T_SPPID_OPC_INFO";
25

    
26
        const string LineProperties_TABLE = "LineProperties";
27
        const string LineTypes_TABLE = "LineTypes";
28
        const string SymbolType_TABLE = "SymbolType";
29
        const string SymbolAttribute_TABLE = "SymbolAttribute";
30
        const string Symbol_TABLE = "Symbol";
31
        const string OPCRelations_TABLE = "OPCRelations";
32

    
33
        private static SqlConnection GetSqlConnection()
34
        {
35
            Project_Info projectInfo = Project_Info.GetInstance();
36
            SqlConnection connection = null;
37
            try
38
            {
39
                connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture,
40
                    @"Data Source = {0},{1}; Initial Catalog = {2}; User ID = {3}; Password = {4};",
41
                    projectInfo.ServerIP,
42
                    projectInfo.Port,
43
                    System.IO.Path.GetFileName(projectInfo.DefaultPath),
44
                    projectInfo.DBUser,
45
                    projectInfo.DBPassword));
46

    
47
                connection.Open();
48
            }
49
            catch (Exception ex)
50
            {
51
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
52
                if (connection != null)
53
                    connection.Dispose();
54
                connection = null;
55
            }
56

    
57
            return connection;
58
        }
59

    
60
        public static bool ConnTestAndCreateTable()
61
        {
62
            bool result = false;
63
            Project_Info projectInfo = Project_Info.GetInstance();
64
            if (projectInfo.DBType == ID2DB_Type.SQLite)
65
            {
66
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)))
67
                {
68
                    try
69
                    {
70
                        connection.Open();
71
                        if (connection.State == ConnectionState.Open)
72
                        {
73
                            using (SQLiteCommand cmd = connection.CreateCommand())
74
                            {
75
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
76
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
77
                                using (DataTable dt = new DataTable())
78
                                {
79
                                    dt.Load(dr);
80

    
81
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
82
                                    {
83
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE);
84
                                        cmd.ExecuteNonQuery();
85
                                    }
86
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
87
                                    {
88
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT, SettingType TEXT)", SPPID_SETTING_TABLE);
89
                                        cmd.ExecuteNonQuery();
90
                                    }
91
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
92
                                    {
93
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT, LEADERLINE BOOLEAN)", SPPID_SYMBOL_MAPPING_TABLE);
94
                                        cmd.ExecuteNonQuery();
95
                                    }
96
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
97
                                    {
98
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
99
                                        cmd.ExecuteNonQuery();
100
                                    }
101
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
102
                                    {
103
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BOOLEAN)", SPPID_LABEL_INFO_TABLE);
104
                                        cmd.ExecuteNonQuery();
105
                                    }
106
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
107
                                    {
108
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
109
                                        cmd.ExecuteNonQuery();
110
                                    }
111
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
112
                                    {
113
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID TEXT PRIMARY KEY, SPPID_OPC_MODELITEM_ID TEXT, ID2_DRAWING_UID TEXT, ATTRIBUTES TEXT, PAIRED BOOL)", SPPID_OPC_INFO);
114
                                        cmd.ExecuteNonQuery();
115
                                    }
116
                                }
117

    
118
                                #region Check Column 업데이트시 예비용
119
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
120
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
121
                                using (DataTable dt = new DataTable())
122
                                {
123
                                    dt.Load(dr);
124
                                    if (!dt.Columns.Contains("LEADERLINE"))
125
                                    {
126
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN LEADERLINE BOOLEAN", SPPID_SYMBOL_MAPPING_TABLE);
127
                                        cmd.ExecuteNonQuery();
128
                                    }
129
                                }
130

    
131
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
132
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
133
                                using (DataTable dt = new DataTable())
134
                                {
135
                                    dt.Load(dr);
136
                                    if (!dt.Columns.Contains("SettingType"))
137
                                    {
138
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN SettingType Text", SPPID_SETTING_TABLE);
139
                                        cmd.ExecuteNonQuery();
140
                                    }
141
                                }
142

    
143
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
144
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
145
                                using (DataTable dt = new DataTable())
146
                                {
147
                                    dt.Load(dr);
148
                                    if (dt.Columns.Contains("DOCUMENT"))
149
                                    {
150
                                        cmd.CommandText = string.Format("DROP TABLE {0}", SPPID_DRAWING_INFO);
151
                                        cmd.ExecuteNonQuery();
152

    
153
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
154
                                        cmd.ExecuteNonQuery();
155
                                    }
156
                                }
157
                                #endregion
158
                            }
159
                            result = true;
160
                        }
161
                        connection.Close();
162
                    }
163
                    catch (Exception ex)
164
                    {
165
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
166
                    }
167
                    finally
168
                    {
169
                        connection.Dispose();
170
                    }
171
                }
172
            }
173
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
174
            {
175
                using (SqlConnection connection = GetSqlConnection())
176
                {
177
                    try
178
                    {
179
                        if (connection != null && connection.State == ConnectionState.Open)
180
                        {
181
                            using (SqlCommand cmd = connection.CreateCommand())
182
                            {
183
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
184
                                using (SqlDataReader dr = cmd.ExecuteReader())
185
                                using (DataTable dt = new DataTable())
186
                                {
187
                                    dt.Load(dr);
188

    
189
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
190
                                    {
191
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX))", SPPID_DB_INFO_TABLE);
192
                                        cmd.ExecuteNonQuery();
193
                                    }
194
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
195
                                    {
196
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX), SettingType varchar(MAX))", SPPID_SETTING_TABLE);
197
                                        cmd.ExecuteNonQuery();
198
                                    }
199
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
200
                                    {
201
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(MAX), SPPID_SYMBOL_PATH varchar(MAX), LEADERLINE BIT)", SPPID_SYMBOL_MAPPING_TABLE);
202
                                        cmd.ExecuteNonQuery();
203
                                    }
204
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
205
                                    {
206
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, SPPID_ATTRIBUTE varchar(MAX))", SPPID_ATTRIBUTE_MAPPING_TABLE);
207
                                        cmd.ExecuteNonQuery();
208
                                    }
209
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
210
                                    {
211
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BIT)", SPPID_LABEL_INFO_TABLE);
212
                                        cmd.ExecuteNonQuery();
213
                                    }
214
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
215
                                    {
216
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID varchar(255) PRIMARY KEY, PATH varchar(MAX), DRAWINGNUMBER varchar(MAX), DRAWINGNAME varchar(MAX))", SPPID_DRAWING_INFO);
217
                                        cmd.ExecuteNonQuery();
218
                                    }
219
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
220
                                    {
221
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID varchar(255) PRIMARY KEY, SPPID_OPC_MODELITEM_ID varchar(MAX), ID2_DRAWING_UID varchar(MAX), ATTRIBUTES varchar(MAX), PAIRED BIT)", SPPID_OPC_INFO);
222
                                        cmd.ExecuteNonQuery();
223
                                    }
224
                                }
225

    
226
                                #region Check Column 업데이트시 예비용
227
                                #endregion
228
                            }
229
                            result = true;
230
                        }
231
                    }
232
                    catch (Exception ex)
233
                    {
234
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
235
                    }
236
                    finally
237
                    {
238
                        if (connection != null)
239
                            connection.Dispose();
240
                    }
241
                }
242
            }
243

    
244
            return result;
245
        }
246

    
247
        public static bool SaveSPPID_DB_INFO(string jsonString)
248
        {
249
            Project_Info projectInfo = Project_Info.GetInstance();
250
            if (projectInfo.DBType == ID2DB_Type.SQLite)
251
            {
252
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
253
                {
254

    
255
                    try
256
                    {
257
                        connection.Open();
258
                        using (SQLiteCommand cmd = connection.CreateCommand())
259
                        {
260
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
261
                            cmd.ExecuteNonQuery();
262

    
263
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
264
                            cmd.Parameters.AddWithValue("@jsonString", jsonString);
265
                            cmd.ExecuteNonQuery();
266
                        }
267
                        connection.Close();
268
                    }
269
                    catch (Exception ex)
270
                    {
271
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
272
                        return false;
273
                    }
274
                    finally
275
                    {
276
                        connection.Dispose();
277
                    }
278
                }
279
            }
280
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
281
            {
282
                using (SqlConnection connection = GetSqlConnection())
283
                {
284
                    try
285
                    {
286
                        if (connection != null && connection.State == ConnectionState.Open)
287
                        {
288
                            using (SqlCommand cmd = connection.CreateCommand())
289
                            {
290
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
291
                                cmd.ExecuteNonQuery();
292

    
293
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
294
                                cmd.Parameters.AddWithValue("@jsonString", jsonString);
295
                                cmd.ExecuteNonQuery();
296
                            }
297
                            connection.Close();
298
                        }
299
                        else
300
                        {
301
                            return false;
302
                        }
303
                    }
304
                    catch (Exception ex)
305
                    {
306
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
307
                        return false;
308
                    }
309
                    finally
310
                    {
311
                        if (connection != null)
312
                            connection.Dispose();
313
                    }
314
                }
315
            }
316

    
317

    
318
            return true;
319
        }
320

    
321
        public static DataTable SelectSPPID_DB_INFO()
322
        {
323
            DataTable dt = new DataTable();
324
            Project_Info projectInfo = Project_Info.GetInstance();
325
            if (projectInfo.DBType == ID2DB_Type.SQLite)
326
            {
327
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
328
                {
329
                    try
330
                    {
331
                        connection.Open();
332
                        using (SQLiteCommand cmd = connection.CreateCommand())
333
                        {
334
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
335
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
336
                                dt.Load(dr);
337
                        }
338
                        connection.Close();
339
                    }
340
                    catch (Exception ex)
341
                    {
342
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
343
                    }
344
                    finally
345
                    {
346
                        connection.Dispose();
347
                    }
348
                }
349
            }
350
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
351
            {
352
                using (SqlConnection connection = GetSqlConnection())
353
                {
354
                    try
355
                    {
356
                        if (connection != null && connection.State == ConnectionState.Open)
357
                        {
358
                            using (SqlCommand cmd = connection.CreateCommand())
359
                            {
360
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
361
                                using (SqlDataReader dr = cmd.ExecuteReader())
362
                                    dt.Load(dr);
363
                            }
364
                            connection.Close();
365
                        }
366
                    }
367
                    catch (Exception ex)
368
                    {
369
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
370
                    }
371
                    finally
372
                    {
373
                        if (connection != null)
374
                            connection.Dispose();
375
                    }
376
                }
377
            }
378

    
379
            return dt;
380
        }
381

    
382
        public static bool SaveETCSetting(Dictionary<string,string> dicSetting)
383
        {
384
            Project_Info projectInfo = Project_Info.GetInstance();
385
            if (projectInfo.DBType == ID2DB_Type.SQLite)
386
            {
387
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
388
                {
389

    
390
                    try
391
                    {
392
                        connection.Open();
393
                        using (SQLiteCommand cmd = connection.CreateCommand())
394
                        {
395
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
396
                            cmd.ExecuteNonQuery();
397

    
398
                            foreach (var item in dicSetting)
399
                            {
400
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
401
                                cmd.Parameters.Clear();
402
                                cmd.Parameters.AddWithValue("@jsonString", item.Value);
403
                                cmd.Parameters.AddWithValue("@SettingType", item.Key);
404
                                cmd.ExecuteNonQuery();
405
                            }
406
                        }
407
                        connection.Close();
408
                    }
409
                    catch (Exception ex)
410
                    {
411
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
412
                        return false;
413
                    }
414
                    finally
415
                    {
416
                        connection.Dispose();
417
                    }
418
                }
419
            }
420
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
421
            {
422
                using (SqlConnection connection = GetSqlConnection())
423
                {
424
                    try
425
                    {
426
                        if (connection != null && connection.State == ConnectionState.Open)
427
                        {
428
                            using (SqlCommand cmd = connection.CreateCommand())
429
                            {
430
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
431
                                cmd.ExecuteNonQuery();
432

    
433
                                foreach (var item in dicSetting)
434
                                {
435
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
436
                                    cmd.Parameters.Clear();
437
                                    cmd.Parameters.AddWithValue("@jsonString", item.Value);
438
                                    cmd.Parameters.AddWithValue("@SettingType", item.Key);
439
                                    cmd.ExecuteNonQuery();
440
                                }
441
                            }
442
                            connection.Close();
443
                        }
444
                        else
445
                            return false;
446
                    }
447
                    catch (Exception ex)
448
                    {
449
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
450
                        return false;
451
                    }
452
                    finally
453
                    {
454
                        if (connection != null)
455
                            connection.Dispose();
456
                    }
457
                }
458
            }
459

    
460
            return true;
461
        }
462

    
463
        public static DataTable SelectSetting()
464
        {
465
            DataTable dt = new DataTable();
466
            Project_Info projectInfo = Project_Info.GetInstance();
467
            if (projectInfo.DBType == ID2DB_Type.SQLite)
468
            {
469
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
470
                {
471
                    try
472
                    {
473
                        connection.Open();
474
                        using (SQLiteCommand cmd = connection.CreateCommand())
475
                        {
476
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
477
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
478
                                dt.Load(dr);
479
                        }
480
                        connection.Close();
481
                    }
482
                    catch (Exception ex)
483
                    {
484
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
485
                    }
486
                    finally
487
                    {
488
                        connection.Dispose();
489
                    }
490
                }
491
            }
492
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
493
            {
494
                using (SqlConnection connection = GetSqlConnection())
495
                {
496
                    try
497
                    {
498
                        if (connection != null && connection.State == ConnectionState.Open)
499
                        {
500
                            using (SqlCommand cmd = connection.CreateCommand())
501
                            {
502
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
503
                                using (SqlDataReader dr = cmd.ExecuteReader())
504
                                    dt.Load(dr);
505
                            }
506
                            connection.Close();
507
                        }
508
                    }
509
                    catch (Exception ex)
510
                    {
511
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
512
                    }
513
                    finally
514
                    {
515
                        if (connection != null)
516
                            connection.Dispose();
517
                    }
518
                }
519
            }
520

    
521
            return dt;
522
        }
523

    
524
        public static DataTable SelectProjectSymbol()
525
        {
526
            DataTable dt = new DataTable();
527
            Project_Info projectInfo = Project_Info.GetInstance();
528
            if (projectInfo.DBType == ID2DB_Type.SQLite)
529
            {
530
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
531
                {
532
                    try
533
                    {
534
                        connection.Open();
535
                        using (SQLiteCommand cmd = connection.CreateCommand())
536
                        {
537
                            cmd.CommandText = string.Format(@"
538
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
539
                                LEFT OUTER JOIN {2} as sp 
540
                                    ON s.UID = SP.UID 
541
                            WHERE s.SymbolType_UID = st.UID 
542
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
543
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
544
                                dt.Load(dr);
545

    
546
                            DataTable dtClone = dt.Clone();
547
                            dtClone.Columns["UID"].DataType = typeof(string);
548
                            foreach (DataRow row in dt.Rows)
549
                            {
550
                                dtClone.ImportRow(row);
551
                            }
552
                            dt.Dispose();
553
                            dt = dtClone;
554
                        }
555
                        connection.Close();
556
                    }
557
                    catch (Exception ex)
558
                    {
559
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
560
                    }
561
                    finally
562
                    {
563
                        connection.Dispose();
564
                    }
565
                }
566
            }
567
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
568
            {
569
                using (SqlConnection connection = GetSqlConnection())
570
                {
571
                    try
572
                    {
573
                        if (connection != null && connection.State == ConnectionState.Open)
574
                        {
575
                            using (SqlCommand cmd = connection.CreateCommand())
576
                            {
577
                                cmd.CommandText = string.Format(@"
578
                            SELECT CONVERT(VARCHAR(255), s.UID) AS UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
579
                                LEFT OUTER JOIN {2} as sp 
580
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
581
                            WHERE s.SymbolType_UID = st.UID 
582
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
583
                                using (SqlDataReader dr = cmd.ExecuteReader())
584
                                    dt.Load(dr);
585
                            }
586
                            connection.Close();
587
                        }
588
                    }
589
                    catch (Exception ex)
590
                    {
591
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
592
                    }
593
                    finally
594
                    {
595
                        if (connection != null)
596
                            connection.Dispose();
597
                    }
598
                }
599
            }
600

    
601
            return dt;
602
        }
603

    
604
        public static DataTable SelectProjectChildSymbol()
605
        {
606
            DataTable result = new DataTable();
607
            result.Columns.Add("UID");
608
            result.Columns.Add("Name");
609
            result.Columns.Add("Type");
610
            result.Columns.Add("SPPID_SYMBOL_PATH");
611
            
612
            Project_Info projectInfo = Project_Info.GetInstance();
613
            if (projectInfo.DBType == ID2DB_Type.SQLite)
614
            {
615
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
616
                using (DataTable dt = new DataTable())
617
                {
618
                    try
619
                    {
620
                        connection.Open();
621
                        using (SQLiteCommand cmd = connection.CreateCommand())
622
                        {
623
                            cmd.CommandText = string.Format(@"
624
                            SELECT AdditionalSymbol FROM Symbol");
625
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
626
                                dt.Load(dr);
627
                            List<string> childList = new List<string>();
628
                            foreach (DataRow row in dt.Rows)
629
                            {
630
                                if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
631
                                {
632
                                    string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
633
                                    foreach (var childString in array)
634
                                    {
635
                                        childList.Add(childString.Split(new char[] { ',' })[2]);
636
                                    }
637
                                }
638

    
639
                            }
640

    
641
                            dt.Clear();
642
                            cmd.Reset();
643
                            cmd.CommandText = string.Format(@"
644
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
645
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
646
                                dt.Load(dr);
647

    
648
                            childList = childList.Distinct().ToList();
649
                            foreach (var child in childList)
650
                            {
651
                                string mappingPath = string.Empty;
652
                                DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
653
                                if (rows.Length == 1)
654
                                    mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
655

    
656
                                DataRow newRow = result.NewRow();
657
                                newRow["UID"] = child;
658
                                newRow["Name"] = child;
659
                                newRow["Type"] = "Child Symbol";
660
                                newRow["SPPID_SYMBOL_PATH"] = mappingPath;
661
                                result.Rows.Add(newRow);
662
                            }
663
                        }
664
                        connection.Close();
665
                    }
666
                    catch (Exception ex)
667
                    {
668
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
669
                    }
670
                    finally
671
                    {
672
                        connection.Dispose();
673
                    }
674
                }
675
            }
676
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
677
            {
678
                using (SqlConnection connection = GetSqlConnection())
679
                using (DataTable dt = new DataTable())
680
                {
681
                    try
682
                    {
683
                        if (connection != null && connection.State == ConnectionState.Open)
684
                        {
685
                            using (SqlCommand cmd = connection.CreateCommand())
686
                            {
687
                                cmd.CommandText = string.Format(@"
688
                            SELECT AdditionalSymbol FROM Symbol");
689
                                using (SqlDataReader dr = cmd.ExecuteReader())
690
                                    dt.Load(dr);
691
                                List<string> childList = new List<string>();
692
                                foreach (DataRow row in dt.Rows)
693
                                {
694
                                    if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
695
                                    {
696
                                        string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
697
                                        foreach (var childString in array)
698
                                        {
699
                                            childList.Add(childString.Split(new char[] { ',' })[2]);
700
                                        }
701
                                    }
702

    
703
                                }
704

    
705
                                dt.Clear();
706
                                cmd.CommandText = string.Format(@"
707
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
708
                                using (SqlDataReader dr = cmd.ExecuteReader())
709
                                    dt.Load(dr);
710

    
711
                                childList = childList.Distinct().ToList();
712
                                foreach (var child in childList)
713
                                {
714
                                    string mappingPath = string.Empty;
715
                                    DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
716
                                    if (rows.Length == 1)
717
                                        mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
718

    
719
                                    DataRow newRow = result.NewRow();
720
                                    newRow["UID"] = child;
721
                                    newRow["Name"] = child;
722
                                    newRow["Type"] = "Child Symbol";
723
                                    newRow["SPPID_SYMBOL_PATH"] = mappingPath;
724
                                    result.Rows.Add(newRow);
725
                                }
726
                            }
727
                            connection.Close();
728
                        }
729
                    }
730
                    catch (Exception ex)
731
                    {
732
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
733
                    }
734
                    finally
735
                    {
736
                        if (connection != null)
737
                            connection.Dispose();
738
                    }
739
                }
740
            }
741
            return result;
742
        }
743

    
744
        public static DataTable SelectProjectLine()
745
        {
746
            DataTable dt = new DataTable();
747
            Project_Info projectInfo = Project_Info.GetInstance();
748
            if (projectInfo.DBType == ID2DB_Type.SQLite)
749
            {
750
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
751
                {
752
                    try
753
                    {
754
                        connection.Open();
755
                        using (SQLiteCommand cmd = connection.CreateCommand())
756
                        {
757
                            cmd.CommandText = string.Format(@"
758
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
759
                                LEFT OUTER JOIN {1} as sp 
760
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
761
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
762
                                dt.Load(dr);
763
                        }
764
                        connection.Close();
765
                    }
766
                    catch (Exception ex)
767
                    {
768
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
769
                    }
770
                    finally
771
                    {
772
                        connection.Dispose();
773
                    }
774
                }
775
            }
776
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
777
            {
778
                using (SqlConnection connection = GetSqlConnection())
779
                {
780
                    try
781
                    {
782
                        if (connection != null && connection.State == ConnectionState.Open)
783
                        {
784
                            using (SqlCommand cmd = connection.CreateCommand())
785
                            {
786
                                cmd.CommandText = string.Format(@"
787
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
788
                                LEFT OUTER JOIN {1} as sp 
789
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
790
                                using (SqlDataReader dr = cmd.ExecuteReader())
791
                                    dt.Load(dr);
792
                            }
793
                            connection.Close();
794
                        }
795
                    }
796
                    catch (Exception ex)
797
                    {
798
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
799
                    }
800
                    finally
801
                    {
802
                        if (connection != null)
803
                            connection.Dispose();
804
                    }
805
                }
806
            }
807

    
808
            return dt;
809
        }
810

    
811
        public static DataTable SelectProjectLineProperties()
812
        {
813
            DataTable dt = new DataTable();
814
            Project_Info projectInfo = Project_Info.GetInstance();
815
            if (projectInfo.DBType == ID2DB_Type.SQLite)
816
            {
817
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
818
                {
819
                    try
820
                    {
821
                        connection.Open();
822
                        using (SQLiteCommand cmd = connection.CreateCommand())
823
                        {
824
                            cmd.CommandText = string.Format(@"
825
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
826
                            FROM {0} as lp 
827
                                 LEFT OUTER JOIN {1} as sp 
828
                                      ON lp.UID = sp.UID
829
                                 LEFT OUTER JOIN {2} as spa 
830
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
831
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
832
                                dt.Load(dr);
833
                        }
834
                        connection.Close();
835
                    }
836
                    catch (Exception ex)
837
                    {
838
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
839
                    }
840
                    finally
841
                    {
842
                        connection.Dispose();
843
                    }
844
                }
845
            }
846
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
847
            {
848
                using (SqlConnection connection = GetSqlConnection())
849
                {
850
                    try
851
                    {
852
                        if (connection != null && connection.State == ConnectionState.Open)
853
                        {
854
                            using (SqlCommand cmd = connection.CreateCommand())
855
                            {
856
                                cmd.CommandText = string.Format(@"
857
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
858
                            FROM {0} as lp 
859
                                 LEFT OUTER JOIN {1} as sp 
860
                                      ON lp.UID = sp.UID
861
                                 LEFT OUTER JOIN {2} as spa 
862
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
863
                                using (SqlDataReader dr = cmd.ExecuteReader())
864
                                    dt.Load(dr);
865
                            }
866
                            connection.Close();
867
                        }
868
                    }
869
                    catch (Exception ex)
870
                    {
871
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
872
                    }
873
                    finally
874
                    {
875
                        if (connection != null)
876
                            connection.Dispose();
877
                    }
878
                }
879
            }
880

    
881
            return dt;
882
        }
883

    
884
        public static DataTable SelectProjectAttribute()
885
        {
886
            DataTable dt = new DataTable();
887
            Project_Info projectInfo = Project_Info.GetInstance();
888
            if (projectInfo.DBType == ID2DB_Type.SQLite)
889
            {
890
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
891
                {
892
                    try
893
                    {
894
                        connection.Open();
895
                        using (SQLiteCommand cmd = connection.CreateCommand())
896
                        {
897
                            cmd.CommandText = string.Format(@"
898
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE, sa.Property
899
                            FROM {1} as sa, {0} as st 
900
                                 LEFT OUTER JOIN {2} as sp 
901
                                      ON sa.UID = SP.UID 
902
                                LEFT OUTER JOIN {3} as spa 
903
                                     ON sa.UID = spa.UID
904
                                LEFT OUTER JOIN {4} as spl 
905
                                     ON sa.UID = spl.UID
906
                            WHERE sa.SymbolType_UID = st.UID AND (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE);
907
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
908
                                dt.Load(dr);
909
                        }
910
                        connection.Close();
911
                    }
912
                    catch (Exception ex)
913
                    {
914
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
915
                    }
916
                    finally
917
                    {
918
                        connection.Dispose();
919
                    }
920
                }
921
            }
922
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
923
            {
924
                using (SqlConnection connection = GetSqlConnection())
925
                {
926
                    try
927
                    {
928
                        if (connection != null && connection.State == ConnectionState.Open)
929
                        {
930
                            using (SqlCommand cmd = connection.CreateCommand())
931
                            {
932
                                cmd.CommandText = string.Format(@"
933
                            SELECT sa.UID, sa.DisplayAttribute, st.Type, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE, sa.Property
934
                            FROM {1} as sa
935
                                 LEFT OUTER JOIN {2} as sp 
936
                                      ON sa.UID = SP.UID 
937
                                LEFT OUTER JOIN {3} as spa 
938
                                     ON sa.UID = spa.UID
939
                                LEFT OUTER JOIN {4} as spl 
940
                                     ON sa.UID = spl.UID
941
                                LEFT OUTER JOIN {0} as st 
942
                                     ON sa.SymbolType_UID = st.UID 
943
                            WHERE (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE);
944
                                using (SqlDataReader dr = cmd.ExecuteReader())
945
                                    dt.Load(dr);
946
                            }
947
                            connection.Close();
948
                        }
949
                    }
950
                    catch (Exception ex)
951
                    {
952
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
953
                    }
954
                    finally
955
                    {
956
                        if (connection != null)
957
                            connection.Dispose();
958
                    }
959
                }
960
            }
961

    
962

    
963
            return dt;
964
        }
965

    
966
        public static DataTable SelectID2SymbolTable()
967
        {
968
            DataTable dt = new DataTable();
969
            Project_Info projectInfo = Project_Info.GetInstance();
970
            if (projectInfo.DBType == ID2DB_Type.SQLite)
971
            {
972
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
973
                {
974
                    try
975
                    {
976
                        connection.Open();
977
                        using (SQLiteCommand cmd = connection.CreateCommand())
978
                        {
979
                            cmd.CommandText = @"SELECT * FROM Symbol";
980
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
981
                                dt.Load(dr);
982
                        }
983
                        connection.Close();
984
                    }
985
                    catch (Exception ex)
986
                    {
987
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
988
                    }
989
                    finally
990
                    {
991
                        connection.Dispose();
992
                    }
993
                }
994
            }
995
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
996
            {
997
                using (SqlConnection connection = GetSqlConnection())
998
                {
999
                    try
1000
                    {
1001
                        if (connection != null && connection.State == ConnectionState.Open)
1002
                        {
1003
                            using (SqlCommand cmd = connection.CreateCommand())
1004
                            {
1005
                                cmd.CommandText = @"SELECT * FROM Symbol";
1006
                                using (SqlDataReader dr = cmd.ExecuteReader())
1007
                                    dt.Load(dr);
1008
                            }
1009
                            connection.Close();
1010
                        }
1011
                    }
1012
                    catch (Exception ex)
1013
                    {
1014
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1015
                    }
1016
                    finally
1017
                    {
1018
                        if (connection != null)
1019
                            connection.Dispose();
1020
                    }
1021
                }
1022
            }
1023

    
1024
            return dt;
1025
        }
1026

    
1027
        public static DataTable SelectOPCRelations()
1028
        {
1029
            DataTable dt = new DataTable();
1030
            Project_Info projectInfo = Project_Info.GetInstance();
1031
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1032
            {
1033
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1034
                {
1035
                    try
1036
                    {
1037
                        connection.Open();
1038
                        using (SQLiteCommand cmd = connection.CreateCommand())
1039
                        {
1040
                            cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1041
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1042
                                dt.Load(dr);
1043
                        }
1044
                        connection.Close();
1045
                    }
1046
                    catch (Exception ex)
1047
                    {
1048
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1049
                    }
1050
                    finally
1051
                    {
1052
                        connection.Dispose();
1053
                    }
1054
                }
1055
            }
1056
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1057
            {
1058
                using (SqlConnection connection = GetSqlConnection())
1059
                {
1060
                    try
1061
                    {
1062
                        if (connection != null && connection.State == ConnectionState.Open)
1063
                        {
1064
                            using (SqlCommand cmd = connection.CreateCommand())
1065
                            {
1066
                                cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1067
                                using (SqlDataReader dr = cmd.ExecuteReader())
1068
                                    dt.Load(dr);
1069
                            }
1070
                            connection.Close();
1071
                        }
1072
                    }
1073
                    catch (Exception ex)
1074
                    {
1075
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1076
                    }
1077
                    finally
1078
                    {
1079
                        if (connection != null)
1080
                            connection.Dispose();
1081
                    }
1082
                }
1083
            }
1084

    
1085
            return dt;
1086
        }
1087

    
1088
        public static DataTable SelectDrawings()
1089
        {
1090
            DataTable dt = new DataTable();
1091
            Project_Info projectInfo = Project_Info.GetInstance();
1092
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1093
            {
1094
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1095
                {
1096
                    try
1097
                    {
1098
                        connection.Open();
1099
                        using (SQLiteCommand cmd = connection.CreateCommand())
1100
                        {
1101
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1102
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1103
                                dt.Load(dr);
1104
                        }
1105
                        connection.Close();
1106
                    }
1107
                    catch (Exception ex)
1108
                    {
1109
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1110
                    }
1111
                    finally
1112
                    {
1113
                        connection.Dispose();
1114
                    }
1115
                }
1116
            }
1117
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1118
            {
1119
                using (SqlConnection connection = GetSqlConnection())
1120
                {
1121
                    try
1122
                    {
1123
                        if (connection != null && connection.State == ConnectionState.Open)
1124
                        {
1125
                            using (SqlCommand cmd = connection.CreateCommand())
1126
                            {
1127
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1128
                                using (SqlDataReader dr = cmd.ExecuteReader())
1129
                                    dt.Load(dr);
1130
                            }
1131
                            connection.Close();
1132
                        }
1133
                    }
1134
                    catch (Exception ex)
1135
                    {
1136
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1137
                    }
1138
                    finally
1139
                    {
1140
                        if (connection != null)
1141
                            connection.Dispose();
1142
                    }
1143
                }
1144
            }
1145

    
1146
            return dt;
1147
        }
1148

    
1149
        public static DataTable SelectOPCInfo()
1150
        {
1151
            DataTable dt = new DataTable();
1152
            Project_Info projectInfo = Project_Info.GetInstance();
1153
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1154
            {
1155
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1156
                {
1157
                    try
1158
                    {
1159
                        connection.Open();
1160
                        using (SQLiteCommand cmd = connection.CreateCommand())
1161
                        {
1162
                            cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO);
1163
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1164
                                dt.Load(dr);
1165
                        }
1166
                        connection.Close();
1167
                    }
1168
                    catch (Exception ex)
1169
                    {
1170
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1171
                    }
1172
                    finally
1173
                    {
1174
                        connection.Dispose();
1175
                    }
1176
                }
1177
            }
1178
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1179
            {
1180
                using (SqlConnection connection = GetSqlConnection())
1181
                {
1182
                    try
1183
                    {
1184
                        if (connection != null && connection.State == ConnectionState.Open)
1185
                        {
1186
                            using (SqlCommand cmd = connection.CreateCommand())
1187
                            {
1188
                                cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = 0", SPPID_OPC_INFO);
1189
                                using (SqlDataReader dr = cmd.ExecuteReader())
1190
                                    dt.Load(dr);
1191
                            }
1192
                            connection.Close();
1193
                        }
1194
                    }
1195
                    catch (Exception ex)
1196
                    {
1197
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1198
                    }
1199
                    finally
1200
                    {
1201
                        if (connection != null)
1202
                            connection.Dispose();
1203
                    }
1204
                }
1205
            }
1206

    
1207
            return dt;
1208
        }
1209

    
1210
        public static DataTable SelectSymbolType()
1211
        {
1212
            DataTable dt = new DataTable();
1213
            Project_Info projectInfo = Project_Info.GetInstance();
1214
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1215
            {
1216
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1217
                {
1218
                    try
1219
                    {
1220
                        connection.Open();
1221
                        using (SQLiteCommand cmd = connection.CreateCommand())
1222
                        {
1223
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1224
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1225
                                dt.Load(dr);
1226
                        }
1227
                        connection.Close();
1228
                    }
1229
                    catch (Exception ex)
1230
                    {
1231
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1232
                    }
1233
                    finally
1234
                    {
1235
                        connection.Dispose();
1236
                    }
1237
                }
1238
            }
1239
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1240
            {
1241
                using (SqlConnection connection = GetSqlConnection())
1242
                {
1243
                    try
1244
                    {
1245
                        if (connection != null && connection.State == ConnectionState.Open)
1246
                        {
1247
                            using (SqlCommand cmd = connection.CreateCommand())
1248
                            {
1249
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1250
                                using (SqlDataReader dr = cmd.ExecuteReader())
1251
                                    dt.Load(dr);
1252
                            }
1253
                            connection.Close();
1254
                        }
1255
                    }
1256
                    catch (Exception ex)
1257
                    {
1258
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1259
                    }
1260
                    finally
1261
                    {
1262
                        if (connection != null)
1263
                            connection.Dispose();
1264
                    }
1265
                }
1266
            }
1267

    
1268
            return dt;
1269
        }
1270

    
1271
        public static DataTable SelectDrawingInfo()
1272
        {
1273
            DataTable dt = new DataTable();
1274
            Project_Info projectInfo = Project_Info.GetInstance();
1275
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1276
            {
1277
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1278
                {
1279
                    try
1280
                    {
1281
                        connection.Open();
1282
                        using (SQLiteCommand cmd = connection.CreateCommand())
1283
                        {
1284
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1285
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1286
                                dt.Load(dr);
1287
                        }
1288
                        connection.Close();
1289
                    }
1290
                    catch (Exception ex)
1291
                    {
1292
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1293
                    }
1294
                    finally
1295
                    {
1296
                        connection.Dispose();
1297
                    }
1298
                }
1299
            }
1300
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1301
            {
1302
                using (SqlConnection connection = GetSqlConnection())
1303
                {
1304
                    try
1305
                    {
1306
                        if (connection != null && connection.State == ConnectionState.Open)
1307
                        {
1308
                            using (SqlCommand cmd = connection.CreateCommand())
1309
                            {
1310
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1311
                                using (SqlDataReader dr = cmd.ExecuteReader())
1312
                                    dt.Load(dr);
1313
                            }
1314
                            connection.Close();
1315
                        }
1316
                    }
1317
                    catch (Exception ex)
1318
                    {
1319
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1320
                    }
1321
                    finally
1322
                    {
1323
                        if (connection != null)
1324
                            connection.Dispose();
1325
                    }
1326
                }
1327
            }
1328

    
1329
            return dt;
1330
        }
1331

    
1332
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas)
1333
        {
1334
            Project_Info projectInfo = Project_Info.GetInstance();
1335
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1336
            {
1337
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1338
                {
1339
                    try
1340
                    {
1341
                        connection.Open();
1342
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1343
                        {
1344
                            try
1345
                            {
1346
                                using (SQLiteCommand cmd = connection.CreateCommand())
1347
                                {
1348
                                    foreach (var item in datas)
1349
                                    {
1350
                                        cmd.Parameters.Clear();
1351
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE);
1352
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1353
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1354
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1355
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1356
                                        cmd.ExecuteNonQuery();
1357
                                    }
1358
                                }
1359
                                transaction.Commit();
1360
                                connection.Close();
1361
                            }
1362
                            catch (Exception ex)
1363
                            {
1364
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1365
                                transaction.Rollback();
1366
                                return false;
1367
                            }
1368
                            finally
1369
                            {
1370
                                transaction.Dispose();
1371
                            }
1372
                        }
1373
                    }
1374
                    catch (Exception ex)
1375
                    {
1376
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1377
                        return false;
1378
                    }
1379
                    finally
1380
                    {
1381
                        connection.Dispose();
1382
                    }
1383
                }
1384
            }
1385
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1386
            {
1387
                using (SqlConnection connection = GetSqlConnection())
1388
                {
1389
                    try
1390
                    {
1391
                        if (connection != null && connection.State == ConnectionState.Open)
1392
                        {
1393
                            using (SqlCommand cmd = connection.CreateCommand())
1394
                            {
1395
                                foreach (var item in datas)
1396
                                {
1397
                                    cmd.Parameters.Clear();
1398
                                    cmd.CommandText = string.Format(@"
1399
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1400
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
1401
                                    ELSE
1402
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, item.Item1);
1403
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1404
                                    if (string.IsNullOrEmpty(item.Item2))
1405
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
1406
                                    else
1407
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1408
                                    if (string.IsNullOrEmpty(item.Item3))
1409
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
1410
                                    else
1411
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1412
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1413
                                    cmd.ExecuteNonQuery();
1414
                                }
1415
                            }
1416
                            connection.Close();
1417
                        }
1418
                    }
1419
                    catch (Exception ex)
1420
                    {
1421
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1422
                        return false;
1423
                    }
1424
                    finally
1425
                    {
1426
                        if (connection != null)
1427
                            connection.Dispose();
1428
                    }
1429
                }
1430
            }
1431

    
1432
            return true;
1433
        }
1434

    
1435
        public static bool InsertAttributeMapping(List<Tuple<string, string>> datas)
1436
        {
1437
            Project_Info projectInfo = Project_Info.GetInstance();
1438
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1439
            {
1440
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1441
                {
1442
                    try
1443
                    {
1444
                        connection.Open();
1445
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1446
                        {
1447
                            try
1448
                            {
1449
                                using (SQLiteCommand cmd = connection.CreateCommand())
1450
                                {
1451
                                    foreach (var item in datas)
1452
                                    {
1453
                                        cmd.Parameters.Clear();
1454
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
1455
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1456
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1457
                                        cmd.ExecuteNonQuery();
1458
                                    }
1459
                                }
1460
                                transaction.Commit();
1461
                                connection.Close();
1462
                            }
1463
                            catch (Exception ex)
1464
                            {
1465
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1466
                                transaction.Rollback();
1467
                            }
1468
                            finally
1469
                            {
1470
                                transaction.Dispose();
1471
                            }
1472
                        }
1473
                    }
1474
                    catch (Exception ex)
1475
                    {
1476
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1477
                        return false;
1478
                    }
1479
                    finally
1480
                    {
1481
                        connection.Dispose();
1482
                    }
1483
                }
1484
            }
1485
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1486
            {
1487
                using (SqlConnection connection = GetSqlConnection())
1488
                {
1489
                    try
1490
                    {
1491
                        if (connection != null && connection.State == ConnectionState.Open)
1492
                        {
1493
                            using (SqlCommand cmd = connection.CreateCommand())
1494
                            {
1495
                                foreach (var item in datas)
1496
                                {
1497
                                    cmd.Parameters.Clear();
1498
                                    cmd.CommandText = string.Format(@"
1499
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1500
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID
1501
                                    ELSE
1502
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, item.Item1);
1503

    
1504
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1505
                                    if (string.IsNullOrEmpty(item.Item2))
1506
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
1507
                                    else
1508
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1509
                                    cmd.ExecuteNonQuery();
1510
                                }
1511
                            }
1512
                            connection.Close();
1513
                        }
1514
                    }
1515
                    catch (Exception ex)
1516
                    {
1517
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1518
                        return false;
1519
                    }
1520
                    finally
1521
                    {
1522
                        if (connection != null)
1523
                            connection.Dispose();
1524
                    }
1525
                }
1526
            }
1527
            return true;
1528
        }
1529

    
1530
        public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas)
1531
        {
1532
            Project_Info projectInfo = Project_Info.GetInstance();
1533
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1534
            {
1535
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1536
                {
1537
                    try
1538
                    {
1539
                        connection.Open();
1540
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1541
                        {
1542
                            try
1543
                            {
1544
                                using (SQLiteCommand cmd = connection.CreateCommand())
1545
                                {
1546
                                    foreach (var item in datas)
1547
                                    {
1548
                                        cmd.Parameters.Clear();
1549
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE);
1550
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1551
                                        cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1552
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1553
                                        cmd.ExecuteNonQuery();
1554
                                    }
1555
                                }
1556
                                transaction.Commit();
1557
                                connection.Close();
1558
                            }
1559
                            catch (Exception ex)
1560
                            {
1561
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1562
                                transaction.Rollback();
1563
                                return false;
1564
                            }
1565
                            finally
1566
                            {
1567
                                transaction.Dispose();
1568
                            }
1569
                        }
1570
                    }
1571
                    catch (Exception ex)
1572
                    {
1573
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1574
                        return false;
1575
                    }
1576
                    finally
1577
                    {
1578
                        connection.Dispose();
1579
                    }
1580
                }
1581
            }
1582
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1583
            {
1584
                using (SqlConnection connection = GetSqlConnection())
1585
                {
1586
                    try
1587
                    {
1588
                        if (connection != null && connection.State == ConnectionState.Open)
1589
                        {
1590
                            using (SqlCommand cmd = connection.CreateCommand())
1591
                            {
1592
                                foreach (var item in datas)
1593
                                {
1594
                                    cmd.Parameters.Clear();
1595
                                    cmd.CommandText = string.Format(@"
1596
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1597
                                        UPDATE {0} SET LOCATION = @LOCATION, LEADERLINE = @LEADERLINE WHERE UID = @UID
1598
                                    ELSE
1599
                                        INSERT INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE, item.Item1);
1600

    
1601
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1602
                                    cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1603
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1604
                                    cmd.ExecuteNonQuery();
1605
                                }
1606
                            }
1607
                            connection.Close();
1608
                        }
1609
                    }
1610
                    catch (Exception ex)
1611
                    {
1612
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1613
                        return false;
1614
                    }
1615
                    finally
1616
                    {
1617
                        if (connection != null)
1618
                            connection.Dispose();
1619
                    }
1620
                }
1621
            }
1622
            return true;
1623
        }
1624

    
1625
        public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
1626
        {
1627
            Project_Info projectInfo = Project_Info.GetInstance();
1628
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1629
            {
1630
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1631
                {
1632
                    try
1633
                    {
1634
                        connection.Open();
1635
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1636
                        {
1637
                            try
1638
                            {
1639
                                using (SQLiteCommand cmd = connection.CreateCommand())
1640
                                {
1641
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO);
1642
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1643
                                    cmd.Parameters.AddWithValue("@PATH", path);
1644
                                    cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1645
                                    cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1646
                                    cmd.ExecuteNonQuery();
1647
                                }
1648

    
1649
                                List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1650
                                foreach (var item in OPCs)
1651
                                {
1652
                                    using (SQLiteCommand cmd = connection.CreateCommand())
1653
                                    {
1654
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, ATTRIBUTES, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @ATTRIBUTES, @PAIRED)", SPPID_OPC_INFO);
1655
                                        cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1656
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1657
                                        cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1658
                                        cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1659
                                        cmd.Parameters.AddWithValue("@PAIRED", false);
1660
                                        cmd.ExecuteNonQuery();
1661
                                    }
1662
                                }
1663

    
1664
                                transaction.Commit();
1665
                                connection.Close();
1666
                            }
1667
                            catch (Exception ex)
1668
                            {
1669
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1670
                                transaction.Rollback();
1671
                                return false;
1672
                            }
1673
                            finally
1674
                            {
1675
                                transaction.Dispose();
1676
                            }
1677
                        }
1678
                    }
1679
                    catch (Exception ex)
1680
                    {
1681
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1682
                        return false;
1683
                    }
1684
                    finally
1685
                    {
1686
                        connection.Dispose();
1687
                    }
1688
                }
1689
            }
1690
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1691
            {
1692
                using (SqlConnection connection = GetSqlConnection())
1693
                {
1694
                    try
1695
                    {
1696
                        if (connection != null && connection.State == ConnectionState.Open)
1697
                        {
1698
                            using (SqlCommand cmd = connection.CreateCommand())
1699
                            {
1700
                                cmd.Parameters.Clear();
1701
                                cmd.CommandText = string.Format(@"
1702
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_DRAWING_UID = '{1}')
1703
                                        UPDATE {0} SET PATH = @PATH, DRAWINGNUMBER = @DRAWINGNUMBER, DRAWINGNAME = @DRAWINGNAME WHERE ID2_DRAWING_UID = @ID2_DRAWING_UID
1704
                                    ELSE
1705
                                        INSERT INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO, document.UID);
1706

    
1707
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1708
                                cmd.Parameters.AddWithValue("@PATH", path);
1709
                                cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1710
                                cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1711
                                cmd.ExecuteNonQuery();
1712
                            }
1713

    
1714
                            List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1715
                            foreach (var item in OPCs)
1716
                            {
1717
                                using (SqlCommand cmd = connection.CreateCommand())
1718
                                {
1719
                                    cmd.Parameters.Clear();
1720
                                    cmd.CommandText = string.Format(@"
1721
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_OPC_UID = '{1}')
1722
                                        UPDATE {0} SET SPPID_OPC_MODELITEM_ID = @SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID = @ID2_DRAWING_UID, ATTRIBUTES = @ATTRIBUTES, PAIRED = @PAIRED WHERE ID2_OPC_UID = @ID2_OPC_UID
1723
                                    ELSE
1724
                                        INSERT INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, ATTRIBUTES, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @ATTRIBUTES, @PAIRED)", SPPID_OPC_INFO, item.UID);
1725

    
1726
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1727
                                    if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
1728
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
1729
                                    else
1730
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1731
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1732
                                    cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1733
                                    cmd.Parameters.AddWithValue("@PAIRED", false);
1734
                                    cmd.ExecuteNonQuery();
1735
                                }
1736
                            }
1737
                            connection.Close();
1738
                        }
1739
                    }
1740
                    catch (Exception ex)
1741
                    {
1742
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1743
                        return false;
1744
                    }
1745
                    finally
1746
                    {
1747
                        if (connection != null)
1748
                            connection.Dispose();
1749
                    }
1750
                }
1751
            }
1752
            return true;
1753
        }
1754

    
1755
        public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired)
1756
        {
1757
            Project_Info projectInfo = Project_Info.GetInstance();
1758
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1759
            {
1760
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1761
                {
1762
                    try
1763
                    {
1764
                        connection.Open();
1765
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1766
                        {
1767
                            try
1768
                            {
1769
                                using (SQLiteCommand cmd = connection.CreateCommand())
1770
                                {
1771
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @PAIRED)", SPPID_OPC_INFO);
1772
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
1773
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
1774
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
1775
                                    cmd.Parameters.AddWithValue("@PAIRED", Paired);
1776
                                    cmd.ExecuteNonQuery();
1777
                                }
1778

    
1779
                                transaction.Commit();
1780
                                connection.Close();
1781
                            }
1782
                            catch (Exception ex)
1783
                            {
1784
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1785
                                transaction.Rollback();
1786
                                return false;
1787
                            }
1788
                            finally
1789
                            {
1790
                                transaction.Dispose();
1791
                            }
1792
                        }
1793
                    }
1794
                    catch (Exception ex)
1795
                    {
1796
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1797
                        return false;
1798
                    }
1799
                    finally
1800
                    {
1801
                        connection.Dispose();
1802
                    }
1803
                }
1804
            }
1805
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1806
            {
1807
                using (SqlConnection connection = GetSqlConnection())
1808
                {
1809
                    try
1810
                    {
1811
                        if (connection != null && connection.State == ConnectionState.Open)
1812
                        {
1813
                            using (SqlCommand cmd = connection.CreateCommand())
1814
                            {
1815
                                cmd.Parameters.Clear();
1816
                                cmd.CommandText = string.Format(@"
1817
                                    UPDATE {0} SET SPPID_OPC_MODELITEM_ID = @SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID = @ID2_DRAWING_UID, PAIRED = @PAIRED WHERE ID2_OPC_UID = @ID2_OPC_UID", SPPID_OPC_INFO);
1818
                                cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
1819
                                if (string.IsNullOrEmpty(ModelItemID))
1820
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
1821
                                else
1822
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
1823
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
1824
                                cmd.Parameters.AddWithValue("@PAIRED", Paired);
1825
                                cmd.ExecuteNonQuery();
1826
                            }
1827
                            connection.Close();
1828
                        }
1829
                    }
1830
                    catch (Exception ex)
1831
                    {
1832
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1833
                        return false;
1834
                    }
1835
                    finally
1836
                    {
1837
                        if (connection != null)
1838
                            connection.Dispose();
1839
                    }
1840
                }
1841
            }
1842
            return true;
1843
        }
1844

    
1845
        public static bool ExportMappingData()
1846
        {
1847
            bool result = true;
1848
            try
1849
            {
1850
                DataSet dataSet = new DataSet();
1851
                DataTable symbolMappingDT = GetTable(SPPID_SYMBOL_MAPPING_TABLE);
1852
                if (symbolMappingDT != null)
1853
                {
1854
                    symbolMappingDT.TableName = SPPID_SYMBOL_MAPPING_TABLE;
1855
                    dataSet.Tables.Add(symbolMappingDT);
1856
                }
1857
                else
1858
                    result = false;
1859

    
1860
                DataTable attributeMappingDT = GetTable(SPPID_ATTRIBUTE_MAPPING_TABLE);
1861
                if (attributeMappingDT != null)
1862
                {
1863
                    attributeMappingDT.TableName = SPPID_ATTRIBUTE_MAPPING_TABLE;
1864
                    dataSet.Tables.Add(attributeMappingDT);
1865
                }
1866
                else
1867
                    result = false;
1868

    
1869
                if (result)
1870
                {
1871
                    string text = JsonConvert.SerializeObject(dataSet);
1872
                    using (StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf", false))
1873
                    {
1874
                        sw.Write(text);
1875
                        sw.Close();
1876
                        sw.Dispose();
1877
                    }
1878
                }
1879
            }
1880
            catch (Exception ex)
1881
            {
1882
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1883
                result = false;
1884
            }
1885

    
1886
            return result;
1887
        }
1888

    
1889
        public static bool ImportMappingData()
1890
        {
1891
            bool result = true;
1892
            try
1893
            {
1894
                string sJson = string.Empty;
1895
                using (StreamReader sw = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf"))
1896
                {
1897
                    sJson = sw.ReadToEnd();
1898
                    sw.Close();
1899
                    sw.Dispose();
1900
                }
1901

    
1902
                DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(sJson);
1903
                DataTable symbolMappingDT = dataSet.Tables[SPPID_SYMBOL_MAPPING_TABLE];
1904
                if (symbolMappingDT == null || !ImportSymbolMappingTable(symbolMappingDT))
1905
                    result = false;
1906

    
1907
                DataTable attributeMappingDT = dataSet.Tables[SPPID_ATTRIBUTE_MAPPING_TABLE];
1908
                if (attributeMappingDT == null || !ImportAttributeMappingTable(attributeMappingDT))
1909
                    result = false;
1910
            }
1911
            catch (Exception ex)
1912
            {
1913
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1914
                result = false;
1915
            }
1916

    
1917
            return result;
1918
        }
1919

    
1920
        private static DataTable GetTable(string tableName)
1921
        {
1922
            DataTable dt = new DataTable();
1923
            Project_Info projectInfo = Project_Info.GetInstance();
1924
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1925
            {
1926
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1927
                {
1928
                    try
1929
                    {
1930
                        connection.Open();
1931
                        using (SQLiteCommand cmd = connection.CreateCommand())
1932
                        {
1933
                            cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
1934
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1935
                                dt.Load(dr);
1936
                        }
1937
                        connection.Close();
1938
                    }
1939
                    catch (Exception ex)
1940
                    {
1941
                        dt = null;
1942
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1943
                    }
1944
                    finally
1945
                    {
1946
                        connection.Dispose();
1947
                    }
1948
                }
1949
            }
1950
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1951
            {
1952
                using (SqlConnection connection = GetSqlConnection())
1953
                {
1954
                    try
1955
                    {
1956
                        if (connection != null && connection.State == ConnectionState.Open)
1957
                        {
1958
                            using (SqlCommand cmd = connection.CreateCommand())
1959
                            {
1960
                                cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
1961
                                using (SqlDataReader dr = cmd.ExecuteReader())
1962
                                    dt.Load(dr);
1963
                            }
1964
                            connection.Close();
1965
                        }
1966
                    }
1967
                    catch (Exception ex)
1968
                    {
1969
                        dt = null;
1970
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1971
                    }
1972
                    finally
1973
                    {
1974
                        if (connection != null)
1975
                            connection.Dispose();
1976
                    }
1977
                }
1978
            }
1979

    
1980
            return dt;
1981
        }
1982

    
1983
        private static bool ImportSymbolMappingTable(DataTable dt)
1984
        {
1985
            bool result = false;
1986

    
1987
            Project_Info projectInfo = Project_Info.GetInstance();
1988
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1989
            {
1990
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1991
                {
1992
                    try
1993
                    {
1994
                        connection.Open();
1995
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1996
                        {
1997
                            try
1998
                            {
1999
                                using (SQLiteCommand cmd = connection.CreateCommand())
2000
                                {
2001
                                    foreach (DataRow item in dt.Rows)
2002
                                    {
2003
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2004
                                        string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2005
                                        string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2006
                                        bool LEADERLINE = Convert.ToBoolean(item["LEADERLINE"]);
2007

    
2008
                                        cmd.Parameters.Clear();
2009
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE);
2010
                                        cmd.Parameters.AddWithValue("@UID", UID);
2011
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2012
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2013
                                        cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2014
                                        cmd.ExecuteNonQuery();
2015
                                    }
2016
                                }
2017
                                transaction.Commit();
2018
                                connection.Close();
2019
                                result = true;
2020
                            }
2021
                            catch (Exception ex)
2022
                            {
2023
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2024
                                transaction.Rollback();
2025
                            }
2026
                            finally
2027
                            {
2028
                                transaction.Dispose();
2029
                            }
2030
                        }
2031
                    }
2032
                    catch (Exception ex)
2033
                    {
2034
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2035
                    }
2036
                    finally
2037
                    {
2038
                        connection.Dispose();
2039
                    }
2040
                }
2041
            }
2042
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2043
            {
2044
                using (SqlConnection connection = GetSqlConnection())
2045
                {
2046
                    try
2047
                    {
2048
                        if (connection != null && connection.State == ConnectionState.Open)
2049
                        {
2050
                            using (SqlCommand cmd = connection.CreateCommand())
2051
                            {
2052
                                foreach (DataRow item in dt.Rows)
2053
                                {
2054
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2055
                                    string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2056
                                    string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2057
                                    bool LEADERLINE = DBNull.Value.Equals(item["LEADERLINE"]) ? false : Convert.ToBoolean(item["LEADERLINE"]);
2058

    
2059
                                    cmd.Parameters.Clear();
2060
                                    cmd.CommandText = string.Format(@"
2061
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2062
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
2063
                                    ELSE
2064
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, UID);
2065
                                    cmd.Parameters.AddWithValue("@UID", UID);
2066
                                    if (string.IsNullOrEmpty(NAME))
2067
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
2068
                                    else
2069
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2070
                                    if (string.IsNullOrEmpty(SPPID_SYMBOL_PATH))
2071
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
2072
                                    else
2073
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2074
                                    cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2075
                                    cmd.ExecuteNonQuery();
2076
                                }
2077
                            }
2078
                            connection.Close();
2079
                            result = true;
2080
                        }
2081
                    }
2082
                    catch (Exception ex)
2083
                    {
2084
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2085
                    }
2086
                    finally
2087
                    {
2088
                        if (connection != null)
2089
                            connection.Dispose();
2090
                    }
2091
                }
2092
            }
2093

    
2094
            return result;
2095
        }
2096

    
2097
        private static bool ImportAttributeMappingTable(DataTable dt)
2098
        {
2099
            bool result = false;
2100

    
2101
            Project_Info projectInfo = Project_Info.GetInstance();
2102
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2103
            {
2104
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
2105
                {
2106
                    try
2107
                    {
2108
                        connection.Open();
2109
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2110
                        {
2111
                            try
2112
                            {
2113
                                using (SQLiteCommand cmd = connection.CreateCommand())
2114
                                {
2115
                                    foreach (DataRow item in dt.Rows)
2116
                                    {
2117
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2118
                                        string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2119

    
2120
                                        cmd.Parameters.Clear();
2121
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
2122
                                        cmd.Parameters.AddWithValue("@UID", UID);
2123
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2124
                                        cmd.ExecuteNonQuery();
2125
                                    }
2126
                                }
2127
                                transaction.Commit();
2128
                                connection.Close();
2129
                                result = true;
2130
                            }
2131
                            catch (Exception ex)
2132
                            {
2133
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2134
                                transaction.Rollback();
2135
                            }
2136
                            finally
2137
                            {
2138
                                transaction.Dispose();
2139
                            }
2140
                        }
2141
                    }
2142
                    catch (Exception ex)
2143
                    {
2144
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2145
                    }
2146
                    finally
2147
                    {
2148
                        connection.Dispose();
2149
                    }
2150
                }
2151
            }
2152
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2153
            {
2154
                using (SqlConnection connection = GetSqlConnection())
2155
                {
2156
                    try
2157
                    {
2158
                        if (connection != null && connection.State == ConnectionState.Open)
2159
                        {
2160
                            using (SqlCommand cmd = connection.CreateCommand())
2161
                            {
2162
                                foreach (DataRow item in dt.Rows)
2163
                                {
2164
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2165
                                    string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2166

    
2167
                                    cmd.Parameters.Clear();
2168
                                    cmd.CommandText = string.Format(@"
2169
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2170
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID
2171
                                    ELSE
2172
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, UID);
2173

    
2174
                                    cmd.Parameters.AddWithValue("@UID", UID);
2175
                                    if (string.IsNullOrEmpty(SPPID_ATTRIBUTE))
2176
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
2177
                                    else
2178
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2179
                                    cmd.ExecuteNonQuery();
2180
                                }
2181
                            }
2182
                            connection.Close();
2183
                            result = true;
2184
                        }
2185
                    }
2186
                    catch (Exception ex)
2187
                    {
2188
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2189
                    }
2190
                    finally
2191
                    {
2192
                        if (connection != null)
2193
                            connection.Dispose();
2194
                    }
2195
                }
2196
            }
2197

    
2198

    
2199

    
2200
            return result;
2201
        }
2202
    }
2203
}
클립보드 이미지 추가 (최대 크기: 500 MB)