BExISMigration.AttributeCreator.CreateAttributes C# (CSharp) Method

CreateAttributes() public method

public CreateAttributes ( DataTable &mappedAttributes ) : void
mappedAttributes System.Data.DataTable
return void
        public void CreateAttributes(ref DataTable mappedAttributes)
        {
            foreach (DataRow mapAttributesRow in mappedAttributes.Rows)
            {
                DataContainerManager attributeManager = new DataContainerManager();
                DataTypeManager dataTypeManager = new DataTypeManager();
                UnitManager unitManager = new UnitManager();
                DataAttribute attribute = new DataAttribute();

                // values of the attribute
                attribute.ShortName = mapAttributesRow["ShortName"].ToString();
                attribute.Name = mapAttributesRow["Name"].ToString();
                attribute.Description = mapAttributesRow["Description"].ToString();
                attribute.IsMultiValue = false;
                attribute.IsBuiltIn = false;
                //attribute.Owner = "BMM";
                attribute.Scope = "";
                attribute.MeasurementScale = MeasurementScale.Categorial; ////////!!!!!!!!fromMapping??????????????????
                attribute.ContainerType = DataContainerType.ReferenceType;
                attribute.EntitySelectionPredicate = "";
                attribute.DataType = dataTypeManager.Repo.Get(Convert.ToInt64(mapAttributesRow["DataTypeId"]));
                attribute.Unit = unitManager.Repo.Get(Convert.ToInt64(mapAttributesRow["UnitId"]));
                attribute.Methodology = null;
                attribute.Classification = null;
                attribute.AggregateFunctions = null;
                attribute.GlobalizationInfos = null;
                attribute.Constraints = null;
                attribute.ExtendedProperties = null;

                DataAttribute dataAttribute = new DataAttribute();
                DataAttribute existAttribute = attributeManager.DataAttributeRepo.Get(a =>
                    attribute.Name.Equals(a.Name) &&
                    attribute.ShortName.Equals(a.ShortName) &&
                    attribute.Unit.Id.Equals(a.Unit.Id) &&
                    attribute.DataType.Id.Equals(a.DataType.Id)
                    ).FirstOrDefault();

                // if attribute not exists (name, shortName) then create
                if (existAttribute == null)
                {
                    dataAttribute = attributeManager.CreateDataAttribute(
                        attribute.ShortName,
                        attribute.Name,
                        attribute.Description,
                        attribute.IsMultiValue,
                        attribute.IsBuiltIn,
                        attribute.Scope,
                        attribute.MeasurementScale,
                        attribute.ContainerType,
                        attribute.EntitySelectionPredicate,
                        attribute.DataType,
                        attribute.Unit,
                        attribute.Methodology,
                        attribute.Classification,
                        attribute.AggregateFunctions,
                        attribute.GlobalizationInfos,
                        attribute.Constraints,
                        attribute.ExtendedProperties
                        );
                }
                else
                {
                    dataAttribute = existAttribute;
                }

                // add attributeId to the mappedAttributes Table
                mapAttributesRow["AttributeId"] = dataAttribute.Id;
            }
        }

Usage Example

