Ejemplo n.º 1
0
 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);
 }