public ActionResult Rediger(Section section) { try { if (ModelState.IsValid) { //Convert back from tinyMCE non-editable for datafields before persisting to API if (section.htmlContent!=null) { section.htmlContent = section.htmlContent.Replace("<span class=\"mceNonEditable\">{{", "{{"); section.htmlContent = section.htmlContent.Replace("}}</span>", "}}"); section.htmlContent = HttpUtility.HtmlDecode(section.htmlContent); } if (section.dataFields!=null) { section.dataFields = section.dataFields.Where(o => o.description != null && o.description != "").ToList(); } var response = HttpClientFactory.getClient(this.ControllerContext).PutAsJsonAsync("section/update/" + section.id, section).Result; if (response.IsSuccessStatusCode) { //return Json(section); return Json(new { validModelstate = true }); } else { //return Json(section); return Json(new { validModelstate = false, statusCode = response.StatusCode }); } } else { //var errorList = (from item in ModelState // where item.Value.Errors.Any() // select item.Value.Errors[0].ErrorMessage).ToList(); //return Json(errorList); return PartialView(section); } } catch (Exception e) { throw new Exception("Could not update!", e); } }
public static DataField cloneDataFieldFromTemplate(DataFieldTemplate t, Section s) { using (ISession session = NHibernateHelper.OpenSession()) { IAuditReader reader = AuditReaderFactory.Get(session); var revision = reader.CreateQuery() .ForRevisionsOfEntity(typeof(DataFieldTemplate), false, true) .AddProjection(AuditEntity.RevisionNumber().Max()) .Add(AuditEntity.Id().Eq(t.id)) .GetSingleResult(); DataField d = new DataField(); d.template = t; d.templateRevisionId = (int)revision; d.description = t.description; d.section = s; new DataFieldRepository().Create(d); return d; } }
public static Section cloneSectionFromTemplate(SectionTemplate t, Document d) { using (ISession session = NHibernateHelper.OpenSession()) { IAuditReader reader = AuditReaderFactory.Get(session); var revision = reader.CreateQuery() .ForRevisionsOfEntity(typeof(SectionTemplate), false, true) .AddProjection(AuditEntity.RevisionNumber().Max()) .Add(AuditEntity.Id().Eq(t.id)) .GetSingleResult(); Section s = new Section(); s.template = t; s.templateRevisionId = (int)revision; s.documentIndex = t.documentIndex; s.headline = t.headline; s.htmlContent = t.htmlContent; s.document = d; SectionRepository repo = new SectionRepository(); repo.Create(s); foreach (DataFieldTemplate item in t.dataFields) { DataField df = cloneDataFieldFromTemplate(item, s); string toReplace = "{dataField=" + item.id + "}"; string replaceWith = "{dataField=" + df.id + "}"; s.htmlContent = s.htmlContent.Replace(toReplace, replaceWith); } repo.Update(s); return s; } }
public Section GetNew(int documentId) { Document document = new DocumentRepository().GetById(documentId); if (document == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } Section newSection = new Section(); newSection.document = document; if (document.sections!=null && document.sections.Count>0) { newSection.documentIndex = document.sections.Max(o => o.documentIndex) + 1; } else { newSection.documentIndex = 1; } repo.Create(newSection); return newSection; }
public HttpResponseMessage Update(int id, Section item) { if (ModelState.IsValid) { item.id = id; if (item.document != null && item.document.id != null && item.document.id != 0) { item.document = new DocumentRepository().GetById(item.document.id); } else { item.document = null; } if (item.template != null && item.template.id != null && item.template.id > 0) { item.template = new SectionTemplateRepository().GetById(item.template.id); } else { item.template = null; } DataFieldRepository dRepo = new DataFieldRepository(); if (item.dataFields != null && item.dataFields.Count>0) { List<String> toRemove = new List<String>(); foreach (DataField d in item.dataFields) { if (item.dataFields.Count(o => o.description == d.description) > 1) { throw new Exception("Too many datafields with name: " + d.description); } d.section = item; if (d.template != null && d.template.id != null && d.template.id > 0) { d.template = new DataFieldTemplateRepository().GetById(d.template.id); } else { d.template = null; } if (!item.htmlContent.Contains("{{" + d.description + "}}")) { if (d.id!=null && d.id>0) { toRemove.Add(d.id.ToString()); } else { toRemove.Add(d.description); } } } //Retrieve from database to make sure that all fields that are no longer in text is removed var fieldsFromDB = dRepo.GetAll().Where(o => o.section.id == item.id).ToList(); foreach (DataField d in fieldsFromDB) { if (!item.htmlContent.Contains("{{" + d.description + "}}")) { if (!toRemove.Any(x => x == d.id.ToString())) { toRemove.Add(d.id.ToString()); } } } foreach (String s in toRemove) { Int32 i; if (Int32.TryParse(s, out i)) { var d = dRepo.GetById(i); item.dataFields = item.dataFields.Where(o => o.id != d.id).ToList(); dRepo.Delete(d); } else { item.dataFields = item.dataFields.Where(o => o.description != s).ToList(); } } //Don't know what the hell I was thinking here! Keep it in comments in case it was important! //item.dataFields = item.dataFields.Where(o => o.id != null && o.id > 0).ToList(); } //Remove potential harmful scripts from content if (item.htmlContent != null && !item.htmlContent.Equals("")) { Regex.Replace(item.htmlContent, "<script.*?</script>", "", RegexOptions.IgnoreCase); } try { repo.Update(item); var response = Request.CreateResponse<Section>(HttpStatusCode.OK, item); return response; } catch (Exception) { throw new HttpResponseException(HttpStatusCode.NotFound); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
public virtual bool Equals(Section s) { // If parameter is null return false. if (s == null) { return false; } //Check id if (!(s.id == null && id == null)) { if (s.id == null || id == null) { return false; } if (s.id != id) { return false; } } //Check headline if (!(s.headline == null && headline == null)) { if (s.headline == null || headline == null) { return false; } if (!s.headline.Equals(headline)) { return false; } } //Check html content if (!(s.htmlContent == null && htmlContent == null)) { if (s.htmlContent == null || htmlContent == null) { return false; } if (!s.htmlContent.Equals(htmlContent)) { return false; } } //Check variables if (s.documentIndex!=documentIndex || s.templateRevisionId!=templateRevisionId) { return false; } //Check object members - document if (!(s.document == null && document == null)) { if (s.document == null || document == null) { return false; } if (s.document.id != document.id) { return false; } } //Check object members - document if (!(s.template == null && template == null)) { if (s.template == null || template == null) { return false; } if (s.template.id != template.id) { return false; } } //Check collections - section if (!(s.dataFields == null && dataFields == null)) { if (s.dataFields == null || dataFields == null) { return false; } return Enumerable.SequenceEqual(s.dataFields.OrderBy(x => x.description), dataFields.OrderBy(x => x.description)); } return true; }