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         return keys.getPrimaryKey();
206     }
207 
208     /***
209      * @param property
210      * @return
211      */
212     public List getReferencesContainedAsLocal(Property property) {
213         return this.getReferences().getReferencesContainedAsLocal(property);
214     }
215 
216     /***
217      * @param javaClass
218      * @return
219      */
220     public List getReferencesContainedAsForeign(Entity javaClass) {
221         return this.getReferences().getReferencesContainedAsForeign(javaClass);
222     }
223 
224     /***
225      * @param property
226      * @return
227      */
228     public List getReferencesContainedAsForeign(Property property) {
229         return getParent().getReferencesContainedAsForeign(property);
230     }
231 
232     public List getReferreds() {
233         return getParent().getReferencesContainedAsForeign(this);
234     }
235 
236     /***
237      * @param javaClass
238      * @param dest
239      */
240     public void findReferencesContainedAsForeign(Entity javaClass, List dest) {
241         getReferences().findReferencesContainedAsForeign(javaClass, dest);
242     }
243 
244     /***
245      * @param javaClass
246      * @param result
247      */
248     public void findReferencesContainedAsLocal(Entity javaClass, List dest) {
249         getReferences().findReferencesContainedAsLocal(javaClass, dest);
250     }
251 
252     /***
253      * @return Returns the imports.
254      */
255     public Set getImports() {
256         return imports;
257     }
258 
259     private String superClassName = null;
260 
261     private Entity superClass = null;
262 
263     /***
264      * @return Returns the superClass.
265      */
266     public Entity getSuperClass() {
267         return superClass;
268     }
269 
270     /***
271      * @param superClass
272      *            The superClass to set.
273      */
274     public void setSuperClass(Entity superClass) {
275         this.superClass = superClass;
276     }
277 
278     /***
279      * @return Returns the superClassName.
280      */
281     public String getSuperClassName() {
282         return superClassName;
283     }
284 
285     /***
286      * @param superClassName
287      *            The superClassName to set.
288      */
289     public void setSuperClassName(String superClassName) {
290         this.superClassName = superClassName;
291     }
292 
293     public List getPropertiesExtended() {
294         if (this.getSuperClass() == null)
295             return new ArrayList();
296         final List result = new ArrayList(this.getSuperClass().getPropertiesExtended());
297         result.addAll(this.getSuperClass().getProperties().toList());
298         return result;
299     }
300 
301     public List getPropertiesAll() {
302         final List result = getPropertiesExtended();
303         result.addAll(this.getProperties().toList());
304         return result;
305     }
306 
307     /***
308      * @return
309      */
310     public List getConstructorParams() {
311         final List result = (this.getSuperClass() != null) ? this.getSuperClass().getConstructorParams()
312                 : new ArrayList();
313         final Set used = new HashSet();
314         for (Iterator i = getProperties().iterator(); i.hasNext();) {
315             final Property property = (Property) i.next();
316             addReferenceOfProperty(result, used, property);
317             if (used.contains(property))
318                 continue;
319             if (property.isExtended())
320                 continue;
321             result.add(property);
322             used.add(property);
323         }
324         return result;
325     }
326 
327     /***
328      * @param result
329      * @param used
330      * @param property
331      */
332     private void addReferenceOfProperty(final List result, final Set used, final Property property) {
333         for (Iterator i = property.getReferencesContainedAsLocal().iterator(); i.hasNext();) {
334             final Reference reference = (Reference) i.next();
335             if (used.contains(reference))
336                 continue;
337             result.add(reference);
338             used.add(reference);
339             addPropertyAsUsed(used, reference);
340         }
341     }
342 
343     /***
344      * @param used
345      * @param reference
346      */
347     private void addPropertyAsUsed(final Set used, final Reference reference) {
348         for (Iterator iterator = reference.iterator(); iterator.hasNext();) {
349             final ReferenceEntry entry = (ReferenceEntry) iterator.next();
350             used.add(entry.getLocal());
351         }
352     }
353 
354 }