public static string SectionToForm(SignEditeurDocumentModel document, int idSection)
 {
     var html = "";
     var fields = (from item in document.Fields where item.IdDocumentSection == idSection orderby item.Order select item);
     if (fields == null) return html;
     foreach (var field in fields)
     {
         html += ElementCreator.FieldToHtml(field);
     }
     return html;
 }
 public JsonResult Create(SignEditeurDocumentModel document)
 {            
     try
     {
         var sessionInstitution = Business.Utility.SessionManager.GetFirmInstitutionSession();
         if (sessionInstitution == null)
         {
             return Json(new { success = false, msg = "Erreur" }, JsonRequestBehavior.AllowGet);
         }
         else
         {
             var doc = DocumentModelBL.Create(sessionInstitution.idFirmInstitution, document);
             return Json(new { success = true, msg = "Document enregistré!", idDoc = doc.IdDocument }, JsonRequestBehavior.AllowGet);
         }                
     }
     catch(Exception e) 
     {
         return Json(new { success = false, msg = e.Message }, JsonRequestBehavior.AllowGet);
     }            
 }
 public JsonResult Update(SignEditeurDocumentModel document)
 {
     try
     {
         var sessionInstitution = Business.Utility.SessionManager.GetFirmInstitutionSession();
         if (sessionInstitution == null)
         {
             return Json(new { success = false, msg = "Erreur" }, JsonRequestBehavior.AllowGet);
         }
         else
         {
             var doc = DocumentModelBL.Update(document);
             var fields = DocumentModelBL.GetFields(doc.IdDocument.GetValueOrDefault());
             return Json(new { success = true, msg = "Document enregistré!", idDoc = doc.IdDocument, fields = fields}, JsonRequestBehavior.AllowGet);
         }
     }
     catch (Exception e)
     {
         return Json(new { success = false, msg = e.Message }, JsonRequestBehavior.AllowGet);
     }
 }
        private static DocumentModel UpdateDocumentModel(SignEditeurDocumentModel document)
        {
            var model = ModelToEntity(document);
            
            using (UpsilabEntities context = new UpsilabEntities())
            {
                var doc = context.DocumentModel.Where(x => x.IdDocumentModel == model.IdDocumentModel).FirstOrDefault();
                if (doc != null)
                {
                    //doc.HtmlTemplate = model.HtmlTemplate;
                    doc.DateModified = DateTime.Now;
                }

                context.ObjectStateManager.ChangeObjectState(doc, System.Data.EntityState.Modified);
                DocumentModelFileBL.SavePdfContentToFile(doc, document.HtmlTemplate);


                var oldFields = context.Fields.Where(x => x.IdDocumentModel == model.IdDocumentModel).ToList();

                if (document.Fields != null)
                {
                    foreach (var item in document.Fields)
                    {
                        var notFound = true;
                        if (item.IdField != null)
                        {
                            var find = oldFields.Find(x => x.IdField == item.IdField && x.IdDocumentModel == model.IdDocumentModel);
                            if (find != null)
                            {
                                find.Label = item.Label;
                                find.NameKey = item.Name;
                                find.Required = item.Required;
                                find.IsShared = item.IsShared;
                                find.IdDocumentSection = item.IdDocumentSection;
                                find.Type = item.Type;
                                find.Order = item.Order;
                                context.ObjectStateManager.ChangeObjectState(find, System.Data.EntityState.Modified);
                                notFound = false;
                            }
                        }
                        if (notFound == true)
                        {
                            item.DateCreated = DateTime.Now;
                            item.IdDocumentModel = model.IdDocumentModel;
                            context.Fields.AddObject(FieldModelToEntity(item));
                        }
                    }

                    //foreach (var item in oldFields)
                    //{
                    //    if (item.IsShared == false || item.IsShared == null)
                    //    {
                    //        var find = document.Fields.Find(x => x.IdField == item.IdField);
                    //        if (find == null)
                    //        {
                    //            var nbValues = context.FieldValue.Where(x => x.IdField == item.IdField).Count();
                    //            if (nbValues == 0) context.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Deleted);
                    //        }
                    //    }
                    //}
                }
                else if(oldFields != null)
                {
                    //foreach (var item in oldFields)
                    //{
                    //    if (item.IsShared == false || item.IsShared == null)
                    //    {
                    //        var nbValues = context.FieldValue.Where(x => x.IdField == item.IdField).Count();
                    //        if (nbValues == 0) context.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Deleted);
                    //    }
                    //}
                }
                context.SaveChanges();

                return doc;
            }
        }
        private static DocumentModel AddDocumentModel(Guid idFirmInstitutionSDG, SignEditeurDocumentModel model)
        {
            using (UpsilabEntities context = new UpsilabEntities())
            {
                DocumentModel entity = new DocumentModel
                {
                    IdDocumentModel = Guid.NewGuid(),
                    IdFirmInstitution = idFirmInstitutionSDG,
                    DocumentType = model.DocumentType,
                    //HtmlTemplate = model.HtmlTemplate,
                    Name = model.Name,
                    DateCreated = DateTime.Now
                };

                context.DocumentModel.AddObject(entity);

                if (model.Fields != null)
                {
                    foreach (var field in model.Fields)
                    {
                        if (field == null) continue;
                        field.DateCreated = DateTime.Now;
                        field.IdDocumentModel = entity.IdDocumentModel;
                        var fieldEntity = FieldModelToEntity(field);
                        context.Fields.AddObject(fieldEntity);
                    }
                }
                context.SaveChanges();

                DocumentModelFileBL.SavePdfContentToFile(entity, model.HtmlTemplate);

                return entity;
            }
        }
 private static DocumentModel ModelToEntity(SignEditeurDocumentModel model)
 {
     return new DocumentModel
     {
         IdDocumentModel = model.IdDocument.GetValueOrDefault(),
         DocumentType = model.DocumentType,
         //HtmlTemplate = model.HtmlTemplate,
         Name = model.Name
     };
 }
 public static SignEditeurDocumentModel EntityToModel(DocumentModel entity) {
     var model = new SignEditeurDocumentModel {
         IdDocument = entity.IdDocumentModel,
         IdFirmInstitution = entity.IdFirmInstitution,
         Name = entity.Name,
         DocumentType = entity.DocumentType,
         //HtmlTemplate = entity.HtmlTemplate,
         DateCreated = entity.DateCreated,
         DateModified = entity.DateModified.GetValueOrDefault()
     };
     model.HtmlTemplate = DocumentModelFileBL.GetPdfContentFromFile(entity);
     return model;
 }
 /// <summary>
 /// Update
 /// </summary>
 /// <param name="document"></param>
 /// <returns></returns>
 public static SignEditeurDocumentModel Update(SignEditeurDocumentModel document)
 {
     var model = UpdateDocumentModel(document);
     return EntityToModel(model);
 }
 /// <summary>
 /// Create SignEditeurDocumentModel
 /// </summary>
 /// <param name="idFirmInstitutionSDG"></param>
 /// <param name="document"></param>
 /// <returns></returns>
 public static SignEditeurDocumentModel Create(Guid idFirmInstitutionSDG, SignEditeurDocumentModel document)
 {
     var model = AddDocumentModel(idFirmInstitutionSDG, document);
     return EntityToModel(model);
 }