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