Ejemplo n.º 1
0
        //private StepModelHelper GetStepModelhelper(long usageId, int number)
        //{
        //    TaskManager = (CreateTaskmanager)Session["CreateDatasetTaskmanager"];
        //    if (TaskManager.Bus.ContainsKey(CreateTaskmanager.METADATA_STEP_MODEL_HELPER))
        //    {
        //        return ((List<StepModelHelper>)TaskManager.Bus[CreateTaskmanager.METADATA_STEP_MODEL_HELPER]).Where(s => s.Usage.Id.Equals(usageId) && s.Number.Equals(number)).FirstOrDefault();
        //    }
        //    return null;
        //}
        //private int GetNumberOfUsageInStepModelHelper(long usageId)
        //{
        //    TaskManager = (CreateTaskmanager)Session["CreateDatasetTaskmanager"];
        //    if (TaskManager.Bus.ContainsKey(CreateTaskmanager.METADATA_STEP_MODEL_HELPER))
        //    {
        //        return ((List<StepModelHelper>)TaskManager.Bus[CreateTaskmanager.METADATA_STEP_MODEL_HELPER]).Where(s => s.Usage.Id.Equals(usageId)).Count() - 1;
        //    }
        //    return 0;
        //}
        //private void GenerateModelsForChildrens(StepModelHelper modelHelper, long metadataStructureId)
        //{
        //    foreach (StepModelHelper item in modelHelper.Childrens)
        //    {
        //        if (item.Childrens.Count() > 0)
        //        {
        //            GenerateModelsForChildrens(item, metadataStructureId);
        //        }
        //        if (item.Model == null)
        //        {
        //            BaseUsage u = LoadUsage(item.Usage);
        //            if (u is MetadataAttributeUsage || u is MetadataNestedAttributeUsage)
        //            {
        //                item.Model = MetadataCompoundAttributeModel.ConvertToModel(u, item.Number);
        //                ((MetadataCompoundAttributeModel)item.Model).ConvertMetadataAttributeModels(u, metadataStructureId, item.StepId);
        //            }
        //            if (u is MetadataPackageUsage)
        //            {
        //                item.Model = MetadataPackageModel.Convert(u, item.Number);
        //                ((MetadataPackageModel)item.Model).ConvertMetadataAttributeModels(u, metadataStructureId, item.StepId);
        //            }
        //        }
        //    }
        //}
        //private StepModelHelper GenerateModelsForChildrens(StepInfo stepInfo, long metadataStructureId)
        //{
        //    StepModelHelper stepModelHelper = GetStepModelhelper(stepInfo.Id);
        //    if (stepModelHelper.Model == null)
        //    {
        //        if (stepModelHelper.Usage is MetadataPackageUsage)
        //            stepModelHelper.Model = CreatePackageModel(stepInfo.Id, false);
        //        if (stepModelHelper.Usage is MetadataNestedAttributeUsage)
        //            stepModelHelper.Model = CreateCompoundModel(stepInfo.Id, false);
        //        getChildModelsHelper(stepModelHelper);
        //    }
        //    return stepModelHelper;
        //}
        private BaseUsage LoadUsage(BaseUsage usage)
        {
            if (usage is MetadataPackageUsage)
            {
                MetadataStructureManager msm = new MetadataStructureManager();
                return msm.PackageUsageRepo.Get(usage.Id);
            }

            if (usage is MetadataNestedAttributeUsage)
            {
                MetadataAttributeManager mam = new MetadataAttributeManager();

                var x = from c in mam.MetadataCompoundAttributeRepo.Get()
                        from u in c.Self.MetadataNestedAttributeUsages
                        where u.Id == usage.Id //&& c.Id.Equals(parentId)
                        select u;

                return x.FirstOrDefault();
            }

            if (usage is MetadataAttributeUsage)
            {
                MetadataPackageManager mpm = new MetadataPackageManager();

                var q = from p in mpm.MetadataPackageRepo.Get()
                        from u in p.MetadataAttributeUsages
                        where u.Id == usage.Id // && p.Id.Equals(parentId)
                        select u;

                return q.FirstOrDefault();
            }

            return usage;
        }
