1
2
3
4
5
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 {
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
55
56
57
58 public boolean isEntity() {
59 return true;
60 }
61
62
63
64
65
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
77
78
79
80 public boolean contains(ReferenceEntry entry) {
81 return super.contains(entry);
82 }
83
84
85
86
87
88
89 public ReferenceEntry getEntry(int index) {
90 return (ReferenceEntry) super.getElement(index);
91 }
92
93
94
95
96
97
98 public ReferenceEntry getEntry(String name) {
99 return (ReferenceEntry) super.getElement(name);
100 }
101
102
103
104
105
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
161
162
163
164
165 final String className = getParent().getName();
166 String relatedByCol = "";
167 final List otherRefs = this.getParent().getParent().getReferencesContainedAsLocal(getParent());
168 if (otherRefs.size() > 1) {
169
170
171
172
173
174
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
182
183
184
185
186
187
188
189
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
203
204
205
206
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
214
215
216
217
218
219
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
229
230
231 if (StringUtils.isNotEmpty(relCol))
232 relCol = "RelatedBy" + relCol;
233
234
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 }