public virtual bool IsDefaultValue(object o) /* throws Net.Vpc.Upa.Exceptions.UPAException */
        {
            object fieldValue = GetValue(o);

            Net.Vpc.Upa.Entity entity = entityBeanAdapter.GetEntity();
            Net.Vpc.Upa.Field  field  = null;
            if (entity != null)
            {
                field = entity.FindField(GetName());
                if (field == null)
                {
                    entity = null;
                }
            }
            if (entity == null || field == null)
            {
                object fieldDefaultValue = Net.Vpc.Upa.Impl.FwkConvertUtils.GetMapValue <System.Type, object>(Net.Vpc.Upa.Impl.Util.PlatformUtils.DEFAULT_VALUES_BY_TYPE, GetFieldType());
                if (fieldDefaultValue == null)
                {
                    return(fieldValue == null);
                }
                else
                {
                    return(fieldDefaultValue.Equals(fieldValue));
                }
            }
            else
            {
                return(field.IsUnspecifiedValue(fieldValue));
            }
        }
Ejemplo n.º 2
0
        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 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();
 }
 public virtual object GetDefaultValue() /* throws Net.Vpc.Upa.Exceptions.UPAException */
 {
     Net.Vpc.Upa.Entity entity = entityBeanAdapter.GetEntity();
     Net.Vpc.Upa.Field  field  = null;
     if (entity != null)
     {
         field = entity.FindField(GetName());
         if (field == null)
         {
             entity = null;
         }
     }
     if (entity == null || field == null)
     {
         return(Net.Vpc.Upa.Impl.FwkConvertUtils.GetMapValue <System.Type, object>(Net.Vpc.Upa.Impl.Util.PlatformUtils.DEFAULT_VALUES_BY_TYPE, GetFieldType()));
     }
     else
     {
         return(field.GetUnspecifiedValueDecoded());
     }
 }
 protected internal virtual System.Collections.Generic.IList <Net.Vpc.Upa.Persistence.ResultField> CreateResultFields(Net.Vpc.Upa.Expressions.Expression expression, string alias, Net.Vpc.Upa.Filters.FieldFilter fieldFilter, System.Collections.Generic.IList <Net.Vpc.Upa.Expressions.QueryStatement> context)
 {
     expression = expressionManager.ParseExpression(expression);
     System.Collections.Generic.IList <Net.Vpc.Upa.Persistence.ResultField> results = new System.Collections.Generic.List <Net.Vpc.Upa.Persistence.ResultField>();
     if (expression is Net.Vpc.Upa.Expressions.Var)
     {
         Net.Vpc.Upa.Expressions.Var        v      = (Net.Vpc.Upa.Expressions.Var)expression;
         Net.Vpc.Upa.Expressions.Expression parent = v.GetApplier();
         if (parent != null)
         {
             System.Collections.Generic.IList <Net.Vpc.Upa.Persistence.ResultField> parentResults = CreateResultFields(parent, null, fieldFilter, context);
             int size = (parentResults).Count;
             foreach (Net.Vpc.Upa.Persistence.ResultField p in parentResults)
             {
                 if (size > 1)
                 {
                     v = (Net.Vpc.Upa.Expressions.Var)v.Copy();
                 }
                 if (p.GetExpression() != parent)
                 {
                     //change parent
                     v.SetApplier((Net.Vpc.Upa.Expressions.Var)p.GetExpression());
                 }
                 if (p.GetEntity() != null)
                 {
                     if (v.GetName().Equals("*"))
                     {
                         foreach (Net.Vpc.Upa.Field field in p.GetEntity().GetFields(fieldFilter))
                         {
                             results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, field.GetDataType(), field, null));
                         }
                     }
                     else
                     {
                         Net.Vpc.Upa.Field field = p.GetEntity().GetField(v.GetName());
                         results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, field.GetDataType(), field, null));
                     }
                 }
                 else if (p.GetField() != null)
                 {
                     if (p.GetField().GetDataType() is Net.Vpc.Upa.Types.ManyToOneType)
                     {
                         Net.Vpc.Upa.Entity entity = ((Net.Vpc.Upa.Types.ManyToOneType)p.GetField().GetDataType()).GetTargetEntity();
                         if (v.GetName().Equals("*"))
                         {
                             foreach (Net.Vpc.Upa.Field field in entity.GetFields(fieldFilter))
                             {
                                 results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, field.GetDataType(), field, null));
                             }
                         }
                         else
                         {
                             Net.Vpc.Upa.Field field = entity.GetField(v.GetName());
                             results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, field.GetDataType(), field, null));
                         }
                     }
                     else
                     {
                         results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, Net.Vpc.Upa.Types.TypesFactory.OBJECT, null, null));
                     }
                 }
                 else
                 {
                     results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, Net.Vpc.Upa.Types.TypesFactory.OBJECT, null, null));
                 }
             }
         }
         else
         {
             string name = v.GetName();
             System.Collections.Generic.IDictionary <string, Net.Vpc.Upa.Expressions.NameOrQuery> declarations = FindDeclarations(context);
             Net.Vpc.Upa.Expressions.NameOrQuery r = Net.Vpc.Upa.Impl.FwkConvertUtils.GetMapValue <string, Net.Vpc.Upa.Expressions.NameOrQuery>(declarations, name);
             if (r != null)
             {
                 if (r is Net.Vpc.Upa.Expressions.EntityName)
                 {
                     Net.Vpc.Upa.Entity entity = pu.GetEntity(((Net.Vpc.Upa.Expressions.EntityName)r).GetName());
                     results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, entity.GetDataType(), null, entity));
                 }
                 else
                 {
                     results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, Net.Vpc.Upa.Types.TypesFactory.OBJECT, null, null));
                 }
             }
             else
             {
                 if ("*".Equals(name))
                 {
                     foreach (System.Collections.Generic.KeyValuePair <string, Net.Vpc.Upa.Expressions.NameOrQuery> entry in new System.Collections.Generic.HashSet <System.Collections.Generic.KeyValuePair <string, Net.Vpc.Upa.Expressions.NameOrQuery> >(declarations))
                     {
                         r = (entry).Value;
                         if (r is Net.Vpc.Upa.Expressions.EntityName)
                         {
                             Net.Vpc.Upa.Entity entity = pu.GetEntity(((Net.Vpc.Upa.Expressions.EntityName)r).GetName());
                             Net.Vpc.Upa.Field  field  = entity.FindField(name);
                             results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, field.GetDataType(), field, null));
                             break;
                         }
                     }
                 }
                 else
                 {
                     Net.Vpc.Upa.Field field = null;
                     foreach (System.Collections.Generic.KeyValuePair <string, Net.Vpc.Upa.Expressions.NameOrQuery> entry in new System.Collections.Generic.HashSet <System.Collections.Generic.KeyValuePair <string, Net.Vpc.Upa.Expressions.NameOrQuery> >(declarations))
                     {
                         r = (entry).Value;
                         if (r is Net.Vpc.Upa.Expressions.EntityName)
                         {
                             Net.Vpc.Upa.Entity entity = pu.GetEntity(((Net.Vpc.Upa.Expressions.EntityName)r).GetName());
                             field = entity.FindField(name);
                             break;
                         }
                     }
                     if (field != null)
                     {
                         results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, field.GetDataType(), field, null));
                     }
                     else
                     {
                         results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(v, alias, Net.Vpc.Upa.Types.TypesFactory.OBJECT, null, null));
                     }
                 }
             }
         }
         return(results);
     }
     results.Add(new Net.Vpc.Upa.Impl.Persistence.DefaultResultField(expression, alias, Net.Vpc.Upa.Types.TypesFactory.OBJECT, null, null));
     return(results);
 }
 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 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());
 }