Ejemplo n.º 2
0
        private MetadataPackageModel CreatePackageModel(int stepId, bool validateIt)
        {
            StepInfo stepInfo = TaskManager.Get(stepId);
            StepModelHelper stepModelHelper = GetStepModelhelper(stepId);

            long metadataPackageId = stepModelHelper.Usage.Id;
            long metadataStructureId = Convert.ToInt64(TaskManager.Bus[CreateTaskmanager.METADATASTRUCTURE_ID]);

            MetadataStructureManager mdsManager = new MetadataStructureManager();
            MetadataPackageManager mdpManager = new MetadataPackageManager();
            MetadataPackageUsage mpu = (MetadataPackageUsage)LoadUsage(stepModelHelper.Usage);
            MetadataPackageModel model = new MetadataPackageModel();

            model = MetadataPackageModel.Convert(mpu, stepModelHelper.Number);
            model.ConvertMetadataAttributeModels(mpu, metadataStructureId, stepId);

            if (stepInfo.IsInstanze == false)
            {
                //get Instance
                if (TaskManager.Bus.ContainsKey(CreateTaskmanager.METADATA_XML))
                {
                    XDocument xMetadata = (XDocument)TaskManager.Bus[CreateTaskmanager.METADATA_XML];
                    model.ConvertInstance(xMetadata, stepModelHelper.XPath);
                }
            }
            else
            {
                if (stepModelHelper.Model != null)
                {
                    model = (MetadataPackageModel)stepModelHelper.Model;
                }
                else
                {
                    stepModelHelper.Model = model;
                }
            }

            //if (validateIt)
            //{
            //    //validate packages
            //    List<Error> errors = validateStep(stepModelHelper.Model);
            //    if (errors != null)
            //        model.ErrorList = errors;
            //    else
            //        model.ErrorList = new List<Error>();

            //}

            model.StepInfo = stepInfo;

            return model;
        }
Ejemplo n.º 3
0
        private void testMetadataStructure()
        {
            MetadataStructureManager mdsManager = new MetadataStructureManager();
            MetadataPackageManager mdpManager = new MetadataPackageManager();
            MetadataAttributeManager mdaManager = new MetadataAttributeManager();

            MetadataStructure root = mdsManager.Repo.Get(p => p.Name == "Root").FirstOrDefault();
            if (root == null) root = mdsManager.Create("Root", "This is the root metadata structure", "", "", null);

            MetadataStructure s1 = mdsManager.Repo.Get(p => p.Name == "S1").FirstOrDefault();
            if (s1 == null) s1 = mdsManager.Create("S1", "This is S1 metadata structure", "", "", root);

            MetadataStructure s11 = mdsManager.Repo.Get(p => p.Name == "S1.1").FirstOrDefault();
            if (s11 == null) s11 = mdsManager.Create("S1.1", "This is S1.1 metadata structure", "", "", s1);

            MetadataStructure s2 = mdsManager.Repo.Get(p => p.Name == "S2").FirstOrDefault();
            if (s2 == null) s2 = mdsManager.Create("S2", "This is S2 metadata structure", "", "", root);

            MetadataPackage p1 = mdpManager.MetadataPackageRepo.Get(p => p.Name == "P1").FirstOrDefault();
            if (p1 == null) p1 = mdpManager.Create("P1", "Sample Package 1", true);

            MetadataPackage p2 = mdpManager.MetadataPackageRepo.Get(p => p.Name == "P2").FirstOrDefault();
            if (p2 == null) p2 = mdpManager.Create("P2", "Sample Package 2", true);

            MetadataPackage p3 = mdpManager.MetadataPackageRepo.Get(p => p.Name == "P3").FirstOrDefault();
            if (p3 == null) p3 = mdpManager.Create("P3", "Sample Package 3", true);

            MetadataPackage p4 = mdpManager.MetadataPackageRepo.Get(p => p.Name == "P4").FirstOrDefault();
            if (p4 == null) p4 = mdpManager.Create("P4", "Sample Package 4", true);

            if (s1.MetadataPackageUsages.Where(p => p.MetadataPackage == p1).Count() <= 0)
                mdsManager.AddMetadataPackageUsage(s1, p1, "P1 in S1", "", 0, 1);

            if (s1.MetadataPackageUsages.Where(p => p.MetadataPackage == p2).Count() <= 0)
                mdsManager.AddMetadataPackageUsage(s1, p2, "P2 in S1", "", 1, 1);

            if (s11.MetadataPackageUsages.Where(p => p.MetadataPackage == p3).Count() <= 0)
                mdsManager.AddMetadataPackageUsage(s11, p3, "P3 in S1.1", "", 0, 10);

            if (s11.MetadataPackageUsages.Where(p => p.MetadataPackage == p4).Count() <= 0)
                mdsManager.AddMetadataPackageUsage(s11, p4, "P4 in S1.1", "", 2, 5);

            var usages = mdsManager.GetEffectivePackages(3);
        }