Example #1
0
        public void DataSetTransfer()
        {
            string filePath = AppConfiguration.GetModuleWorkspacePath("BMM");
            //string xsdPath = @"C:\Data\Temp\Administrator";
            string xsdPath = Path.Combine(AppConfiguration.DataPath, "Temp", "Administrator");
            string xsdUserName = "******";
            string researchPlanName = "Research plan";

            MappingReader mappingReader = new MappingReader();
            AttributeCreator attributeCreator = new AttributeCreator();
            MetadataCreator metadataCreator = new MetadataCreator();
            MigrationHelpers migrationHelpers = new MigrationHelpers();

            #region create datatypes, unit, dimensions for datastructure from mapping file
            // read data types from excel mapping file
            DataTable mappedDataTypes = mappingReader.readDataTypes(filePath);
            // create read data types in bpp
            attributeCreator.CreateDataTypes(ref mappedDataTypes);

            // read dimensions from excel mapping file
            DataTable mappedDimensions = mappingReader.readDimensions(filePath);
            // create dimensions in bpp
            attributeCreator.CreateDimensions(ref mappedDimensions);

            // read units from excel mapping file
            DataTable mappedUnits = mappingReader.readUnits(filePath, mappedDimensions);
            // create read units in bpp
            attributeCreator.CreateUnits(ref mappedUnits, mappedDataTypes);

            // read attributes from excel mapping file
            DataTable mappedAttributes = mappingReader.readAttributes(filePath, mappedUnits, mappedDataTypes);
            // Speicher freigeben
            mappedDataTypes.Clear();
            mappedDimensions.Clear();
            // create read attributes in bpp
            attributeCreator.CreateAttributes(ref mappedAttributes);

            // read variables from XLSX or TSV (tab seperated values) mapping file
            //DataTable mappedVariables = mappingReader.readVariables(filePath, mappedAttributes);
            DataTable mappedVariables = mappingReader.readVariablesTSV(filePath, mappedAttributes, mappedUnits);
            // Speicher freigeben
            mappedAttributes.Clear();
            mappedUnits.Clear();

            #endregion

            #region create metadatastructure and prepare a xmltemplate based on the structure

            // if not existing: import METADATA structure, using file "schema.xsd" similar to bexis1 schema
            long metadataStructureId = metadataCreator.importMetadataStructure(xsdPath, xsdUserName);
            // create BppmetadataXmlTemplate to get XmlAttributes
            XmlDocument metadataXmlTemplate = BExIS.Xml.Helpers.XmlMetadataWriter.ToXmlDocument(metadataCreator.createXmlTemplate(metadataStructureId));

            #endregion

            #region create Dataset(Metadata,Datastructure)
            // the dataStructure, metadata and dataset of these bexis1 datasets will be transfered
            DataStructureCreator dataStructureCreator = new DataStructureCreator();
            DatasetCreator datasetCreator = new DatasetCreator();
            List<string> datasetIds = migrationHelpers.getDatasets(filePath);
            foreach (string dataSetID in datasetIds)
            {
                dataStructureCreator = new DataStructureCreator();
                datasetCreator = new DatasetCreator();
                XmlDocument metadataXml = new XmlDocument();
                List<string> variableNames = new List<string>();
                string fileType = "";

                // get metadata from Bexis1 DB and restructure it for Bpp using xml-mapping file
                metadataXml = metadataCreator.createMetadata(dataSetID, filePath, DataBase, ref variableNames, ref fileType);

                //long dataStructureId = -1;
                BExIS.Dlm.Entities.DataStructure.DataStructure dataStructure = null;
                if (fileType == "structuredData" || fileType == "lookupTable")
                {
                    // create dataStructure on bpp
                    dataStructure = dataStructureCreator.CreateDataStructure(dataSetID, mappedVariables, variableNames);
                }
                else if (fileType.Length > 0)
                {
                    // create unstructured dataStructure on bpp
                    dataStructure = dataStructureCreator.CreateDataStructure(fileType);
                }

                long datasetId = -1;
                if (dataStructure.Id > 0)
                {
                    datasetId = datasetCreator.DatasetExistsByOldDatasetId(dataSetID, dataStructure.Id);
                }

                if (datasetId <= 0) // dataset not exists => create dataset
                {
                    // integrate BppXmlAttributes by using metadataXmlTemplate
                    metadataXml = metadataCreator.fillInXmlAttributes(metadataXml, metadataXmlTemplate);

                    // create dataset with Bexis1_metadata
                    datasetId = datasetCreator.createDataset(dataSetID, metadataXml, metadataStructureId, dataStructure, researchPlanName);
                }

                string DatasetMappingPath = Path.Combine(AppConfiguration.DataPath, "DatasetMapping.txt");
                string[] DatasetMappingtexts = File.ReadAllLines(DatasetMappingPath);
                string DatasetMappingtext = DatasetMappingtexts.FirstOrDefault(item => item.Split('\t')[0] == dataSetID);
                //to prevent of repeating data
                if (DatasetMappingtext != null)
                    File.WriteAllLines(DatasetMappingPath, DatasetMappingtexts.Where(item => item != DatasetMappingtext));
                File.AppendAllText(DatasetMappingPath, dataSetID + "\t" + datasetId + "\r\n");

            }
            #endregion
        }