public virtual Net.Vpc.Upa.Key ExtractKey(Net.Vpc.Upa.Record sourceRecord)
        {
            switch (GetSourceRole().GetRelationshipUpdateType())
            {
            case Net.Vpc.Upa.RelationshipUpdateType.COMPOSED:
            {
                object targetEntityVal = sourceRecord.GetObject <T>(GetSourceRole().GetEntityField().GetName());
                if (targetEntityVal == null)
                {
                    return(null);
                }
                Net.Vpc.Upa.EntityBuilder targetConverter = GetTargetRole().GetEntity().GetBuilder();
                return(targetConverter.ObjectToKey(targetEntityVal));
            }

            case Net.Vpc.Upa.RelationshipUpdateType.FLAT:
            {
                System.Collections.Generic.IList <Net.Vpc.Upa.Field> relFields = GetSourceRole().GetFields();
                System.Collections.Generic.List <object>             keys      = new System.Collections.Generic.List <object>((relFields).Count);
                foreach (Net.Vpc.Upa.Field field in relFields)
                {
                    object keyPart = sourceRecord.GetObject <T>(field.GetName());
                    if (keyPart == null)
                    {
                        return(null);
                    }
                    keys.Add(keyPart);
                }
                return(GetTargetRole().GetEntity().CreateKey(keys.ToArray()));
            }
            }
            return(null);
        }
Beispiel #2
0
 public virtual Net.Vpc.Upa.Record ObjectToRecord(object @object, System.Collections.Generic.ISet <string> fields, bool ignoreUnspecified, bool ensureIncludeIds)
 {
     Net.Vpc.Upa.Record r = CreateRecord();
     Net.Vpc.Upa.Record allFieldsRecord = ObjectToRecord(@object, ignoreUnspecified);
     if (fields == null || (fields.Count == 0))
     {
         r.SetAll(r);
         return(r);
     }
     else
     {
         foreach (string k in fields)
         {
             r.SetObject(k, allFieldsRecord.GetObject <T>(k));
         }
         if (ensureIncludeIds)
         {
             foreach (Net.Vpc.Upa.Field o in GetEntity().GetPrimaryFields())
             {
                 string idname = o.GetName();
                 if (!r.IsSet(idname))
                 {
                     r.SetObject(idname, allFieldsRecord.GetObject <T>(idname));
                 }
             }
         }
         return(r);
     }
 }
        public override R RecordToObject <R>(Net.Vpc.Upa.Record record)
        {
            if (record is Net.Vpc.Upa.Impl.BeanAdapterRecord)
            {
                Net.Vpc.Upa.Impl.BeanAdapterRecord g = (Net.Vpc.Upa.Impl.BeanAdapterRecord)record;
                return((R)g.UserObject());
            }
            object obj = CreateObject <R>();

            Net.Vpc.Upa.Record ur = ObjectToRecord(obj, true);
            foreach (string k in record.KeySet())
            {
                object o = record.GetObject <T>(k);
                if (o is Net.Vpc.Upa.Record)
                {
                    Net.Vpc.Upa.Field          f  = entity.FindField(k);
                    Net.Vpc.Upa.Types.DataType dt = f.GetDataType();
                    if (dt is Net.Vpc.Upa.Types.ManyToOneType)
                    {
                        Net.Vpc.Upa.Entity oe = ((Net.Vpc.Upa.Types.ManyToOneType)dt).GetRelationship().GetTargetEntity();
                        o = oe.GetBuilder().RecordToObject <R>((Net.Vpc.Upa.Record)o);
                    }
                }
                ur.SetObject(k, o);
            }
            //        ur.setAll(unstructuredRecord);
            return((R)obj);
        }
        public virtual object ExtractIdByEntityField(Net.Vpc.Upa.Record sourceRecord)
        {
            object targetEntityVal = sourceRecord.GetObject <T>(GetSourceRole().GetEntityField().GetName());

            if (targetEntityVal == null)
            {
                return(null);
            }
            Net.Vpc.Upa.EntityBuilder targetConverter = GetTargetRole().GetEntity().GetBuilder();
            return(targetConverter.ObjectToId(targetEntityVal));
        }
 public virtual string Convert(string v)
 {
     if (v.Equals("#"))
     {
         return(System.Convert.ToString(replacement));
     }
     if (record != null && record.IsSet(v))
     {
         return(System.Convert.ToString(record.GetObject <T>(v)));
     }
     Net.Vpc.Upa.Expressions.Select s = new Net.Vpc.Upa.Expressions.Select();
     s.Field(new Net.Vpc.Upa.Expressions.UserExpression(v), "customValue");
     return(System.Convert.ToString(field.GetEntity().GetPersistenceUnit().CreateQuery(s).GetSingleValue()));
 }
