コード例 #1
0
 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);
 }