public virtual void PrepareFieldForPersist(Net.Vpc.Upa.Field field, object @value, Net.Vpc.Upa.Record userRecord, Net.Vpc.Upa.Record persistentRecord, Net.Vpc.Upa.Persistence.EntityExecutionContext executionContext, System.Collections.Generic.ISet <Net.Vpc.Upa.Field> persistNonNullable, System.Collections.Generic.ISet <Net.Vpc.Upa.Field> persistWithDefaultValue) /* throws Net.Vpc.Upa.Exceptions.UPAException */
 {
     Net.Vpc.Upa.Types.ManyToOneType e = (Net.Vpc.Upa.Types.ManyToOneType)field.GetDataType();
     if (e.IsUpdatable())
     {
         Net.Vpc.Upa.Entity masterEntity = executionContext.GetPersistenceUnit().GetEntity(e.GetTargetEntityName());
         Net.Vpc.Upa.Key    k            = null;
         if (@value is Net.Vpc.Upa.Record)
         {
             k = masterEntity.GetBuilder().RecordToKey((Net.Vpc.Upa.Record)@value);
         }
         else
         {
             k = masterEntity.GetBuilder().ObjectToKey(@value);
         }
         int x = 0;
         foreach (Net.Vpc.Upa.Field fk in e.GetRelationship().GetSourceRole().GetFields())
         {
             persistNonNullable.Remove(fk);
             persistWithDefaultValue.Remove(fk);
             persistentRecord.SetObject(fk.GetName(), k == null ? null : k.GetObjectAt(x));
             x++;
         }
     }
 }
 public ComposedToFlatFieldPersister(Net.Vpc.Upa.Field field)
 {
     this.field = field;
     Net.Vpc.Upa.Types.ManyToOneType t       = (Net.Vpc.Upa.Types.ManyToOneType)field.GetDataType();
     Net.Vpc.Upa.Entity           master     = t.GetRelationship().GetTargetRole().GetEntity();
     Net.Vpc.Upa.RelationshipRole detailRole = t.GetRelationship().GetSourceRole();
     flatFields = detailRole.GetFields();
     relationshipTargetConverter = master.GetBuilder();
 }
Beispiel #3
0
 public virtual void Parse(System.Collections.Generic.IList <System.Reflection.FieldInfo> fields, bool nullable)
 {
     fieldsList = fields;
     System.Collections.Generic.IList <Net.Vpc.Upa.Config.Decoration> manyToOneDecorations = new System.Collections.Generic.List <Net.Vpc.Upa.Config.Decoration>();
     System.Collections.Generic.IList <Net.Vpc.Upa.Config.Decoration> hierarchyDecorations = new System.Collections.Generic.List <Net.Vpc.Upa.Config.Decoration>();
     foreach (System.Reflection.FieldInfo javaField in fields)
     {
         Net.Vpc.Upa.Config.Decoration gid  = repo.GetFieldDecoration(javaField, typeof(Net.Vpc.Upa.Config.ManyToOne));
         Net.Vpc.Upa.Config.Decoration gid2 = repo.GetFieldDecoration(javaField, typeof(Net.Vpc.Upa.Config.Hierarchy));
         if (gid != null)
         {
             //                ConfigInfo config = gid.getConfig();
             manyToOneDecorations.Add(gid);
         }
         if (gid2 != null)
         {
             //                ConfigInfo config = gid.getConfig();
             hierarchyDecorations.Add(gid2);
         }
     }
     if ((manyToOneDecorations).Count > 1)
     {
         Net.Vpc.Upa.Impl.FwkConvertUtils.ListSort(manyToOneDecorations, Net.Vpc.Upa.Impl.Config.Annotationparser.DecorationComparator.INSTANCE);
     }
     if ((hierarchyDecorations).Count > 1)
     {
         Net.Vpc.Upa.Impl.FwkConvertUtils.ListSort(hierarchyDecorations, Net.Vpc.Upa.Impl.Config.Annotationparser.DecorationComparator.INSTANCE);
     }
     foreach (Net.Vpc.Upa.Config.Decoration gid in manyToOneDecorations)
     {
         MergeManyToOne(gid);
     }
     foreach (Net.Vpc.Upa.Config.Decoration gid in hierarchyDecorations)
     {
         MergeHierarchy(gid);
     }
     if (GetTargetEntityType() == null || GetTargetEntityType().Equals(typeof(void)))
     {
         SetTargetEntityType(GetFieldType());
     }
     if (IsManyToOne())
     {
         System.Type nativeClass = GetFieldType();
         if (!Net.Vpc.Upa.Impl.Util.UPAUtils.IsSimpleFieldType(nativeClass))
         {
             Net.Vpc.Upa.Types.ManyToOneType manyToOneType = new Net.Vpc.Upa.Types.ManyToOneType(name, nativeClass, null, true, nullable);
             preferredDataType = (manyToOneType);
         }
     }
 }
Beispiel #4
0
        public virtual object GetMainValue(object instance)
        {
            object v = GetValue(instance);

            if (v != null)
            {
                Net.Vpc.Upa.Types.DataType d = GetDataType();
                if (d is Net.Vpc.Upa.Types.ManyToOneType)
                {
                    Net.Vpc.Upa.Types.ManyToOneType ed = (Net.Vpc.Upa.Types.ManyToOneType)d;
                    v = ed.GetRelationship().GetTargetEntity().GetBuilder().GetMainValue(v);
                }
            }
            return(v);
        }
 public virtual void Insert(Net.Vpc.Upa.Entity entity, Net.Vpc.Upa.Record originalRecord, Net.Vpc.Upa.Record record, Net.Vpc.Upa.Persistence.EntityExecutionContext context) /* throws Net.Vpc.Upa.Exceptions.UPAException */
 {
     Net.Vpc.Upa.PersistenceUnit    pu     = context.GetPersistenceUnit();
     Net.Vpc.Upa.Expressions.Insert insert = new Net.Vpc.Upa.Expressions.Insert().Into(entity.GetName());
     foreach (System.Collections.Generic.KeyValuePair <string, object> entry in record.EntrySet())
     {
         object            @value = (entry).Value;
         string            key    = (entry).Key;
         Net.Vpc.Upa.Field field  = entity.FindField(key);
         //should process specific entity fields
         if ((field.GetDataType() is Net.Vpc.Upa.Types.ManyToOneType))
         {
             Net.Vpc.Upa.Types.ManyToOneType e = (Net.Vpc.Upa.Types.ManyToOneType)field.GetDataType();
             if (e.IsUpdatable())
             {
                 Net.Vpc.Upa.Entity masterEntity = pu.GetEntity(e.GetTargetEntityName());
                 Net.Vpc.Upa.Key    k            = null;
                 if (@value is Net.Vpc.Upa.Record)
                 {
                     k = masterEntity.GetBuilder().RecordToKey((Net.Vpc.Upa.Record)@value);
                 }
                 else
                 {
                     k = masterEntity.GetBuilder().ObjectToKey(@value);
                 }
                 int x = 0;
                 foreach (Net.Vpc.Upa.Field fk in e.GetRelationship().GetSourceRole().GetFields())
                 {
                     insert.Set(fk.GetName(), new Net.Vpc.Upa.Expressions.Param(fk.GetName(), k.GetObjectAt(x)));
                     x++;
                 }
             }
         }
         else
         {
             Net.Vpc.Upa.Expressions.Expression valueExpression = (@value is Net.Vpc.Upa.Expressions.Expression) ? ((Net.Vpc.Upa.Expressions.Expression)((Net.Vpc.Upa.Expressions.Expression)@value)) : new Net.Vpc.Upa.Expressions.Param(field.GetName(), @value);
             insert.Set(key, valueExpression);
         }
     }
     context.GetPersistenceStore().CreateQuery(insert, context).ExecuteNonQuery();
 }
