private void OnEditExpression(object sender, EventArgs e) { TreeNode exprNode = _treeView.SelectedNode; Debug.Assert(exprNode.Tag != null); ExpressionMappingInfo expr = exprNode.Tag as ExpressionMappingInfo; if (expr != null) { string newExpr = ExpressionEditor.EditExpression(Parent.GetSourceConnection(), Parent.SourceClass, null, expr.Expression, ExpressionMode.Normal); if (newExpr != null) //null = cancel { exprNode.ToolTipText = "Expression: " + newExpr; expr.Expression = newExpr; } } }
public void AddExpression(string srcAlias, string expressionText) { TreeNode exprNode = _node.Nodes[srcAlias]; if (exprNode == null) { exprNode = new TreeNode(); exprNode.Text = srcAlias; exprNode.ToolTipText = "Expression: " + expressionText; exprNode.Name = srcAlias; ExpressionMappingInfo map = new ExpressionMappingInfo(); map.Expression = expressionText; exprNode.Tag = map; exprNode.ContextMenuStrip = _mapExprContextMenu; _node.Nodes.Add(exprNode); _node.Expand(); _conv[exprNode.Name] = new PropertyConversionNodeDecorator(exprNode); exprNode.ExpandAll(); } }
public void MapExpression(string srcAlias, string destProperty, bool createIfNotExists) { var dstCls = Parent.TargetClass; TreeNode exprNode = _node.Nodes[srcAlias]; ExpressionMappingInfo map = (ExpressionMappingInfo)exprNode.Tag; if (destProperty != null) { if (dstCls != null) { PropertyDefinition dst = dstCls.Properties[destProperty]; DataPropertyDefinition dp = dst as DataPropertyDefinition; if (string.IsNullOrEmpty(map.Expression)) { throw new MappingException("Cannot map alias. There is no expression defined"); } Expression expr = null; try { expr = Expression.Parse(map.Expression); } catch (OSGeo.FDO.Common.Exception ex) { throw new MappingException("Cannot map alias. Invalid expression: " + ex.Message); } if (typeof(Function).IsAssignableFrom(expr.GetType())) { Function func = expr as Function; FdoConnection conn = Parent.GetSourceConnection(); FunctionDefinitionCollection funcDefs = (FunctionDefinitionCollection)conn.Capability.GetObjectCapability(CapabilityType.FdoCapabilityType_ExpressionFunctions); FunctionDefinition funcDef = null; //Shouldn't happen because Expression Editor ensures a valid function if (!funcDefs.Contains(func.Name)) { throw new MappingException("Cannot map alias. Expression contains unsupported function: " + func.Name); } //Try to get the return type foreach (FunctionDefinition fd in funcDefs) { if (fd.Name == func.Name) { funcDef = fd; break; } } if (funcDef.ReturnPropertyType != dst.PropertyType) { throw new MappingException("Cannot map alias. Expression evaluates to an un-mappable property type"); } if (funcDef.ReturnPropertyType == PropertyType.PropertyType_GeometricProperty) { } else if (funcDef.ReturnPropertyType == PropertyType.PropertyType_DataProperty) { if (!ValueConverter.IsConvertible(funcDef.ReturnType, dp.DataType)) { throw new MappingException("Cannot map alias to property " + dst.Name + ". Expression evaluates to a data type that cannot be mapped to " + dp.DataType); } } else //Association, Object, Raster { throw new MappingException("Cannot map alias. Expression evaluates to an un-mappable property type"); } } else if (typeof(BinaryExpression).IsAssignableFrom(expr.GetType())) { if (dp == null) { throw new MappingException("Cannot map alias. Expression evaluates to an un-mappable property type"); } //We're assuming that this evalutes to a boolean value if (!ValueConverter.IsConvertible(DataType.DataType_Boolean, dp.DataType)) { throw new MappingException("Cannot map alias to property " + dst.Name + ". Expression evaluates to a data type that cannot be mapped to " + dp.DataType); } } else if (typeof(DataValue).IsAssignableFrom(expr.GetType())) { if (dp == null) { throw new MappingException("Cannot map alias. Expression evaluates to an un-mappable property type"); } DataValue dv = (DataValue)expr; if (!ValueConverter.IsConvertible(dv.DataType, dp.DataType)) { throw new MappingException("Cannot map alias to property " + dst.Name + ". Expression evaluates to a data type that cannot be mapped to " + dp.DataType); } } //else if (expr.GetType() == typeof(Identifier)) //{ // //TODO: use the property type of the referenced property //} else //Cannot be evaluated { throw new MappingException("Cannot map alias. Expression evaluates to an un-mappable value"); } } else { if (!createIfNotExists) { throw new MappingException("Cannot map alias. The specified target property " + destProperty + " does not exist and the \"create if necessary\" option was not specified"); } } } //All good exprNode.Text = srcAlias + " ( => " + destProperty + " )"; map.TargetProperty = destProperty; if (destProperty != null) { exprNode.Text = exprNode.Name + " => " + destProperty; } else { exprNode.Text = exprNode.Name; } GetConversionRule(srcAlias).CreateIfNotExists = createIfNotExists; }