public void GetMetadataSetTest()
        {
            MetadataService ms = mDh.Ms;

            int fieldCount = 10;

            string passName = "Good Metadata";
            string failName = "Bad Metadata";

            string passDescription = "Good Description";
            string failDescription = "Bad Description";

            IEnumerable <FormField> fields = CreateFormFields(fieldCount);

            CFMetadataSet check = CreateMetadata(passName, passDescription, fields);

            mDh.Db.SaveChanges();

            CFMetadataSet result = ms.GetMetadataSet(check.Id);

            Assert.IsNotNull(result);
            Assert.AreNotEqual(failName, result.Name);
            Assert.AreEqual(passName, result.Name);
            Assert.AreNotEqual(failDescription, result.Description);
            Assert.AreEqual(passDescription, result.Description);
            Assert.AreEqual(fieldCount, result.Fields.Count);
        }
        private Ingestion DeserializeMetadatasets(System.Xml.XmlReader reader)
        {
            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    if (reader.Name == "metadata-set")
                    {
                        string        strGuid = reader.GetAttribute("guid");
                        CFMetadataSet set     = new CFMetadataSet();
                        set.Content    = reader.ReadOuterXml();
                        set.Guid       = strGuid;
                        set.MappedGuid = strGuid;
                        MetadataSets.Add(set);
                    }
                    else
                    {
                        throw new FormatException("Invalid XML element: " + reader.Name);
                    }
                }

                if (reader.NodeType == System.Xml.XmlNodeType.EndElement)
                {
                    if (reader.Name == "metadata-sets")
                    {
                        return(this);
                    }
                }
            }

            return(this);
        }
Exemple #3
0
        private CFEntity UpdateEntityMetadataSet(CFEntity entity, CFMetadataSet metadata)
        {
            CFMetadataSet entityMetadata = entity.MetadataSets.Where(m => m.Guid == metadata.Guid).FirstOrDefault();

            if (entityMetadata != null)
            {
                List <FormField> entityFields = new List <FormField>(metadata.Fields.Count);

                foreach (FormField field in metadata.Fields)
                {
                    FormField entityField = entityMetadata.Fields.Where(f => f.Guid == field.Guid).FirstOrDefault();

                    if (entityField == null)
                    {
                        entityFields.Add(field);
                    }
                    else
                    {
                        entityField.Merge(field);
                        entityFields.Add(entityField);
                    }
                }

                entityMetadata.Fields = entityFields;

                return(entity);
            }

            return(null);
        }
Exemple #4
0
        private void CreateMetadata()
        {
            CFMetadataSet metadata = new CFMetadataSet();

            metadata.SetName("Basic Metadata");
            metadata.SetDescription("Metadata Description");

            List <FormField> fields = new List <FormField>();

            FormField field = new TextField();

            field.Name        = "Name";
            field.Description = "The Description";
            fields.Add(field);

            field             = new TextField();
            field.Name        = "Description";
            field.Description = "Description Description";
            fields.Add(field);

            metadata.Fields = fields;
            metadata.Serialize();

            Ms.UpdateMetadataSet(metadata);

            Db.SaveChanges();
        }
Exemple #5
0
 public MetadataSetViewModel(CFMetadataSet metadataset, string[] languageCodes) : this()
 {
     foreach (FormField field in metadataset.Fields)
     {
         Fields.Add(new FormFieldViewModel(field, languageCodes));
     }
 }
