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     /***
    240      * @param javaClass
    241      * @param dest
    242      */
    243     public void findReferencesContainedAsForeign(Entity javaClass, List dest) {
    244         getReferences().findReferencesContainedAsForeign(javaClass, dest);
    245     }
    246
    247     /***
    248      * @param javaClass
    249      * @param result
    250      */
    251     public void findReferencesContainedAsLocal(Entity javaClass, List dest) {
    252         getReferences().findReferencesContainedAsLocal(javaClass, dest);
    253     }
    254
    255     /***
    256      * @return Returns the imports.
    257      */
    258     public Set getImports() {
    259         return imports;
    260     }
    261
    262     private String superClassName = null;
    263
    264     private Entity superClass = null;
    265
    266     /***
    267      * @return Returns the superClass.
    268      */
    269     public Entity getSuperClass() {
    270         return superClass;
    271     }
    272
    273     /***
    274      * @param superClass
    275      *            The superClass to set.
    276      */
    277     public void setSuperClass(Entity superClass) {
    278         this.superClass = superClass;
    279     }
    280
    281     /***
    282      * @return Returns the superClassName.
    283      */
    284     public String getSuperClassName() {
    285         return superClassName;
    286     }
    287
    288     /***
    289      * @param superClassName
    290      *            The superClassName to set.
    291      */
    292     public void setSuperClassName(String superClassName) {
    293         this.superClassName = superClassName;
    294     }
    295
    296     public List getPropertiesExtended() {
    297         if (this.getSuperClass() == null)
    298             return new ArrayList();
    299         final List result = new ArrayList(this.getSuperClass().getPropertiesExtended());
    300         result.addAll(this.getSuperClass().getProperties().toList());
    301         return result;
    302     }
    303
    304     public List getPropertiesAll() {
    305         final List result = getPropertiesExtended();
    306         result.addAll(this.getProperties().toList());
    307         return result;
    308     }
    309
    310     /***
    311      * @return
    312      */
    313     public List getConstructorParams() {
    314         final List result = (this.getSuperClass() != null) ? this.getSuperClass().getConstructorParams()
    315                 : new ArrayList();
    316         final Set used = new HashSet();
    317         for (Iterator i = getProperties().iterator(); i.hasNext();) {
    318             final Property property = (Property) i.next();
    319             addReferenceOfProperty(result, used, property);
    320             if (used.contains(property))
    321                 continue;
    322             if (property.isExtended())
    323                 continue;
    324             result.add(property);
    325             used.add(property);
    326         }
    327         return result;
    328     }
    329
    330     /***
    331      * @param result
    332      * @param used
    333      * @param property
    334      */
    335     private void addReferenceOfProperty(final List result, final Set used, final Property property) {
    336         for (Iterator i = property.getReferencesContainedAsLocal().iterator(); i.hasNext();) {
    337             final Reference reference = (Reference) i.next();
    338             if (used.contains(reference))
    339                 continue;
    340             result.add(reference);
    341             used.add(reference);
    342             addPropertyAsUsed(used, reference);
    343         }
    344     }
    345
    346     /***
    347      * @param used
    348      * @param reference
    349      */
    350     private void addPropertyAsUsed(final Set used, final Reference reference) {
    351         for (Iterator iterator = reference.iterator(); iterator.hasNext();) {
    352             final ReferenceEntry entry = (ReferenceEntry) iterator.next();
    353             used.add(entry.getLocal());
    354         }
    355     }
    356
    357     public List getSuperClasses() {
    358         final List result;
    359         if (this.getSuperClass() != null) {
    360             result = this.getSuperClass().getSuperClasses();
    361         } else {
    362             result = new ArrayList();
    363         }
    364         result.add(this);
    365         return result;
    366     }
    367
    368     /***
    369      * getConstructorParamsと同じものを使いたいんだけど、名前が一緒だと
    370      * 変かなと思って適当に名前をつけてみた。表示に使用されるプロパティ＆参照。
    371      *
    372      * @return
    373      */
    374     public List getPresentProperties() {
    375         return getConstructorParams();
    376     }
    377
    378     public Set getReferenceEntities() {
    379         final Set result = new HashSet();
    380         for (Iterator i = getReferences().iterator(); i.hasNext();) {
    381             final Reference reference = (Reference) i.next();
    382             result.add(reference.getReferenceClass());
    383         }
    384         return result;
    385     }
    386
    387     private final List assignableEntities = new ArrayList();
    388
    389     public List getAssignableEntities() {
    390         return assignableEntities;
    391     }
    392
    393     public Set getReferenceEntitiesAll() {
    394         final Set result = new HashSet();
    395         for (Entity current = this; current != null; current = current.getSuperClass())
    396             result.addAll(current.getReferenceEntities());
    397         return result;
    398     }
    399
    400     public List getReferencesAll() {
    401         final List result = new ArrayList();
    402         for (Entity current = this; current != null; current = current.getSuperClass())
    403             result.addAll(current.getReferences().toList());
    404         return result;
    405     }
    406
    407     private Property captionProperty = null;
    408
    409     public Property getCaptionProperty() {
    410         return captionProperty;
    411     }
    412
    413     public void setCaptionProperty(Property captionProperty) {
    414         this.captionProperty = captionProperty;
    415     }
    416 }