private string GetFunctions(FunctionDefinitionCollection functionDefinitionCollection)
        {
            List <string> values = new List <string>();

            foreach (FunctionDefinition fd in functionDefinitionCollection)
            {
                values.Add(fd.Name);
            }
            return(String.Join(", ", values.ToArray()));
        }
Beispiel #2
0
        /// <summary>
        /// Parses the type of the expression.
        /// </summary>
        /// <param name="exprStr">The expr STR.</param>
        /// <param name="conn">The conn.</param>
        /// <returns></returns>
        public static FdoPropertyType?ParseExpressionType(string exprStr, FdoConnection conn)
        {
            Expression expr = null;

            try
            {
                expr = Expression.Parse(exprStr);
            }
            catch (OSGeo.FDO.Common.Exception)
            {
                return(null);
            }

            if (expr.GetType() == typeof(Function))
            {
                Function func = expr as Function;
                FunctionDefinitionCollection funcDefs = (FunctionDefinitionCollection)conn.Capability.GetObjectCapability(CapabilityType.FdoCapabilityType_ExpressionFunctions);
                FunctionDefinition           funcDef  = null;

                //Try to get the return type
                foreach (FunctionDefinition fd in funcDefs)
                {
                    if (fd.Name == func.Name)
                    {
                        funcDef = fd;
                        break;
                    }
                }

                if (funcDef == null)
                {
                    return(null);
                }

                switch (funcDef.ReturnPropertyType)
                {
                case PropertyType.PropertyType_AssociationProperty:
                    return(FdoPropertyType.Association);

                case PropertyType.PropertyType_GeometricProperty:
                    return(FdoPropertyType.Geometry);

                case PropertyType.PropertyType_ObjectProperty:
                    return(FdoPropertyType.Object);

                case PropertyType.PropertyType_RasterProperty:
                    return(FdoPropertyType.Raster);

                case PropertyType.PropertyType_DataProperty:
                {
                    return(ValueConverter.GetPropertyType(funcDef.ReturnType));
                }
                }
            }
            else if (expr.GetType() == typeof(BinaryExpression))
            {
                return(FdoPropertyType.Boolean);
            }
            else if (expr.GetType() == typeof(DataValue))
            {
                DataValue dv = (DataValue)expr;
                return(ValueConverter.GetPropertyType(dv.DataType));
            }
            return(null);
        }
 private string GetFunctions(FunctionDefinitionCollection functionDefinitionCollection)
 {
     List<string> values = new List<string>();
     foreach (FunctionDefinition fd in functionDefinitionCollection)
     {
         values.Add(fd.Name);
     }
     return String.Join(", ", values.ToArray());
 }
Beispiel #4
0
        internal static PropertyDefinition CreatePropertyFromExpressionType(string exprText, ClassDefinition clsDef, FunctionDefinitionCollection functionDefs, string defaultSpatialContextName)
        {
            string name = string.Empty;
            using (var expr = Expression.Parse(exprText))
            {
                var et = expr.GetType();
                if (typeof(ComputedIdentifier).IsAssignableFrom(et))
                {
                    var subExpr = ((ComputedIdentifier)expr).Expression;
                    return CreatePropertyFromExpressionType(subExpr.ToString(), clsDef, functionDefs, defaultSpatialContextName);
                }
                else if (typeof(Identifier).IsAssignableFrom(et))
                {
                    var id = (Identifier)expr;
                    return CloneProperty(clsDef.Properties[id.Name]);
                }
                else if (typeof(Function).IsAssignableFrom(et))
                {
                    var func = (Function)expr;
                    if (functionDefs != null)
                    {
                        var fidx = functionDefs.IndexOf(func.Name);
                        if (fidx >= 0)
                        {
                            var funcDef = functionDefs[fidx];
                            switch (funcDef.ReturnPropertyType)
                            {
                                case PropertyType.PropertyType_DataProperty:
                                    {
                                        var dp = new DataPropertyDefinition(name, "");
                                        dp.DataType = funcDef.ReturnType;
                                        dp.Nullable = true;
                                        if (dp.DataType == DataType.DataType_String)
                                            dp.Length = 255;

                                        return dp;
                                    }
                                    break;
                                case PropertyType.PropertyType_GeometricProperty:
                                    {
                                        var geom = new GeometricPropertyDefinition(name, "");
                                        geom.SpatialContextAssociation = defaultSpatialContextName;
                                        geom.GeometryTypes = (int)GeometricType.GeometricType_All;

                                        return geom;
                                    }
                                    break;

                            }
                        }
                    }
                }
                else if (typeof(BinaryExpression).IsAssignableFrom(et))
                {
                    var dp = new DataPropertyDefinition(name, "");
                    dp.DataType = DataType.DataType_Boolean;
                    dp.Nullable = true;

                    return dp;
                }
                else if (typeof(DataValue).IsAssignableFrom(et))
                {
                    var dv = (DataValue)expr;
                    var dp = new DataPropertyDefinition(name, "");
                    dp.DataType = dv.DataType;
                    if (dp.DataType == DataType.DataType_String)
                        dp.Length = 255;

                    dp.Nullable = true;
                    return dp;
                }
                else if (typeof(GeometryValue).IsAssignableFrom(et))
                {
                    var geom = new GeometricPropertyDefinition(name, "");
                    geom.GeometryTypes = (int)GeometricType.GeometricType_All;

                    return geom;
                }
            }
            return null;
        }