Beispiel #6
0
        public override string GetSQL(object oo, Net.Vpc.Upa.Persistence.EntityExecutionContext qlContext, Net.Vpc.Upa.Impl.Persistence.SQLManager sqlManager, Net.Vpc.Upa.Impl.Uql.ExpressionDeclarationList declarations)
        {
            Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledLiteral o = (Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledLiteral)oo;
            Net.Vpc.Upa.Impl.Util.ExprTypeInfo ei = Net.Vpc.Upa.Impl.Util.UPAUtils.ResolveExprTypeInfo(o);
            object objectValue = o.GetValue();

            Net.Vpc.Upa.Types.DataTypeTransform d = null;
            if (ei.GetOldReferrer() != null)
            {
                Net.Vpc.Upa.Field oldField = (Net.Vpc.Upa.Field)ei.GetOldReferrer();
                if (oldField.GetDataType() is Net.Vpc.Upa.Types.ManyToOneType)
                {
                    Net.Vpc.Upa.Types.ManyToOneType et = (Net.Vpc.Upa.Types.ManyToOneType)oldField.GetDataType();
                    objectValue = et.GetRelationship().GetTargetEntity().GetBuilder().ObjectToId(objectValue);
                }
            }
            else if (ei.GetReferrer() != null && ei.GetReferrer() is Net.Vpc.Upa.Field)
            {
                Net.Vpc.Upa.Field field = (Net.Vpc.Upa.Field)ei.GetReferrer();
                if (field.GetDataType() is Net.Vpc.Upa.Types.ManyToOneType)
                {
                    Net.Vpc.Upa.Types.ManyToOneType et = (Net.Vpc.Upa.Types.ManyToOneType)field.GetDataType();
                    objectValue = et.GetRelationship().GetTargetEntity().GetBuilder().ObjectToId(objectValue);
                    System.Collections.Generic.IList <Net.Vpc.Upa.Field> tf = et.GetRelationship().GetTargetEntity().GetPrimaryFields();
                    if ((tf).Count != 1)
                    {
                        throw new System.ArgumentException("Unsupported");
                    }
                    d = Net.Vpc.Upa.Impl.Util.UPAUtils.GetTypeTransformOrIdentity(tf[0]);
                }
            }
            if (d == null)
            {
                d = o.GetEffectiveDataType();
            }
            Net.Vpc.Upa.Impl.Persistence.TypeMarshaller mm = sqlManager.GetMarshallManager().GetTypeMarshaller(d);
            return(mm.ToSQLLiteral(objectValue));
        }
 public virtual void Synchronize()
 {
     if (field.GetDataType() is Net.Vpc.Upa.Types.ManyToOneType)
     {
         Net.Vpc.Upa.Types.ManyToOneType t        = (Net.Vpc.Upa.Types.ManyToOneType)field.GetDataType();
         Net.Vpc.Upa.Relationship        relation = t.GetRelationship();
         if (relation == null)
         {
             throw new Net.Vpc.Upa.Exceptions.UPAException("MissingRelationForField", field);
         }
         Net.Vpc.Upa.RelationshipRole       detailRole = relation.GetSourceRole();
         Net.Vpc.Upa.RelationshipUpdateType u          = detailRole.GetRelationshipUpdateType();
         if (u == Net.Vpc.Upa.RelationshipUpdateType.COMPOSED)
         {
             persistFieldPersister = new Net.Vpc.Upa.Impl.Persistence.ComposedToFlatFieldPersister(field);
             updateFieldPersister  = new Net.Vpc.Upa.Impl.Persistence.ComposedToFlatFieldPersister(field);
         }
         persistFormulaPass = 0;
         updateFormulaPass  = 0;
         postPersistFormula = false;
         postUpdateFormula  = false;
         return;
     }
     Net.Vpc.Upa.Formula persistFormula = field.GetPersistFormula();
     if (persistFormula != null)
     {
         if (persistFormula is Net.Vpc.Upa.Sequence)
         {
             persistFieldPersister = UpdateFieldPersister(persistFieldPersister, persistenceStore.CreatePersistSequenceGenerator(field));
             persistFormulaPass    = 0;
             postPersistFormula    = false;
         }
         else if (persistFormula is Net.Vpc.Upa.ExpressionFormula)
         {
             Net.Vpc.Upa.Impl.Persistence.RebuildExpressionInfo re = RebuildExpression((Net.Vpc.Upa.ExpressionFormula)persistFormula);
             if (re.rebuiltFormula != null)
             {
                 field.SetPersistFormula(re.rebuiltFormula);
             }
             bool complex = re.compiledExpression.FindFirstExpression <T>(Net.Vpc.Upa.Impl.Uql.Compiledfilters.CompiledExpressionHelper.THIS_VAR_FILTER) != default(T);
             persistFormulaPass = field.GetPersistFormulaOrder();
             if (!complex && persistFormulaPass == 0)
             {
                 insertExpression      = re.expression;
                 persistFieldPersister = UpdateFieldPersister(persistFieldPersister, new Net.Vpc.Upa.Impl.Persistence.ExpressionFieldPersister(field.GetName(), re.expression));
                 postPersistFormula    = false;
             }
             else
             {
                 postPersistFormula = true;
             }
         }
         else
         {
             postPersistFormula = true;
         }
     }
     else
     {
         postPersistFormula = false;
     }
     Net.Vpc.Upa.Formula updateFormula = field.GetUpdateFormula();
     if (updateFormula != null)
     {
         if (updateFormula is Net.Vpc.Upa.Sequence)
         {
             updateFieldPersister = UpdateFieldPersister(updateFieldPersister, persistenceStore.CreateUpdateSequenceGenerator(field));
             updateFormulaPass    = 0;
             postUpdateFormula    = false;
         }
         else if (updateFormula is Net.Vpc.Upa.ExpressionFormula)
         {
             Net.Vpc.Upa.Impl.Persistence.RebuildExpressionInfo re = RebuildExpression((Net.Vpc.Upa.ExpressionFormula)persistFormula);
             if (re.rebuiltFormula != null)
             {
                 field.SetUpdateFormula(re.rebuiltFormula);
             }
             //                List<DefaultCompiledExpression> complex = re.compiledExpression.findExpressionsList(CompiledExpressionHelper.THIS_VAR_FILTER);
             updateFormulaPass = field.GetUpdateFormulaOrder();
             if (updateFormulaPass == 0)
             {
                 updateExpression     = re.expression;
                 updateFieldPersister = UpdateFieldPersister(updateFieldPersister, new Net.Vpc.Upa.Impl.Persistence.ExpressionFieldPersister(field.GetName(), re.expression));
                 postUpdateFormula    = false;
             }
             else
             {
                 postUpdateFormula = true;
             }
         }
         else
         {
             postUpdateFormula = true;
         }
     }
     else
     {
         postUpdateFormula = true;
     }
     Net.Vpc.Upa.Formula selectFormula = field.GetSelectFormula();
     if (selectFormula != null)
     {
         if (selectFormula is Net.Vpc.Upa.ExpressionFormula)
         {
             findtExpression = ((Net.Vpc.Upa.ExpressionFormula)selectFormula).GetExpression();
         }
         else if (selectFormula is Net.Vpc.Upa.Sequence)
         {
             throw new System.ArgumentException("Sequences are not supported as Select Expressions");
         }
         else if (updateFormula is Net.Vpc.Upa.CustomUpdaterFormula)
         {
             throw new System.ArgumentException("CustomUpdaterFormulas are not supported as Select Expressions");
         }
     }
 }
