프로젝트

일반

사용자정보

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

markus / KCOM_API / MarkusService.svc.cs @ b2948d06

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

1
using KCOMDataModel.DataModel;
2
using log4net;
3
using Newtonsoft.Json.Linq;
4
using System;
5
using System.Collections.Generic;
6
using System.Configuration;
7
using System.Linq;
8
using System.Runtime.Serialization;
9
using System.ServiceModel;
10
using System.ServiceModel.Activation;
11
using System.ServiceModel.Web;
12
using System.Text;
13

    
14
namespace KCOM_API
15
{
16
    [ServiceContract(Namespace = "")]
17
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
18
    public class MarkusService : System.Web.Services.WebService
19
    {
20
        protected ILog logger = LogManager.GetLogger(typeof(MarkusService));
21

    
22
        [WebGet(UriTemplate = "AddMember?UserId={UserId}&Name={Name}&Company={Company}&Department={Department}&Position={Position}&email={email}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
23
        [OperationContract]
24
        public AddMemberResults AddMember(string UserId,string Name, string Company, string Department,string Position,string email)
25
        {
26
            AddMemberResults results = new AddMemberResults(false,null);
27

    
28
            try
29
            {
30
                string sCIConnString = ConfigurationManager.ConnectionStrings["CIConnectionString"].ConnectionString;
31
                using (CIEntities _ci = new CIEntities(sCIConnString))
32
                {
33
                    if (_ci.MEMBER.Count(x => x.ID == UserId) == 0)
34
                    {
35
                        _ci.AddToMEMBER(new MEMBER
36
                        {
37
                            ID = UserId,
38
                            NAME = Name,
39
                            COMPANY = Company,
40
                            DEPARTMENT = Department,
41
                            POSITION = Position,
42
                            EMAIL_ADDRESS = email,
43
                            MODIFIED_DATETIME = DateTime.Now,
44
                            CREATE_DATETIME = DateTime.Now
45
                        });
46

    
47
                        _ci.SaveChanges();
48

    
49
                        results.IsSuccess = true;
50
                    }
51
                    else
52
                    {
53
                        results.Message = "User already exists.";
54
                    }
55
                }
56
            }
57
            catch (Exception ex)
58
            {
59
                results.Message = "system Error";
60
            }
61
            finally
62
            {
63

    
64
            }
65

    
66
            return results;
67
        }
68

    
69
        [WebInvoke(UriTemplate = "GetMembers",Method ="POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
70
        [OperationContract]
71
        public GetMembersResults GetMembers()
72
        {
73
            GetMembersResults results = new GetMembersResults();
74

    
75
            try
76
            {
77
                string sCIConnString = ConfigurationManager.ConnectionStrings["CIConnectionString"].ConnectionString;
78
                using (CIEntities _ci = new CIEntities(sCIConnString))
79
                {
80
                    if (_ci.MEMBER.Count() > 0)
81
                    {
82
                        results.Members = _ci.MEMBER.Select(m => new Member
83
                        {
84
                            ID = m.ID,
85
                            NAME = m.NAME,
86
                            COMPANY = m.COMPANY,
87
                            DEPARTMENT = m.DEPARTMENT,
88
                            POSITION = m.POSITION,
89
                            EMAIL_ADDRESS = m.EMAIL_ADDRESS
90
                        }).ToArray();
91
                    }
92
                }
93
            }
94
            catch (Exception ex)
95
            {
96
            }
97
            finally
98
            {
99

    
100
            }
101

    
102
            return results;
103
        }
104

    
105
        [WebGet(UriTemplate = "ConvertRun?rev_No={rev_No}&document_No={document_No}&document_Name={document_Name}&group_No={group_No}&prj_No={prj_No}&document_Id={document_Id}&document_Url={document_Url}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
106
        [OperationContract]
107
        public string ConvertRun(string rev_No, string document_No, string document_Name, string group_No, string prj_No, string document_Id, string document_Url)
108
        {
109
            string _result = null;
110
            try
111
            {
112
                logger.Info($"Conversion Web Service ConvertRun call rev_No :{rev_No} document_No : {document_No}  document_Name : {document_Name}  group_No : {group_No}  prj_No : {prj_No}  document_Id : {document_Id}  document_Url : {document_Url}");
113
                
114
                string sConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
115
                using (KCOMEntities deepViewEntity = new KCOMEntities(sConnString))
116
                {
117
                    //_result = ConnectStringBuilder.DeepViewConnectionString().ToString();
118
                    //var _RunProjects = deepViewEntity.RUN_PROJECTS.Where(r => r.PROJECT_NO == projNumber && DateTime.Parse(r.RUN_DATETIME)<= DateTime.Now);
119
                    //var _RunProjects = deepViewEntity.RUN_PROJECTS.Where(r => r.PROJECT_NO == projNumber);
120
                    var _RunProjects = deepViewEntity.RUN_PROJECTS.Where(r => r.PROJECT_NO == prj_No).ToList();
121

    
122
                    if (_RunProjects.Count() == 0)
123
                    {
124
                        deepViewEntity.RUN_PROJECTS.AddObject(new RUN_PROJECTS { PROJECT_NO = prj_No, IS_ACTIVITY = 1, PROJECT_NAME = "", RUN_DATETIME = DateTime.Today });
125

    
126
                        var items = deepViewEntity.PROPERTIES.Where(x => x.PROPERTY == "SAMPLEPRJ");
127

    
128
                        foreach (var item in items)
129
                        {
130
                            deepViewEntity.PROPERTIES.AddObject(new PROPERTIES { TYPE = item.TYPE, PROPERTY = prj_No, VALUE = item.VALUE });
131
                        }
132

    
133
                        deepViewEntity.SaveChanges();
134
                    }
135
                }
136

    
137
                _result = SendKcom(rev_No, document_No, document_Name, group_No, prj_No, document_Id, document_Url);
138
            }
139
            catch (Exception ex)
140
            {
141
                logger.Error($"Conversion Web Service ConvertRun Error rev_No :{rev_No} document_No : {document_No}  document_Name : {document_Name}  group_No : {group_No}  prj_No : {prj_No}  document_Id : {document_Id}  document_Url : {document_Url}", ex);
142
                _result = "Error";
143
            }
144
            return _result;
145
        }
146

    
147

    
148
        [WebGet(UriTemplate = "SetFinalPDFUrl?prj_No={prj_No}&document_Id={document_Id}&document_Url={document_Url}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
149
        [OperationContract]
150
        public string SetFinalPDFUrl(string prj_No, string document_Id, string document_Url)
151
        {
152
            string _result = null;
153
            try
154
            {
155
                logger.Info($"SetFinalPDFUrl call prj_No : {prj_No}  document_Id : {document_Id}  document_Url : {document_Url}");
156

    
157
                string sCIConnString = ConfigurationManager.ConnectionStrings["CIConnectionString"].ConnectionString;
158
                using (CIEntities _kcomEntity = new CIEntities(sCIConnString))
159
                {
160
                    var doc = _kcomEntity.DOCUMENT_ITEM.Where(d => d.PROJECT_NO == prj_No && d.ID == document_Id).FirstOrDefault();
161
                    if (doc != null)
162
                    {
163
                        doc.RESULT = document_Url;
164
                        _kcomEntity.SaveChanges();
165
                        _result = true.ToString();
166
                    }
167
                    else
168
                    {
169
                        _result = $"prj_No : {prj_No}  document_Id : {document_Id}  document_Url : {document_Url} Not Found";
170
                    }
171
                }
172
            }
173
            catch (Exception ex)
174
            {
175
                logger.Error($"SetFinalPDFUrl call prj_No : {prj_No}  document_Id : {document_Id}  document_Url : {document_Url}",ex);
176
                _result = "Error";
177
            }
178
            return _result;
179
        }
180

    
181
        private string SendKcom(string rev_No, string document_No, string document_Name, string group_No, string prj_No, string document_Id, string document_Url)
182
        {
183
            string result = false.ToString();
184

    
185
            //string _id = GetGuid(0, GuidGenerator.GetUniqueGuid()).ToString();
186
            string _id = shortGuid();
187
            string _doc_id = document_Id;// ((UInt32)document_Id.GetHashCode()).ToString();
188

    
189
            string sCIConnString = ConfigurationManager.ConnectionStrings["CIConnectionString"].ConnectionString;
190
            using (CIEntities _kcomEntity = new CIEntities(sCIConnString))
191
            {
192
                if (string.IsNullOrEmpty(group_No))
193
                {
194
                    group_No = "0";
195
                }
196
                //if (!document_Url.ToLower().Contains("http%3a%2f%2f"))
197
                //{
198
                //    document_Url = Base64Decode(document_Url);
199
                //    document_Url = System.Net.WebUtility.UrlEncode(document_Url);
200
                //}
201
                //document_Name = Base64DecodeFromStr(document_Name);
202

    
203
                var doc = _kcomEntity.DOCUMENT_ITEM.Where(d => d.PROJECT_NO == prj_No && d.ID == document_Id).FirstOrDefault();
204
                if (doc != null)
205
                {
206
                    doc.DOCUMENT_ID = _doc_id;
207
                    doc.REVISION = rev_No;
208
                    doc.GROUP_NO = group_No;
209
                    doc.DOCUMENT_NO = document_No;
210
                    doc.DOCUMENT_NAME = document_Name;
211
                    doc.ORIGINAL_FILE = document_Url;
212
                }
213
                else
214
                {
215
                    _kcomEntity.AddToDOCUMENT_ITEM(new DOCUMENT_ITEM
216
                    {
217
                        ID = document_Id,
218
                        REVISION = rev_No,
219
                        DOCUMENT_NO = document_No,
220
                        DOCUMENT_NAME = document_Name,
221
                        GROUP_NO = group_No,
222
                        ORIGINAL_FILE = document_Url,
223
                        DOCUMENT_ID = _doc_id,
224
                        PROJECT_NO = prj_No,
225
                    });
226
                }
227

    
228
                _kcomEntity.SaveChanges();
229

    
230
            }
231

    
232
            string sConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
233
            using (KCOMEntities _entity = new KCOMEntities(sConnString))
234
            {
235
                _entity.AddToCONVERTER_DOC(new CONVERTER_DOC
236
                {
237
                    ID = _id,
238
                    CREATE_DATETIME = DateTime.Now,
239
                    DOCUMENT_URL = document_Url,
240
                    PROJECT_NO = prj_No,
241
                    DOCUMENT_ID = _doc_id,
242
                });
243

    
244
                _entity.SaveChanges();
245
            }
246

    
247
            try
248
            {
249
                System.Net.WebClient client = new System.Net.WebClient();
250
                var convertResult = client.DownloadString($"{Properties.Settings.Default.CovnertService}/Rest/ConvertAdd?ProjectNo={prj_No}&ConvertID={_id}");
251

    
252
                JObject jObject = JObject.Parse(convertResult, new JsonLoadSettings());
253
                result = jObject["ConvertAddResult"].ToString();
254
            }
255
            catch (Exception ex)
256
            {
257
                logger.Error("Conversion Web Service SendKcom Error", ex);
258
                result = $"Markus V3 Connect Error {ex.ToString()}";
259
            }
260

    
261

    
262
            return result;
263
        }
264

    
265
        public string Base64DecodeFromStr(string data)
266
        {
267
            try
268
            {
269
                System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
270
                System.Text.Decoder utf8Decode = encoder.GetDecoder();
271

    
272
                byte[] todecode_byte = Convert.FromBase64String(data);
273

    
274
                int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
275

    
276
                char[] decoded_char = new char[charCount];
277

    
278
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
279

    
280
                string result = new String(decoded_char);
281

    
282
                return result;
283
            }
284
            catch (Exception)
285
            {
286
                return data;
287
            }
288
        }
289

    
290
        public string Base64Decode(string data)
291
        {
292
            try
293
            {
294
                if (data.Contains("http://"))
295
                {
296
                    return data;
297
                }
298
                else
299
                {
300
                    System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
301
                    System.Text.Decoder utf8Decode = encoder.GetDecoder();
302

    
303
                    byte[] todecode_byte = Convert.FromBase64String(data);
304

    
305
                    int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
306

    
307
                    char[] decoded_char = new char[charCount];
308

    
309
                    utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
310

    
311
                    string result = new String(decoded_char);
312

    
313
                    return result;
314
                }
315

    
316

    
317
            }
318

    
319
            catch (Exception e)
320
            {
321
                throw new Exception("Error in Base64Decode: " + e.Message);
322
            }
323
        }
324
        public string shortGuid()
325
        {
326
            byte[] bytes = new byte[16];
327
            using (var provider = System.Security.Cryptography.RandomNumberGenerator.Create())
328
            {
329
                provider.GetBytes(bytes);
330
            }
331

    
332
            var guid = new Guid(bytes);
333

    
334
            return Convert.ToBase64String(guid.ToByteArray())
335
                .Substring(0, 10)
336
                .Replace("/", "")
337
                .Replace("+", "") + DateTime.UtcNow.Ticks.ToString("x");
338
        }
339

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