View Javadoc

    1   /*
    2    * joey-gen and its relative products are published under the terms
    3    * of the Apache Software License.
    4    *
    5    * Created on 2004/08/29 4:06:55
    6    */
    7   package org.asyrinx.joey.gen.model.java;
    8
    9   import java.util.ArrayList;
    10  import java.util.HashMap;
    11  import java.util.HashSet;
    12  import java.util.Iterator;
    13  import java.util.List;
    14  import java.util.Map;
    15  import java.util.Set;
    16
    17  import org.asyrinx.joey.gen.model.Element;
    18
    19  /***
    20   * @author akima
    21   */
    22  public class Entity extends Element implements Type {
    23
    24      /***
    25       *
    26       */
    27      public Entity() {
    28          super();
    29      }
    30
    31      /***
    32       * @param parent
    33       */
    34      public Entity(AppDomain parent) {
    35          super(parent);
    36      }
    37
    38      /***
    39       * @param name
    40       */
    41      public Entity(String name) {
    42          super(name);
    43      }
    44
    45      /***
    46       * @param parent
    47       * @param name
    48       */
    49      public Entity(AppDomain parent, String name) {
    50          super(parent, name);
    51      }
    52
    53      /***
    54       * @param parent
    55       * @param name
    56       * @param label
    57       */
    58      public Entity(Element parent, String name, String label) {
    59          super(parent, name, label);
    60      }
    61
    62      private final PropertySet properties = new PropertySet(this);
    63
    64      private final EntityKeySet keys = new EntityKeySet(this);
    65
    66      private final ReferenceSet references = new ReferenceSet(this);
    67
    68      private final Set imports = new HashSet();
    69
    70      /*
    71       * (non-Javadoc)
    72       *
    73       * @see org.asyrinx.joey.gen.model.Element#getParentElement()
    74       */
    75      public AppDomain getParent() {
    76          return (AppDomain) super.getParentElement();
    77      }
    78
    79      /*
    80       * (non-Javadoc)
    81       *
    82       * @see org.asyrinx.joey.gen.model.java.Type#getFqn()
    83       */
    84      public String getFqn() {
    85          return getPackage() + PACKAGE_SEPARATER + getName();
    86      }
    87
    88      public String getPackage() {
    89          return getPackageName();
    90      }
    91
    92      public boolean isPrimitive() {
    93          return false;
    94      }
    95
    96      public Type toClass() {
    97          return this;
    98      }
    99
    100     public Type toPrimitive() {
    101         return null;
    102     }
    103
    104     /*
    105      * (non-Javadoc)
    106      *
    107      * @see org.asyrinx.joey.gen.model.java.Type#getCategory()
    108      */
    109     public TypeCategory getCategory() {
    110         return TypeCategory.OTHERS;
    111     }
    112
    113     /*
    114      * (non-Javadoc)
    115      *
    116      * @see org.asyrinx.joey.gen.model.java.Type#isNumber()
    117      */
    118     public boolean isNumber() {
    119         return getCategory() == TypeCategory.NUMBER;
    120     }
    121
    122     /*
    123      * (non-Javadoc)
    124      *
    125      * @see org.asyrinx.joey.gen.model.Element#add(org.asyrinx.joey.gen.model.Element)
    126      */
    127     public void add(Element element) {
    128         if (element instanceof Property)
    129             properties.add((Property) element);
    130         else if (element instanceof Reference)
    131             references.add((Reference) element);
    132         else if (element instanceof EntityKey)
    133             keys.add((EntityKey) element);
    134         else
    135             super.add(element);
    136     }
    137
    138     privateong> String packageTemplate = null;
    139
    140     /***
    141      * @return Returns the packageName.
    142      */
    143     public String getPackageTemplate() {
    144         return</strong> packageTemplate;
    145     }
    146
    147     /***
    148      * @param packageName
    149      *            The packageName to set.
    150      */
    151     publicong> void setPackageTemplate(String packageTemplate) {
    152         this.packageTemplate = packageTemplate;
    153     }
    154
    155     /***
    156      * @return Returns the packageName.
    157      */
    158     public String getPackageName() {
    159         return getPackage(PackageType.ENTITY.getName());
    160     }
    161
    162     /***
    163      * @param packageName
    164      *            The packageName to set.
    165      */
    166     publicong> void setPackageName(String packageName) {
    167         setPackage(PackageType.ENTITY.getName(), packageName);
    168     }
    169
    170     privateong> final Map packageNames = new HashMap();
    171
    172     publicong> String getPackage(String packageType) {
    173         return</strong> (String) packageNames.get(packageType);
    174     }
    175
    176     public void setPackage(String type, String value) {
    177         packageNames.put(type, value);
    178     }
    179
    180     /***
    181      * @return Returns the properties.
    182      */
    183     public PropertySet getProperties() {
    184         return properties;
    185     }
    186
    187     /***
    188      * @return Returns the keys.
    189      */
    190     public EntityKeySet getKeys() {
    191         return keys;
    192     }
    193
    194     /***
    195      * @return Returns the references.
    196      */
    197     public ReferenceSet getReferences() {
    198         return references;
    199     }
    200
    201     /***
    202      * @return
    203      */
    204     public EntityKey getPrimaryKey() {
    205         if (this.getSuperClass() != null)
    206             return this.getSuperClass().getPrimaryKey();
    207         else
    208             return keys.getPrimaryKey();
    209     }
    210
    211     /***
    212      * @param property
    213      * @return
    214      */
    215     public List getReferencesContainedAsLocal(Property property) {
    216         return this.getReferences().getReferencesContainedAsLocal(property);
    217     }
    218
    219     /***
    220      * @param javaClass
    221      * @return
    222      */
    223     public List getReferencesContainedAsForeign(Entity javaClass) {
    224         return this.getReferences().getReferencesContainedAsForeign(javaClass);
    225     }
    226
    227     /***
    228      * @param property
    229      * @return
    230      */
    231     public List getReferencesContainedAsForeign(Property property) {
    232         return getParent().getReferencesContainedAsForeign(property);
    233     }
    234
    235     public List getReferreds() {
    236         return getParent().getReferencesContainedAsForeign(this);
    237     }
    238
    239     public Set getReferredEntities() {
    240         final List referreds = this.getReferreds();
    241         final Set result = new HashSet();
    242         for (Iterator i = referreds.iterator(); i.hasNext();) {
    243             final Reference reference = (Reference) i.next();
    244             result.add(reference.getParent());
    245         }
    246         return result;
    247     }
    248
    249     /***
    250      * @param javaClass
    251      * @param dest
    252      */
    253     public void findReferencesContainedAsForeign(Entity javaClass, List dest) {
    254         getReferences().findReferencesContainedAsForeign(javaClass, dest);
    255     }
    256
    257     /***
    258      * @param javaClass
    259      * @param result
    260      */
    261     public void findReferencesContainedAsLocal(Entity javaClass, List dest) {
    262         getReferences().findReferencesContainedAsLocal(javaClass, dest);
    263         if (getSuperClass() != null) {
    264             getSuperClass().findReferencesContainedAsLocal(javaClass, dest);
    265         }
    266     }
    267
    268     /***
    269      * @return Returns the imports.
    270      */
    271     public Set getImports() {
    272         return imports;
    273     }
    274
    275     private String superClassName = null;
    276
    277     private Entity superClass = null;
    278
    279     /***
    280      * @return Returns the superClass.
    281      */
    282     public Entity getSuperClass() {
    283         return superClass;
    284     }
    285
    286     /***
    287      * @param superClass
    288      *            The superClass to set.
    289      */
    290     public void setSuperClass(Entity superClass) {
    291         this.superClass = superClass;
    292     }
    293
    294     /***
    295      * @return Returns the superClassName.
    296      */
    297     public String getSuperClassName() {
    298         return superClassName;
    299     }
    300
    301     /***
    302      * @param superClassName
    303      *            The superClassName to set.
    304      */
    305     public void setSuperClassName(String superClassName) {
    306         this.superClassName = superClassName;
    307     }
    308
    309     public List getPropertiesExtended() {
    310         if (this.getSuperClass() == null)
    311             return new ArrayList();
    312         final List result = new ArrayList(this.getSuperClass().getPropertiesExtended());
    313         result.addAll(this.getSuperClass().getProperties().toList());
    314         return result;
    315     }
    316
    317     public List getPropertiesAll() {
    318         final List result = getPropertiesExtended();
    319         result.addAll(this.getProperties().toList());
    320         return result;
    321     }
    322
    323     /***
    324      * @return
    325      */
    326     public List getConstructorParams() {
    327         final List result = (this.getSuperClass() != null) ? this.getSuperClass()
    328                 .getConstructorParams() : new ArrayList();
    329         final Set used = new HashSet();
    330         for (Iterator i = getProperties().iterator(); i.hasNext();) {
    331             final Property property = (Property) i.next();
    332             addReferenceOfProperty(result, used, property);
    333             if (used.contains(property))
    334                 continue;
    335             if (property.isExtended())
    336                 continue;
    337             result.add(property);
    338             used.add(property);
    339         }
    340         return result;
    341     }
    342
    343     /***
    344      * @param result
    345      * @param used
    346      * @param property
    347      */
    348     private void addReferenceOfProperty(final List result, final Set used, final Property property) {
    349         for (Iterator i = property.getReferencesContainedAsLocal().iterator(); i.hasNext();) {
    350             final Reference reference = (Reference) i.next();
    351             if (used.contains(reference))
    352                 continue;
    353             result.add(reference);
    354             used.add(reference);
    355             addPropertyAsUsed(used, reference);
    356         }
    357     }
    358
    359     /***
    360      * @param used
    361      * @param reference
    362      */
    363     private void addPropertyAsUsed(final Set used, final Reference reference) {
    364         for (Iterator iterator = reference.iterator(); iterator.hasNext();) {
    365             final ReferenceEntry entry = (ReferenceEntry) iterator.next();
    366             used.add(entry.getLocal());
    367         }
    368     }
    369
    370     public List getSuperClasses() {
    371         final List result;
    372         if (this.getSuperClass() != null) {
    373             result = this.getSuperClass().getSuperClasses();
    374         } else {
    375             result = new ArrayList();
    376         }
    377         result.add(this);
    378         return result;
    379     }
    380
    381     /***
    382      * getConstructorParamsと同じものを使いたいんだけど、名前が一緒だと
    383      * 変かなと思って適当に名前をつけてみた。表示に使用されるプロパティ＆参照。
    384      *
    385      * @return
    386      */
    387     public List getPresentProperties() {
    388         return getConstructorParams();
    389     }
    390
    391     public Set getReferenceEntities() {
    392         final Set result = new HashSet();
    393         for (Iterator i = getReferences().iterator(); i.hasNext();) {
    394             final Reference reference = (Reference) i.next();
    395             result.add(reference.getReferenceClass());
    396         }
    397         return result;
    398     }
    399
    400     private final List assignableEntities = new ArrayList();
    401
    402     public List getAssignableEntities() {
    403         return assignableEntities;
    404     }
    405
    406     public Set getReferenceEntitiesAll() {
    407         final Set result = new HashSet();
    408         for (Entity current = this; current != null; current = current.getSuperClass())
    409             result.addAll(current.getReferenceEntities());
    410         return result;
    411     }
    412
    413     public List getReferencesAll() {
    414         final List result = new ArrayList();
    415         for (Entity current = this; current != null; current = current.getSuperClass())
    416             result.addAll(current.getReferences().toList());
    417         return result;
    418     }
    419
    420     private Property captionProperty = null;
    421
    422     public Property getCaptionProperty() {
    423         return captionProperty;
    424     }
    425
    426     public void setCaptionProperty(Property captionProperty) {
    427         this.captionProperty = captionProperty;
    428     }
    429
    430     public List getReferredAll() {
    431         final List result = new ArrayList();
    432         for (Entity current = this; current != null; current = current.getSuperClass())
    433             result.addAll(current.getReferreds());
    434         return result;
    435     }
    436
    437     public Set getReferredEntitiesAll() {
    438         final Set result = new HashSet();
    439         for (Entity current = this; current != null; current = current.getSuperClass())
    440             result.addAll(current.getReferredEntities());
    441         return result;
    442     }
    443
    444     public Set getRelatedEntities() {
    445         final Set result = new HashSet();
    446         result.addAll(getReferenceEntities());
    447         result.addAll(getReferredEntities());
    448         return result;
    449     }
    450
    451     public Set getRelatedEntitiesAll() {
    452         final Set result = new HashSet();
    453         result.addAll(getReferenceEntitiesAll());
    454         result.addAll(getReferredEntitiesAll());
    455         return result;
    456     }
    457
    458     public boolean isAssignable(Entity entity) {
    459         for (Entity current = this; current != null; current = current.getSuperClass()) {
    460             if (current == entity)
    461                 return true;
    462         }
    463         return false;
    464     }
    465
    466     private String interfaces = null;
    467
    468     public String getInterfaces() {
    469         return interfaces;
    470     }
    471
    472     public void setInterfaces(String interfaces) {
    473         this.interfaces = interfaces;
    474     }
    475 }