Exemple #6
0
        public void UpdateExistingEntityMetadataTest()
        {
            const string metadataName          = "UpdateExistingEntityMetadataTest";
            const string metadataDescription   = "Test for function UpdateExistingEntityMetadata.";
            const string entityTypeName        = "UpdateExistingEntityMetadataTest";
            const string entityTypeDescription = "Test for function UpdateExistingEntityMetadata.";

            // Create an entity
            CFMetadataSet metadata = CreateBasicMetadataSet(metadataName, metadataDescription);

            CFEntityType entityType = CreateEntityType(mDh, entityTypeName, entityTypeDescription, "Items", metadata);

            entityType.AttributeMappings.Add(new CFEntityTypeAttributeMapping()
            {
                Name = "Name Mapping", MetadataSet = metadata, FieldName = NAME_NAME
            });
            entityType.AttributeMappings.Add(new CFEntityTypeAttributeMapping()
            {
                Name = "DescriptionMapping", MetadataSet = metadata, FieldName = NAME_DESCRIPTION
            });

            mDh.Db.SaveChanges();

            CFItem item1 = mDh.CreateItem(mDh.Is, entityType.Id, "Item 1", "Item 1 Description", true);
            CFItem item2 = mDh.CreateItem(mDh.Is, entityType.Id, "Item 2", "Item 2 Description", true);
        }
        public CFMetadataSet GetMetadataSet(int id)
        {
            CFMetadataSet metadata = Db.MetadataSets.Where(m => m.Id == id).FirstOrDefault();

            //if (metadata != null)
            //    metadata.Deserialize();
            return(metadata);
        }
        public CFMetadataSet GetMetadataSet(string guid)
        {
            // TODO: Generalze to xpath lookup
            CFMetadataSet metadata = Db.MetadataSets.ToList().Where(m => m.Guid == guid).FirstOrDefault();

            //if (metadata != null)
            //    metadata.Deserialize();
            return(metadata);
        }
        private CFMetadataSet CreateMetadata(string name, string description, IEnumerable <FormField> fields)
        {
            CFMetadataSet metadata = new CFMetadataSet();

            metadata.Name        = name;
            metadata.Description = description;

            metadata.Fields = new List <FormField>(fields);

            metadata.Serialize();

            return(mDh.Ms.UpdateMetadataSet(metadata));
        }
        protected CFMetadataSet CreateMetadataSet(DatabaseHelper dh, string name, string description, IEnumerable <FormField> fields)
        {
            CFMetadataSet metadata = new CFMetadataSet();

            metadata.Name        = name;
            metadata.Description = description;

            metadata.Fields = new List <FormField>(fields);

            metadata.Serialize();

            return(dh.Ms.UpdateMetadataSet(metadata));
        }
Exemple #11
0
        private string GetGraphFieldString(string metadataSetGuid, string fieldGuid, string languageCode = "en", bool wrapInFunction = false)
        {
            string baseSearch = wrapInFunction ? "\"unique(value_{0}_{1}_{2}_ss)\"" : "value_{0}_{1}_{2}_ss";

            CFMetadataSet metadataSet = MetadataSrv.GetMetadataSet(metadataSetGuid);
            FormField     field       = metadataSet.Fields.Where(f => f.Guid.Equals(fieldGuid)).FirstOrDefault();

            if (field != null)
            {
                if (typeof(NumberField).IsAssignableFrom(field.GetType()))
                {
                    baseSearch = wrapInFunction ? "\"sum(field(value_{0}_{1}_is, max))\"" : "value_{0}_{1}_is";
                }
            }

            return(string.Format(baseSearch, metadataSetGuid.Replace('-', '_'), fieldGuid.Replace('-', '_'), languageCode));
        }
