프로젝트

일반

사용자정보

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

markus / ConvertService / ServiceBase / Markus.Service.DataBase / ConvertDatabase.cs @ b92f142f

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

1
using Markus.Message;
2
using Markus.Service.Extensions;
3
using System;
4
using System.Collections.Generic;
5
using System.IO;
6
using System.Linq;
7

    
8
namespace Markus.Service.DataBase
9
{
10
    public class ConvertDatabase : IDisposable
11
    {
12
        MarkusEntities entities;
13

    
14
        public ConvertDatabase(string ConnectionString)
15
        {
16
            try
17
            {
18
                //해당 프로젝트의 데이터베이스 연결
19
                string databaseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database");
20
                entities = new MarkusEntities(ConnectionString);
21
            }
22
            catch (Exception ex)
23
            {
24
                System.Diagnostics.Debug.WriteLine(ex.ToString());
25
                throw ex;
26
            }
27
        }
28

    
29
        public void Dispose()
30
        {
31
            entities.Dispose();
32
        }
33

    
34
        /// <summary>
35
        /// 컨버터서비스를 이용하는 문서 정보 저장
36
        /// UniqueID
37
        /// using Markus.Service.Extensions;  new Guid().CreateUniqueGuid().ToString();
38
        /// </summary>
39
        /// <param name="ProjectNo">프로젝트</param>
40
        /// <param name="UniqueID">컨버터 서비스의 아이템 ID</param>
41
        /// <param name="originfilePath"></param>
42
        /// <param name="DocumentID"></param>
43
        /// <returns></returns>
44
        public bool SetConvertDoc(string ProjectNo, string UniqueID, string originfilePath, string DocumentID)
45
        {
46
            bool result = false;
47

    
48
            if (ProjectNo.IsNullOrEmpty())
49
            {
50
                throw new Exception("ProjectNo is Null");
51
            }
52

    
53
            if (UniqueID.IsNullOrEmpty())
54
            {
55
                throw new Exception("UniqueID is Null");
56
            }
57

    
58
            if (originfilePath.IsNullOrEmpty())
59
            {
60
                throw new Exception("originfilePath is Null");
61
            }
62

    
63
            if (DocumentID.IsNullOrEmpty())
64
            {
65
                throw new Exception("DocumentID is Null");
66
            }
67

    
68
            if (DocumentID.IsNullOrEmpty())
69
            {
70
                long _lastDocId = 0;
71

    
72
                if (entities.CONVERTER_DOC.Count() > 0)
73
                {
74
                    try
75
                    {
76
                        string qry = @"SELECT * FROM CONVERTER_DOC order by CAST(DOCUMENT_ID as int) desc";
77
                        var _item = entities.CONVERTER_DOC.SqlQuery(qry).FirstOrDefault();
78

    
79
                        if (_item != null)
80
                        {
81
                            _lastDocId = Convert.ToInt64(_item.DOCUMENT_ID) + 1;
82
                        }
83
                    }
84
                    catch (Exception)
85
                    {
86

    
87
                        throw;
88
                    }
89
                }
90

    
91
                DocumentID = _lastDocId.ToString();
92
            }
93

    
94
            CONVERTER_DOC convert_doc = new CONVERTER_DOC
95
            {
96
                ID = UniqueID,
97
                PROJECT_NO = ProjectNo,
98
                DOCUMENT_URL = originfilePath,
99
                DOCUMENT_ID = DocumentID,
100
                CREATE_DATETIME = DateTime.Now,
101
                STATUS = 0,
102
                TOTAL_PAGE = 0,
103
                CURRENT_PAGE = 0,
104
                RECONVERTER = 0
105
            };
106

    
107
            entities.CONVERTER_DOC.Add(convert_doc);
108

    
109
            if (entities.SaveChanges() == 1)
110
            {
111
                result = true;
112
            }
113

    
114
            return result;
115
        }
116

    
117
        /// <summary>
118
        /// 컨버터서비스를 이용하는 문서 정보 저장
119
        /// UniqueID
120
        /// using Markus.Service.Extensions;  new Guid().CreateUniqueGuid().ToString();
121
        /// </summary>
122
        /// <param name="ProjectNo">프로젝트</param>
123
        /// <param name="UniqueID">컨버터 서비스의 아이템 ID</param>
124
        /// <param name="originfilePath"></param>
125
        /// <param name="DocumentID"></param>
126
        /// <returns></returns>
127
        public bool SetDocumentInfo(string ConvertID, int totalPage, List<DOCPAGE> pageinfo)
128
        {
129
            bool result = false;
130

    
131
            var convertDocItems = entities.CONVERTER_DOC.Where(f => f.ID == ConvertID);
132

    
133
            if (convertDocItems.Count() > 0)
134
            {
135
                var convertDoc = convertDocItems.First();
136

    
137
                string docId = new Guid().CreateUniqueGuid().ToString();
138

    
139
                var docinfo = entities.DOCINFO.FirstOrDefault(x => x.PROJECT_NO == convertDoc.PROJECT_NO && x.DOCUMENT_ID == convertDoc.DOCUMENT_ID);
140
                
141
                if (docinfo != null)
142
                {
143
                    // 기존 Docpage 삭제.
144
                    entities.DOCPAGE.RemoveRange(docinfo.DOCPAGE);
145

    
146
                    // 기존 코멘트 유지를 위한 Docinfo update                    
147
                    docinfo.PROJECT_NO = convertDoc.PROJECT_NO;
148
                    docinfo.ORIGINAL_FILE = convertDoc.DOCUMENT_URL;
149
                    docinfo.PAGE_COUNT = totalPage;                    
150
                    docinfo.DOCPAGE = pageinfo;                    
151
                    entities.SaveChanges();
152
                    result = true;
153
                }
154
                else
155
                {
156
                    DOCINFO instace = new DOCINFO
157
                    {
158
                        ID = docId,
159
                        PROJECT_NO = convertDoc.PROJECT_NO,
160
                        ORIGINAL_FILE = convertDoc.DOCUMENT_URL,
161
                        DOCUMENT_ID = convertDoc.DOCUMENT_ID,
162
                        PAGE_COUNT = totalPage,
163
                        DOCPAGE = pageinfo
164
                    };
165

    
166
                    entities.DOCINFO.Add(instace);
167
                    var save = entities.SaveChanges();
168

    
169
                    if (save == pageinfo.Count() + 1)
170
                    {
171
                        result = true;
172
                    }
173
                }
174

    
175
                
176
                
177
            }
178

    
179
            return result;
180
        }
181

    
182
        public Interface.ConvertItem GetConvertItem(string ProjectNo, string UniqueID)
183
        {
184
            Interface.ConvertItem result = null;
185

    
186
            //var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID);
187
            var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == ProjectNo && f.ID == UniqueID);
188

    
189
            if (items.Count() > 0)
190
            {
191
                var convertItem = items.First();
192
                string convertPath = Path.Combine(BaseStorage(convertItem.PROJECT_NO), convertItem.PROJECT_NO + "_Tile", (System.Convert.ToInt64(convertItem.DOCUMENT_ID) / 100).ToString(), convertItem.DOCUMENT_ID);
193

    
194
                result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath);
195
            }
196

    
197
            return result;
198
        }
