internal static FdoClassCopyOptions FromElement(FdoCopyTaskElement el, FeatureSchemaCache cache, FdoConnection sourceConn, FdoConnection targetConn, out TargetClassModificationItem mod) { mod = null; if (!cache.HasConnection(el.Source.connection)) throw new TaskLoaderException("The referenced source connection is not defined"); if (!cache.HasConnection(el.Target.connection)) throw new TaskLoaderException("The referenced target connection is not defined"); FdoClassCopyOptions opts = new FdoClassCopyOptions(el.Source.connection, el.Target.connection, el.Source.schema, el.Source.@class, el.Target.schema, el.Target.@class); opts.DeleteTarget = el.Options.DeleteTarget; opts.SourceFilter = el.Options.Filter; if (!el.Options.FlattenGeometriesSpecified) opts.FlattenGeometries = false; else opts.FlattenGeometries = el.Options.FlattenGeometries; if (!el.Options.ForceWKBSpecified) opts.ForceWkb = false; else opts.ForceWkb = el.Options.ForceWKB; if (!string.IsNullOrEmpty(el.Options.BatchSize)) opts.BatchSize = Convert.ToInt32(el.Options.BatchSize); opts.Name = el.name; opts.CreateIfNotExists = el.createIfNotExists; ClassDefinition srcClass = cache.GetClassByName(el.Source.connection, el.Source.schema, el.Source.@class); ClassDefinition dstClass = cache.GetClassByName(el.Target.connection, el.Target.schema, el.Target.@class); if (!el.createIfNotExists && dstClass == null) throw new InvalidOperationException("Target class " + el.Target.@class + " does not exist and the createIfNotExist option is false"); SpatialContextInfo defaultSc = null; FunctionDefinitionCollection availableFunctions = (FunctionDefinitionCollection)sourceConn.Capability.GetObjectCapability(CapabilityType.FdoCapabilityType_ExpressionFunctions); using (var svc = targetConn.CreateFeatureService()) { defaultSc = svc.GetActiveSpatialContext(); } if (dstClass != null) { foreach (FdoPropertyMappingElement propMap in el.PropertyMappings) { if (srcClass.Properties.IndexOf(propMap.source) < 0) throw new TaskLoaderException("The property mapping (" + propMap.source + " -> " + propMap.target + ") in task (" + el.name + ") contains a source property not found in the source class definition (" + el.Source.@class + ")"); //Add to list of properties to check for if (propMap.createIfNotExists && dstClass.Properties.IndexOf(propMap.target) < 0) { if (mod == null) mod = new UpdateTargetClass(dstClass.Name); opts.AddSourcePropertyToCheck(propMap.source); //Clone copy of source property of same name var srcProp = srcClass.Properties[srcClass.Properties.IndexOf(propMap.source)]; srcProp = FdoSchemaUtil.CloneProperty(srcProp); mod.AddProperty(srcProp); } else { if (dstClass.Properties.IndexOf(propMap.target) < 0) throw new TaskLoaderException("The property mapping (" + propMap.source + " -> " + propMap.target + ") in task (" + el.name + ") contains a target property not found in the target class definition (" + el.Target.@class + ")"); PropertyDefinition sp = srcClass.Properties[propMap.source]; PropertyDefinition tp = dstClass.Properties[propMap.target]; if (sp.PropertyType != tp.PropertyType) throw new TaskLoaderException("The properties in the mapping (" + propMap.source + " -> " + propMap.target + ") are of different types"); //if (sp.PropertyType != PropertyType.PropertyType_DataProperty) // throw new TaskLoaderException("One or more properties in the mapping (" + propMap.source + " -> " + propMap.target + ") is not a data property"); DataPropertyDefinition sdp = sp as DataPropertyDefinition; DataPropertyDefinition tdp = tp as DataPropertyDefinition; opts.AddPropertyMapping(propMap.source, propMap.target); //Property mapping is between two data properties if (sdp != null && tdp != null) { //Types not equal, so add a conversion rule if (sdp.DataType != tdp.DataType) { FdoDataPropertyConversionRule rule = new FdoDataPropertyConversionRule( propMap.source, propMap.target, sdp.DataType, tdp.DataType, propMap.nullOnFailedConversion, propMap.truncate); opts.AddDataConversionRule(propMap.source, rule); } } } } // foreach (FdoExpressionMappingElement exprMap in el.ExpressionMappings) { if (string.IsNullOrEmpty(exprMap.target)) continue; opts.AddSourceExpression(exprMap.alias, exprMap.Expression, exprMap.target); //Add to list of properties to check for if (exprMap.createIfNotExists) { //Class exists but property doesn't if (dstClass.Properties.IndexOf(exprMap.target) < 0) { if (mod == null) { mod = new UpdateTargetClass(el.Target.@class); } var prop = FdoSchemaUtil.CreatePropertyFromExpressionType(exprMap.Expression, srcClass, availableFunctions, defaultSc.Name); if (prop == null) { throw new InvalidOperationException("Could not derive a property definition from the expression: " + exprMap.Expression); } prop.Name = exprMap.target; mod.AddProperty(prop); } } else //Conversion rules can only apply if both properties exist. { FdoPropertyType? pt = ExpressionUtility.ParseExpressionType(exprMap.Expression, sourceConn); if (pt.HasValue) { DataType? srcDt = ValueConverter.GetDataType(pt.Value); if (srcDt.HasValue) { PropertyDefinition tp = dstClass.Properties[exprMap.target]; DataPropertyDefinition tdp = tp as DataPropertyDefinition; if (tdp != null) { if (srcDt.Value != tdp.DataType) { FdoDataPropertyConversionRule rule = new FdoDataPropertyConversionRule( exprMap.alias, exprMap.target, srcDt.Value, tdp.DataType, exprMap.nullOnFailedConversion, exprMap.truncate); opts.AddDataConversionRule(exprMap.alias, rule); } } } } } } } else //class doesn't exist { mod = new CreateTargetClassFromSource(el.Source.schema, el.Target.@class); foreach (var propMap in el.PropertyMappings) { opts.AddPropertyMapping(propMap.source, propMap.target); if (propMap.createIfNotExists) { opts.AddSourcePropertyToCheck(propMap.source); } } foreach (var exprMap in el.ExpressionMappings) { opts.AddSourceExpression(exprMap.alias, exprMap.Expression, exprMap.target); if (exprMap.createIfNotExists) opts.AddSourcePropertyToCheck(exprMap.alias); var prop = FdoSchemaUtil.CreatePropertyFromExpressionType(exprMap.Expression, srcClass, availableFunctions, defaultSc.Name); if (prop == null) { throw new InvalidOperationException("Could not derive a property definition from the expression: " + exprMap.Expression); } prop.Name = exprMap.target; mod.AddProperty(prop); } } return opts; }
internal FdoCopyTaskElement ToElement() { FdoCopyTaskElement el = new FdoCopyTaskElement(); el.name = this.Name; el.createIfNotExists = this.CreateIfNotExists; el.Options = new FdoCopyOptionsElement(); el.Options.DeleteTarget = this.DeleteTarget; el.Options.Filter = this.SourceFilter; el.Options.FlattenGeometries = this.FlattenGeometries; el.Options.FlattenGeometriesSpecified = true; el.Options.ForceWKB = this.ForceWkb; el.Options.ForceWKBSpecified = true; if (this.BatchSize > 0) el.Options.BatchSize = this.BatchSize.ToString(); el.Source = new FdoCopySourceElement(); el.Source.connection = this.SourceConnectionName; el.Source.schema = this.SourceSchema; el.Source.@class = this.SourceClassName; el.Target = new FdoCopyTargetElement(); el.Target.connection = this.TargetConnectionName; el.Target.schema = this.TargetSchema; el.Target.@class = this.TargetClassName; List<FdoPropertyMappingElement> propMappings = new List<FdoPropertyMappingElement>(); List<FdoExpressionMappingElement> exprMappings = new List<FdoExpressionMappingElement>(); List<string> check = new List<string>(this.CheckSourceProperties); Dictionary<string, FdoPropertyMappingElement> convRules = new Dictionary<string, FdoPropertyMappingElement>(); foreach (FdoDataPropertyConversionRule rule in this.ConversionRules) { FdoPropertyMappingElement map = new FdoPropertyMappingElement(); //map.sourceDataType = rule.SourceDataType.ToString(); //map.targetDataType = rule.TargetDataType.ToString(); map.nullOnFailedConversion = rule.NullOnFailure; map.truncate = rule.Truncate; map.source = rule.SourceProperty; map.target = rule.TargetProperty; if (check.Contains(map.source)) map.createIfNotExists = true; convRules.Add(map.source, map); } foreach (string prop in this.SourcePropertyNames) { if (convRules.ContainsKey(prop)) { propMappings.Add(convRules[prop]); } else { FdoPropertyMappingElement map = new FdoPropertyMappingElement(); map.source = prop; map.target = this.GetTargetProperty(prop); if (check.Contains(map.source)) map.createIfNotExists = true; propMappings.Add(map); } } foreach (string alias in this.SourceAliases) { FdoExpressionMappingElement map = new FdoExpressionMappingElement(); map.alias = alias; map.Expression = this.GetExpression(alias); map.target = this.GetTargetPropertyForAlias(alias); if (check.Contains(map.alias)) map.createIfNotExists = true; exprMappings.Add(map); } el.PropertyMappings = propMappings.ToArray(); el.ExpressionMappings = exprMappings.ToArray(); return el; }
private FdoBulkCopyTaskDefinition Save() { FdoBulkCopyTaskDefinition def = new FdoBulkCopyTaskDefinition(); def.name = txtName.Text; List<FdoConnectionEntryElement> conns = new List<FdoConnectionEntryElement>(); foreach (DataGridViewRow row in grdConnections.Rows) { FdoConnectionEntryElement entry = new FdoConnectionEntryElement(); entry.name = row.Cells[0].Value.ToString(); entry.provider = row.Cells[1].Value.ToString(); entry.ConnectionString = row.Cells[3].Value.ToString(); conns.Add(entry); } List<FdoCopyTaskElement> tasks = new List<FdoCopyTaskElement>(); foreach (CopyTaskNodeDecorator dec in _tasks.Values) { FdoCopyTaskElement task = new FdoCopyTaskElement(); task.name = dec.Name; task.createIfNotExists = dec.CreateIfNotExists; task.Source = new FdoCopySourceElement(); task.Target = new FdoCopyTargetElement(); task.Options = new FdoCopyOptionsElement(); List<FdoPropertyMappingElement> pmaps = new List<FdoPropertyMappingElement>(); List<FdoExpressionMappingElement> emaps = new List<FdoExpressionMappingElement>(); //Source task.Source.@class = dec.SourceClassName; task.Source.connection = dec.SourceConnectionName; task.Source.schema = dec.SourceSchemaName; //Target task.Target.@class = dec.TargetClassName; task.Target.connection = dec.TargetConnectionName; task.Target.schema = dec.TargetSchemaName; //Options task.Options.BatchSize = dec.Options.BatchSize.ToString(); task.Options.FlattenGeometries = dec.Options.Flatten; task.Options.FlattenGeometriesSpecified = true; task.Options.DeleteTarget = dec.Options.Delete; task.Options.Filter = dec.Options.SourceFilter; task.Options.ForceWKB = dec.Options.ForceWkb; task.Options.ForceWKBSpecified = true; //Property Mappings NameValueCollection mappings = dec.PropertyMappings.GetPropertyMappings(); foreach (string srcProp in mappings.Keys) { string dstProp = mappings[srcProp]; FdoPropertyMappingElement p = new FdoPropertyMappingElement(); p.source = srcProp; p.target = dstProp; PropertyConversionNodeDecorator conv = dec.PropertyMappings.GetConversionRule(p.source); p.nullOnFailedConversion = conv.NullOnFailedConversion; p.truncate = conv.Truncate; p.createIfNotExists = conv.CreateIfNotExists; pmaps.Add(p); } foreach (string alias in dec.ExpressionMappings.GetAliases()) { FdoExpressionMappingElement e = new FdoExpressionMappingElement(); e.alias = alias; ExpressionMappingInfo exMap = dec.ExpressionMappings.GetMapping(alias); e.Expression = exMap.Expression; e.target = exMap.TargetProperty; PropertyConversionNodeDecorator conv = dec.ExpressionMappings.GetConversionRule(e.alias); e.nullOnFailedConversion = conv.NullOnFailedConversion; e.truncate = conv.Truncate; e.createIfNotExists = conv.CreateIfNotExists; emaps.Add(e); } task.PropertyMappings = pmaps.ToArray(); task.ExpressionMappings = emaps.ToArray(); tasks.Add(task); } def.Connections = conns.ToArray(); def.CopyTasks = tasks.ToArray(); return def; }