Beispiel #8
0
        public virtual Net.Vpc.Upa.Expressions.Expression RecordToExpression(Net.Vpc.Upa.Record record, string alias) /* throws Net.Vpc.Upa.Exceptions.UPAException */
        {
            if (record == null)
            {
                return(null);
            }
            Net.Vpc.Upa.Expressions.Expression a = null;
            foreach (System.Collections.Generic.KeyValuePair <string, object> entry in record.EntrySet())
            {
                string            key    = (entry).Key;
                object            @value = (entry).Value;
                Net.Vpc.Upa.Field field  = GetEntity().GetField(key);
                if (!field.IsUnspecifiedValue(@value))
                {
                    Net.Vpc.Upa.Expressions.Expression e = null;
                    Net.Vpc.Upa.Expressions.Var        p = new Net.Vpc.Upa.Expressions.Var(Net.Vpc.Upa.Impl.Util.StringUtils.IsNullOrEmpty(alias) ? GetEntity().GetName() : alias);
                    switch (field.GetSearchOperator())
                    {
                    case Net.Vpc.Upa.SearchOperator.DEFAULT:
                    case Net.Vpc.Upa.SearchOperator.EQ:
                    {
                        if (field.GetDataType() is Net.Vpc.Upa.Types.ManyToOneType)
                        {
                            Net.Vpc.Upa.Types.ManyToOneType et   = (Net.Vpc.Upa.Types.ManyToOneType)field.GetDataType();
                            Net.Vpc.Upa.Key foreignKey           = et.GetRelationship().GetTargetRole().GetEntity().GetBuilder().ObjectToKey(@value);
                            Net.Vpc.Upa.Expressions.Expression b = null;
                            int i = 0;
                            foreach (Net.Vpc.Upa.Field df in et.GetRelationship().GetSourceRole().GetFields())
                            {
                                e = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var((Net.Vpc.Upa.Expressions.Var)p.Copy(), df.GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(foreignKey.GetObjectAt(i)));
                                b = b == null ? ((Net.Vpc.Upa.Expressions.Expression)(b)) : new Net.Vpc.Upa.Expressions.And(b, e);
                                i++;
                            }
                        }
                        else
                        {
                            e = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value));
                        }
                        break;
                    }

                    case Net.Vpc.Upa.SearchOperator.NE:
                    {
                        e = new Net.Vpc.Upa.Expressions.Different(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value));
                        break;
                    }

                    case Net.Vpc.Upa.SearchOperator.GT:
                    {
                        e = new Net.Vpc.Upa.Expressions.GreaterThan(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value));
                        break;
                    }

                    case Net.Vpc.Upa.SearchOperator.GTE:
                    {
                        e = new Net.Vpc.Upa.Expressions.GreaterEqualThan(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value));
                        break;
                    }

                    case Net.Vpc.Upa.SearchOperator.LT:
                    {
                        e = new Net.Vpc.Upa.Expressions.LessThan(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value));
                        break;
                    }

                    case Net.Vpc.Upa.SearchOperator.LTE:
                    {
                        e = new Net.Vpc.Upa.Expressions.LessEqualThan(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value));
                        break;
                    }

                    case Net.Vpc.Upa.SearchOperator.CONTAINS:
                    {
                        e = new Net.Vpc.Upa.Expressions.Like(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral("%" + @value + "%"));
                        break;
                    }

                    case Net.Vpc.Upa.SearchOperator.STARTS_WITH:
                    {
                        e = new Net.Vpc.Upa.Expressions.Like(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(@value + "%"));
                        break;
                    }

                    case Net.Vpc.Upa.SearchOperator.ENDS_WITH:
                    {
                        e = new Net.Vpc.Upa.Expressions.Like(new Net.Vpc.Upa.Expressions.Var(p, key), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral("%" + @value));
                        break;
                    }
                    }
                    if (e != null)
                    {
                        a = a == null ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(a, e);
                    }
                }
            }
            return(a);
        }