Beispiel #6
0
 public virtual System.Collections.Generic.IDictionary <string, object> ToMap(object o, bool?includeDefaults)  /* throws Net.Vpc.Upa.Exceptions.UPAException */
 {
     Net.Vpc.Upa.Record r = (Net.Vpc.Upa.Record)o;
     System.Collections.Generic.Dictionary <string, object> map = new System.Collections.Generic.Dictionary <string, object>();
     if (includeDefaults == null)
     {
         foreach (string k in GetPropertyNames())
         {
             map[k] = r.GetObject <T>(k);
         }
     }
     else
     {
         foreach (string k in GetPropertyNames())
         {
             if (includeDefaults == IsDefaultValue(o, k))
             {
                 map[k] = r.GetObject <T>(k);
             }
         }
     }
     return(map);
 }
 public virtual object ExtractIdByForeignFields(Net.Vpc.Upa.Record sourceRecord)
 {
     System.Collections.Generic.IList <Net.Vpc.Upa.Field> relFields = GetSourceRole().GetFields();
     System.Collections.Generic.List <object>             keys      = new System.Collections.Generic.List <object>((relFields).Count);
     foreach (Net.Vpc.Upa.Field field in relFields)
     {
         object keyPart = sourceRecord.GetObject <T>(field.GetName());
         if (keyPart == null)
         {
             return(null);
         }
         keys.Add(keyPart);
     }
     return(GetTargetRole().GetEntity().CreateId(keys.ToArray()));
 }
 protected internal virtual Net.Vpc.Upa.Expressions.Expression EvalVar(Net.Vpc.Upa.Expressions.Var expression)
 {
     if (expression.GetApplier() == null)
     {
         //this is the very root
         string name = expression.GetName();
         if (vars.ContainsKey(name))
         {
             return(new Net.Vpc.Upa.Expressions.Literal(Net.Vpc.Upa.Impl.FwkConvertUtils.GetMapValue <string, object>(vars, name), null));
         }
     }
     else
     {
         Net.Vpc.Upa.Expressions.Expression x = expression.GetApplier();
         // no need for evalVar() in post order DFS
         if (x is Net.Vpc.Upa.Expressions.Literal)
         {
             object v = ((Net.Vpc.Upa.Expressions.Literal)x).GetValue();
             if (v == null)
             {
                 return(Net.Vpc.Upa.Expressions.Literal.NULL);
             }
             else if (v is Net.Vpc.Upa.Record)
             {
                 Net.Vpc.Upa.Record r = (Net.Vpc.Upa.Record)v;
                 return(new Net.Vpc.Upa.Expressions.Literal(r.GetObject <object>(expression.GetName()), null));
             }
             else if (v is System.Collections.IDictionary)
             {
                 System.Collections.IDictionary r = (System.Collections.IDictionary)v;
                 return(new Net.Vpc.Upa.Expressions.Literal(Net.Vpc.Upa.Impl.FwkConvertUtils.GetMapValue0(r, expression.GetName()), null));
             }
             else
             {
                 Net.Vpc.Upa.Entity entity = pu.GetEntity(v.GetType());
                 Net.Vpc.Upa.Field  field  = entity.GetField(expression.GetName());
                 Net.Vpc.Upa.Record r      = entity.GetBuilder().ObjectToRecord(v);
                 return(new Net.Vpc.Upa.Expressions.Literal(r.GetObject <object>(field.GetName()), null));
             }
         }
     }
     return(expression);
 }