Exemple #12
0
        public bool IsOptionField(string metadataSetGuid, string fieldGuid)
        {
            //Sept 12 2018 -- check if the field is a numberField otherwise text field
            CFMetadataSet metadataSet = (new MetadataService(Db)).GetMetadataSet(metadataSetGuid);

            foreach (FormField formField in metadataSet.Fields)
            {
                if (formField.Guid.Equals(fieldGuid))
                {
                    if (typeof(OptionsField).IsAssignableFrom(formField.GetType()))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemple #13
0
        private Ingestion DeserializeMetadatasets(XElement element)
        {
            foreach (XElement setElement in element.Elements())
            {
                string name    = setElement.Name.LocalName;
                string strGuid = setElement.Attribute("guid").Value;
                if (name == "metadata-set")
                {
                    CFMetadataSet set = new CFMetadataSet();
                    set.Content    = setElement.ToString();
                    set.Guid       = strGuid;
                    set.MappedGuid = strGuid;
                    MetadataSets.Add(set);
                }
            }

            return(this);
        }
        public CFMetadataSet UpdateMetadataSet(CFMetadataSet metadataSet)
        {
            ////MetadataSet ms;
            if (metadataSet.Id > 0)
            {
                ////ms = Db.MetadataSets.Where(m => m.Id == metadataDefinition.Id).FirstOrDefault();
                ////if (ms == null)
                ////    return null;

                Db.Entry(metadataSet).State = System.Data.Entity.EntityState.Modified;
            }
            else
            {
                ////ms = new MetadataSet();
                Db.MetadataSets.Add(metadataSet);
            }
            ////ms.Definition = metadataDefinition;
            return(metadataSet);
        }
Exemple #15
0
        private void CreateEntityTypes()
        {
            CFMetadataSet metadata = Ms.GetMetadataSets().FirstOrDefault();

            for (int i = 0; i < TOTAL_ENTITYTYPES; ++i)
            {
                CFEntityType et = new CFEntityType();
                et.Name = "Entity" + (i + 1);
                et.MetadataSets.Add(metadata);

                List <eTarget> targets = new List <eTarget>();

                if (i % 2 == 0)
                {
                    targets.Add(eTarget.Items);
                }

                if (i % 5 == 1)
                {
                    targets.Add(eTarget.Collections);
                }

                et.TargetTypesList = targets;

                et.AttributeMappings.Add(new CFEntityTypeAttributeMapping()
                {
                    Name        = "Name Mapping",
                    MetadataSet = metadata,
                    FieldName   = "Name"
                });

                et.AttributeMappings.Add(new CFEntityTypeAttributeMapping()
                {
                    Name        = "Description Mapping",
                    MetadataSet = metadata,
                    FieldName   = "Description"
                });

                Db.EntityTypes.Add(et);
            }

            Db.SaveChanges();
        }
Exemple #16
0
        public int UpdateExistingEntityMetadata(CFMetadataSet metadata)
        {
            int             totalChanged = 0;
            CFEntity        result;
            List <CFEntity> entities = GetEntitiesWithMetadataSet(metadata.Id).ToList();

            foreach (CFEntity entity in entities)
            {
                result = UpdateEntityMetadataSet(entity, metadata);

                if (result != null)
                {
                    ++totalChanged;
                    Db.Entry(result).State = EntityState.Modified;
                }
            }

            return(totalChanged);
        }
Exemple #17
0
        public void MetadataFieldsTest()
        {
            string path = GetSampleDataFilePathName("MetadataSet.xml");

            Assert.IsTrue(File.Exists(path));

            XElement root = XElement.Load(path);

            Assert.IsNotNull(root);

            CFMetadataSet model = CFXmlModel.Parse(root) as CFMetadataSet;

            Assert.IsNotNull(model);

            IReadOnlyList <FormField> fields = model.Fields;

            Assert.AreEqual(3, fields.Count);

            var field = fields.Where(f => f is TextField).FirstOrDefault();

            Assert.IsNotNull(field);
            Assert.AreEqual("Subject", field.GetName("en"));
            Assert.AreEqual("Sujet", field.GetName("fr"));
            Assert.AreEqual("Enter the subject here.", field.GetDescription("en"));

            field = fields.Where(f => f is TextArea).FirstOrDefault();
            Assert.IsNotNull(field);
            Assert.AreEqual("Description", field.GetName("en"));
            Assert.AreEqual("La description", field.GetName("fr"));
            Assert.AreEqual("Enter the description here.", field.GetDescription("en"));

            field = fields.Where(f => f is RadioButtonSet).FirstOrDefault();
            Assert.IsNotNull(field);
            Assert.AreEqual("Radio button set 1", field.GetName("en"));
            Assert.AreEqual("Réglage du bouton radio 1", field.GetName("fr"));
            Assert.AreEqual("This is a radio button set.", field.GetDescription("en"));

            ////var options = (field as RadioButtonSet).GetOptions("fr");
            ////Assert.AreEqual("Première option\nDeuxième option\nTroisième option", options);
            ////Assert.AreEqual("First option\nSecond option\nThird option", (field as RadioButtonSet).Options);
        }
Exemple #18
0
        public JsonResult AddMetadataSet(EntityTypeViewModel vm)
        {
            vm.AssociatedMetadataSets.Add(vm.SelectedMetadataSets);
            //  vm.MetadataSetMappingSrc.Add(vm.SelectedMetadataSets);

            CFMetadataSet metadataSet = MetadataService.GetMetadataSet(vm.SelectedMetadataSets.Id);

            //update attribute mapping
            if (metadataSet != null)
            {
                if (!vm.MetadataSetFields.ContainsKey(metadataSet.Id.ToString()))
                {
                    List <string> addList = new List <string>();
                    addList.Add("");
                    addList = addList.Concat((metadataSet.Fields.Select(f => f.Name).ToList())).ToList();
                    vm.MetadataSetFields.Add(metadataSet.Id.ToString(), addList);
                }
            }
            vm.SelectedMetadataSets = new MetadataSetListItem();
            return(Json(vm));
        }
Exemple #19
0
        public void UpdateMetadataSetTest()
        {
            MetadataService ms           = mDh.Ms;
            string          originalName = "First Name";
            string          newName      = "Second Name";

            string originalDescription = "Original Description";
            string newDescription      = "New description";

            int fieldCount = 4;
            IEnumerable <FormField> fields = CreateFormFields(fieldCount);

            CFMetadataSet check = CreateMetadata(originalName, originalDescription, new FormField[] { });

            mDh.Db.SaveChanges();

            CFMetadataSet result = ms.GetMetadataSet(check.Id);

            Assert.AreEqual(originalName, check.Name);
            Assert.AreEqual(originalDescription, check.Description);
            Assert.AreEqual(0, check.Fields.Count);

            result.Name        = newName;
            result.Description = newDescription;
            result.Fields      = new List <FormField>(fields);
            result.Serialize();

            ms.UpdateMetadataSet(result);
            mDh.Db.SaveChanges();

            result = ms.GetMetadataSet(check.Id);

            Assert.AreEqual(newName, check.Name);
            Assert.AreEqual(newDescription, check.Description);
            Assert.AreEqual(fieldCount, check.Fields.Count);
        }
Exemple #20
0
        public void CanCreateSolrDictionary()
        {
            const string     entityTypeName    = "Entity type name";
            const double     numericValue      = 42.0;
            MockEntity       MockEntityChanges = new MockEntity();
            List <FormField> formFields        = new List <FormField>();

            TextValue stringTextValue = new TextValue();

            stringTextValue.LanguageCode = "en";
            stringTextValue.Value        = "Forty two";
            TextField textField = new TextField();

            textField.Name = "Text";
            textField.SetTextValues(new List <TextValue>()
            {
                stringTextValue
            });
            formFields.Add(textField);

            TextValue numericTextValue = new TextValue();

            numericTextValue.LanguageCode = "en";
            numericTextValue.Value        = numericValue.ToString();

            TextField numericField = new TextField();

            numericField.Name = "Numeric";
            numericField.SetTextValues(new List <TextValue>()
            {
                numericTextValue
            });
            formFields.Add(numericField);


            // Add two options and select second

            OptionsField optionsField = new OptionsField();

            optionsField.Name = "Options field";

            TextValue nonSelectedValue = new TextValue();

            nonSelectedValue.LanguageCode = "en";
            nonSelectedValue.Value        = "Not selected";

            TextValue selectedValue = new TextValue();

            selectedValue.LanguageCode = "en";
            selectedValue.Value        = "Selected";

            Option notSelectedOption = new Option();

            notSelectedOption.Selected = false;
            notSelectedOption.Value.Add(nonSelectedValue);

            Option selectedOption = new Option();

            selectedOption.Selected = true;
            selectedOption.Value.Add(selectedValue);

            List <Option> optList = new List <Option>();

            optList.Add(notSelectedOption);
            optList.Add(selectedOption);

            optionsField.Options = optList;

            formFields.Add(optionsField);

            CFMetadataSet metadataSet = CreateMetadataSet(
                mDh,
                "name metadataset",
                "description metadataset",
                formFields);

            CFEntityType entityType = CreateEntityType(mDh,
                                                       entityTypeName,
                                                       "description entity type",
                                                       "Items",
                                                       metadataSet
                                                       );

            MockEntity.EntityType        = entityType;
            MockEntityChanges.EntityType = entityType;
            //MockEntityChanges.MetadataSets
            MockEntity.MetadataSets = new List <CFMetadataSet>()
            {
                metadataSet
            };
            MockEntityChanges.MetadataSets = new List <CFMetadataSet>()
            {
                metadataSet
            };
            MockEntityChanges.MetadataSets[0].UpdateValueFields(formFields);

            MockEntity.UpdateValues(MockEntityChanges);

            // set vales

            //MockEntity.MetadataSets[0].UpdateValueFields(test);


            Dictionary <string, object> solrDictionary =
                MockEntity.ToSolrDictionary();

            string metadataSetGuid  = metadataSet.MappedGuid.ToString().Replace("-", "_");
            string textFieldGuid    = textField.MappedGuid.ToString().Replace("-", "_");
            string numericFieldGuid = numericField.MappedGuid.ToString().Replace("-", "_");
            string optionFieldGuid  = optionsField.MappedGuid.ToString().Replace("-", "_");

            Dictionary <string, object> expectedResult = new Dictionary <string, object>()
            {
                { "id", solrDictionary["id"] },
                { "modeltype_s", MockEntity.GetType().Name },
                { "entitytype_s", entityTypeName },
                { $@"name_{metadataSetGuid}_{textFieldGuid}_txt_en",
                  textField.Name },
                { $@"name_{metadataSetGuid}_{numericFieldGuid}_txt_en",
                  numericField.Name },
                { $@"name_{metadataSetGuid}_{optionFieldGuid}_txt_en",
                  optionsField.Name },
                { $@"value_{metadataSetGuid}_{textFieldGuid}_txts_en",
                  new List <string>()
                  {
                      textField.Values[0].Value
                  } },
                { $@"value_{metadataSetGuid}_{numericFieldGuid}_txts_en",
                  new List <string>()
                  {
                      numericValue.ToString()
                  } },
                { $@"value_{metadataSetGuid}_{numericFieldGuid}_ds",
                  new List <decimal>()
                  {
                      (decimal)numericValue
                  } },
                { $@"value_{metadataSetGuid}_{numericFieldGuid}_is",
                  new List <int>()
                  {
                      (int)numericValue
                  } },
                { $@"value_{metadataSetGuid}_{optionFieldGuid}_txts_en",
                  new List <string>()
                  {
                      selectedValue.Value
                  } }
            };

            foreach (KeyValuePair <string, object> entry in expectedResult)
            {
                Assert.AreEqual(entry.Value, solrDictionary[entry.Key]);
            }
        }
Exemple #21
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
            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 override void UpdateDataModel(object dataModel, CatfishDbContext db)
        {
            CFEntityType model = dataModel as CFEntityType;

            model.Name        = Name;
            model.Description = Description;

            //Mr jan 15 2018

            var TargetTypesList = new List <CFEntityType.eTarget>();

            for (int i = 0; i < TargetType.Count; ++i)
            {
                if (TargetType[i])
                {
                    TargetTypesList.Add((CFEntityType.eTarget)i);
                }
            }
            model.TargetTypesList = TargetTypesList;

            List <int> dataModelMetadataSetIds = model.MetadataSets.Select(m => m.Id).ToList();
            List <int> viewModelMetadataSetIds = AssociatedMetadataSets.Select(m => m.Id).ToList();

            //Removing metadata sets that are already associated with the data model but not with the view model
            foreach (int id in dataModelMetadataSetIds)
            {
                if (!viewModelMetadataSetIds.Contains(id))
                {
                    model.MetadataSets.Remove(model.MetadataSets.Where(m => m.Id == id).FirstOrDefault());
                }
            }

            //Adding metadata sets that are in the view model but not in the data model to the data model.
            foreach (int id in viewModelMetadataSetIds)
            {
                if (!dataModelMetadataSetIds.Contains(id))
                {
                    CFMetadataSet ms = db.MetadataSets.Where(s => s.Id == id).FirstOrDefault();
                    if (ms != null)
                    {
                        model.MetadataSets.Add(ms);
                    }
                }
            }

            // Remvoe all the missing attribute mappings.
            model.AttributeMappings.Clear();
            foreach (var map in AttributeMappings)
            {
                CFEntityTypeAttributeMapping attrMapping = new CFEntityTypeAttributeMapping
                {
                    Id            = map.Id,
                    Name          = map.Name,
                    FieldName     = map.Field,
                    MetadataSetId = map.MetadataSetFieldId,
                    Label         = map.Label,
                    Deletable     = map.Deletable
                };

                if (map.Id > 0)
                {
                    CFEntityTypeAttributeMapping oldAttrMapping = db.EntityTypeAttributeMappings.Find(map.Id);

                    db.Entry(oldAttrMapping).CurrentValues.SetValues(attrMapping);

                    attrMapping = oldAttrMapping;
                }

                model.AttributeMappings.Add(attrMapping);
            }
        }
Exemple #23
0
 public EntityViewModel(CFMetadataSet src)
 {
     Id    = src.Id;
     Label = src.GetName();
 }
Exemple #24
0
        public void Import(Ingestion ingestion)
        {
#if DEBUG
            Console.Error.WriteLine("Starting ingestion of Ingestion object.");
#endif

            //create new GUID and new EntityType-Id
            Dictionary <string, string> GuidMap = new Dictionary <string, string>();
            Dictionary <int, int>       IdMap   = new Dictionary <int, int>();


            foreach (CFMetadataSet ms in ingestion.MetadataSets)
            {
                string oldId   = ms.Guid;
                string newGuid = NewGuid();
                var    meta    = ms;
                if (ingestion.Overwrite)
                {
                    //use whatever GUID in the file
                    //check to make sure the GUID in not existing in the Db, if it's replace the one in the database
                    GuidMap.Add(oldId, oldId); //if overwrite is true use existing GUID
                }
                else
                {
                    GuidMap.Add(oldId, newGuid);
                }
                //GuidMap = buildMapGuid(ingestion.Overwrite, oldId);
                ms.Guid       = GuidMap[oldId];
                ms.MappedGuid = GuidMap[oldId];

                ms.Serialize();
                //meta.Serialize();
                if (ingestion.Overwrite)
                {
                    //check if the metadataset with this guid is existed in the database
                    //if yes, replace the one in th edatabase with this one from the file
                    CFMetadataSet metadataSet = Db.MetadataSets.Where(m => m.MappedGuid == ms.Guid).FirstOrDefault();
                    if (metadataSet != null)
                    {
                        metadataSet = ms;
                        Db.Entry(metadataSet).State = System.Data.Entity.EntityState.Modified;
                    }
                    else
                    {
                        Db.MetadataSets.Add(ms);
                    }
                }
                else
                {
                    try
                    {
                        Db.MetadataSets.Add(ms);
                    }catch (ProviderIncompatibleException ex)
                    {
                        Console.Error.WriteLine("Current Connection String: " + Db.Database.Connection.ConnectionString);

                        throw ex;
                    }
                }
            }

            Db.SaveChanges();
            //add entityType
            foreach (CFEntityType et in ingestion.EntityTypes)
            {
                int oldId = et.Id;

                //I think below is not necessary since the DeserialiseEntytyType mtehod already grabbing the right metadataset

                List <CFMetadataSet> newSets = new List <CFMetadataSet>();
                foreach (CFMetadataSet set in et.MetadataSets)
                {
                    string mGuid = set.Guid;
                    if (GuidMap.ContainsKey(mGuid))
                    {
                        mGuid = GuidMap[mGuid];
                    }

                    CFMetadataSet dbSet = Db.MetadataSets.Where(s => s.MappedGuid == mGuid).FirstOrDefault();
                    newSets.Add(dbSet);
                }
                et.MetadataSets.Clear();
                et.MetadataSets = newSets;

                foreach (CFEntityTypeAttributeMapping mapping in et.AttributeMappings)
                {
                    string mGuid = mapping.MetadataSet.Guid;
                    if (GuidMap.ContainsKey(mGuid))
                    {
                        mGuid = GuidMap[mGuid];
                    }
                    mapping.MetadataSet = Db.MetadataSets.Where(m => m.MappedGuid == mGuid).FirstOrDefault();
                }

                CFEntityType newEt = null;
                if (ingestion.Overwrite)
                {
                    CFEntityType oldEt = Db.EntityTypes.Where(e => e.Name == et.Name).FirstOrDefault();
                    if (oldEt != null)
                    {
                        //modified it --replace with current value
                        oldEt = et;
                        Db.Entry(oldEt).State = System.Data.Entity.EntityState.Modified;
                    }
                    else
                    {
                        newEt = Db.EntityTypes.Add(et);
                    }
                }
                else
                {
                    newEt = Db.EntityTypes.Add(et);
                }
                Db.SaveChanges();
                IdMap.Add(oldId, newEt.Id);
            }

            //add aggregations
            GuidMap.Clear();
            int completed = 0;
            int failed    = 0;
            IDictionary <Type, Func <IngestionService, CFXmlModel, CFAggregation> > createAggrigationCache = new Dictionary <Type, Func <IngestionService, CFXmlModel, CFAggregation> >();
            Func <CFXmlModel, CFAggregation> getNewAggregation = agg =>
            {
                Type t = agg.GetType();

                try
                {
                    return(createAggrigationCache[t](this, agg));
                }
                catch (KeyNotFoundException ex)
                {
                    MethodInfo method    = this.GetType().GetMethod("CreateAggregation");
                    MethodInfo genMethod = method.MakeGenericMethod(t);
                    Func <IngestionService, CFXmlModel, CFAggregation> func = (Func <IngestionService, CFXmlModel, CFAggregation>)Delegate.CreateDelegate(typeof(Func <IngestionService, CFXmlModel, CFAggregation>), genMethod);
                    createAggrigationCache.Add(t, func);
                    Console.WriteLine("Exception : {0}", ex.Message);
                    return(func(this, agg));
                }
            };

            ingestion.Aggregations.ForEach((agg) => {
                Regex guidRegex = new Regex(@"(guid)=[""']?((?:.(?![""']?\s + (?:\S +)=|[> ""']))+.)[""']?");
                string oldId    = guidRegex.Match(agg.Content).Groups[2].Value;
                string newGuid  = NewGuid();

                //saving the aggregation object
                if (ingestion.Overwrite)
                {
                    //use whatever GUID in the file
                    //check to make sure the GUID in not existing in the Db, if it's replace the one in the database
                    GuidMap.Add(oldId, oldId); //if overwrite is true use existing GUID

                    CFAggregation _aggregation = Db.XmlModels.Where(a => a.MappedGuid == oldId).FirstOrDefault() as CFAggregation;
                    if (_aggregation != null)
                    {
                        _aggregation = (CFAggregation)agg;
                        Db.Entry(_aggregation).State = System.Data.Entity.EntityState.Modified;
                    }
                    else
                    {
                        var _agg = getNewAggregation(agg);
                        Db.Entities.Add(_agg);
                    }
                }
                else
                {
                    agg.Guid       = newGuid;
                    agg.MappedGuid = newGuid;

                    GuidMap.Add(oldId, newGuid);

                    var _agg = getNewAggregation(agg);

                    try
                    {
                        Db.Entities.Add(_agg);
                    }catch (Exception ex)
                    {
#if DEBUG
                        Console.Error.WriteLine("{0} Error reading aggrigation: {1}", ex.Message, _agg.Name);
#endif
                        return(false);
                    }
                }

                return(true);
            }, (successCount, failCount) =>
            {
                completed += successCount;
                failed    += failCount;

#if DEBUG
                Console.Error.WriteLine("{0} Completed, {1} Failed", completed, failed);
#endif

                Db.SaveChanges();
                return(true);
            });

            Db.SaveChanges();
            foreach (var rel in ingestion.Relationships)
            {
                string pGuid = rel.ParentRef;
                if (GuidMap.ContainsKey(pGuid))
                {
                    pGuid = GuidMap[pGuid];
                }

                string cGuid = rel.ChildRef;
                if (GuidMap.ContainsKey(cGuid))
                {
                    cGuid = GuidMap[cGuid];
                }

                rel.ParentRef = pGuid;
                rel.ChildRef  = cGuid;
                if (rel.IsMember)
                {
                    AddMember(rel.ParentRef, rel.ChildRef);
                }

                if (rel.IsRelated)
                {
                    //save it in Aggregation has related
                    AddRelatedMember(rel.ParentRef, rel.ChildRef);
                }
            }
        }
Exemple #25
0
        public void Import(Ingestion ingestion)
        {
            //create new GUID and new EntityType-Id
            Dictionary <string, string> GuidMap = new Dictionary <string, string>();
            Dictionary <int, int>       IdMap   = new Dictionary <int, int>();


            foreach (CFMetadataSet ms in ingestion.MetadataSets)
            {
                string oldId   = ms.Guid;
                string newGuid = NewGuid();
                var    meta    = ms;
                if (ingestion.Overwrite)
                {
                    //use whatever GUID in the file
                    //check to make sure the GUID in not existing in the Db, if it's replace the one in the database
                    GuidMap.Add(oldId, oldId); //if overwrite is true use existing GUID
                }
                else
                {
                    GuidMap.Add(oldId, newGuid);
                }
                //GuidMap = buildMapGuid(ingestion.Overwrite, oldId);
                ms.Guid       = GuidMap[oldId];
                ms.MappedGuid = GuidMap[oldId];

                ms.Serialize();
                //meta.Serialize();
                if (ingestion.Overwrite)
                {
                    //check if the metadataset with this guid is existed in the database
                    //if yes, replace the one in th edatabase with this one from the file
                    CFMetadataSet metadataSet = Db.MetadataSets.Where(m => m.MappedGuid == ms.Guid).FirstOrDefault();
                    if (metadataSet != null)
                    {
                        metadataSet = ms;
                        Db.Entry(metadataSet).State = System.Data.Entity.EntityState.Modified;
                    }
                    else
                    {
                        Db.MetadataSets.Add(ms);
                    }
                }
                else
                {
                    Db.MetadataSets.Add(ms);
                }
            }

            Db.SaveChanges();
            //add entityType
            foreach (CFEntityType et in ingestion.EntityTypes)
            {
                int oldId = et.Id;

                //I think below is not necessary since the DeserialiseEntytyType mtehod already grabbing the right metadataset

                List <CFMetadataSet> newSets = new List <CFMetadataSet>();
                foreach (CFMetadataSet set in et.MetadataSets)
                {
                    string mGuid = set.Guid;
                    if (GuidMap.ContainsKey(mGuid))
                    {
                        mGuid = GuidMap[mGuid];
                    }

                    CFMetadataSet dbSet = Db.MetadataSets.Where(s => s.MappedGuid == mGuid).FirstOrDefault();
                    newSets.Add(dbSet);
                }
                et.MetadataSets.Clear();
                et.MetadataSets = newSets;

                foreach (CFEntityTypeAttributeMapping mapping in et.AttributeMappings)
                {
                    string mGuid = mapping.MetadataSet.Guid;
                    if (GuidMap.ContainsKey(mGuid))
                    {
                        mGuid = GuidMap[mGuid];
                    }
                    mapping.MetadataSet = Db.MetadataSets.Where(m => m.MappedGuid == mGuid).FirstOrDefault();
                }

                CFEntityType newEt = null;
                if (ingestion.Overwrite)
                {
                    CFEntityType oldEt = Db.EntityTypes.Where(e => e.Name == et.Name).FirstOrDefault();
                    if (oldEt != null)
                    {
                        //modified it --replace with current value
                        oldEt = et;
                        Db.Entry(oldEt).State = System.Data.Entity.EntityState.Modified;
                    }
                    else
                    {
                        newEt = Db.EntityTypes.Add(et);
                    }
                }
                else
                {
                    newEt = Db.EntityTypes.Add(et);
                }
                Db.SaveChanges();
                IdMap.Add(oldId, newEt.Id);
            }

            //add aggregations
            GuidMap.Clear();
            foreach (var agg in ingestion.Aggregations)
            {
                string oldId   = agg.Guid;
                string newGuid = NewGuid();

                if (ingestion.Overwrite)
                {
                    //use whatever GUID in the file
                    //check to make sure the GUID in not existing in the Db, if it's replace the one in the database
                    GuidMap.Add(oldId, oldId); //if overwrite is true use existing GUID
                }
                else
                {
                    agg.Guid       = newGuid;
                    agg.MappedGuid = newGuid;

                    GuidMap.Add(oldId, newGuid);
                }

                //saving the aggregation object
                if (ingestion.Overwrite)
                {
                    CFAggregation _aggregation = Db.XmlModels.Where(a => a.MappedGuid == newGuid).FirstOrDefault() as CFAggregation;
                    if (_aggregation != null)
                    {
                        _aggregation = (CFAggregation)agg;
                        Db.Entry(_aggregation).State = System.Data.Entity.EntityState.Modified;
                    }
                    else
                    {
                        Type       t         = agg.GetType();
                        MethodInfo method    = this.GetType().GetMethod("CreateAggregation");
                        MethodInfo genMethod = method.MakeGenericMethod(t);
                        var        _agg      = (CFAggregation)genMethod.Invoke(this, new object[] { agg });
                        Db.Entities.Add(_agg);
                    }
                }
                else
                {
                    Type       t         = agg.GetType();
                    MethodInfo method    = this.GetType().GetMethod("CreateAggregation");
                    MethodInfo genMethod = method.MakeGenericMethod(t);
                    var        _agg      = (CFAggregation)genMethod.Invoke(this, new object[] { agg });
                    Db.Entities.Add(_agg);
                }
            }
            Db.SaveChanges();
            foreach (var rel in ingestion.Relationships)
            {
                string pGuid = rel.ParentRef;
                if (GuidMap.ContainsKey(pGuid))
                {
                    pGuid = GuidMap[pGuid];
                }

                string cGuid = rel.ChildRef;
                if (GuidMap.ContainsKey(cGuid))
                {
                    cGuid = GuidMap[cGuid];
                }

                rel.ParentRef = pGuid;
                rel.ChildRef  = cGuid;
                if (rel.IsMember)
                {
                    AddMember(rel.ParentRef, rel.ChildRef);
                }

                if (rel.IsRelated)
                {
                    //save it in Aggregation has related
                    AddRelatedMember(rel.ParentRef, rel.ChildRef);
                }
            }
        }
Exemple #26
0
        private Ingestion DeserializeEntityTypes(XElement element)
        {
            foreach (XElement entityElement in element.Elements())
            {
                CFEntityType entityType = new CFEntityType();
                entityType.Id = entityElement.Attribute("id") == null ? 0 : int.Parse(entityElement.Attribute("id").Value);

                foreach (XElement child in entityElement.Elements())
                {
                    string name = child.Name.LocalName;

                    if (name == "name")
                    {
                        entityType.Name = child.Value;
                    }
                    else if (name == "description")
                    {
                        entityType.Description = child.Value;
                    }
                    else if (name == "target-type")
                    {
                        entityType.TargetTypes = child.Value;
                    }
                    else if (name == "metadata-sets")
                    {
                        foreach (XElement metadata in child.Elements())
                        {
                            if (metadata.Name.LocalName == "metadata-set")
                            {
                                //MetadataSet set = MetadataSets.Where(m => m.Guid == metadata.Attribute("ref").Value).FirstOrDefault();
                                //if (set == null)
                                //{
                                CFMetadataSet set = new CFMetadataSet();
                                set.Guid = metadata.Attribute("ref").Value;
                                set.Id   = -1;
                                //}

                                entityType.MetadataSets.Add(set);

                                foreach (XElement attrElement in metadata.Elements())
                                {
                                    if (attrElement.Name.LocalName == "attribute-mapping")
                                    {
                                        CFEntityTypeAttributeMapping mapping = new CFEntityTypeAttributeMapping();
                                        mapping.MetadataSet = set;

                                        foreach (XElement attrChild in attrElement.Elements())
                                        {
                                            string attrName = attrChild.Name.LocalName;
                                            if (attrName == "name")
                                            {
                                                mapping.Name = attrChild.Value;
                                            }
                                            else if (attrName == "field-name")
                                            {
                                                mapping.FieldName = attrChild.Value;
                                            }
                                            else if (attrName == "label")
                                            {
                                                //When we implement the new mappings in a later sprint, we will need this field.
                                            }
                                        }

                                        entityType.AttributeMappings.Add(mapping);
                                    }
                                }
                            }
                        }
                    }
                }

                EntityTypes.Add(entityType);
            }

            return(this);
        }