KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > RelationRole


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.ide;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Vector JavaDoc;
23 import org.objectweb.jac.core.ObjectRepository;
24 import org.objectweb.jac.core.rtti.ClassItem;
25 import org.objectweb.jac.core.rtti.ClassRepository;
26 import org.objectweb.jac.core.rtti.FieldItem;
27 import org.objectweb.jac.core.rtti.NamingConventions;
28 import org.objectweb.jac.util.Strings;
29
30 public class RelationRole extends Role implements Typed {
31
32     protected static final String JavaDoc UNDEFINED = "<UNDEFINED END>";
33
34     public RelationRole(Link link) {
35         super(link);
36     }
37
38     public RelationRole() {
39     }
40
41     String JavaDoc cardinality = "0-1";
42     public String JavaDoc getCardinality() {
43         return cardinality;
44     }
45     public void setCardinality(String JavaDoc cardinality) {
46         this.cardinality = cardinality;
47     }
48
49     /**
50      * Returns the role name used for code generation
51      */

52     public String JavaDoc getGenerationName() {
53         if (end==null)
54             return UNDEFINED;
55         return Strings.toUSAscii(getRoleName());
56     }
57
58     public String JavaDoc getGenerationFullName() {
59         return start.getGenerationFullName()+"."+getGenerationName();
60     }
61
62     public String JavaDoc getRoleName() {
63         if (name==null || name.equals("")) {
64             if (end==null) {
65                 return UNDEFINED;
66             } else {
67                 if (isMultiple()) {
68                     return NamingConventions.maybeLowerFirst(
69                         Projects.plurals.getPlural(end.getName()));
70                 } else {
71                     return NamingConventions.maybeLowerFirst(end.getName());
72                 }
73             }
74         } else {
75             return name;
76         }
77     }
78
79     public Type getType() {
80         if (end == null)
81             return null;
82         if (isMultiple()) {
83             if (primaryKey!=null)
84                 return Projects.types.resolveType("HashMap","java.util");
85             else
86                 return Projects.types.resolveType("Vector","java.util");
87         } else {
88             return (Type)end;
89         }
90     }
91
92     public Type getAbstractType() {
93         if (end == null)
94             return null;
95         if (isMultiple()) {
96             if (primaryKey!=null)
97                 return Projects.types.resolveType("Map","java.util");
98             else
99                 return Projects.types.resolveType("List","java.util");
100         } else {
101             return (Type)end;
102         }
103     }
104
105     public boolean isMultiple() {
106         if( cardinality!=null && (
107             cardinality.equals("*") || cardinality.equals("0-*")||
108             cardinality.equals("1-*"))) {
109             return true;
110         } else {
111             return false;
112         }
113     }
114
115     public String JavaDoc getPrototype() {
116         if (end==null)
117             return UNDEFINED;
118         String JavaDoc role = getGenerationName();
119         String JavaDoc prototype = (getAbstractType().getGenerationFullName())+" "+role;
120         if (isMultiple()) {
121             return prototype+" = new "+getType().getGenerationFullName()+"()";
122         } else {
123             return prototype;
124         }
125     }
126
127     Method adder;
128     public Method getAdder() {
129         return adder;
130     }
131     public void setAdder(Method method) {
132         this.adder = method;
133         if (method!=null)
134             method.setParent((Class JavaDoc)start);
135     }
136
137     /**
138      * Initialize adder. Sets its name, parameters and return type.
139      *
140      * @param adder adder method to initialize
141      */

142     public void initAdder(Method adder) {
143         adder.setType(Projects.types.resolveType("void"));
144         adder.addParameter(new Parameter(NamingConventions.lowerFirst(end.getName()),(Type)end));
145         adder.setName(CodeGeneration.getAdderName(getGenerationName()));
146     }
147
148     Method remover;
149     public Method getRemover() {
150         return remover;
151     }
152     public void setRemover(Method method) {
153         this.remover = method;
154         if (method!=null)
155             method.setParent((Class JavaDoc)start);
156     }
157     /**
158      * Initialize remover. Sets its name, parameters and return type.
159      *
160      * @param remover remover method to initialize
161      */

162     public void initRemover(Method remover) {
163         remover.setType(Projects.types.resolveType("void"));
164         remover.addParameter(new Parameter(NamingConventions.lowerFirst(end.getName()),(Type)end));
165         remover.setName(CodeGeneration.getRemoverName(getGenerationName()));
166     }
167
168     Method clearer;
169     public Method getClearer() {
170         return clearer;
171     }
172     public void setClearer(Method method) {
173         this.clearer = method;
174         if (method!=null)
175             method.setParent((Class JavaDoc)start);
176     }
177     /**
178      * Initialize clearer. Sets its name, parameters and return type.
179      *
180      * @param clearer clearer method to initialize
181      */

182     public void initClearer(Method clearer) {
183         clearer.setType(Projects.types.resolveType("void"));
184         clearer.setName(CodeGeneration.getClearerName(getGenerationName()));
185     }
186
187     Method getter;
188     public Method getGetter() {
189         return getter;
190     }
191     public void setGetter(Method method) {
192         this.getter = method;
193         if (method!=null)
194             method.setParent((Class JavaDoc)start);
195     }
196     /**
197      * Initialize getter. Sets its name, and return type.
198      *
199      * @param getter getter method to initialize
200      */

201     public void initGetter(Method getter) {
202         getter.setType(getAbstractType());
203         getter.setName(CodeGeneration.getGetterName(getGenerationName()));
204     }
205
206     public boolean isNavigable() {
207         RelationLink rel = (RelationLink)link;
208         if (rel.getOrientation()==RelationLink.ORIENTATION_BOTH) {
209             return true;
210         } else {
211             if (link.getStartRole()==this) {
212                 return rel.getOrientation()==RelationLink.ORIENTATION_STRAIGHT;
213             } else {
214                 return rel.getOrientation()==RelationLink.ORIENTATION_REVERSE;
215             }
216         }
217     }
218
219     public boolean isAggregation() {
220         RelationLink rel = (RelationLink)link;
221         return rel.isAggregation() && isStartRole();
222     }
223
224     /** Field or RelationRole */
225     Typed primaryKey;
226     public Typed getPrimaryKey() {
227         return primaryKey;
228     }
229     public void setPrimaryKey(Typed primaryKey) {
230         this.primaryKey = primaryKey;
231     }
232
233     /**
234      * Returns the name of the getter method.
235      */

236     public String JavaDoc getRemoverName() {
237         if (remover!=null) {
238             return remover.getGenerationName();
239         } else {
240             return CodeGeneration.getRemoverName(getGenerationName());
241         }
242     }
243
244     public Collection JavaDoc primaryKeyChoices() {
245         if (end==null) {
246             return new Vector JavaDoc(0);
247         } else {
248             Class JavaDoc cl = (Class JavaDoc)end;
249             Vector JavaDoc result = new Vector JavaDoc();
250             result.addAll(cl.getReferenceRoles());
251             result.addAll(cl.getAllFields());
252             return result;
253         }
254     }
255
256     public Collection JavaDoc methodChoices() {
257         ClassItem cl = ClassRepository.get().getClass(Method.class);
258         FieldItem field = cl.getField("parent");
259         return ObjectRepository.getObjectsWhere(cl,field,null);
260     }
261 }
262
263
Popular Tags