199

    
200

    
201
        public Interface.ConvertItem GetConvertItembyDocumentID(string ProjectNo, string DocumentID)
202
        {
203
            Interface.ConvertItem result = null;
204

    
205
            //var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID);
206
            var items = entities.CONVERTER_DOC.Where(f => f.PROJECT_NO == ProjectNo && f.DOCUMENT_ID == DocumentID);
207

    
208
            if (items.Count() > 0)
209
            {
210
                var convertItem = items.First();
211
                string convertPath = Path.Combine(BaseStorage(convertItem.PROJECT_NO), convertItem.PROJECT_NO + "_Tile", (System.Convert.ToInt64(convertItem.DOCUMENT_ID) / 100).ToString(), convertItem.DOCUMENT_ID);
212

    
213
                if (convertItem.STATUS == (int)StatusCodeType.Completed)
214
                {
215
                    result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath,
216
                                                        convertItem.STATUS.ToString(), convertItem.CURRENT_PAGE, convertItem.TOTAL_PAGE, convertItem.EXCEPTION);
217
                }
218
                else
219
                {
220
                    result = new Interface.ConvertItem(convertItem.DOCUMENT_ID, convertItem.PROJECT_NO, convertItem.ID, convertItem.DOCUMENT_URL, convertPath);
221
                }
222
            }
223

    
224
            return result;
225
        }
226

    
227
        /// <summary>
228
        /// service에서 대기중이거나 오류인 아이템을 가져가서 컨버터 프로세스를 호출한다.
229
        /// 컨버터 상태가 에러이거나 2보다 작은 경우 
230
        /// UniqueKey는 각 데이터베이스의 문서 정보에서 유일한 키이며 컨버터저장에서 같은 폴더로의 저장을 방지 한다.
231
        /// 
232
        /// </summary>
233
        /// <param name="aliveItems"></param>
234
        /// <param name="TakeCount"></param>
235
        /// <returns></returns>
236
        public List<CONVERTER_DOC> GetWaitConvertItems(List<string> projectList, int TakeCount)