Ejemplo n.º 4
0
        public long GenerateMetadataStructure(string nameOfStartNode,string schemaName)
        {
            if (!String.IsNullOrEmpty(schemaName))
                SchemaName = schemaName;

            string rootElementName = nameOfStartNode;

            newXsdFilePath = Path.Combine(AppConfiguration.GetModuleWorkspacePath("DCM"), "Metadata", schemaName, FileName);

            #region prepare mappingFiles

            #region intern to extern
                // add schema to mappingfile
                mappingFileInternalToExternal.Header.AddToSchemas(schemaName, "Metadata/" + schemaName + "/" + FileName);
            #endregion

            #region extern to intern
                mappingFileExternalToInternal.Header.AddToDestination("Metadata");
                mappingFileExternalToInternal.Header.AddToSchemas(schemaName, "Metadata/" + schemaName + "/" + FileName);

            // id and name of metadatastructure fehlt

            #endregion

            #endregion

            //List<MetadataAttribute> metadataAttributes = new List<MetadataAttribute>();
            //metadataAttributes = GenerateAllMetadataAttributes();
            List<XmlSchemaElement> elementsWithSimpleType = GetAllElementsTypeIsSimpleType();
            List<XmlSchemaComplexType> complexTypesWithSimpleTypesAsChildrensOnly = GetAllComplextTypesWithSimpleTypesAsChildrens();

            // create default
            MetadataStructureManager mdsManager = new MetadataStructureManager();
            MetadataPackageManager mdpManager = new MetadataPackageManager();

             // create default metadataStructure
            MetadataStructure test = null; //mdsManager.Repo.Get(p => p.Name == SchemaName).FirstOrDefault();
            if (test == null) test = mdsManager.Create(SchemaName, SchemaName, "", "", null);

            XmlSchemaObject root = new XmlSchemaElement();
            string xpathFromRoot = "";

            int count = 0;
            foreach(XmlSchemaObject obj in Schema.Items)
            {
                if (obj is XmlSchemaElement)
                {
                    count++;
                    if (count > 1)
                    {
                        throw new Exception("Root node is not able to declare");
                    }
                }
            }

            foreach(XmlSchemaObject obj in Schema.Items)
            {
                if (obj is XmlSchemaElement)
                    root = (XmlSchemaElement)obj;
            }

            if (String.IsNullOrEmpty(nameOfStartNode))
            {
                XmlSchemaElement rootElement = (XmlSchemaElement)root;
                mappingFileInternalToExternal.Header.AddToDestination(rootElement.Name, rootElement.Name);

                rootElementName = rootElement.Name;
                xpathFromRoot = rootElementName;
            }
            else
            {
                //XXX finde path from rootnode the defined root node
                //XPath in mapping file needs to be complete based on the original xsd
                xpathFromRoot = findPathFromRoot((XmlSchemaElement)root, nameOfStartNode, "");

                root = Elements.Where(e => e.Name.ToLower().Equals(nameOfStartNode.ToLower())).FirstOrDefault();
                if (root == null)
                {
                    root = Groups.Where(g => g.Name.ToLower().Equals(nameOfStartNode.ToLower())).FirstOrDefault();
                }

                if (nameOfStartNode != "")
                {
                    XmlSchemaElement rootElement = (XmlSchemaElement)root;
                    mappingFileInternalToExternal.Header.AddToDestination(nameOfStartNode, rootElement.Name);
                }
            }

                List<XmlSchemaElement> childrenOfRoot = XmlSchemaUtility.GetAllElements(root, false, Elements);
                List<XmlSchemaElement> packages = new List<XmlSchemaElement>();

                if (XmlSchemaUtility.IsAllSimpleType(childrenOfRoot))
                {
                    #region root with only simple type childrens

                    XmlSchemaGroup rootAsGroup = (XmlSchemaGroup)root;

                    MetadataPackage package = getExistingMetadataPackage(rootAsGroup.Name);

                    if (package == null)
                    {
                        package = mdpManager.Create(rootAsGroup.Name, GetDescription(rootAsGroup.Annotation), true);
                        createdPackagesDic.Add(package.Id, package.Name);
                    }

                    if (test.MetadataPackageUsages.Where(p => p.MetadataPackage == package).Count() <= 0)
                    {
                        string xpath = "Metadata/" + rootAsGroup.Name;

                        foreach (XmlSchemaElement child in childrenOfRoot)
                        {
                            //Debug.Writeline("packageChild : " + child.Name);
                            //Debug.Writeline("-->");

                            if (XmlSchemaUtility.IsSimpleType(child))
                            {
                                addMetadataAttributeToMetadataPackageUsage(package, child, xpath, rootAsGroup.Name);
                            }
                            else
                            {
                                List<string> parents = new List<string>();
                                parents.Add(rootAsGroup.Name);

                                MetadataCompoundAttribute compoundAttribute = get(child, parents, xpath, rootAsGroup.Name);

                                // add compound to package
                                addUsageFromMetadataCompoundAttributeToPackage(package, compoundAttribute, child);

                            }

                        }

                        mdsManager.AddMetadataPackageUsage(test, package, rootAsGroup.Name, GetDescription(rootAsGroup.Annotation), 1, 1);
                    }

                    #endregion
                }
                else
                {
                    packages.AddRange(childrenOfRoot);

                    #region packages with complext types

                    #region create a basic package of SimpleAttributes from Root Node

                    // get all simpleTypes
                    // for the main package
                    List<XmlSchemaElement> simpleElements = XmlSchemaUtility.GetAllSimpleElements(packages);
                    string rootNodePackageUsage = "Basic";
                    string rootNodePackage = "BasicType";

                    string rootNodePackageDescription = "Attributes from the root node";

                    if (simpleElements.Count > 0)
                    {
                        MetadataPackage package = getExistingMetadataPackage(rootNodePackage);// = mdpManager.MetadataPackageRepo.Get(p => p.Name == rootNodePackage).FirstOrDefault();
                        if (package == null)
                        {
                            package = mdpManager.Create(rootNodePackage, rootNodePackageDescription, true);
                            createdPackagesDic.Add(package.Id, package.Name);
                        }

                        if (test.MetadataPackageUsages.Where(p => p.MetadataPackage == package).Count() <= 0)
                        {

                            foreach (XmlSchemaElement child in simpleElements)
                            {
                                if (XmlSchemaUtility.IsSimpleType(child))
                                {
                                    addMetadataAttributeToMetadataPackageUsage(package, child,"Metadata/Basic/BasicType", xpathFromRoot);
                                }
                            }

                            mdsManager.AddMetadataPackageUsage(test, package, rootNodePackageUsage, rootNodePackageDescription, 1, 1);
                        }

                    }

                    #endregion

                    #region create Packages
                    List<XmlSchemaElement> otherElements = XmlSchemaUtility.GetAllComplexElements(packages);

                    foreach (XmlSchemaElement element in otherElements)
                    {
                       //Debug.Writeline("package : " + element.Name);
                        //Debug.Writeline("--------------------------");

                        string typeName = GetTypeOfName(element.Name);
                        string rootName = ((XmlSchemaElement)root).Name;

                        string xpathInternal = "Metadata/" + element.Name+"/"+typeName;
                        string xpathExternal = xpathFromRoot+"/" + element.Name;

                        if (!XmlSchemaUtility.IsSimpleType(element))
                        {
                            #region complexType
                            MetadataPackage package = getExistingMetadataPackage(element.Name);// = mdpManager.MetadataPackageRepo.Get(p => p.Name == element.Name).FirstOrDefault();
                            if (package == null)
                            {
                                package = mdpManager.Create(typeName, GetDescription(element.Annotation), true);
                                createdPackagesDic.Add(package.Id, package.Name);
                            }
                            // add package to structure
                            if (test.MetadataPackageUsages != null && test.MetadataPackageUsages.Where(p => p.Label.Equals(element.Name)).Count() > 0)
                            {

                                if (test.MetadataPackageUsages.Where(p => p.MetadataPackage == package).Count() <= 0)
                                {
                                    List<XmlSchemaElement> childrens = XmlSchemaUtility.GetAllElements(element, false, Elements);

                                    foreach (XmlSchemaElement child in childrens)
                                    {
                                       //Debug.Writeline("packageChild : " + child.Name);
                                        //Debug.Writeline("-->");

                                        if (XmlSchemaUtility.IsSimpleType(child))
                                        {
                                            addMetadataAttributeToMetadataPackageUsage(package, child, xpathInternal, xpathExternal);
                                        }
                                        else
                                        {
                                            List<string> parents = new List<string>();
                                            parents.Add(element.Name);

                                            MetadataCompoundAttribute compoundAttribute = get(child, parents, xpathInternal, xpathExternal);

                                            // add compound to package
                                            addUsageFromMetadataCompoundAttributeToPackage(package, compoundAttribute, child);

                                        }

                                    }

                                    int min = 0;

                                    if (element.MinOccurs > int.MinValue)
                                        min = Convert.ToInt32(element.MinOccurs);
                                    else
                                        min = int.MinValue;

                                    int max = 0;
                                    if (element.MaxOccurs < int.MaxValue)
                                        max = Convert.ToInt32(element.MaxOccurs);
                                    else
                                        max = int.MaxValue;

                                    mdsManager.AddMetadataPackageUsage(test, package, element.Name, GetDescription(element.Annotation), min, max);
                                }
                            }
                            else
                            {
                                List<XmlSchemaElement> childrens = XmlSchemaUtility.GetAllElements(element, false, Elements);

                                foreach (XmlSchemaElement child in childrens)
                                {
                                    if (XmlSchemaUtility.IsSimpleType(child))
                                    {
                                        addMetadataAttributeToMetadataPackageUsage(package, child, xpathInternal, xpathExternal);
                                    }
                                    else
                                    {
                                        List<string> parents = new List<string>();
                                        parents.Add(element.Name);

                                        MetadataCompoundAttribute compoundAttribute = get(child, parents, xpathInternal, xpathExternal);

                                        // add compound to package
                                        addUsageFromMetadataCompoundAttributeToPackage(package, compoundAttribute, child);

                                    }

                                }

                                int min = 0;

                                if (element.MinOccurs > int.MinValue)
                                    min = Convert.ToInt32(element.MinOccurs);
                                else
                                    min = int.MinValue;

                                int max = 0;
                                if (element.MaxOccurs < int.MaxValue)
                                    max = Convert.ToInt32(element.MaxOccurs);
                                else
                                    max = int.MaxValue;

                                //check if element is a choice
                                if (!XmlSchemaUtility.IsChoiceType(element))
                                {

                                    MetadataPackageUsage mpu = mdsManager.AddMetadataPackageUsage(test, package,
                                        element.Name, GetDescription(element.Annotation), min, max);
                                }
                                else
                                {
                                    // if mpu is a choice, add a info to extra
                                    MetadataPackageUsage mpu = mdsManager.AddMetadataPackageUsage(test, package,
                                           element.Name, GetDescription(element.Annotation), min, max,
                                           XmlDatasetHelper.AddReferenceToXml(new XmlDocument(), "choice", "true", "elementType", @"extra/type"));
                                }

                            }
                            #endregion
                        }

                    }
                    #endregion

                    #endregion

                }

                if (!File.Exists(newXsdFilePath))
                {
                    checkDirectory(newXsdFilePath);
                    MoveFile(xsdFilePath, newXsdFilePath);
                }

                #region store additionaly xsds

                string tmpDestinationPath = Path.GetDirectoryName(newXsdFilePath);
                string tmpSourcePath = Path.GetDirectoryName(xsdFilePath);

                if (additionalFiles != null)
                {
                    foreach (var filename in additionalFiles.Distinct())
                    {
                        MoveFile(Path.Combine(tmpSourcePath, filename), Path.Combine(tmpDestinationPath, filename));
                    }
                }

                #endregion

            #region Generate Mapping File

            string internalMetadataStructrueName = schemaName;
                mappingFileExternalToInternal.Id = test.Id;

                //generate mapping file Xml Document
                generateXmlMappingFile(mappingFileInternalToExternal, internalMetadataStructrueName, FileName);
                generateXmlMappingFile(mappingFileExternalToInternal, FileName, internalMetadataStructrueName, 1);

                #endregion

                return test.Id;
        }