Beispiel #9
0
        public static Net.Vpc.Upa.Expressions.FunctionExpression CreateFunction(string name, System.Collections.Generic.IList <Net.Vpc.Upa.Expressions.Expression> args)
        {
            string uniformName = name.ToLower();

            if (uniformName.Equals("average"))
            {
                CheckArgCount(name, args, 1);
                return(new Net.Vpc.Upa.Expressions.Avg(args[0]));
            }
            if (uniformName.Equals("min"))
            {
                CheckArgCount(name, args, 1);
                return(new Net.Vpc.Upa.Expressions.Min(args[0]));
            }
            if (uniformName.Equals("max"))
            {
                CheckArgCount(name, args, 1);
                return(new Net.Vpc.Upa.Expressions.Max(args[0]));
            }
            if (uniformName.Equals("sum"))
            {
                CheckArgCount(name, args, 1);
                return(new Net.Vpc.Upa.Expressions.Sum(args[0]));
            }
            if (uniformName.Equals("count"))
            {
                CheckArgCount(name, args, 1);
                return(new Net.Vpc.Upa.Expressions.Count(args[0]));
            }
            if (uniformName.Equals("d2v"))
            {
                CheckArgCount(name, args, 1);
                return(new Net.Vpc.Upa.Expressions.D2V(args[0]));
            }
            if (uniformName.Equals("i2v"))
            {
                CheckArgCount(name, args, 1);
                return(new Net.Vpc.Upa.Expressions.I2V(args[0]));
            }
            if (uniformName.Equals("concat"))
            {
                return(new Net.Vpc.Upa.Expressions.Concat(args.ToArray()));
            }
            if (uniformName.Equals("cast"))
            {
                CheckArgCount(name, args, 2);
                Net.Vpc.Upa.Expressions.Expression e    = (Net.Vpc.Upa.Expressions.Expression)args[0];
                Net.Vpc.Upa.Expressions.Expression type = (Net.Vpc.Upa.Expressions.Expression)args[1];
                Net.Vpc.Upa.Types.DataType         d    = null;
                if (type is Net.Vpc.Upa.Expressions.Var)
                {
                    string s = ((Net.Vpc.Upa.Expressions.Var)type).GetName();
                    if ("string".Equals(s))
                    {
                        d = Net.Vpc.Upa.Types.TypesFactory.STRING;
                    }
                    else if ("int".Equals(s))
                    {
                        d = Net.Vpc.Upa.Types.TypesFactory.INT;
                    }
                    else if ("double".Equals(s))
                    {
                        d = Net.Vpc.Upa.Types.TypesFactory.DOUBLE;
                    }
                    else if ("float".Equals(s))
                    {
                        d = Net.Vpc.Upa.Types.TypesFactory.FLOAT;
                    }
                    else if ("long".Equals(s))
                    {
                        d = Net.Vpc.Upa.Types.TypesFactory.LONG;
                    }
                    else if ("date".Equals(s))
                    {
                        d = Net.Vpc.Upa.Types.TypesFactory.DATE;
                    }
                    else if ("datetime".Equals(s))
                    {
                        d = Net.Vpc.Upa.Types.TypesFactory.DATETIME;
                    }
                    else if ("boolean".Equals(s))
                    {
                        d = Net.Vpc.Upa.Types.TypesFactory.BOOLEAN;
                    }
                    else
                    {
                        d = new Net.Vpc.Upa.Types.ManyToOneType(name, null, name, true, true);
                    }
                }
                else
                {
                    throw new System.ArgumentException("Unupported cast type");
                }
                return(new Net.Vpc.Upa.Expressions.Cast(e, d));
            }
            if (uniformName.Equals("coalesce"))
            {
                return(new Net.Vpc.Upa.Expressions.Coalesce(args));
            }
            if (uniformName.Equals("decode"))
            {
                return(new Net.Vpc.Upa.Expressions.Decode(args));
            }
            if (uniformName.Equals("sign"))
            {
                CheckArgCount(name, args, 1);
                return(new Net.Vpc.Upa.Expressions.Sign(args[0]));
            }
            if (uniformName.Equals("now") || uniformName.Equals("currenttimestamp"))
            {
                CheckArgCount(name, args, 0);
                return(new Net.Vpc.Upa.Expressions.CurrentTimestamp());
            }
            if (uniformName.Equals("currenttime"))
            {
                CheckArgCount(name, args, 0);
                return(new Net.Vpc.Upa.Expressions.CurrentTime());
            }
            if (uniformName.Equals("today") || uniformName.Equals("currentdate"))
            {
                CheckArgCount(name, args, 0);
                return(new Net.Vpc.Upa.Expressions.CurrentDate());
            }
            if (uniformName.Equals("currentuser"))
            {
                CheckArgCount(name, args, 0);
                return(new Net.Vpc.Upa.Expressions.CurrentUser());
            }
            if (uniformName.Equals("dateadd"))
            {
                CheckArgCount(name, args, 3);
                Net.Vpc.Upa.Expressions.Expression type  = (Net.Vpc.Upa.Expressions.Expression)args[0];
                Net.Vpc.Upa.Expressions.Expression count = (Net.Vpc.Upa.Expressions.Expression)args[1];
                Net.Vpc.Upa.Expressions.Expression date  = (Net.Vpc.Upa.Expressions.Expression)args[2];
                return(new Net.Vpc.Upa.Expressions.DateAdd((Net.Vpc.Upa.Expressions.DatePartType)(System.Enum.Parse(typeof(Net.Vpc.Upa.Expressions.DatePartType), ((Net.Vpc.Upa.Expressions.Var)type).GetName().ToUpper())), count, date));
            }
            if (uniformName.Equals("datediff"))
            {
                CheckArgCount(name, args, 3);
                Net.Vpc.Upa.Expressions.Expression type  = (Net.Vpc.Upa.Expressions.Expression)args[0];
                Net.Vpc.Upa.Expressions.Expression start = (Net.Vpc.Upa.Expressions.Expression)args[1];
                Net.Vpc.Upa.Expressions.Expression end   = (Net.Vpc.Upa.Expressions.Expression)args[2];
                return(new Net.Vpc.Upa.Expressions.DateDiff((Net.Vpc.Upa.Expressions.DatePartType)(System.Enum.Parse(typeof(Net.Vpc.Upa.Expressions.DatePartType), ((Net.Vpc.Upa.Expressions.Var)type).GetName().ToUpper())), start, end));
            }
            if (uniformName.Equals("datetrunc"))
            {
                CheckArgCount(name, args, 2);
                Net.Vpc.Upa.Expressions.Expression type = (Net.Vpc.Upa.Expressions.Expression)args[0];
                Net.Vpc.Upa.Expressions.Expression e    = (Net.Vpc.Upa.Expressions.Expression)args[1];
                return(new Net.Vpc.Upa.Expressions.DateTrunc((Net.Vpc.Upa.Expressions.DatePartType)(System.Enum.Parse(typeof(Net.Vpc.Upa.Expressions.DatePartType), ((Net.Vpc.Upa.Expressions.Var)type).GetName().ToUpper())), e));
            }
            if (uniformName.Equals("datepart"))
            {
                CheckArgCount(name, args, 2);
                Net.Vpc.Upa.Expressions.Expression type = (Net.Vpc.Upa.Expressions.Expression)args[0];
                Net.Vpc.Upa.Expressions.Expression e    = (Net.Vpc.Upa.Expressions.Expression)args[1];
                return(new Net.Vpc.Upa.Expressions.DatePart((Net.Vpc.Upa.Expressions.DatePartType)(System.Enum.Parse(typeof(Net.Vpc.Upa.Expressions.DatePartType), ((Net.Vpc.Upa.Expressions.Var)type).GetName().ToUpper())), e));
            }
            if (uniformName.Equals("monthstart"))
            {
                switch ((args).Count)
                {
                case 0:
                {
                    return(new Net.Vpc.Upa.Expressions.MonthStart());
                }

                case 1:
                {
                    return(new Net.Vpc.Upa.Expressions.MonthStart(args[0]));
                }

                case 2:
                {
                    return(new Net.Vpc.Upa.Expressions.MonthStart(args[0], args[2]));
                }
                }
                CheckArgCount(name, args, 2);
            }
            if (uniformName.Equals("monthend"))
            {
                switch ((args).Count)
                {
                case 0:
                {
                    return(new Net.Vpc.Upa.Expressions.MonthEnd());
                }

                case 1:
                {
                    return(new Net.Vpc.Upa.Expressions.MonthEnd(args[0]));
                }

                case 2:
                {
                    return(new Net.Vpc.Upa.Expressions.MonthEnd(args[0], args[2]));
                }
                }
                CheckArgCount(name, args, 2);
            }
            if (uniformName.Equals("exists"))
            {
                CheckArgCount(name, args, 1);
                return(new Net.Vpc.Upa.Expressions.Exists((Net.Vpc.Upa.Expressions.QueryStatement)args[0]));
            }
            if (uniformName.Equals("ishierarchydescendant"))
            {
                if ((args).Count == 3)
                {
                    return(new Net.Vpc.Upa.Expressions.IsHierarchyDescendent(args[0], args[1], args[2]));
                }
                else if ((args).Count == 2)
                {
                    return(new Net.Vpc.Upa.Expressions.IsHierarchyDescendent(args[0], args[1], null));
                }
                else
                {
                    throw new System.Exception("function " + name + " expects 2 or 3 argument(s) but found " + (args).Count);
                }
            }
            return(new Net.Vpc.Upa.Impl.Uql.QLFunctionExpression(name, args.ToArray()));
        }