237
        {
238
            var convertWaitingItems = entities.CONVERTER_DOC.OrderBy(x => x.CREATE_DATETIME).Where(
239
                                        x =>
240
                                        //projectList.Contains(x.PROJECT_NO) && x.RECONVERTER < 2 &&
241
                                         x.RECONVERTER < 2 &&
242
                                        (x.STATUS == (int)StatusCodeType.None || x.STATUS == (int)StatusCodeType.FileError && x.STATUS == (int)StatusCodeType.Error))
243
                                    .GroupBy(x=>x.DOCUMENT_ID).Select(x => x.FirstOrDefault())
244
                                    .Take(TakeCount);
245

    
246
            return convertWaitingItems.ToList();
247
        }
248

    
249
        public List<CONVERTER_DOC> GetConvertingItems(List<string> projectList)
250
        {
251
            var convertWaitingItems = entities.CONVERTER_DOC.OrderBy(x => x.CREATE_DATETIME).Where(
252
                                        x =>
253
                                        projectList.Contains(x.PROJECT_NO) &&
254
                                         (x.STATUS > (int)StatusCodeType.None && x.STATUS < (int)StatusCodeType.Completed));
255

    
256
            return convertWaitingItems.ToList();
257
        }
258

    
259
        private string GetConvertPath(string ProjectNo,string DocumentID)
260
        {
261
            try
262
            {
263
                return Path.Combine(BaseStorage(ProjectNo), ProjectNo + "_Tile", (System.Convert.ToInt64(DocumentID) / 100).ToString(), DocumentID);
264
            }
265
            catch (Exception)
266
            {
267
                throw new Exception("GetConvertPath Error");
268
            }
269

    
270
        }
271

    
272

    
273
        /// <summary>
274
        /// 오류나 wait으로 남아 있는 데이터베이스의 Item을 StatusCodeType.None으로 복구
275
        /// </summary>
276
        /// <param name="aliveItems"></param>
277
        /// <param name="TakeCount"></param>
278
        /// <returns></returns>
279
        public int SetCleanUpItem(string convertID)
280
        {
281
            int result = 0;
282

    
283
            var items = entities.CONVERTER_DOC.Where(x=> x.ID == convertID);
284

    
285
            result = items.Count();
286

    
287
            if (items.Count() > 0)
288
            {
289
                var item = items.First();
290

    
291
                item.RECONVERTER = item.RECONVERTER + 1;
292
                item.CREATE_DATETIME = DateTime.Now;
293
                item.START_DATETIME = null;
294
                item.END_DATETIME = null;
295
                item.STATUS = (int)StatusCodeType.None;
296

    
297
                result = entities.SaveChanges();
298
            }
299

    
300
            return result;
301
        }
302

    
303
        /// <summary>
304
        /// 전체 컨버터를 가져온다. 비동기로 변경하여야 한다.
305
        /// </summary>
306
        /// <returns></returns>
307
        public List<Interface.ConvertItem> GetConvertItems( StatusCodeType statusCodeType)
308
        {
309
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
310

    
311
            var items = entities.CONVERTER_DOC.Where(x=>x.STATUS == (int)statusCodeType).ToList(); //.Where(x => x.PROJECT_NO == gProjectNo);
312

    
313
            if (items.Count() > 0)
314
            {
315
                foreach (var item in items)
316
                {
317
                    var convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL)
318
                    {
319
                        ReConverter = item.RECONVERTER,
320
                        ServiceID = item.SERVICE_ID,
321
                        ConvertState = ((StatusCodeType)item.STATUS).ToString(),
322
                        CreateTime = item.CREATE_DATETIME,
323
                        TotalPage = item.TOTAL_PAGE,
324
                        CurrentPageNo = item.CURRENT_PAGE,
325

    
326
                        ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID)
327
                    };
328

    
329

    
330
                    convertItems.Add(convertitem);
331
                }
332
            }
333
            return convertItems;
334
        }
335

    
336
        /// <summary>
337
        /// 전체 컨버터를 가져온다. 비동기로 변경하여야 한다.
338
        /// </summary>
339
        /// <returns></returns>
340
        public List<Interface.ConvertItem> GetConvertItems(StatusCodeType statusCodeType,Func<CONVERTER_DOC, bool> @where,int takeCount = 100)
341
        {
342
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
343

    
344
            var items = entities.CONVERTER_DOC.Where(x => x.STATUS == (int)statusCodeType && where(x)).Take(takeCount).ToList(); //.Where(x => x.PROJECT_NO == gProjectNo);
345

    
346
            if (items.Count() > 0)
347
            {
348
                foreach (var item in items)
349
                {
350
                    var convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL)
351
                    {
352
                        ReConverter = item.RECONVERTER,
353
                        ServiceID = item.SERVICE_ID,
354
                        ConvertState = ((StatusCodeType)item.STATUS).ToString(),
355
                        CreateTime = item.CREATE_DATETIME,
356
                        TotalPage = item.TOTAL_PAGE,
357
                        CurrentPageNo = item.CURRENT_PAGE,
358

    
359
                        ConvertPath = GetConvertPath(item.PROJECT_NO, item.DOCUMENT_ID)
360
                    };
361

    
362

    
363
                    convertItems.Add(convertitem);
364
                }
365
            }
