public JsonResult MoveItemToSystemCollection(int itemId, int sysCollectionId)
        {
            try
            {
                CFItem item = ItemService.GetItem(itemId);
                //remove all parents from this item
                if (item.ParentMembers.Count > 1)
                {
                    foreach (CFAggregation p in item.ParentMembers)
                    {
                        p.RemoveChild(item);
                    }
                }

                //add item to systemcollection
                CFCollection systemCollection = CollectionService.GetCollection(sysCollectionId);
                systemCollection.AddChild(item);

                Db.Entry(item).State             = EntityState.Modified;
                Db.Entry(systemCollection).State = EntityState.Modified;
                Db.SaveChanges(User.Identity);

                //SuccessMessage(Catfish.Resources.Views.Items.Edit.MoveSuccess);
                return(Json(systemCollection.Name));
            }
            catch (Exception ex)
            {
                //ErrorMessage(Catfish.Resources.Views.Items.Edit.MoveFailed);
                return(Json(Catfish.Resources.Views.Items.Edit.MoveFailed));
            }
        }
Exemple #2
0
        public CFItem SaveSubmission(Form form, string formSubmissionRef, int itemId, int entityTypeId, int formTemplateId, int collectionId, IDictionary <string, string> metadataAttributeMapping = null)
        {
            CFItem submissionItem;

            if (itemId == 0)
            {
                submissionItem = CreateEntity <CFItem>(entityTypeId);
                // submissionItem.m
                Db.Items.Add(submissionItem);
            }
            else
            {
                submissionItem = Db.Items.Where(m => m.Id == itemId).FirstOrDefault();
                if (submissionItem == null)
                {
                    throw new Exception("Specified item not found");
                }
                submissionItem.LogChange(submissionItem.Guid, "Updated.");
                Db.Entry(submissionItem).State = System.Data.Entity.EntityState.Modified;
            }

            CFFormSubmission storedFormSubmission = submissionItem.GetFormSubmission(formSubmissionRef);

            if (storedFormSubmission == null)
            {
                //if no stored form is available, we need to clone the template
                Form template = Db.FormTemplates.Where(m => m.Id == formTemplateId).FirstOrDefault();
                if (template == null)
                {
                    throw new Exception("Form template does not exist.");
                }

                storedFormSubmission = new CFFormSubmission();
                storedFormSubmission.ReplaceFormData(new XElement(template.Data));
                submissionItem.AddData(storedFormSubmission);
            }

            storedFormSubmission.UpdateFormData(form);

            //If any attachments have been submitted through the form and they have not yet been included in the
            //submission item, then include them and remove them from the main XMLModel table
            IEnumerable <Attachment> attachments = GetAttachmentFields(form.Fields, submissionItem);

            UpdateFiles(attachments, submissionItem);

            if (collectionId > 0)
            {
                CFCollection collection = Db.Collections.Where(c => c.Id == collectionId).FirstOrDefault();
                if (collection == null)
                {
                    throw new Exception("Specified collection not found");
                }

                collection.AddChild(submissionItem);
            }

            //update metadata field's value based on the attribute mapping
            //for example if "Name mapping" mapped to the Form's Title field, grab the value of the form title and set it to Metadata Set "Name Mapping Attribute"
            EntityTypeService entityTypeService = new EntityTypeService(Db);
            CFEntityType      entityType        = entityTypeService.GetEntityTypeById(entityTypeId);

            foreach (KeyValuePair <string, string> map in metadataAttributeMapping)
            {
                //key: attributeMapping, value Form's Field's Name
                string    attMapping = map.Key;
                string    FieldName  = map.Value;
                FormField formField  = GetFormField(storedFormSubmission.FormData.Fields, FieldName);

                if (formField == null)
                {
                    // The field currently does not exist
                    continue;
                }

                var FieldValues = formField.GetValues();

                CFEntityTypeAttributeMapping am = entityType.AttributeMappings.Where(a => a.Name == attMapping).FirstOrDefault();
                CFMetadataSet ms = null;
                if (am != null)
                {
                    ms = entityType.MetadataSets.Where(m => m.Id == am.MetadataSetId).FirstOrDefault();
                }

                FormField field;
                if (ms != null)
                {
                    field = ms.Fields.Where(f => f.Name == am.FieldName).FirstOrDefault();
                }

                foreach (var fVal in FieldValues)
                {
                    ms.SetFieldValue(am.FieldName, fVal.Value, fVal.LanguageCode);
                }
            }

            submissionItem.Serialize();
            return(submissionItem);
        }