public static void exportMappingSet(MappingSet mappingSet, string filePath) { if (mappingSet != null) { using (TextWriter writer = new StreamWriter(filePath, false, System.Text.Encoding.UTF8)) { var csv = new CSV.CsvWriter(writer); csv.Configuration.RegisterClassMap <CSVMappingRecordMap>(); csv.Configuration.Delimiter = ";"; var csvMappingRecords = new List <CSVMappingRecord>(); //create the CSV mapping records foreach (var mapping in mappingSet.mappings) { //create the record var mappingRecord = new CSVMappingRecord(); mappingRecord.sourcePath = ((MappingNode)mapping.source).getMappingPathExportString(); mappingRecord.targetPath = ((MappingNode)mapping.target).getMappingPathExportString(); mappingRecord.mappingLogic = createMappingLogicString(mapping); //add the record to the list csvMappingRecords.Add(mappingRecord); } //write the CSV mapping records to the filename csv.WriteRecords(csvMappingRecords); } } }
public static MappingSet createMappingSet(ElementWrapper sourceRoot, ElementWrapper targetRoot, MappingSettings settings) { //first create the root nodes var sourceRootNode = createNewRootNode(sourceRoot, settings); var targetRootNode = createNewRootNode(targetRoot, settings); //then create the new mappingSet var mappingSet = new MappingSet(sourceRootNode, targetRootNode, settings); return(mappingSet); }
public static void exportMappingSet(MappingSet mappingSet, string filePath) { if (mappingSet != null) { var engine = new FileHelperEngine <CSVMappingRecord>(); List <CSVMappingRecord> csvMappingRecords = new List <CSVMappingRecord>(); //create the CSV mapping records foreach (var mapping in mappingSet.mappings) { //create the record var mappingRecord = new CSVMappingRecord(); mappingRecord.sourcePath = mapping.source.fullMappingPath; mappingRecord.targetPath = mapping.target.fullMappingPath; mappingRecord.mappingLogic = mapping.mappingLogic != null ? mapping.mappingLogic.description : string.Empty; //add the record to the list csvMappingRecords.Add(mappingRecord); } //write the CSV mapping records to the filename engine.WriteFile(filePath, csvMappingRecords); } }
/// <summary> /// import the mapings specified in the file into the given mappingSet /// </summary> /// <param name="mappingSet">the mappingset to import the mappings into</param> /// <param name="filePath">the path to the file containing the mappings</param> public static void importMappings(MappingSet mappingSet, string filePath, Model model) { IEnumerable <CSVMappingRecord> mappingRecords; //remove all existing mappings foreach (var mapping in mappingSet.mappings) { mapping.delete(); } //map source and target to be sure mappingSet.source.mapTo(mappingSet.target); //make sure the target node tree has been build ((MappingNode)mappingSet.target).buildNodeTree(); //read the csv file using (var textReader = new StreamReader(filePath)) { var csv = new CSV.CsvReader(textReader, false); csv.Configuration.RegisterClassMap <CSVMappingRecordMap>(); csv.Configuration.Delimiter = ";"; mappingRecords = csv.GetRecords <CSVMappingRecord>(); var sourceNodes = new Dictionary <string, MP.MappingNode>(); var targetNodes = new Dictionary <string, MP.MappingNode>(); //now loop the records foreach (var csvRecord in mappingRecords) { if (string.IsNullOrEmpty(csvRecord.sourcePath) || (string.IsNullOrEmpty(csvRecord.targetPath) && string.IsNullOrEmpty(csvRecord.mappingLogic))) { //don't even bother if not both fields are filled in continue; } //convert any newLines (\n") coming from excel (Alt-Enter) to "real" newlines csvRecord.mappingLogic = csvRecord.mappingLogic.Replace("\n", Environment.NewLine); //find the source //first check if we already known the node MP.MappingNode sourceNode = null; if (!string.IsNullOrEmpty(csvRecord.sourcePath) && !sourceNodes.TryGetValue(csvRecord.sourcePath, out sourceNode)) { //find out if we know a parent node of this node var parentNode = findParentNode(sourceNodes, csvRecord.sourcePath); if (parentNode == null) { //no parent found, start at the top parentNode = mappingSet.source; } //find the node from the parent sourceNode = parentNode.findNode(csvRecord.sourcePath.Split('.').ToList()); } if (sourceNode == null) { EAOutputLogger.log($"Could not find source element corresponding to '{csvRecord.sourcePath}'", 0, LogTypeEnum.warning); //don't bother going any further continue; } //find the target MP.MappingNode targetNode = null; //first check if we already known the node if (!string.IsNullOrEmpty(csvRecord.targetPath) && !targetNodes.TryGetValue(csvRecord.targetPath, out targetNode)) { //find out if we know a parent node of this node var parentNode = findParentNode(targetNodes, csvRecord.targetPath); if (parentNode == null) { //no parent found, start at the top parentNode = mappingSet.target; } //find the node from the parent targetNode = parentNode.findNode(csvRecord.targetPath.Split('.').ToList()); if (targetNode == null) { EAOutputLogger.log($"Could not find target element corresponding to '{csvRecord.targetPath}'", 0, LogTypeEnum.warning); } } //if we found both then we map them if (sourceNode != null) { if (targetNode != null) { var newMapping = sourceNode.mapTo(targetNode); newMapping.mappingLogics = createMappingLogicsFromCSVString(csvRecord.mappingLogic, mappingSet.EAContexts, model); newMapping.save(); EAOutputLogger.log($"Mapping created from '{csvRecord.sourcePath}' to '{csvRecord.targetPath}'", 0); } else { var newMapping = sourceNode.createEmptyMapping(false); newMapping.mappingLogics = createMappingLogicsFromCSVString(csvRecord.mappingLogic, mappingSet.EAContexts, model); newMapping.save(); EAOutputLogger.log($"Empty mapping created for '{csvRecord.sourcePath}' ", 0); } } } } }
/// <summary> /// create a mappingSet based on the data in the CSV file /// </summary> /// <param name="model">the model that contains the elements</param> /// <param name="filePath">the path to the CSV file</param> /// <returns>a mapping set representing the mapping in the file</returns> public static MappingSet createMappingSet(Model model, string filePath, MappingSettings settings, ElementWrapper sourceRootElement, ElementWrapper targetRootElement) { MappingSet newMappingSet = null; IEnumerable <CSVMappingRecord> mappingRecords; //read the csv file using (var textReader = new StreamReader(filePath)) { var csv = new CSV.CsvReader(textReader, false); csv.Configuration.RegisterClassMap <CSVMappingRecordMap>(); csv.Configuration.Delimiter = ";"; mappingRecords = csv.GetRecords <CSVMappingRecord>(); } //create the new mapping set newMappingSet = createMappingSet(sourceRootElement, targetRootElement, settings); //remove all existing mappings //TODO: warn if mappings exist? foreach (var mapping in newMappingSet.mappings) { mapping.delete(); } //make sure the target node tree has been build ((MappingNode)newMappingSet.target).buildNodeTree(); //now loop the records foreach (var csvRecord in mappingRecords) { EAOutputLogger.log(model, settings.outputName , $"Parsed source: '{csvRecord.sourcePath}' target: '{csvRecord.targetPath}' logic: '{csvRecord.mappingLogic}'" , 0, LogTypeEnum.log); } return(newMappingSet); // int i = 1; //Package rootPackage = null; //foreach (CSVMappingRecord mappingRecord in parsedFile) //{ // //find source // var source = findElement(model, mappingRecord.sourcePath, sourceRootElement); // //find target // var target = findElement(model, mappingRecord.targetPath,targetRootElement); // if (source == null ) // { // EAOutputLogger.log(model,settings.outputName // ,string.Format("Could not find element that matches: '{0}'",mappingRecord.sourcePath) // ,0,LogTypeEnum.error); // } // else if( target == null) // { // EAOutputLogger.log(model,settings.outputName // ,string.Format("Could not find element that matches: '{0}'",mappingRecord.targetPath) // ,0,LogTypeEnum.error); // } // else // { // //first check if the mappingSet is already created // if (newMappingSet == null) // { // //determine if this should be a PackageMappingSet or an ElementMappingSet // if (sourceRootElement is Package) // { // rootPackage = sourceRootElement as Package; // //newMappingSet = new PackageMappingSet(sourceRootElement as Package); // } // else if (sourceRootElement is ElementWrapper) // { // rootPackage = sourceRootElement.owningPackage as Package; // //newMappingSet = new ElementMappingSet(sourceRootElement as ElementWrapper); // } // else // { // rootPackage = source.owningPackage as Package; // //newMappingSet = new PackageMappingSet((Package)source.owningPackage); // } // } // MappingLogic newMappingLogic = null; // //check if there is any mapping logic // if (! string.IsNullOrEmpty(mappingRecord.mappingLogic)) // { // if (settings.useInlineMappingLogic) // { // newMappingLogic = new MappingLogic(mappingRecord.mappingLogic); // } // else // { // //Check fo an existing mapping logic // newMappingLogic = getExistingMappingLogic(model, settings, mappingRecord.mappingLogic, rootPackage); // if (newMappingLogic == null) // { // var mappingElement = model.factory.createNewElement(rootPackage, "mapping logic " + i,settings.mappingLogicType) as ElementWrapper; // if (mappingElement != null) // { // //increase counter for new mapping element name // i++; // mappingElement.notes = mappingRecord.mappingLogic; // mappingElement.save(); // //create the mappingLogic // newMappingLogic = new MappingLogic(mappingElement); // } // else // { // //else we create an inline mapping logic anyway // newMappingLogic = new MappingLogic(mappingRecord.mappingLogic); // } // } // } // } // Mapping newMapping = null; // var sourceAssociationEnd = source as AssociationEnd; // var targetAssociationEnd = target as AssociationEnd; // //create the new mapping // //we can't create connector mappings for mappings to or from associations so we have to use tagged value mappings for those. // if (settings.useTaggedValues // || sourceAssociationEnd != null || targetAssociationEnd != null) // { // //if the source or target are associationEnds then we replace them by their association // if (sourceAssociationEnd != null) source = sourceAssociationEnd.association as Element; // if (targetAssociationEnd != null) target = targetAssociationEnd.association as Element; // //newMapping = new TaggedValueMapping(source,target,mappingRecord.sourcePath,mappingRecord.targetPath,settings); // } // else // { // //newMapping = new ConnectorMapping(source,target,mappingRecord.sourcePath,mappingRecord.targetPath,settings); // } // if (newMappingLogic != null) newMapping.mappingLogic = newMappingLogic; // newMapping.save(); // newMappingSet.addMapping(newMapping); // } //} }
/// <summary> /// create a mappingSet based on the data in the CSV file /// </summary> /// <param name="model">the model that contains the elements</param> /// <param name="filePath">the path to the CSV file</param> /// <returns>a mapping set representing the mapping in the file</returns> public static MappingSet createMappingSet(Model model, string filePath, MappingSettings settings, Element sourceRootElement = null, Element targetRootElement = null) { MappingSet newMappingSet = null; var engine = new FileHelperEngine <CSVMappingRecord>(); var parsedFile = engine.ReadFile(filePath); int i = 1; Package rootPackage = null; foreach (CSVMappingRecord mappingRecord in parsedFile) { //find source var source = findElement(model, mappingRecord.sourcePath, sourceRootElement); //find target var target = findElement(model, mappingRecord.targetPath, targetRootElement); if (source == null) { EAOutputLogger.log(model, settings.outputName , string.Format("Could not find element that matches: '{0}'", mappingRecord.sourcePath) , 0, LogTypeEnum.error); } else if (target == null) { EAOutputLogger.log(model, settings.outputName , string.Format("Could not find element that matches: '{0}'", mappingRecord.targetPath) , 0, LogTypeEnum.error); } else { //first check if the mappingSet is already created if (newMappingSet == null) { //determine if this should be a PackageMappingSet or an ElementMappingSet if (sourceRootElement is Package) { rootPackage = sourceRootElement as Package; newMappingSet = new PackageMappingSet(sourceRootElement as Package); } else if (sourceRootElement is ElementWrapper) { rootPackage = sourceRootElement.owningPackage as Package; newMappingSet = new ElementMappingSet(sourceRootElement as ElementWrapper); } else { rootPackage = source.owningPackage as Package; newMappingSet = new PackageMappingSet((Package)source.owningPackage); } } MappingLogic newMappingLogic = null; //check if there is any mapping logic if (!string.IsNullOrEmpty(mappingRecord.mappingLogic)) { if (settings.useInlineMappingLogic) { newMappingLogic = new MappingLogic(mappingRecord.mappingLogic); } else { //Check fo an existing mapping logic newMappingLogic = getExistingMappingLogic(model, settings, mappingRecord.mappingLogic, rootPackage); if (newMappingLogic == null) { var mappingElement = model.factory.createNewElement(rootPackage, "mapping logic " + i, settings.mappingLogicType) as ElementWrapper; if (mappingElement != null) { //increase counter for new mapping element name i++; mappingElement.notes = mappingRecord.mappingLogic; mappingElement.save(); //create the mappingLogic newMappingLogic = new MappingLogic(mappingElement); } else { //else we create an inline mapping logic anyway newMappingLogic = new MappingLogic(mappingRecord.mappingLogic); } } } } Mapping newMapping = null; var sourceAssociationEnd = source as AssociationEnd; var targetAssociationEnd = target as AssociationEnd; //create the new mapping //we can't create connector mappings for mappings to or from associations so we have to use tagged value mappings for those. if (settings.useTaggedValues || sourceAssociationEnd != null || targetAssociationEnd != null) { //if the source or target are associationEnds then we replace them by their association if (sourceAssociationEnd != null) { source = sourceAssociationEnd.association as Element; } if (targetAssociationEnd != null) { target = targetAssociationEnd.association as Element; } newMapping = new TaggedValueMapping(source, target, mappingRecord.sourcePath, mappingRecord.targetPath, settings); } else { newMapping = new ConnectorMapping(source, target, mappingRecord.sourcePath, mappingRecord.targetPath, settings); } if (newMappingLogic != null) { newMapping.mappingLogic = newMappingLogic; } newMapping.save(); newMappingSet.addMapping(newMapping); } } return(newMappingSet); }