프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / DB / Project_DB.cs @ fb2d9638

이력 | 보기 | 이력해설 | 다운로드 (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
                connection.Dispose();
53
                connection = null;
54
            }
55

    
56
            return connection;
57
        }
58

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

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

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

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

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

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

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

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

    
243
            return result;
244
        }
245

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

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

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

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

    
316

    
317
            return true;
318
        }
319

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

    
378
            return dt;
379
        }
380

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

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

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

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

    
459
            return true;
460
        }
461

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

    
520
            return dt;
521
        }
522

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

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

    
600
            return dt;
601
        }
602

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

    
638
                            }
639

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

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

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

    
702
                                }
703

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

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

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

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

    
807
            return dt;
808
        }
809

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

    
880
            return dt;
881
        }
882

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

    
961

    
962
            return dt;
963
        }
964

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

    
1023
            return dt;
1024
        }
1025

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

    
1084
            return dt;
1085
        }
1086

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

    
1145
            return dt;
1146
        }
1147

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

    
1206
            return dt;
1207
        }
1208

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

    
1267
            return dt;
1268
        }
1269

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

    
1328
            return dt;
1329
        }
1330

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

    
1431
            return true;
1432
        }
1433

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

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

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

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

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

    
1648
                                List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1649
                                foreach (var item in OPCs)
1650
                                {
1651
                                    using (SQLiteCommand cmd = connection.CreateCommand())
1652
                                    {
1653
                                        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);
1654
                                        cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1655
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1656
                                        cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1657
                                        cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1658
                                        cmd.Parameters.AddWithValue("@PAIRED", false);
1659
                                        cmd.ExecuteNonQuery();
1660
                                    }
1661
                                }
1662

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

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

    
1713
                            List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1714
                            foreach (var item in OPCs)
1715
                            {
1716
                                using (SqlCommand cmd = connection.CreateCommand())
1717
                                {
1718
                                    cmd.Parameters.Clear();
1719
                                    cmd.CommandText = string.Format(@"
1720
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1721
                                        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
1722
                                    ELSE
1723
                                        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);
1724

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

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

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

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

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

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

    
1885
            return result;
1886
        }
1887

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

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

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

    
1916
            return result;
1917
        }
1918

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

    
1979
            return dt;
1980
        }
1981

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

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

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

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

    
2093
            return result;
2094
        }
2095

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

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

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

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

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

    
2197

    
2198

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