Beispiel #10
0
 public override string GetSQL(object o, Net.Vpc.Upa.Persistence.EntityExecutionContext qlContext, Net.Vpc.Upa.Impl.Persistence.SQLManager sqlManager, Net.Vpc.Upa.Impl.Uql.ExpressionDeclarationList declarations)
 {
     Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledKeyEnumerationExpression ee = (Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledKeyEnumerationExpression)o;
     Net.Vpc.Upa.Entity entity = null;
     Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledVar compiledVar = null;
     if (ee.GetAlias() != null)
     {
         compiledVar = ee.GetAlias();
         entity      = (compiledVar.GetReferrer() is Net.Vpc.Upa.Entity) ? ((Net.Vpc.Upa.Entity)compiledVar.GetReferrer()) : null;
     }
     else
     {
         //check if alias
         System.Collections.Generic.IList <Net.Vpc.Upa.Impl.Uql.ExpressionDeclaration> dvalues = ee.GetDeclarations(null);
         if (dvalues != null)
         {
             foreach (Net.Vpc.Upa.Impl.Uql.ExpressionDeclaration @ref in dvalues)
             {
                 switch (@ref.GetReferrerType())
                 {
                 case Net.Vpc.Upa.Impl.Uql.DecObjectType.ENTITY:
                 {
                     entity = qlContext.GetPersistenceUnit().GetEntity((string)@ref.GetReferrerName());
                     break;
                 }
                 }
             }
         }
     }
     if (entity == null)
     {
         throw new System.ArgumentException("Key enumeration must by associated to and entity");
     }
     if ((ee.GetKeys().Count == 0))
     {
         return(sqlManager.GetSQL(new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEquals(new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledLiteral(1), new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledLiteral(2)), qlContext, declarations));
     }
     System.Collections.Generic.IList <Net.Vpc.Upa.Field> pfs             = entity.GetPrimaryFields();
     Net.Vpc.Upa.Impl.Uql.Compiledexpression.DefaultCompiledExpression o2 = null;
     foreach (object key in ee.GetKeys())
     {
         Net.Vpc.Upa.Impl.Uql.Compiledexpression.DefaultCompiledExpression a = null;
         bool processed = false;
         if (entity.GetPersistenceUnit().ContainsEntity(entity.GetIdType()))
         {
             if (!entity.GetIdType().IsInstanceOfType(key))
             {
                 //primitive seen as entity?
                 // A's id is A.b where b is an entity
                 //TODO fix all cases!
                 if ((entity.GetPrimaryFields()).Count == 1)
                 {
                     Net.Vpc.Upa.Types.ManyToOneType et = (Net.Vpc.Upa.Types.ManyToOneType)entity.GetPrimaryFields()[0].GetDataType();
                     System.Collections.Generic.IList <Net.Vpc.Upa.Field> ff = et.GetRelationship().GetSourceRole().GetFields();
                     Net.Vpc.Upa.Key key2 = et.GetRelationship().GetTargetEntity().GetBuilder().IdToKey(key);
                     for (int j = 0; j < (ff).Count; j++)
                     {
                         Net.Vpc.Upa.Field f = ff[j];
                         Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledVar rr = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledVar(f);
                         Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledVar p2 = compiledVar == null ? null : (Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledVar)compiledVar.Copy();
                         if (p2 == null)
                         {
                             p2 = rr;
                         }
                         else
                         {
                             p2.SetChild(rr);
                         }
                         Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEquals v = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEquals(p2, new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledLiteral(key2.GetObjectAt(j), Net.Vpc.Upa.Impl.Util.UPAUtils.GetTypeTransformOrIdentity(f)));
                         if (a == null)
                         {
                             a = v;
                         }
                         else
                         {
                             a = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledAnd(a, v);
                         }
                     }
                     if (o2 == null)
                     {
                         o2 = a;
                     }
                     else
                     {
                         o2 = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledOr(o2, a);
                     }
                     processed = true;
                 }
             }
         }
         if (!processed)
         {
             Net.Vpc.Upa.Key uKey = entity.GetBuilder().IdToKey(key);
             for (int j = 0; j < (pfs).Count; j++)
             {
                 Net.Vpc.Upa.Field f = pfs[j];
                 Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledVar rr = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledVar(f);
                 Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledVar p2 = compiledVar == null ? null : (Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledVar)compiledVar.Copy();
                 if (p2 == null)
                 {
                     p2 = rr;
                 }
                 else
                 {
                     p2.SetChild(rr);
                 }
                 Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEquals v = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledEquals(p2, new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledLiteral(uKey.GetObjectAt(j), Net.Vpc.Upa.Impl.Util.UPAUtils.GetTypeTransformOrIdentity(f)));
                 if (a == null)
                 {
                     a = v;
                 }
                 else
                 {
                     a = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledAnd(a, v);
                 }
             }
             if (o2 == null)
             {
                 o2 = a;
             }
             else
             {
                 o2 = new Net.Vpc.Upa.Impl.Uql.Compiledexpression.CompiledOr(o2, a);
             }
         }
     }
     return(sqlManager.GetSQL(o2, qlContext, declarations));
 }
 private bool Build(bool throwErrors)
 {
     if (sourceEntity == null)
     {
         if (relationDescriptor.GetSourceEntity() != null)
         {
             if (persistenceUnit.ContainsEntity(relationDescriptor.GetSourceEntity()))
             {
                 sourceEntity = persistenceUnit.GetEntity(relationDescriptor.GetSourceEntity());
             }
         }
     }
     if (sourceEntity == null)
     {
         if (relationDescriptor.GetSourceEntityType() != null)
         {
             if (persistenceUnit.ContainsEntity(relationDescriptor.GetSourceEntityType()))
             {
                 sourceEntity = persistenceUnit.GetEntity(relationDescriptor.GetSourceEntityType());
             }
         }
     }
     if (targetEntity == null)
     {
         if (relationDescriptor.GetTargetEntity() != null)
         {
             if (persistenceUnit.ContainsEntity(relationDescriptor.GetTargetEntity()))
             {
                 targetEntity = persistenceUnit.GetEntity(relationDescriptor.GetTargetEntity());
             }
         }
     }
     if (targetEntity == null)
     {
         if (relationDescriptor.GetTargetEntityType() != null)
         {
             if (persistenceUnit.ContainsEntity(relationDescriptor.GetTargetEntityType()))
             {
                 targetEntity = persistenceUnit.GetEntity(relationDescriptor.GetTargetEntityType());
             }
         }
     }
     if (sourceEntity == null)
     {
         if (throwErrors)
         {
             throw new Net.Vpc.Upa.Exceptions.UPAException("InvalidRelationEntityNotFound", relationDescriptor.GetSourceEntityType());
         }
         else
         {
             return(false);
         }
     }
     if (targetEntity == null)
     {
         if (throwErrors)
         {
             throw new Net.Vpc.Upa.Exceptions.UPAException("InvalidRelationEntityNotFound", relationDescriptor.GetTargetEntityType());
         }
         else
         {
             return(false);
         }
     }
     sourceUpdateType = Net.Vpc.Upa.RelationshipUpdateType.FLAT;
     sourceFieldNames = new System.Collections.Generic.List <string>();
     if (relationDescriptor.GetBaseField() == null)
     {
         Net.Vpc.Upa.Impl.FwkConvertUtils.ListAddRange(sourceFieldNames, new System.Collections.Generic.List <string>(relationDescriptor.GetSourceFields()));
         if (relationDescriptor.GetMappedTo() != null && relationDescriptor.GetMappedTo().Length > 0)
         {
             if (relationDescriptor.GetMappedTo().Length > 1)
             {
                 throw new System.ArgumentException("mappedTo cannot only apply to single Entity Field");
             }
             manyToOneField = sourceEntity.GetField(relationDescriptor.GetMappedTo()[0]);
         }
     }
     else
     {
         Net.Vpc.Upa.Field          baseField     = sourceEntity.GetField(relationDescriptor.GetBaseField());
         Net.Vpc.Upa.Types.DataType baseFieldType = baseField.GetDataType();
         if (baseFieldType is Net.Vpc.Upa.Types.ManyToOneType)
         {
             Net.Vpc.Upa.Types.ManyToOneType et = (Net.Vpc.Upa.Types.ManyToOneType)baseFieldType;
             if (et.GetTargetEntityName() == null || (et.GetTargetEntityName().Length == 0))
             {
                 et.SetTargetEntityName(targetEntity.GetName());
             }
             sourceUpdateType = Net.Vpc.Upa.RelationshipUpdateType.COMPOSED;
             System.Collections.Generic.IList <Net.Vpc.Upa.Field> masterPK = targetEntity.GetPrimaryFields();
             if (relationDescriptor.GetMappedTo() == null || relationDescriptor.GetMappedTo().Length == 0)
             {
                 if ((masterPK.Count == 0))
                 {
                     if (throwErrors)
                     {
                         throw new Net.Vpc.Upa.Exceptions.UPAException("PrimaryFieldsNotFoundException", targetEntity.GetName());
                     }
                     else
                     {
                         return(false);
                     }
                 }
                 else
                 {
                     foreach (Net.Vpc.Upa.Field masterField in masterPK)
                     {
                         string f = masterField.GetName();
                         if ((f).Length == 1)
                         {
                             f = f.ToUpper();
                         }
                         else if ((f).Length > 1)
                         {
                             f = f.Substring(0, 1).ToUpper() + f.Substring(1);
                         }
                         string extraName = baseField.GetName() + f;
                         sourceFieldNames.Add(extraName);
                     }
                 }
             }
             else
             {
                 Net.Vpc.Upa.Impl.FwkConvertUtils.ListAddRange(sourceFieldNames, new System.Collections.Generic.List <string>(relationDescriptor.GetMappedTo()));
             }
             if ((sourceFieldNames).Count != (masterPK).Count)
             {
                 if (throwErrors)
                 {
                     throw new System.ArgumentException("Incorrect parameters");
                 }
                 else
                 {
                     return(false);
                 }
             }
             if ((sourceFieldNames.Count == 0))
             {
                 if (throwErrors)
                 {
                     throw new System.ArgumentException("Incorrect parameters");
                 }
                 else
                 {
                     return(false);
                 }
             }
             for (int i = 0; i < (sourceFieldNames).Count; i++)
             {
                 string            extraName = sourceFieldNames[i];
                 Net.Vpc.Upa.Field idField   = sourceEntity.FindField(extraName);
                 if (idField == null)
                 {
                     Net.Vpc.Upa.Types.DataType dt = (Net.Vpc.Upa.Types.DataType)masterPK[i].GetDataType().Copy();
                     bool nullable = baseFieldType.IsNullable();
                     dt.SetNullable(nullable);
                     idField = sourceEntity.AddField(extraName, "system", Net.Vpc.Upa.FlagSets.Of <E>(Net.Vpc.Upa.UserFieldModifier.SYSTEM), Net.Vpc.Upa.FlagSets.Of <E>(Net.Vpc.Upa.UserFieldModifier.UPDATE), null, dt, -1);
                     idField.SetAccessLevel(Net.Vpc.Upa.AccessLevel.PRIVATE);
                 }
                 else
                 {
                     idField.SetUserExcludeModifiers(idField.GetUserExcludeModifiers().Add(Net.Vpc.Upa.UserFieldModifier.UPDATE));
                 }
             }
             manyToOneField = baseField;
         }
         else
         {
             sourceFieldNames.Add(baseField.GetName());
             if (relationDescriptor.GetMappedTo() != null && relationDescriptor.GetMappedTo().Length > 0)
             {
                 if (relationDescriptor.GetMappedTo().Length > 1)
                 {
                     throw new System.ArgumentException("mappedTo cannot only apply to single Entity Field");
                 }
                 manyToOneField = sourceEntity.GetField(relationDescriptor.GetMappedTo()[0]);
             }
         }
     }
     nullable = true;
     //TODO FIX ME
     for (int i = 0; i < (sourceFieldNames).Count; i++)
     {
         Net.Vpc.Upa.Field          slaveField = sourceEntity.GetField(sourceFieldNames[i]);
         Net.Vpc.Upa.Types.DataType dataType   = slaveField.GetDataType();
         if (dataType == null)
         {
             //inherit master DataType
             if ((targetEntity.GetPrimaryFields()).Count > i)
             {
                 Net.Vpc.Upa.Types.DataType d = targetEntity.GetPrimaryFields()[i].GetDataType();
                 d = (Net.Vpc.Upa.Types.DataType)d.Copy();
                 d.SetNullable(nullable);
                 slaveField.SetDataType(d);
                 //reset transform!
                 slaveField.SetTypeTransform(null);
             }
             else
             {
                 throw new System.ArgumentException("Invalid Relation");
             }
         }
     }
     filter = relationDescriptor.GetFilter();
     //        if (baseFieldType instanceof ManyToOneType) {
     //            manyToOneField = baseField;
     //        } else if (sourceFieldNames.size() == 1) {
     //            DataType slaveType = slaveField.getDataType();
     //            if (slaveType instanceof ManyToOneType) {
     //                manyToOneField = slaveField;
     //            }
     //        }
     return(true);
 }
        public virtual void CommitModelChanged() /* throws Net.Vpc.Upa.Exceptions.UPAException */
        {
            Net.Vpc.Upa.Entity sourceEntity = GetSourceRole().GetEntity();
            Net.Vpc.Upa.Entity targetEntity = GetTargetRole().GetEntity();
            if (sourceEntity == null || targetEntity == null)
            {
                throw new Net.Vpc.Upa.Exceptions.UPAException("InvalidRelationDefinition");
            }
            if (!sourceEntity.GetUserExcludeModifiers().Contains(Net.Vpc.Upa.EntityModifier.VALIDATE_PERSIST))
            {
                sourceEntity.GetModifiers().Add(Net.Vpc.Upa.EntityModifier.VALIDATE_PERSIST);
            }
            if (!sourceEntity.GetUserExcludeModifiers().Contains(Net.Vpc.Upa.EntityModifier.VALIDATE_UPDATE))
            {
                sourceEntity.GetModifiers().Add(Net.Vpc.Upa.EntityModifier.VALIDATE_UPDATE);
            }
            System.Collections.Generic.IList <Net.Vpc.Upa.Field> sourceFieldsList = sourceRole.GetFields();
            Net.Vpc.Upa.Field[] sourceFields = sourceFieldsList.ToArray();
            Net.Vpc.Upa.KeyType keyType      = new Net.Vpc.Upa.KeyType(targetEntity, filter, false);
            SetDataType(keyType);
            tuningMaxInline = GetPersistenceUnit().GetProperties().GetInt((typeof(Net.Vpc.Upa.Relationship)).FullName + ".maxInline", 10);
            System.Collections.Generic.IList <Net.Vpc.Upa.Field> targetFieldsList = targetEntity.GetPrimaryFields();
            Net.Vpc.Upa.Field[] targetFields = targetFieldsList.ToArray();
            ;
            // some checks
            if (sourceFields.Length == 0)
            {
                throw new System.ArgumentException("No source fields are specified");
            }
            if (targetFields.Length == 0)
            {
                throw new System.ArgumentException("Target Entity " + targetEntity.GetName() + " has no primary fields");
            }
            if (sourceFields.Length != targetFields.Length)
            {
                throw new System.ArgumentException("source fields and target fields have not the same count");
            }
            sourceEntity.AddDependencyOnEntity(targetEntity.GetName());
            if (dataType == null)
            {
                dataType = targetEntity.GetDataType();
            }
            if (dataType.IsNullable() != nullable)
            {
                Net.Vpc.Upa.Types.DataType trCloned = (Net.Vpc.Upa.Types.DataType)dataType.Copy();
                trCloned.SetNullable(nullable);
                dataType = trCloned;
            }
            this.sourceToTargetKeyMap = new System.Collections.Generic.Dictionary <string, string>(sourceFields.Length);
            this.targetToSourceKeyMap = new System.Collections.Generic.Dictionary <string, string>(sourceFields.Length);
            //        if ((theSourceTable  instanceof View))
            //            this.type = 0;
            for (int i = 0; i < sourceFields.Length; i++)
            {
                if (sourceFields[i].GetModifiers().Contains(Net.Vpc.Upa.FieldModifier.TRANSIENT) && this.relationType != Net.Vpc.Upa.RelationshipType.TRANSIENT)
                {
                    //Log. System.err.println("Type should be VIEW for " + getName());
                    this.relationType = Net.Vpc.Upa.RelationshipType.TRANSIENT;
                }
                else if (sourceFields[i].GetUpdateFormula() != null && this.relationType != Net.Vpc.Upa.RelationshipType.TRANSIENT && this.relationType != Net.Vpc.Upa.RelationshipType.SHADOW_ASSOCIATION)
                {
                    // Log. System.err.println("Type should be either VIEW or SHADOW for " + name);
                    this.relationType = Net.Vpc.Upa.RelationshipType.SHADOW_ASSOCIATION;
                }
                this.sourceToTargetKeyMap[sourceFields[i].GetName()] = targetFields[i].GetName();
                this.targetToSourceKeyMap[targetFields[i].GetName()] = sourceFields[i].GetName();
                //            targetFields[i].addManyToOneRelation(this);
                ((Net.Vpc.Upa.Impl.AbstractField)sourceFields[i]).SetEffectiveModifiers(sourceFields[i].GetModifiers().Add(Net.Vpc.Upa.FieldModifier.FOREIGN));
                ((Net.Vpc.Upa.Impl.AbstractField)targetFields[i]).SetEffectiveModifiers(targetFields[i].GetModifiers().Add(Net.Vpc.Upa.FieldModifier.REFERENCED));
                //            if (sourceFields[i].getTitle() == null) {
                //                sourceFields[i].setTitle(targetFields[i].getTitle());
                //            }
                if (sourceFields[i].GetDataType() == null)
                {
                    Net.Vpc.Upa.Types.DataType tr = targetFields[i].GetDataType();
                    if (tr.IsNullable() == nullable)
                    {
                        sourceFields[i].SetDataType(tr);
                    }
                    else
                    {
                        Net.Vpc.Upa.Types.DataType trCloned = (Net.Vpc.Upa.Types.DataType)tr.Copy();
                        trCloned.SetNullable(nullable);
                        sourceFields[i].SetDataType(trCloned);
                    }
                }
            }
            if (GetSourceRole().GetEntityField() != null)
            {
                Net.Vpc.Upa.Field          sourceEntityField = GetSourceRole().GetEntityField();
                Net.Vpc.Upa.Types.DataType dt = sourceEntityField.GetDataType();
                if (dt is Net.Vpc.Upa.Types.ManyToOneType)
                {
                    Net.Vpc.Upa.Types.ManyToOneType edt = (Net.Vpc.Upa.Types.ManyToOneType)dt;
                    edt.SetRelationship(this);
                }
            }
            if (GetTargetRole().GetEntityField() != null)
            {
                Net.Vpc.Upa.Field          targetEntityField = GetTargetRole().GetEntityField();
                Net.Vpc.Upa.Types.DataType dt = targetEntityField.GetDataType();
                if (dt is Net.Vpc.Upa.Types.ManyToOneType)
                {
                    Net.Vpc.Upa.Types.ManyToOneType edt = (Net.Vpc.Upa.Types.ManyToOneType)dt;
                    edt.SetRelationship(this);
                }
            }
            this.sourceToTargetKeyMap = Net.Vpc.Upa.Impl.Util.PlatformUtils.UnmodifiableMap <string, string>(sourceToTargetKeyMap);
            this.targetToSourceKeyMap = Net.Vpc.Upa.Impl.Util.PlatformUtils.UnmodifiableMap <string, string>(targetToSourceKeyMap);
            SetI18NString(new Net.Vpc.Upa.Types.I18NString("Relationship").Append(GetName()));
            SetTitle(GetI18NString().Append(".title"));
            SetDescription(GetI18NString().Append(".desc"));
            System.Text.StringBuilder preferredPersistenceName = new System.Text.StringBuilder((GetName()).Length);
            for (int i = 0; i < (GetName()).Length; i++)
            {
                if (Net.Vpc.Upa.Expressions.ExpressionHelper.IsIdentifierPart(GetName()[i]))
                {
                    preferredPersistenceName.Append(GetName()[i]);
                }
                else
                {
                    preferredPersistenceName.Append('_');
                }
            }
            SetPersistenceName(preferredPersistenceName.ToString());
            if (GetRelationshipType() == Net.Vpc.Upa.RelationshipType.COMPOSITION)
            {
                ((Net.Vpc.Upa.Impl.DefaultEntity)sourceEntity).SetCompositionRelationship(this);
            }
            targetRole.SetFields(targetFields);
            Net.Vpc.Upa.Impl.Util.UPAUtils.Prepare(GetPersistenceUnit(), targetRole, this.GetName() + "_" + targetRole.GetRelationshipRoleType());
            Net.Vpc.Upa.Impl.Util.UPAUtils.Prepare(GetPersistenceUnit(), sourceRole, this.GetName() + "_" + sourceRole.GetRelationshipRoleType());
            if ((((GetTargetRole().GetEntity().GetExtensionDefinitions <Net.Vpc.Upa.Extensions.ViewEntityExtensionDefinition>(typeof(Net.Vpc.Upa.Extensions.ViewEntityExtensionDefinition))).Count > 0) || ((GetSourceRole().GetEntity().GetExtensionDefinitions <Net.Vpc.Upa.Extensions.ViewEntityExtensionDefinition>(typeof(Net.Vpc.Upa.Extensions.ViewEntityExtensionDefinition))).Count > 0)) && relationType != Net.Vpc.Upa.RelationshipType.TRANSIENT)
            {
                throw new System.ArgumentException(this + " : Relationship Type must be TYPE_VIEW");
            }
            if (((GetTargetRole().GetEntity().GetShield().IsTransient()) || (GetSourceRole().GetEntity().GetShield().IsTransient())) && relationType != Net.Vpc.Upa.RelationshipType.TRANSIENT)
            {
                throw new System.ArgumentException(this + " : Relationship Type must be TYPE_VIEW");
            }
            Net.Vpc.Upa.FlagSet <Net.Vpc.Upa.FieldModifier> modifierstoRemove = Net.Vpc.Upa.FlagSets.OfType <Net.Vpc.Upa.FieldModifier>().AddAll(Net.Vpc.Upa.FieldModifier.PERSIST, Net.Vpc.Upa.FieldModifier.PERSIST_DEFAULT, Net.Vpc.Upa.FieldModifier.PERSIST_FORMULA, Net.Vpc.Upa.FieldModifier.PERSIST_SEQUENCE, Net.Vpc.Upa.FieldModifier.UPDATE, Net.Vpc.Upa.FieldModifier.UPDATE_DEFAULT, Net.Vpc.Upa.FieldModifier.UPDATE_FORMULA, Net.Vpc.Upa.FieldModifier.UPDATE_SEQUENCE);
            switch (GetSourceRole().GetRelationshipUpdateType())
            {
            case Net.Vpc.Upa.RelationshipUpdateType.FLAT:
            {
                Net.Vpc.Upa.Field f = GetSourceRole().GetEntityField();
                if (f != null)
                {
                    ((Net.Vpc.Upa.Impl.AbstractField)f).SetEffectiveModifiers(f.GetModifiers().RemoveAll(modifierstoRemove));
                }
                break;
            }

            case Net.Vpc.Upa.RelationshipUpdateType.COMPOSED:
            {
                System.Collections.Generic.IList <Net.Vpc.Upa.Field> fields = GetSourceRole().GetFields();
                if (fields != null)
                {
                    foreach (Net.Vpc.Upa.Field f in fields)
                    {
                        ((Net.Vpc.Upa.Impl.AbstractField)f).SetEffectiveModifiers(f.GetModifiers().RemoveAll(modifierstoRemove));
                    }
                }
                break;
            }
            }
        }
 public virtual int Update(Net.Vpc.Upa.Entity entity, Net.Vpc.Upa.Persistence.EntityExecutionContext context, Net.Vpc.Upa.Record updates, Net.Vpc.Upa.Expressions.Expression condition) /* throws Net.Vpc.Upa.Exceptions.UPAException */
 {
     Net.Vpc.Upa.Expressions.Update u = new Net.Vpc.Upa.Expressions.Update().Entity(entity.GetName());
     foreach (string fieldName in updates.KeySet())
     {
         Net.Vpc.Upa.Field f = entity.FindField(fieldName);
         if (f != null && Net.Vpc.Upa.Impl.Util.Filters.Fields2.UPDATE.Accept(f))
         {
             object @value = updates.GetObject <T>(fieldName);
             if ((f.GetDataType() is Net.Vpc.Upa.Types.ManyToOneType))
             {
                 Net.Vpc.Upa.Types.ManyToOneType e = (Net.Vpc.Upa.Types.ManyToOneType)f.GetDataType();
                 if (e.IsUpdatable())
                 {
                     Net.Vpc.Upa.Entity        masterEntity = context.GetPersistenceUnit().GetEntity(e.GetTargetEntityName());
                     Net.Vpc.Upa.EntityBuilder mbuilder     = masterEntity.GetBuilder();
                     if (@value is Net.Vpc.Upa.Expressions.Expression)
                     {
                         Net.Vpc.Upa.Expressions.Expression evalue;
                         System.Collections.Generic.IList <Net.Vpc.Upa.Field> sfields = e.GetRelationship().GetSourceRole().GetFields();
                         System.Collections.Generic.IList <Net.Vpc.Upa.Field> tfields = e.GetRelationship().GetTargetRole().GetFields();
                         for (int i = 0; i < (sfields).Count; i++)
                         {
                             Net.Vpc.Upa.Field fk  = sfields[i];
                             Net.Vpc.Upa.Field fid = tfields[i];
                             evalue = ((Net.Vpc.Upa.Expressions.Expression)@value).Copy();
                             evalue = context.GetPersistenceUnit().GetExpressionManager().ParseExpression(evalue);
                             if (evalue is Net.Vpc.Upa.Expressions.Select)
                             {
                                 Net.Vpc.Upa.Expressions.Select svalue = (Net.Vpc.Upa.Expressions.Select)evalue;
                                 if (svalue.CountFields() != 1)
                                 {
                                     throw new System.Exception("Invalid Expression " + svalue + " as formula for field " + f.GetAbsoluteName());
                                 }
                                 if (svalue.GetField(0).GetExpression() is Net.Vpc.Upa.Expressions.Var)
                                 {
                                     svalue.GetField(0).SetExpression(new Net.Vpc.Upa.Expressions.Var((Net.Vpc.Upa.Expressions.Var)svalue.GetField(0).GetExpression(), fid.GetName()));
                                 }
                                 else
                                 {
                                     throw new System.Exception("Invalid Expression " + svalue + " as formula for field " + f.GetAbsoluteName());
                                 }
                             }
                             else if (evalue is Net.Vpc.Upa.Expressions.Var)
                             {
                                 evalue = (new Net.Vpc.Upa.Expressions.Var((Net.Vpc.Upa.Expressions.Var)evalue, fk.GetName()));
                             }
                             else if (evalue is Net.Vpc.Upa.Expressions.Param)
                             {
                             }
                             else if (evalue is Net.Vpc.Upa.Expressions.Literal)
                             {
                             }
                             else
                             {
                                 throw new System.Exception("Invalid Expression " + evalue + " as formula for field " + f.GetAbsoluteName());
                             }
                             u.Set(fk.GetName(), evalue);
                         }
                     }
                     else
                     {
                         Net.Vpc.Upa.Key k = null;
                         if (@value is Net.Vpc.Upa.Record)
                         {
                             k = mbuilder.RecordToKey((Net.Vpc.Upa.Record)@value);
                         }
                         else
                         {
                             k = mbuilder.ObjectToKey(@value);
                         }
                         int x = 0;
                         foreach (Net.Vpc.Upa.Field fk in e.GetRelationship().GetSourceRole().GetFields())
                         {
                             u.Set(fk.GetName(), new Net.Vpc.Upa.Expressions.Param(fk.GetName(), k == null ? null : k.GetObjectAt(x)));
                             x++;
                         }
                     }
                 }
             }
             else
             {
                 Net.Vpc.Upa.Expressions.Expression expression = (@value is Net.Vpc.Upa.Expressions.Expression) ? ((Net.Vpc.Upa.Expressions.Expression)((Net.Vpc.Upa.Expressions.Expression)@value)) : new Net.Vpc.Upa.Expressions.Param(null, @value);
                 u.Set(fieldName, expression);
             }
         }
     }
     u.Where(condition);
     return(context.GetPersistenceStore().CreateQuery(u, context).ExecuteNonQuery());
 }