Ejemplo n.º 5
0
        public static BaseUsage GetSimpleUsageById(BaseUsage parent, long Id)
        {
            BaseUsage usage = new BaseUsage();

            if (parent is MetadataPackageUsage)
            {
                MetadataPackageManager mpm = new MetadataPackageManager();

                var q = from p in mpm.MetadataPackageRepo.Get()
                        from u in p.MetadataAttributeUsages
                        where p.Id.Equals(parent.Id) && u.Id == Id && u.MetadataAttribute.Self is MetadataSimpleAttribute
                        select u;

                if (q != null && q.ToList().Count > 0)
                {
                    return q.FirstOrDefault();
                }
                else return null;
            }

            else
            if (parent is MetadataNestedAttributeUsage)
            {
                MetadataAttributeManager mam = new MetadataAttributeManager();

                MetadataNestedAttributeUsage pUsage = (MetadataNestedAttributeUsage)parent;

                MetadataCompoundAttribute mca = mam.MetadataCompoundAttributeRepo.Get(pUsage.Member.Self.Id);

                var x = from nestedUsage in mca.MetadataNestedAttributeUsages
                        where nestedUsage.Id == Id && nestedUsage.Member.Self is MetadataSimpleAttribute
                        select nestedUsage;

                //var x = from c in mam.MetadataCompoundAttributeRepo.Get()
                //        from u in c.Self.MetadataNestedAttributeUsages
                //        where u.Id.Equals(parent.Id) && u.Member.Self.Id == Id && u.Member.Self is MetadataSimpleAttribute
                //        select u;

                return x.FirstOrDefault();
            }
            else if (parent is MetadataAttributeUsage)
            {
                MetadataAttributeUsage mau = (MetadataAttributeUsage)parent;
                if (mau.MetadataAttribute.Self is MetadataCompoundAttribute)
                {
                    MetadataCompoundAttribute mca = (MetadataCompoundAttribute)mau.MetadataAttribute.Self;
                    return mca.MetadataNestedAttributeUsages.Where(m => m.Id.Equals(Id)).FirstOrDefault();
                }

            }

            return null;
        }
Ejemplo n.º 6
0
        public static BaseUsage GetMetadataAttributeUsageById(long Id)
        {
            BaseUsage usage = new BaseUsage();

            MetadataPackageManager mpm = new MetadataPackageManager();

            var q = from p in mpm.MetadataPackageRepo.Get()
                    from u in p.MetadataAttributeUsages
                    where u.Id == Id // && p.Id.Equals(parentId)
                    select u;

            return q.FirstOrDefault();
        }