/// <summary> /// Updates a Item in the database with the the values provided. A new Item is created if one does not already exist. /// </summary> /// <param name="changedItem">The item content to be modified.</param> /// <returns>The modified database item.</returns> public CFItem UpdateStoredItem(CFItem changedItem) { CFItem dbModel = new CFItem(); if (changedItem.Id > 0) { dbModel = Db.XmlModels.Find(changedItem.Id) as CFItem; } else { dbModel = CreateEntity <CFItem>(changedItem.EntityTypeId.Value); } //updating the "value" text elements dbModel.UpdateValues(changedItem); //Processing any file attachments that have been submitted UpdateFiles(changedItem, dbModel); if (changedItem.Id > 0) {//update Item Db.Entry(dbModel).State = EntityState.Modified; } else { dbModel.Serialize(); Db.XmlModels.Add(dbModel); } return(dbModel); }
/// <summary> /// Updates a Item in the database with the the values provided. A new Item is created if one does not already exist. /// </summary> /// <param name="changedItem">The item content to be modified.</param> /// <returns>The modified database item.</returns> public CFItem UpdateStoredItem(CFItem changedItem) { CFItem dbModel = new CFItem(); if (changedItem.Id > 0) { //dbModel = Db.XmlModels.Find(changedItem.Id) as CFItem; dbModel = GetItem(changedItem.Id, AccessMode.Write); if (dbModel == null) { throw new UnauthorizedAccessException("User does not have WRITE access to item " + changedItem.Id); } } else { dbModel = CreateEntity <CFItem>(changedItem.EntityTypeId.Value); } //updating the "value" text elements dbModel.UpdateValues(changedItem); //oct 10 2018 -- attemp to add attchment in the metadtaset to item.Attachment -- this might be wrong approach! foreach (CFMetadataSet ms in changedItem.MetadataSets) { foreach (FormField field in ms.Fields) { if (field.GetType().FullName == "Catfish.Core.Models.Forms.Attachment") { if (!string.IsNullOrEmpty((field as Catfish.Core.Models.Forms.Attachment).FileGuids)) { if (string.IsNullOrEmpty(changedItem.AttachmentField.FileGuids)) { changedItem.AttachmentField.FileGuids = (field as Catfish.Core.Models.Forms.Attachment).FileGuids; } else { changedItem.AttachmentField.FileGuids += "|" + (field as Catfish.Core.Models.Forms.Attachment).FileGuids; } } } } } //Processing any file attachments that have been submitted UpdateFiles(changedItem, dbModel); if (changedItem.Id > 0) {//update Item Db.Entry(dbModel).State = EntityState.Modified; } else { dbModel.Serialize(); Db.XmlModels.Add(dbModel); } return(dbModel); }