Beispiel #5
0
        internal static PropertyDefinition CreatePropertyFromExpressionType(string exprText, ClassDefinition clsDef, FunctionDefinitionCollection functionDefs, string defaultSpatialContextName)
        {
            string name = string.Empty;

            using (var expr = Expression.Parse(exprText))
            {
                var et = expr.GetType();
                if (typeof(ComputedIdentifier).IsAssignableFrom(et))
                {
                    var subExpr = ((ComputedIdentifier)expr).Expression;
                    return(CreatePropertyFromExpressionType(subExpr.ToString(), clsDef, functionDefs, defaultSpatialContextName));
                }
                else if (typeof(Identifier).IsAssignableFrom(et))
                {
                    var id = (Identifier)expr;
                    return(CloneProperty(clsDef.Properties[id.Name]));
                }
                else if (typeof(Function).IsAssignableFrom(et))
                {
                    var func = (Function)expr;
                    if (functionDefs != null)
                    {
                        var fidx = functionDefs.IndexOf(func.Name);
                        if (fidx >= 0)
                        {
                            var funcDef = functionDefs[fidx];
                            switch (funcDef.ReturnPropertyType)
                            {
                            case PropertyType.PropertyType_DataProperty:
                            {
                                var dp = new DataPropertyDefinition(name, "");
                                dp.DataType = funcDef.ReturnType;
                                dp.Nullable = true;
                                if (dp.DataType == DataType.DataType_String)
                                {
                                    dp.Length = 255;
                                }

                                return(dp);
                            }
                            break;

                            case PropertyType.PropertyType_GeometricProperty:
                            {
                                var geom = new GeometricPropertyDefinition(name, "");
                                geom.SpatialContextAssociation = defaultSpatialContextName;
                                geom.GeometryTypes             = (int)GeometricType.GeometricType_All;

                                return(geom);
                            }
                            break;
                            }
                        }
                    }
                }
                else if (typeof(BinaryExpression).IsAssignableFrom(et))
                {
                    var dp = new DataPropertyDefinition(name, "");
                    dp.DataType = DataType.DataType_Boolean;
                    dp.Nullable = true;

                    return(dp);
                }
                else if (typeof(DataValue).IsAssignableFrom(et))
                {
                    var dv = (DataValue)expr;
                    var dp = new DataPropertyDefinition(name, "");
                    dp.DataType = dv.DataType;
                    if (dp.DataType == DataType.DataType_String)
                    {
                        dp.Length = 255;
                    }

                    dp.Nullable = true;
                    return(dp);
                }
                else if (typeof(GeometryValue).IsAssignableFrom(et))
                {
                    var geom = new GeometricPropertyDefinition(name, "");
                    geom.GeometryTypes = (int)GeometricType.GeometricType_All;

                    return(geom);
                }
            }
            return(null);
        }
        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);
        }
        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;
        }
Beispiel #8
0
        private void LoadFunctionDefinitions(FunctionDefinitionCollection funcs)
        {
            foreach (FunctionDefinition funcDef in funcs)
            {
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Text = item.Name = funcDef.Name;
                item.ToolTipText = funcDef.Description;
                item.Tag = funcDef;
                item.Click += new EventHandler(function_Click);
                _FunctionMenuItems[funcDef.FunctionCategoryType].DropDown.Items.Add(item);

                _autoCompleteItems[funcDef.Name] = new FunctionItem(funcDef);
            }
        }