Beispiel #9
0
        public override void OnPreUpdate(Net.Vpc.Upa.Callbacks.UpdateEvent @event) /* throws Net.Vpc.Upa.Exceptions.UPAException */
        {
            Net.Vpc.Upa.Persistence.EntityExecutionContext       executionContext = @event.GetContext();
            System.Collections.Generic.IList <Net.Vpc.Upa.Field> updateTreeFields = GetUpdateTreeFields();
            Net.Vpc.Upa.Record updates = @event.GetUpdatesRecord();
            if (!updates.IsSet(updateTreeFields[0].GetName()))
            {
                return;
            }
            object val = updates.GetObject <T>(updateTreeFields[0].GetName());

            if (val is Net.Vpc.Upa.Expressions.Literal)
            {
                val = ((Net.Vpc.Upa.Expressions.Literal)val).GetValue();
            }
            else if (val is Net.Vpc.Upa.Expressions.Expression)
            {
                //                    Log.bug("1232123");
                return;
            }
            if (val != null)
            {
                object parentId = relation.ExtractId(updates);
                if (parentId != null)
                {
                    Net.Vpc.Upa.Entity entity = @event.GetEntity();
                    string             k      = "recurse";
                    if (!executionContext.IsSet(k))
                    {
                        System.Collections.Generic.IList <object> idList = entity.CreateQueryBuilder().ByExpression(@event.GetFilterExpression()).OrderBy(entity.GetUpdateFormulasOrder()).GetIdList <K>();
                        executionContext.SetObject(k, idList);
                    }
                    System.Collections.Generic.IList <object> r = (System.Collections.Generic.IList <object>)executionContext.GetObject <T>("recurse");
                    foreach (object aR in r)
                    {
                        if (support.IsEqualOrIsParent(aR, parentId))
                        {
                            throw new Net.Vpc.Upa.Exceptions.UPAException("RedundancyProblem");
                        }
                    }
                }
            }
        }
        public virtual void BeforePersist(Net.Vpc.Upa.Record record, Net.Vpc.Upa.Persistence.EntityExecutionContext context) /* throws Net.Vpc.Upa.Exceptions.UPAException */
        {
            object o = record.GetObject <T>(field.GetName());

            Net.Vpc.Upa.Key key = relationshipTargetConverter.ObjectToKey(o);
            if (key == null)
            {
                foreach (Net.Vpc.Upa.Field ff in flatFields)
                {
                    record.SetObject(ff.GetName(), ff.GetUnspecifiedValueDecoded());
                }
            }
            else
            {
                int i = 0;
                foreach (Net.Vpc.Upa.Field ff in flatFields)
                {
                    record.SetObject(ff.GetName(), key.GetObjectAt(i));
                    i++;
                }
            }
        }
Beispiel #11
0
        protected internal virtual void ValidatePathField(object id, Net.Vpc.Upa.Persistence.EntityExecutionContext executionContext) /* throws Net.Vpc.Upa.Exceptions.UPAException */
        {
            System.Collections.Generic.IList <Net.Vpc.Upa.Field> lfs = GetTreeRelationship().GetSourceRole().GetFields();
            object[]           parent_id = new object[(lfs).Count];
            Net.Vpc.Upa.Record values    = GetEntity().CreateQueryBuilder().ByExpression(GetEntity().GetBuilder().IdToExpression(id, null)).SetFieldFilter(Net.Vpc.Upa.Filters.Fields.Regular().And(Net.Vpc.Upa.Filters.Fields.ByList(lfs))).GetRecord();
            if (values == null)
            {
                parent_id = null;
            }
            else
            {
                for (int i = 0; i < parent_id.Length; i++)
                {
                    Net.Vpc.Upa.Field field = lfs[i];
                    parent_id[i] = values.GetObject <T>(field.GetName());
                    if (parent_id[i] != null)
                    {
                        continue;
                    }
                    parent_id = null;
                    break;
                }
            }
            string path = ToStringId(id);

            if (parent_id != null)
            {
                Net.Vpc.Upa.Record r = GetEntity().CreateQueryBuilder().ByExpression(GetEntity().GetBuilder().IdToExpression(GetEntity().CreateId(parent_id), null)).SetFieldFilter(Net.Vpc.Upa.Filters.Fields.ByName(GetHierarchyPathField())).GetRecord();
                if (r != null)
                {
                    path = r.GetString(GetHierarchyPathField()) + GetHierarchyPathSeparator() + path;
                }
            }
            Net.Vpc.Upa.Record r2 = GetEntity().GetBuilder().CreateRecord();
            r2.SetString(GetHierarchyPathField(), path);
            GetEntity().UpdateCore(r2, GetEntity().GetBuilder().IdToExpression(id, GetEntity().GetName()), executionContext);
        }
