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