366
            return convertItems;
367
        }
368

    
369

    
370
        /// <summary>
371
        /// 현재 서비스에서 컨버터중인 아이템.
372
        /// </summary>
373
        /// <returns></returns>
374
        public List<Interface.ConvertItem> GetServiceAliveItems(string ServiceID)
375
        {
376
            List<Interface.ConvertItem> convertItems = new List<Interface.ConvertItem>();
377

    
378
            var items = entities.CONVERTER_DOC.Where(x => x.SERVICE_ID == ServiceID && x.STATUS > (int)StatusCodeType.None && x.STATUS < (int)StatusCodeType.Completed).ToList(); //.Where(x => x.PROJECT_NO == gProjectNo);
379

    
380
            if (items.Count() > 0)
381
            {
382
                foreach (var item in items)
383
                {
384
                    var convertitem = new Interface.ConvertItem(item.DOCUMENT_ID, item.PROJECT_NO, item.ID, item.DOCUMENT_URL);
385
                    convertItems.Add(convertitem);
386
                }
387
            }
388
            return convertItems;
389
        }
390
        
391
        public SERVICE_PROPERTIES GetServiceProperties(string ServiceID)
392
        {
393
            SERVICE_PROPERTIES result = null;
394

    
395
            var items =  entities.SERVICE_PROPERTIES.Where(x => x.ID == ServiceID);
396

    
397
            if(items.Count() > 0)
398
            {
399
                result = items.First();
400
            }
401

    
402
            return result;
403
        }
404

    
405
        public bool SetConvertState(string ServiceID, string UniqueID,int State,int CurrentPage,int TotalPage,string Error)
406
        {
407
            bool result = false;
408

    
409
            try
410
            {
411
                var items = entities.CONVERTER_DOC.SingleOrDefault(f => f.ID == UniqueID);
412
                //var items = entities.CONVERTER_DOC.SingleOrDefault(f => f.PROJECT_NO == gProjectNo && f.ID == UniqueID);
413

    
414
                if (items != null)
415
                {
416
                    items.SERVICE_ID = ServiceID;
417
                    items.STATUS = State;
418
                    items.CURRENT_PAGE = CurrentPage;
419
                    items.TOTAL_PAGE = TotalPage;
420

    
421
                    StatusCodeType statusCode = (StatusCodeType)State;
422

    
423
                    switch (statusCode)
424
                    {
425
                        case StatusCodeType.None:
426
                            break;
427
                        case StatusCodeType.Wait:
428
                            items.START_DATETIME = DateTime.Now;
429
                            break;
430
                        case StatusCodeType.PageLoading:
431
                            break;
432
                        case StatusCodeType.Saving:
433
                            break;
434
                        case StatusCodeType.Completed:
435
                            items.END_DATETIME= DateTime.Now;
436
                            break;
437
                        case StatusCodeType.FileError:
438
                            Error = $" file Error :" + Error;
439
                            items.END_DATETIME = DateTime.Now;
440
                            break;
441
                        case StatusCodeType.PageError:
442
                            Error = $" Page No {CurrentPage} Error :" + Error;
443
                            break;
444
                        case StatusCodeType.NeedsPassword:
445
                            break;
446
                        case StatusCodeType.Error:
447
                            Error = $" Error :" + Error;
448
                            items.END_DATETIME = DateTime.Now;
449
                            break;
450
                        default:
451
                            break;
452
                    }
453

    
454
                    if(items.EXCEPTION == null)
455
                    {
456
                        items.EXCEPTION = "";
457
                    }
458

    
459
                    if (items.EXCEPTION.Length < 255)
460
                    {
461
                        items.EXCEPTION = items.EXCEPTION + Error;
462
                    }
463

    
464
                    entities.SaveChanges();
465
                    result = true;
466
                }
467
            }
468
            catch (Exception ex)
469
            {
470
                throw new Exception("SetConvertState",ex);
471
            }
472

    
473
            return result;
474
        }
475

    
476
        private string BaseStorage(string ProjectNo)
477
        {
478
            string result = "";
479

    
480
            var properties = entities.PROPERTIES.Where(f => f.PROPERTY == ProjectNo && f.TYPE == PROPERTIES_DEFINE.TILE_SOURCE_STORAGE);
481

    
482
            if(properties.Count() > 0)
483
            {
484
                result = properties.First().VALUE;
485
            }
486
            else
487
            {
488
                throw new Exception($"Project {ProjectNo} BaseStorage Not Setting.");
489
            }
490

    
491
            return result;
492
        }
493

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