Beispiel #12
0
 public virtual Net.Vpc.Upa.Key RecordToKey(Net.Vpc.Upa.Record record) /* throws Net.Vpc.Upa.Exceptions.UPAException */
 {
     if (record == null)
     {
         return(null);
     }
     Net.Vpc.Upa.Entity entity = GetEntity();
     System.Collections.Generic.IList <Net.Vpc.Upa.Field> f = entity.GetPrimaryFields();
     object[] rawKey = new object[(f).Count];
     for (int i = 0; i < rawKey.Length; i++)
     {
         Net.Vpc.Upa.Field field = f[i];
         string            name  = field.GetName();
         if (record.IsSet(name))
         {
             rawKey[i] = record.GetObject <T>(name);
         }
         else
         {
             return(entity.GetBuilder().CreateKey((object[])null));
         }
     }
     return(entity.GetBuilder().CreateKey(rawKey));
 }
        public virtual void CheckUpdate(Net.Vpc.Upa.Record updates, Net.Vpc.Upa.Expressions.Expression condition) /* throws Net.Vpc.Upa.Exceptions.UPAException */
        {
            if (!entity.GetPersistenceUnit().GetSecurityManager().IsAllowedUpdate(entity))
            {
                throw new Net.Vpc.Upa.Exceptions.UpdateRecordNotAllowedException(entity);
            }
            if (!IsUpdateSupported())
            {
                throw new Net.Vpc.Upa.Exceptions.UnupdatableRecordException(entity);
            }
            Net.Vpc.Upa.Expressions.Expression e = GetFullNonUpdatableRecordsExpression();
            if (e != null && e.IsValid())
            {
                Net.Vpc.Upa.Expressions.Expression a = (condition == null) ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(condition, e);
                if (entity.GetEntityCount(a) > 0)
                {
                    throw new Net.Vpc.Upa.Exceptions.UnupdatableRecordException(entity);
                }
            }
            long updated = 0;

            if ((updated = entity.GetEntityCount(condition)) == 0)
            {
                throw new Net.Vpc.Upa.Exceptions.UpdateRecordKeyNotFoundException(entity, condition);
            }
            //TODO c koa cet unique fields qui n'impose pas toutes les validations
            if (false)
            {
                return;
            }
            else if (updated == 1)
            {
                if (condition != null)
                {
                    if (updates != null)
                    {
                        Net.Vpc.Upa.Expressions.Expression or = null;
                        foreach (Net.Vpc.Upa.Index index in entity.GetIndexes(true))
                        {
                            Net.Vpc.Upa.Field[] f = index.GetFields();
                            Net.Vpc.Upa.Expressions.Expression a = null;
                            int found = 0;
                            foreach (Net.Vpc.Upa.Field aF in f)
                            {
                                if (updates.IsSet(aF.GetName()))
                                {
                                    found++;
                                    Net.Vpc.Upa.Expressions.Expression b = (new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(aF.GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(updates.GetObject <T>(aF.GetName()))));
                                    a = a == null ? ((Net.Vpc.Upa.Expressions.Expression)(b)) : new Net.Vpc.Upa.Expressions.And(a, b);
                                }
                            }
                            if (found != 0 && found != f.Length)
                            {
                                throw new Net.Vpc.Upa.Exceptions.UPAException("NotFound");
                            }
                            else if (found == f.Length)
                            {
                                or = or == null ? ((Net.Vpc.Upa.Expressions.Expression)(a)) : new Net.Vpc.Upa.Expressions.Or(or, a);
                            }
                        }
                        if (or != null)
                        {
                            Net.Vpc.Upa.Expressions.And and = new Net.Vpc.Upa.Expressions.And(new Net.Vpc.Upa.Expressions.Not(condition), or);
                            if (entity.GetEntityCount(and) > 0)
                            {
                                throw new Net.Vpc.Upa.Exceptions.UpdateRecordDuplicateKeyException(entity);
                            }
                        }
                    }
                }
            }
            else
            {
                if (updates != null)
                {
                    foreach (Net.Vpc.Upa.Index index in entity.GetIndexes(true))
                    {
                        Net.Vpc.Upa.Field[] f = index.GetFields();
                        foreach (Net.Vpc.Upa.Field aF in f)
                        {
                            if (updates.IsSet(aF.GetName()))
                            {
                                throw new Net.Vpc.Upa.Exceptions.UpdateRecordDuplicateKeyException(entity);
                            }
                        }
                    }
                }
            }
            Net.Vpc.Upa.Entity p = entity.GetParentEntity();
            if (p != null)
            {
                Net.Vpc.Upa.Expressions.Expression ss = entity.ChildToParentExpression(condition);
                try {
                    p.GetShield().CheckUpdate(null, ss);
                } catch (Net.Vpc.Upa.Exceptions.UpdateRecordKeyNotFoundException ex) {
                    log.Warning(entity.GetName() + "'s parent seems not to be resolvable for condition (" + condition + "): " + ex);
                }
            }
            //ignore if parent not found!
            CheckVeto(Net.Vpc.Upa.VetoableOperation.checkUpdate, updates, condition);
        }
