Esempio n. 1
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);
        }
Esempio n. 2
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;
        }