/// <summary> /// Updates a Collection in the database with the the values provided. A new Collection is created if one does not already exist. /// </summary> /// <param name="changedCollection">The collection content to be modified.</param> /// <returns>The modified database collection.</returns> public CFCollection UpdateStoredCollection(CFCollection changedCollection) { CFCollection dbModel = new CFCollection(); if (changedCollection.Id > 0) { dbModel = GetCollection(changedCollection.Id); } else { dbModel = CreateEntity <CFCollection>(changedCollection.EntityTypeId.Value); } //updating the "value" text elements dbModel.UpdateValues(changedCollection); dbModel.Serialize(); if (changedCollection.Id > 0) //update Item { Db.Entry(dbModel).State = EntityState.Modified; } else { dbModel = Db.Collections.Add(dbModel); } return(dbModel); }
public void CreateCollections() { int COUNT = 20; CatfishDbContext db = new CatfishDbContext(); List <CFEntityType> collectionTypes = db.EntityTypes.Where(t => t.TargetTypes.Contains(CFEntityType.eTarget.Collections.ToString())).ToList();//db.EntityTypes.Where(t => t.TargetType == EntityType.eTarget.Collections).ToList(); if (collectionTypes.Count == 0) { throw new Exception("No entity types have been defined for collections."); } var rand = new Random(); CollectionService srv = new CollectionService(db); for (int i = 0; i < COUNT; ++i) { CFEntityType cType = collectionTypes[rand.Next(0, collectionTypes.Count)]; CFCollection c = srv.CreateEntity <CFCollection>(cType.Id); string name = TestData.LoremIpsum(5, 10); c.SetName("Collection: " + name); c.SetDescription(TestData.LoremIpsum(20, 100, 1, 10)); c.Serialize(); db.Collections.Add(c); } db.SaveChanges(); }
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)); } }
// GET: Manager/Collections/children/5 public ActionResult Associations(int id) { SecurityService.CreateAccessContext(); CFCollection model = CollectionService.GetCollection(id); if (model == null) { return(HttpNotFound("Collection was not found")); } //EntityContentViewModel childCollections = new EntityContentViewModel(); //childCollections.Id = model.Id; //childCollections.LoadNextChildrenSet(model.ChildCollections); //childCollections.LoadNextMasterSet(CollectionService.GetCollections()); //ViewBag.ChildCollections = childCollections; //EntityContentViewModel childItems = new EntityContentViewModel(); //childItems.Id = model.Id; //childItems.LoadNextChildrenSet(model.ChildItems); //childItems.LoadNextMasterSet(ItemService.GetItems()); //ViewBag.ChildItems = childItems; //EntityContentViewModel relatedItems = new EntityContentViewModel(); //relatedItems.Id = model.Id; //relatedItems.LoadNextChildrenSet(model.RelatedMembers); //relatedItems.LoadNextMasterSet(ItemService.GetItems()); //ViewBag.RelatedItems = relatedItems; return(View(model)); }
public void TestNoAccessDefinitionWithParents() { SecurityServiceBase srv = new TestSecurityService(Db); string collectionName = "TestUserIsAdminCollection"; string collectionDesciption = "Test collection description"; string itemName = "TestUserIsAdminItem"; string itemDesciption = "Test item description"; AccessMode defaultAccess = GetDefaultPermissions(); AccessMode modes; int entityType = mDh.Ets.GetEntityTypes(CFEntityType.eTarget.Collections).FirstOrDefault().Id; CFCollection c1 = mDh.CreateCollection(mDh.Cs, entityType, collectionName, collectionDesciption, true); entityType = mDh.Ets.GetEntityTypes(CFEntityType.eTarget.Items).FirstOrDefault().Id; CFItem i1 = mDh.CreateItem(mDh.Is, entityType, itemName, itemDesciption, true); c1.ChildMembers.Add(i1); Db.SaveChanges(); foreach (TestUser user in Users) { modes = srv.GetPermissions(user.Guid, i1); Assert.AreEqual(defaultAccess, modes); Assert.AreNotEqual(AccessMode.All, modes); } }
public void DeleteCollection() { DatabaseHelper Dh = new DatabaseHelper(true); CollectionService Cs = new CollectionService(Dh.Db); Piranha.Entities.User admin = Dh.PDb.Users.First(); IIdentity identity = new GenericIdentity(admin.Login, Dh.PDb.Groups.Find(admin.GroupId).Name); CFCollection test = Cs.GetCollections().FirstOrDefault(); int id = test.Id; IQueryable <CFCollection> collections = Cs.GetCollections(); Assert.AreEqual(DatabaseHelper.TOTAL_COLLECTIONS, collections.Count()); Cs.DeleteCollection(id); Dh.Db.SaveChanges(identity); Assert.AreNotEqual(DatabaseHelper.TOTAL_COLLECTIONS, collections.Count()); Assert.AreEqual(DatabaseHelper.TOTAL_COLLECTIONS - 1, collections.Count()); try { Cs.DeleteCollection(id); Dh.Db.SaveChanges(identity); Assert.Fail("An Exception should have been thrown on a bad delete."); } catch (ArgumentException ex) { } Assert.AreNotEqual(DatabaseHelper.TOTAL_COLLECTIONS, collections.Count()); Assert.AreEqual(DatabaseHelper.TOTAL_COLLECTIONS - 1, collections.Count()); }
// GET: Manager/Collections/Edit/5 public ActionResult Edit(int?id, int?entityTypeId) { CFCollection model; if (id.HasValue && id.Value > 0) { model = CollectionService.GetCollection(id.Value); if (model == null) { return(HttpNotFound("Collection was not found")); } } else { if (entityTypeId.HasValue) { model = CollectionService.CreateCollection(entityTypeId.Value); } else { List <CFEntityType> entityTypes = EntityTypeService.GetEntityTypes(CFEntityType.eTarget.Collections).ToList(); ViewBag.SelectEntityViewModel = new SelectEntityTypeViewModel() { EntityTypes = entityTypes }; model = new CFCollection(); } } return(View(model)); }
// GET: Manager/Collections/children/5 public ActionResult Associations(int id) { CFCollection model = CollectionService.GetCollection(id); if (model == null) { return(HttpNotFound("Collection was not found")); } EntityContentViewModel childCollections = new EntityContentViewModel(); childCollections.Id = model.Id; childCollections.LoadNextChildrenSet(model.ChildCollections); childCollections.LoadNextMasterSet(CollectionService.GetCollections()); ViewBag.ChildCollections = childCollections; EntityContentViewModel childItems = new EntityContentViewModel(); childItems.Id = model.Id; childItems.LoadNextChildrenSet(model.ChildItems); childItems.LoadNextMasterSet(ItemService.GetItems()); ViewBag.ChildItems = childItems; EntityContentViewModel relatedItems = new EntityContentViewModel(); relatedItems.Id = model.Id; relatedItems.LoadNextChildrenSet(model.ChildRelations); relatedItems.LoadNextMasterSet(ItemService.GetItems()); ViewBag.RelatedItems = relatedItems; return(View(model)); }
public void ExportPartialTest() { string result = ""; DatabaseHelper Dh = new DatabaseHelper(true); // Build the Ingestion CFCollection collection = Dh.Db.Collections.First(); IEnumerable <CFItem> items = Dh.Db.Items.Take(2); IEnumerable <CFEntityType> entitytypes = Dh.Db.EntityTypes.Where(e => e.Id == collection.EntityType.Id || items.Select(i => i.EntityTypeId).Contains(e.Id)).Distinct(); IEnumerable <CFMetadataSet> metadatasets = Dh.Db.MetadataSets.Where(m => entitytypes.SelectMany(e => e.MetadataSets.Select(mm => mm.Id)).Contains(m.Id)).Distinct(); Ingestion ingestion = new Ingestion(); ingestion.MetadataSets.AddRange(metadatasets); ingestion.EntityTypes.AddRange(entitytypes); ingestion.Aggregations.Add(collection); ingestion.Aggregations.AddRange(items); CFItem[] itemArray = items.ToArray(); ingestion.Relationships.Add(new Relationship() { ParentRef = collection.Guid, ChildRef = itemArray[0].Guid }); ingestion.Relationships.Add(new Relationship() { IsMember = true, IsRelated = false, ParentRef = collection.Guid, ChildRef = itemArray[1].Guid }); ingestion.Relationships.Add(new Relationship() { IsRelated = true, IsMember = false, ParentRef = itemArray[0].Guid, ChildRef = itemArray[1].Guid }); // Export the injestion using (MemoryStream stream = new MemoryStream()) { Dh.Igs.Export(stream, ingestion); stream.Position = 0; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } } XElement testResult = XElement.Parse(result); // Test result AsserXmlEqualIngestion(testResult, ingestion); }
// GET: Manager/Collections/Edit/5 public ActionResult Edit(int?id, int?entityTypeId) { SecurityService.CreateAccessContext(); CFCollection model; if (id.HasValue && id.Value > 0) { model = CollectionService.GetCollection(id.Value); if (model == null) { return(HttpNotFound("Collection was not found")); } //Sept 27 2019 -- set the READ ONLY if (!Catfish.Core.Contexts.AccessContext.current.IsAdmin)//not admin { string accessMode = ""; foreach (CFAccessGroup ag in model.AccessGroups) { accessMode = ag.AccessDefinition.AccessModes.ToString(); if (accessMode == "Read") { ViewBag.ReadOnly = true; } else if (accessMode == "Write") { ViewBag.ReadOnly = false; break; } } } } else { if (entityTypeId.HasValue) { model = CollectionService.CreateCollection(entityTypeId.Value); } else { List <CFEntityType> entityTypes = EntityTypeService.GetEntityTypes(CFEntityType.eTarget.Collections).ToList(); ViewBag.SelectEntityViewModel = new SelectEntityTypeViewModel() { EntityTypes = entityTypes }; model = new CFCollection(); } } return(View(model)); }
public void TestCreateCollection() { DatabaseHelper Dh = new DatabaseHelper(true); CollectionService Cs = new CollectionService(Dh.Db); int entityTypeId = Dh.Db.EntityTypes.Where(e => e.TargetTypes.Contains("Collection")).Select(e => e.Id).FirstOrDefault(); string name = "Test 1"; string description = "Description"; CFCollection c = CreateCollection(Cs, entityTypeId, name, description); Assert.AreEqual(name, c.Name); Assert.AreEqual(description, c.Description); }
private Ingestion DeserializeAggregations(XmlReader reader) { while (reader.Read()) { if (reader.IsStartElement()) { string name = reader.LocalName; CFXmlModel model = null; string strGuid = reader.GetAttribute("guid"); switch (name) { case "collection": model = new CFCollection(); break; case "item": model = new CFItem(); break; case "form": model = new Form(); break; case "file": model = new CFDataFile(); break; default: throw new FormatException("Invalid XML element: " + reader.Name); } if (model != null) { model.Guid = strGuid; model.MappedGuid = strGuid; model.Content = reader.ReadOuterXml(); Aggregations.Add(model); } } if (reader.NodeType == System.Xml.XmlNodeType.EndElement) { if (reader.Name == "aggregations") { return(this); } } } return(this); }
public CFCollection CreateCollection(CollectionService cs, int entityTypeId, string name, string description, bool store = false) { CFCollection c = cs.CreateCollection(entityTypeId); c.Name = name; c.Description = description; if (store) { c = cs.UpdateStoredCollection(c); } return(c); }
public ActionResult AddUserAccessDefinition(EntityAccessDefinitionsViewModel entityAccessVM) { CFCollection collection = CollectionService.GetCollection(entityAccessVM.Id);//ItemService.GetItem(entityAccessVM.Id); AccessGroupService accessGroupService = new AccessGroupService(Db); collection = accessGroupService.UpdateEntityAccessGroups(collection, entityAccessVM) as CFCollection; collection = EntityService.UpdateEntity(collection) as CFCollection; collection.Serialize(); Db.SaveChanges(); return(RedirectToAction("AccessGroup", new { id = entityAccessVM.Id })); }
private void CreateCollections() { List <int> ets = Ets.GetEntityTypes().ToList().Where(et => et.TargetTypesList.Contains(eTarget.Collections)).Select(et => et.Id).ToList(); for (int i = 0; i < TOTAL_COLLECTIONS; ++i) { int index = i % ets.Count; CFCollection c = Cs.CreateEntity <CFCollection>(ets[index]); c.SetName("Collection " + (i + 1)); c.SetDescription("Description for Collection " + (i + 1)); Cs.UpdateStoredCollection(c); } Db.SaveChanges(); }
// GET: Items public JsonResult Index(string fields, int offset = 0, int limit = 25, bool randomize = false, int entityTypeId = 0, int collectionId = 0) { try { //SecurityService IEnumerable <CFItem> items; if (collectionId > 0) { CFCollection collection = CollectionService.GetCollection(collectionId); items = collection.ChildItems.Select(it => it as CFItem); } else { items = ItemService.GetItems(); } if (entityTypeId > 0) { items = items.Where(it => it.EntityTypeId == entityTypeId); } items = randomize ? items.OrderBy(item => Guid.NewGuid()) : items.OrderBy(item => item.Id); if (offset > 0) { items = items.Skip(offset); } if (limit > 0) { items = items.Take(limit); } else { items = items.Take(25); } List <BulletinBoardItem> ret = items.Select(it => new BulletinBoardItem(it, Request.RequestContext, fields)).ToList(); return(Json(ret, JsonRequestBehavior.AllowGet)); } catch (Exception) { Response.StatusCode = 500; Response.StatusDescription = "An error occurred."; return(Json(new List <BulletinBoardItem>(), JsonRequestBehavior.AllowGet)); } }
public ActionResult Edit(CFCollection model) { if (ModelState.IsValid) { CFCollection dbModel = CollectionService.UpdateStoredCollection(model); Db.SaveChanges(User.Identity); if (model.Id == 0) { return(RedirectToAction("Edit", new { id = dbModel.Id })); } else { return(View(dbModel)); } } return(View(model)); }
/// <summary> /// Removes a collection from the database. /// </summary> /// <param name="id">The id of the collection to be removed.</param> public void DeleteCollection(int id) { CFCollection model = null; if (id > 0) { model = Db.Collections.Where(et => et.Id == id).FirstOrDefault(); if (model != null) { Db.Entry(model).State = EntityState.Deleted; } else { throw new ArgumentException(string.Format("Collection {0} not found.", id)); } } else { throw new ArgumentException(string.Format("Invalid collection id {0}.", id)); } }
public void TestUpdateCollection() { DatabaseHelper Dh = new DatabaseHelper(true); CollectionService Cs = new CollectionService(Dh.Db); int entityTypeId = Dh.Db.EntityTypes.Where(e => e.TargetTypes.Contains("Collection")).Select(e => e.Id).FirstOrDefault(); string name = "Test 2"; string description = "Description"; string name2 = "Test 3"; string description2 = "New Description"; CFCollection c = CreateCollection(Cs, entityTypeId, name, description, true); Dh.Db.SaveChanges(); c = Cs.GetCollection(c.Id); int id = c.Id; string content = c.Content; Assert.AreEqual(name, c.Name); Assert.AreEqual(description, c.Description); Assert.AreNotEqual(name2, c.Name); Assert.AreNotEqual(description2, c.Description); c = Cs.CreateCollection(entityTypeId); // Since we are using an in memory database, we need to duplicate our collection. c.Id = id; c.Name = name2; c.Description = description2; Cs.UpdateStoredCollection(c); Dh.Db.SaveChanges(); CFCollection c2 = Cs.GetCollection(id); Assert.AreNotEqual(name, c2.Name); Assert.AreNotEqual(description, c2.Description); Assert.AreEqual(name2, c2.Name); Assert.AreEqual(description2, c2.Description); Assert.AreEqual(id, c2.Id); }
public ActionResult AccessGroup(int id, EntityAccessDefinitionsViewModel entityAccessVM) { SecurityService.CreateAccessContext(); CFCollection collection = CollectionService.GetCollection(entityAccessVM.Id, AccessMode.Control); if (collection != null) { AccessGroupService accessGroupService = new AccessGroupService(Db); collection = accessGroupService.UpdateEntityAccessGroups(collection, entityAccessVM) as CFCollection; collection = EntityService.UpdateEntity(collection) as CFCollection; collection.Serialize(); Db.SaveChanges(); } collection.Serialize(); Db.SaveChanges(); SuccessMessage(Catfish.Resources.Views.Shared.EntityAccessGroup.SaveSuccess); return(AccessGroup(entityAccessVM.Id)); }
/// <summary> /// Removes a collection from the database. /// </summary> /// <param name="id">The id of the collection to be removed.</param> public void DeleteCollection(int id) { CFCollection model = null; if (id > 0) { model = GetCollection(id, AccessMode.Control); if (model != null) { DisassociateFromAggregations(model); Db.Entry(model).State = EntityState.Deleted; } else { throw new ArgumentException(string.Format("Collection {0} not found.", id)); } } else { throw new ArgumentException(string.Format("Invalid collection id {0}.", id)); } }
public ActionResult Edit(CFCollection model) { if (ModelState.IsValid) { CFCollection dbModel = CollectionService.UpdateStoredCollection(model); Db.SaveChanges(User.Identity); SuccessMessage(Catfish.Resources.Views.Collections.Edit.SaveSuccess); if (model.Id == 0) { return(RedirectToAction("Edit", new { id = dbModel.Id })); } else { return(View(dbModel)); } } ErrorMessage(Catfish.Resources.Views.Collections.Edit.SaveInvalid); model = CollectionService.CreateCollection(model.EntityTypeId.Value); return(View(model)); }
public void TestUserIsAdmin() { SecurityServiceBase srv = new TestSecurityService(Db); TestUser admin = CreateUser(srv, "A1", true); TestUser user = CreateUser(srv, "U1"); string collectionName = "TestUserIsAdminCollection"; string collectionDesciption = "Test collection description"; string itemName = "TestUserIsAdminItem"; string itemDesciption = "Test item description"; AccessMode defaultAccess = GetDefaultPermissions(); int entityType = mDh.Ets.GetEntityTypes(CFEntityType.eTarget.Collections).FirstOrDefault().Id; CFCollection c1 = mDh.CreateCollection(mDh.Cs, entityType, collectionName, collectionDesciption, true); entityType = mDh.Ets.GetEntityTypes(CFEntityType.eTarget.Items).FirstOrDefault().Id; CFItem i1 = mDh.CreateItem(mDh.Is, entityType, itemName, itemDesciption, true); Db.SaveChanges(); AccessMode modes = srv.GetPermissions(admin.Guid, c1); Assert.AreNotEqual(defaultAccess, modes); Assert.AreEqual(AccessMode.All, modes); modes = srv.GetPermissions(user.Guid, c1); Assert.AreEqual(defaultAccess, modes); Assert.AreNotEqual(AccessMode.All, modes); modes = srv.GetPermissions(admin.Guid, i1); Assert.AreNotEqual(defaultAccess, modes); Assert.AreEqual(AccessMode.All, modes); modes = srv.GetPermissions(user.Guid, i1); Assert.AreEqual(defaultAccess, modes); Assert.AreNotEqual(AccessMode.All, modes); }
/// <summary> /// Updates a Collection in the database with the the values provided. A new Collection is created if one does not already exist. /// </summary> /// <param name="changedCollection">The collection content to be modified.</param> /// <returns>The modified database collection.</returns> public CFCollection UpdateStoredCollection(CFCollection changedCollection) { CFCollection dbModel = new CFCollection(); if (changedCollection.Id > 0) { dbModel = GetCollection(changedCollection.Id, AccessMode.Write); } else { dbModel = CreateEntity <CFCollection>(changedCollection.EntityTypeId.Value); } //updating the "value" text elements dbModel.UpdateValues(changedCollection); //update collection attribute -- issystemCollection -- Aug 21 2019 XAttribute isSysColl = changedCollection.Data.Attribute("issystemcollection"); if (isSysColl != null) { dbModel.Data.SetAttributeValue("issystemcollection", isSysColl.Value); } dbModel.Serialize(); if (changedCollection.Id > 0) //update Item { Db.Entry(dbModel).State = EntityState.Modified; } else { dbModel = Db.Collections.Add(dbModel); } return(dbModel); }
public void TestGetCollection() { DatabaseHelper Dh = new DatabaseHelper(true); CollectionService Cs = new CollectionService(Dh.Db); int entityTypeId = Dh.Db.EntityTypes.Where(e => e.TargetTypes.Contains("Collection")).Select(e => e.Id).FirstOrDefault(); string name = "Test 4"; string description = "Descriptiony"; CFCollection c = CreateCollection(Cs, entityTypeId, name, description, true); Dh.Db.SaveChanges(); CFCollection c2 = Cs.GetCollection(c.Id); int id = c.Id; Assert.AreEqual(name, c2.Name); Assert.AreEqual(description, c2.Description); CFCollection c3 = Cs.GetCollection(c.Guid); Assert.AreEqual(id, c.Id); Assert.AreEqual(c2.Content, c3.Content); }
private Ingestion DeserializeAggregations(XElement element) { foreach (XElement child in element.Elements()) { string name = child.Name.LocalName; CFXmlModel model = null; string strGuid = child.Attribute("guid").Value; switch (name) { case "collection": model = new CFCollection(); break; case "item": model = new CFItem(); break; case "form": model = new Form(); break; case "file": model = new CFDataFile(); break; } if (model != null) { model.Guid = strGuid; model.MappedGuid = strGuid; model.Content = child.ToString(); Aggregations.Add(model); } } return(this); }
public Dictionary <int, List <string> > ExportFormData(int collectionId) { //TODO: Restrict exporting data only from items that are viewable to the current user. CollectionService collectionSrv = new CollectionService(Db); CFCollection collection = collectionSrv.GetCollection(collectionId, AccessMode.Read); if (collection == null) { return(null); } List <Dictionary <string, string> > submissionDataSet = new List <Dictionary <string, string> >(); foreach (var item in collection.ChildItems) { foreach (var submission in (item as CFItem).FormSubmissions) { Form form = submission.FormData; //Exporting data from all fields in the form Dictionary <string, List <string> > formData = new Dictionary <string, List <string> >(); foreach (var field in form.Fields) { Dictionary <string, List <string> > fieldValues = ExportValues(field); foreach (var val in fieldValues) { if (formData.ContainsKey(val.Key)) { formData[val.Key].AddRange(val.Value); } else { formData.Add(val.Key, val.Value); } } } Dictionary <string, string> response = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(form.ReferenceCode)) { response.Add("-1", form.ReferenceCode); } foreach (var answer in formData) { response.Add(answer.Key, string.Join("|", answer.Value)); } submissionDataSet.Add(response); } } //Exporting data into a spreadsheet List <int> questionKeys = new List <int>(); foreach (var submissionData in submissionDataSet) { var keys = submissionData.Keys.Select(k => int.Parse(k)); questionKeys = questionKeys.Union(keys).ToList(); } questionKeys.Sort(); Dictionary <int, List <string> > exportData = new Dictionary <int, List <string> >(); foreach (var key in questionKeys) { exportData.Add(key, new List <string>()); } foreach (var submission in submissionDataSet) { foreach (var key in questionKeys) { string val = submission.ContainsKey(key.ToString()) ? submission[key.ToString()] : ""; exportData[key].Add(val); } } return(exportData); }
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 var attachmentFields = form.Fields.Where(f => f is Attachment).Select(f => f as Attachment); foreach (var att in attachmentFields) { UpdateFiles(att, 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.AppendChild(submissionItem); } //MR April 10 2018 //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 = storedFormSubmission.FormData.Fields.Where(f => f.Name == FieldName).FirstOrDefault(); 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); } } //end of MR submissionItem.Serialize(); return(submissionItem); }
public void TestCircularParents() { SecurityServiceBase srv = new TestSecurityService(Db); string collectionName = "TestUserIsAdminCollection"; string collectionDesciption = "Test collection description"; string itemName = "TestUserIsAdminItem"; string itemDesciption = "Test item description"; AccessMode defaultAccess = GetDefaultPermissions(); CFAccessDefinition ad1 = new CFAccessDefinition() { Name = "Test 1", AccessModes = AccessMode.Write }; CFAccessDefinition ad2 = new CFAccessDefinition() { Name = "Test 2", AccessModes = AccessMode.Control | AccessMode.Append }; CFAccessDefinition ad3 = new CFAccessDefinition() { Name = "Test 3", AccessModes = AccessMode.Discover }; int entityType = mDh.Ets.GetEntityTypes(CFEntityType.eTarget.Items).FirstOrDefault().Id; CFItem i1 = mDh.CreateItem(mDh.Is, entityType, itemName, itemDesciption, true); entityType = mDh.Ets.GetEntityTypes(CFEntityType.eTarget.Collections).FirstOrDefault().Id; CFCollection c1 = mDh.CreateCollection(mDh.Cs, entityType, collectionName, collectionDesciption, true); entityType = mDh.Ets.GetEntityTypes(CFEntityType.eTarget.Items).FirstOrDefault().Id; CFItem i2 = mDh.CreateItem(mDh.Is, entityType, itemName, itemDesciption, true); List <CFAccessGroup> groups = new List <CFAccessGroup>() { new CFAccessGroup() { AccessGuids = new List <Guid>() { Guid.Parse(Groups[0]) }, AccessDefinition = ad1 }, }; c1.AccessGroups = groups; c1.Serialize(); groups = new List <CFAccessGroup>() { new CFAccessGroup() { AccessGuids = new List <Guid>() { Guid.Parse(Groups[1]) }, AccessDefinition = ad2 }, new CFAccessGroup() { AccessGuids = new List <Guid>() { Guid.Parse(Groups[2]) }, AccessDefinition = ad3 } }; i2.AccessGroups = groups; i2.Serialize(); i1.Serialize(); c1.ChildMembers.Add(i1); c1.ChildMembers.Add(i2); //NOTE: Here is our circular relationship i2.ChildMembers.Add(i1); Db.SaveChanges(); AccessMode modes1 = srv.GetPermissions(Users[0].Guid, i1); AccessMode modes2 = srv.GetPermissions(Users[1].Guid, i1); AccessMode modes3 = srv.GetPermissions(Users[2].Guid, i1); AccessMode modes4 = srv.GetPermissions(Users[3].Guid, i1); Assert.AreEqual(defaultAccess | ad1.AccessModes | ad3.AccessModes, modes1); Assert.AreEqual(defaultAccess | ad1.AccessModes | ad2.AccessModes, modes2); Assert.AreEqual(defaultAccess | ad2.AccessModes, modes3); Assert.AreEqual(defaultAccess | ad2.AccessModes | ad3.AccessModes, modes4); modes1 = srv.GetPermissions(Users[0].Guid, i2); modes2 = srv.GetPermissions(Users[1].Guid, i2); modes3 = srv.GetPermissions(Users[2].Guid, i2); modes4 = srv.GetPermissions(Users[3].Guid, i2); Assert.AreEqual(defaultAccess | ad1.AccessModes | ad3.AccessModes, modes1); Assert.AreEqual(defaultAccess | ad1.AccessModes | ad2.AccessModes, modes2); Assert.AreEqual(defaultAccess | ad2.AccessModes, modes3); Assert.AreEqual(defaultAccess | ad2.AccessModes | ad3.AccessModes, modes4); modes1 = srv.GetPermissions(Users[0].Guid, c1); modes2 = srv.GetPermissions(Users[1].Guid, c1); modes3 = srv.GetPermissions(Users[2].Guid, c1); modes4 = srv.GetPermissions(Users[3].Guid, c1); Assert.AreEqual(defaultAccess | ad1.AccessModes, modes1); Assert.AreEqual(defaultAccess | ad1.AccessModes, modes2); Assert.AreEqual(defaultAccess, modes3); Assert.AreEqual(defaultAccess, modes4); }