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/11/08 11:25:21
    6    */
    7   package org.asyrinx.joey.gen.model.java;
    8
    9   import java.util.ArrayList;
    10  import java.util.Iterator;
    11  import java.util.List;
    12  import java.util.Set;
    13
    14  import org.apache.commons.lang.StringUtils;
    15  import org.asyrinx.joey.gen.hibernate.Cascade;
    16  import org.asyrinx.joey.gen.model.ElementSet;
    17
    18  /***
    19   * @author takeshi
    20   */
    21  public class Reference extends ElementSet implements Parameter {
    22
    23      /***
    24       * @param parent
    25       */
    26      public Reference(Entity parent) {
    27          super(parent);
    28      }
    29
    30      /***
    31       * @param parent
    32       * @param name
    33       */
    34      public Reference(Entity parent, String name) {
    35          super(parent, name);
    36      }
    37
    38      private Entity referenceClass = null;
    39
    40      private ReferenceType type = ReferenceType.NORMAL;
    41
    42      /*
    43       * (non-Javadoc)
    44       *
    45       * @see org.asyrinx.joey.gen.model.ElementSet#isEntity()
    46       */
    47      public boolean isEntity() {
    48          return true;
    49      }
    50
    51      /*
    52       * (non-Javadoc)
    53       *
    54       * @see org.asyrinx.joey.gen.model.Element#getParentElement()
    55       */
    56      public Entity getParent() {
    57          return (Entity) super.getParentElement();
    58      }
    59
    60      public void add(ReferenceEntry entry) {
    61          super.add(entry);
    62      }
    63
    64      /*
    65       * (non-Javadoc)
    66       *
    67       * @see org.asyrinx.joey.gen.model.ElementSet#contains(org.asyrinx.joey.gen.model.Element)
    68       */
    69      public boolean contains(ReferenceEntry entry) {
    70          return super.contains(entry);
    71      }
    72
    73      /*
    74       * (non-Javadoc)
    75       *
    76       * @see org.asyrinx.joey.gen.model.ElementSet#getElement(int)
    77       */
    78      public ReferenceEntry getEntry(int index) {
    79          return (ReferenceEntry) super.getElement(index);
    80      }
    81
    82      /*
    83       * (non-Javadoc)
    84       *
    85       * @see org.asyrinx.joey.gen.model.ElementSet#get(java.lang.String)
    86       */
    87      public ReferenceEntry getEntry(String name) {
    88          return (ReferenceEntry) super.getElement(name);
    89      }
    90
    91      /*
    92       * (non-Javadoc)
    93       *
    94       * @see org.asyrinx.joey.gen.model.ElementSet#remove(java.lang.String)
    95       */
    96      public ReferenceEntry removeEntry(String name) {
    97          return (ReferenceEntry) super.removeElement(name);
    98      }
    99
    100     /***
    101      * @return
    102      */
    103     public String getReferenceClassName() {
    104         return (getReferenceClass() == null) ? null : getReferenceClass().getName();
    105     }
    106
    107     /***
    108      * @return Returns the referenceClass.
    109      */
    110     public Entity getReferenceClass() {
    111         return referenceClass;
    112     }
    113
    114     /***
    115      * @param referenceClass
    116      *            The referenceClass to set.
    117      */
    118     public void setReferenceClass(Entity referenceClass) {
    119         this.referenceClass = referenceClass;
    120     }
    121
    122     /***
    123      * @param property
    124      * @return
    125      */
    126     public boolean containsAsLocal(Property property) {
    127         for (Iterator i = this.iterator(); i.hasNext();) {
    128             final ReferenceEntry entry = (ReferenceEntry) i.next();
    129             if (entry.getLocal() == property)
    130                 return true;
    131         }
    132         return false;
    133     }
    134
    135     /***
    136      * @param property
    137      * @return
    138      */
    139     public boolean containsAsForeign(Property property) {
    140         for (Iterator i = this.iterator(); i.hasNext();) {
    141             final ReferenceEntry entry = (ReferenceEntry) i.next();
    142             if (entry.getForeign() == property)
    143                 return true;
    144         }
    145         return false;
    146     }
    147
    148     public String getPropertyNameInReferred(boolean plural) {
    149         final String className = getParent().getName();
    150         String relatedByCol = "";
    151         final Set otherRefs = this.getParent().getParent().findReferences(getParent(),
    152                 getReferenceClass());
    153         if (otherRefs.size() > 1) {
    154             for (Iterator i = this.iterator(); i.hasNext();) {
    155                 final ReferenceEntry entry = (ReferenceEntry) i.next();
    156                 if (!entry.getLocal().getReferencesContainedAsLocal().isEmpty())
    157                     relatedByCol = relatedByCol + entry.getLocal().getCapitalizedName();
    158             }
    159         }
    160         if (StringUtils.isEmpty(relatedByCol)) {
    161             return (plural) ? StringUtils.uncapitalize(className + "s") : StringUtils
    162                     .uncapitalize(className);
    163         }
    164         //final String refClassName = getReferenceClassName();
    165         final String s = ((plural) ? "s" : "") + "RelatedBy";
    166         return StringUtils.uncapitalize(className) + s + relatedByCol;
    167     }
    168
    169     public String getPropertyNameInReferred() {
    170         return getPropertyNameInReferred(true);
    171     }
    172
    173     public String getPropertyNameInLocal() {
    174         final Entity foreignClass = this.getReferenceClass();
    175         final String className = foreignClass.getName();
    176         String relCol = "";
    177         final List otherRefs = new ArrayList();
    178         for (Entity current = this.getParent(); current != null; current = current.getSuperClass()) {
    179             otherRefs.addAll(current.getReferencesContainedAsForeign(foreignClass));
    180         }
    181         if (otherRefs.size() > 1) {
    182             for (Iterator i = this.iterator(); i.hasNext();) {
    183                 final ReferenceEntry entry = (ReferenceEntry) i.next();
    184                 if ((!entry.getLocal().getReferencesContainedAsLocal().isEmpty())
    185                         || (getReferenceClass() == this.getParent()))
    186                     relCol = relCol + entry.getLocal().getCapitalizedName();
    187             }
    188         }
    189         if (StringUtils.isNotEmpty(relCol))
    190             relCol = "RelatedBy" + relCol;
    191         return StringUtils.uncapitalize(className + relCol);
    192     }
    193
    194     /***
    195      * @return Returns the type.
    196      */
    197     public ReferenceType getType() {
    198         return type;
    199     }
    200
    201     /***
    202      * @param type
    203      *            The type to set.
    204      */
    205     public void setType(ReferenceType type) {
    206         this.type = type;
    207     }
    208
    209     public boolean isForward() {
    210         return getType() != ReferenceType.EXTENDS;
    211     }
    212
    213     public boolean isBackward() {
    214         return getType() == ReferenceType.BIDIRECTION;
    215     }
    216
    217     /*
    218      * (non-Javadoc)
    219      *
    220      * @see org.asyrinx.joey.gen.model.java.Parameter#getParamName()
    221      */
    222     public String getParamName() {
    223         return this.getPropertyNameInLocal();
    224     }
    225
    226     /*
    227      * (non-Javadoc)
    228      *
    229      * @see org.asyrinx.joey.gen.model.java.Parameter#getParamType()
    230      */
    231     public Type getParamType() {
    232         return this.getReferenceClass();
    233     }
    234
    235     private Cascade cascade = null;
    236
    237     public Cascade getCascade() {
    238         return cascade;
    239     }
    240
    241     public void setCascade(Cascade cascade) {
    242         this.cascade = cascade;
    243     }
    244
    245     public String getCaption() {
    246         final StringBuffer result = new StringBuffer();
    247         result.append(getParent().getName());
    248         result.append("-->");
    249         result.append(getReferenceClass().getName());
    250         result.append("[");
    251         for (Iterator i = this.iterator(); i.hasNext();) {
    252             final ReferenceEntry entry = (ReferenceEntry) i.next();
    253             result.append(entry.getLocal().getName());
    254             result.append("-->");
    255             result.append(entry.getForeign().getName());
    256             if (i.hasNext())
    257                 result.append(" ");
    258         }
    259         result.append("]");
    260         return result.toString();
    261     }
    262
    263     /*
    264      * (non-Javadoc)
    265      *
    266      * @see org.asyrinx.joey.gen.model.Element#toString()
    267      */
    268     public String toString() {
    269         return getCaption();
    270     }
    271
    272 }