예제 #1
0
        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;
            }
            }
        }