Beispiel #14
0
 public virtual object GetProperty(object o, string field)
 {
     Net.Vpc.Upa.Record r = (Net.Vpc.Upa.Record)o;
     return(r.GetObject <T>(field));
 }
 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());
 }
 private object GetNavigateKey(Net.Vpc.Upa.Entity entity, object id, char @operator) /* throws Net.Vpc.Upa.Exceptions.UPAException */
 {
     System.Collections.Generic.IList <Net.Vpc.Upa.Field> pk = entity.GetPrimaryFields();
     if ((pk).Count == 1)
     {
         Net.Vpc.Upa.Expressions.Select s = new Net.Vpc.Upa.Expressions.Select().From(entity.GetName());
         s.From(entity.GetName());
         string fieldName = pk[0].GetName();
         if (id != null)
         {
             object[] @value = entity.GetBuilder().IdToKey(id).GetValue();
             if (@operator == '<')
             {
                 s.Field(new Net.Vpc.Upa.Expressions.Max(new Net.Vpc.Upa.Expressions.Var(fieldName)), "next");
                 s.SetWhere(new Net.Vpc.Upa.Expressions.LessThan(new Net.Vpc.Upa.Expressions.Var(fieldName), new Net.Vpc.Upa.Expressions.Param(null, @value[0])));
             }
             else if (@operator == '>')
             {
                 s.Field(new Net.Vpc.Upa.Expressions.Min(new Net.Vpc.Upa.Expressions.Var(fieldName)), "next");
                 s.SetWhere(new Net.Vpc.Upa.Expressions.GreaterThan(new Net.Vpc.Upa.Expressions.Var(fieldName), new Net.Vpc.Upa.Expressions.Param(null, @value[0])));
             }
             else
             {
                 throw new System.Exception("WouldNeverBeThrownException");
             }
         }
         else
         {
             if (@operator == '<')
             {
                 s.Field(new Net.Vpc.Upa.Expressions.Min(new Net.Vpc.Upa.Expressions.Var(fieldName)), "next");
             }
             else if (@operator == '>')
             {
                 s.Field(new Net.Vpc.Upa.Expressions.Max(new Net.Vpc.Upa.Expressions.Var(fieldName)), "next");
             }
             else
             {
                 throw new System.Exception("WouldNeverBeThrownException");
             }
         }
         Net.Vpc.Upa.Record next = entity.GetPersistenceUnit().CreateQuery(s).GetRecord();
         if (next != null)
         {
             object o = next.GetObject <T>("next");
             if (o != null)
             {
                 return(entity.CreateId(o));
             }
         }
         return(null);
     }
     else
     {
         object[] v;
         Net.Vpc.Upa.Expressions.Select sb = new Net.Vpc.Upa.Expressions.Select();
         sb.Top(1);
         foreach (Net.Vpc.Upa.Field aPk in pk)
         {
             sb.Field(new Net.Vpc.Upa.Expressions.Var(aPk.GetName()));
         }
         sb.From(entity.GetName());
         if (id != null)
         {
             object[] @value = entity.GetBuilder().IdToKey(id).GetValue();
             Net.Vpc.Upa.Expressions.Expression or = null;
             for (int i = 0; i < (pk).Count; i++)
             {
                 Net.Vpc.Upa.Field pki = pk[i];
                 Net.Vpc.Upa.Expressions.Expression a = null;
                 for (int j = 0; j < i; j++)
                 {
                     Net.Vpc.Upa.Field pkj = pk[j];
                     Net.Vpc.Upa.Expressions.Expression e = (new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(pkj.GetName()), (new Net.Vpc.Upa.Expressions.Param(null, @value[j]))));
                     a = (a == null) ? ((Net.Vpc.Upa.Expressions.Expression)(e)) : new Net.Vpc.Upa.Expressions.And(a, e);
                 }
                 Net.Vpc.Upa.Expressions.Expression e2 = new Net.Vpc.Upa.Expressions.LessThan(new Net.Vpc.Upa.Expressions.Var(pki.GetName()), new Net.Vpc.Upa.Expressions.Param(null, @value[i]));
                 a  = (a == null) ? ((Net.Vpc.Upa.Expressions.Expression)(e2)) : new Net.Vpc.Upa.Expressions.And(a, e2);
                 or = or == null ? ((Net.Vpc.Upa.Expressions.Expression)(a)) : new Net.Vpc.Upa.Expressions.Or(or, a);
             }
             sb.SetWhere(or);
         }
         foreach (Net.Vpc.Upa.Field aPk in pk)
         {
             sb.OrderBy(new Net.Vpc.Upa.Expressions.Var(aPk.GetName()), @operator == '>');
         }
         Net.Vpc.Upa.Record r = entity.GetPersistenceUnit().CreateQuery(sb).GetRecord();
         if (r != null)
         {
             object[] k = new object[(pk).Count];
             for (int i = 0; i < k.Length; i++)
             {
                 k[i] = r.GetObject <T>(pk[i].GetName());
             }
             return(entity.CreateId(k));
         }
     }
     return(null);
 }
 public virtual void CheckPersist(Net.Vpc.Upa.Record record) /* throws Net.Vpc.Upa.Exceptions.UPAException */
 {
     if (!entity.GetPersistenceUnit().GetSecurityManager().IsAllowedPersist(entity))
     {
         throw new Net.Vpc.Upa.Exceptions.PersistRecordNotAllowedException(entity);
     }
     if (!IsPersistSupported())
     {
         throw new Net.Vpc.Upa.Exceptions.PersistRecordNotAllowedException(entity);
     }
     if (record != null)
     {
         // check parent is not read only
         if (entity.GetParentEntity() != null)
         {
             Net.Vpc.Upa.Expressions.Expression parentUnupdatable = entity.GetParentEntity().GetShield().GetFullNonUpdatableRecordsExpression();
             if (parentUnupdatable != null && parentUnupdatable.IsValid())
             {
                 Net.Vpc.Upa.Relationship r = entity.GetCompositionRelation();
                 System.Collections.Generic.IList <Net.Vpc.Upa.Field> df = r.GetSourceRole().GetFields();
                 System.Collections.Generic.IList <Net.Vpc.Upa.Field> mf = r.GetTargetRole().GetFields();
                 object[] pko = new object[(mf).Count];
                 for (int i = 0; i < pko.Length; i++)
                 {
                     pko[i] = record.GetObject <T>(df[i].GetName());
                 }
                 object pk = entity.CreateId(pko);
                 long   c  = entity.GetParentEntity().GetEntityCount(new Net.Vpc.Upa.Expressions.And(parentUnupdatable, entity.GetParentEntity().GetBuilder().IdToExpression(pk, null)));
                 if (c > 0)
                 {
                     throw new Net.Vpc.Upa.Exceptions.UnupdatableRecordException(entity.GetParentEntity());
                 }
             }
         }
         bool keyGenerated = IsGeneratedId();
         Net.Vpc.Upa.Expressions.Expression keyExpresson = null;
         if (!keyGenerated)
         {
             object key = entity.GetBuilder().RecordToId(record);
             keyExpresson = entity.GetBuilder().IdToExpression(key, null);
         }
         Net.Vpc.Upa.Entity p = entity.GetParentEntity();
         if (p != null)
         {
             //Expression ss = childToParentExpression(toExpression(key));
             Net.Vpc.Upa.Expressions.Expression ss = entity.ChildToParentExpression(record);
             if (ss != null)
             {
                 p.GetShield().CheckUpdate(null, ss);
             }
         }
         System.Collections.Generic.IList <Net.Vpc.Upa.Index> uniqueIndexes = entity.GetIndexes(true);
         if ((uniqueIndexes.Count == 0))
         {
             if (!keyGenerated)
             {
                 if (entity.GetEntityCount(keyExpresson) > 0)
                 {
                     throw new Net.Vpc.Upa.Exceptions.InsertRecordDuplicateKeyException(entity);
                 }
             }
         }
         else
         {
             Net.Vpc.Upa.Expressions.Expression or = null;
             if (!keyGenerated)
             {
                 or = keyExpresson;
             }
             foreach (Net.Vpc.Upa.Index index in uniqueIndexes)
             {
                 Net.Vpc.Upa.Field[] f = index.GetFields();
                 Net.Vpc.Upa.Expressions.Expression e1 = null;
                 if (f.Length == 1)
                 {
                     e1 = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(f[0].GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(record.GetObject <T>(f[0].GetName())));
                 }
                 else
                 {
                     Net.Vpc.Upa.Expressions.Expression a = null;
                     foreach (Net.Vpc.Upa.Field aF in f)
                     {
                         Net.Vpc.Upa.Expressions.Expression b = (new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(aF.GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(record.GetObject <T>(aF.GetName()))));
                         a = a == null ? ((Net.Vpc.Upa.Expressions.Expression)(b)) : new Net.Vpc.Upa.Expressions.And(a, b);
                     }
                     e1 = a;
                 }
                 or = or == null ? ((Net.Vpc.Upa.Expressions.Expression)(e1)) : new Net.Vpc.Upa.Expressions.Or(or, e1);
             }
             if (entity.GetEntityCount(or) > 0)
             {
                 // finer lookup of problem
                 if (!keyGenerated)
                 {
                     if (entity.GetEntityCount(keyExpresson) > 0)
                     {
                         throw new Net.Vpc.Upa.Exceptions.InsertRecordDuplicateKeyException(entity);
                     }
                 }
                 foreach (Net.Vpc.Upa.Index index in uniqueIndexes)
                 {
                     Net.Vpc.Upa.Field[] f = index.GetFields();
                     Net.Vpc.Upa.Expressions.Expression e1 = null;
                     if (f.Length == 1)
                     {
                         e1 = new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(f[0].GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(record.GetObject <T>(f[0].GetName())));
                     }
                     else
                     {
                         Net.Vpc.Upa.Expressions.Expression a = null;
                         foreach (Net.Vpc.Upa.Field aF in f)
                         {
                             Net.Vpc.Upa.Expressions.Expression b = (new Net.Vpc.Upa.Expressions.Equals(new Net.Vpc.Upa.Expressions.Var(aF.GetName()), Net.Vpc.Upa.Expressions.ExpressionFactory.ToLiteral(record.GetObject <T>(aF.GetName()))));
                             a = a == null ? ((Net.Vpc.Upa.Expressions.Expression)(b)) : new Net.Vpc.Upa.Expressions.And(a, b);
                         }
                         e1 = a;
                     }
                     if (entity.GetEntityCount(e1) > 0)
                     {
                         throw new Net.Vpc.Upa.Exceptions.InsertRecordDuplicateUniqueFieldsException(entity, index, record.GetObject <T>(f[0].GetName()));
                     }
                 }
                 throw new System.Exception("WouldNeverBeThrownException");
             }
         }
     }
     CheckVeto(Net.Vpc.Upa.VetoableOperation.checkPersist, record);
 }