KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > EntityHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.ddloaders.multiview;
20
21 import java.util.Collections JavaDoc;
22 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
23 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
24 import org.netbeans.modules.j2ee.dd.api.ejb.Query;
25 import org.netbeans.modules.j2ee.ejbcore.api.ui.CallEjb;
26 import org.netbeans.modules.j2ee.ejbcore.api.methodcontroller.EntityMethodController;
27 import org.netbeans.modules.j2ee.ejbcore.api.codegeneration.EntityGenerator;
28
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Comparator JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * @author pfiala
40  */

41 public class EntityHelper extends EntityAndSessionHelper {
42     
43     private final Entity entity;
44     public final CmpFields cmpFields;
45     public final EntityHelper.Queries queries;
46     private static final String JavaDoc PRIMARY_KEY_FINDER_METHOD = "findByPrimaryKey"; // NOI18N
47
private EntityMethodController entityMethodController;
48     
49     
50     public EntityHelper(EjbJarMultiViewDataObject ejbJarMultiViewDataObject, Entity entity) {
51         super(ejbJarMultiViewDataObject, entity);
52         this.entity = ((Entity) ejb);
53         cmpFields = new CmpFields();
54         queries = new Queries();
55         entityMethodController = null;
56 // new EntityMethodController(entity, sourceClassPath,
57
// ejbJarMultiViewDataObject.getEjbJar());
58
super.abstractMethodController = entityMethodController;
59     }
60     
61 // public Method createAccessMethod(String fieldName, Type type, boolean get) {
62
// entityMethodController.beginWriteJmiTransaction();
63
// boolean rollback = true;
64
// try {
65
// JavaClass beanClass = getBeanClass();
66
// assert beanClass != null;
67
// entityMethodController.registerClassForSave(beanClass);
68
// Method prototype = JMIUtils.createMethod(beanClass);
69
// prototype.setName(EntityMethodController.getMethodName(fieldName, get));
70
// if (get) {
71
// prototype.setType(type);
72
// } else {
73
// prototype.getParameters().add(JMIUtils.createParameter(prototype, fieldName, type, false));
74
// prototype.setType(JMIUtils.resolveType("void"));
75
// }
76
// Utils.addMethod(beanClass, prototype, false, Modifier.PUBLIC | Modifier.ABSTRACT);
77
// Method accessMethod = Utils.getMethod(beanClass, prototype);
78
// rollback = false;
79
// return accessMethod;
80
// } finally {
81
// entityMethodController.endWriteJmiTransaction(rollback);
82
// }
83
// }
84
//
85
// public Method getSetterMethod(String fieldName, Method getterMethod) {
86
// if (getterMethod == null) {
87
// return null;
88
// } else {
89
// JMIUtils.beginJmiTransaction();
90
// try {
91
// return EntityMethodController.getSetterMethod(getBeanClass(), fieldName, getterMethod.getType());
92
// } finally {
93
// JMIUtils.endJmiTransaction();
94
// }
95
// }
96
// }
97
//
98
// public Method getGetterMethod(String fieldName) {
99
// return getGetterMethod(getBeanClass(), fieldName);
100
// }
101

102     /**
103      * Gets the getter method matching the given <code>fieldName</code> from
104      * the given <code>javaClass</class>. If no matching method is found,
105      * searches recursively from super classes.
106      * @param javaClass the java class from whose hierarchy the getter method is searched.
107      * @param fieldName the name of the field whose getter method we're looking for.
108      * @return the getter method matching the <code>fieldName</code> or null if not found.
109      */

110 // private static Method getGetterMethod(JavaClass javaClass, String fieldName){
111
// Method result = javaClass.getMethod(EntityMethodController.getMethodName(fieldName, true), Collections.EMPTY_LIST, false);
112
// if (result != null){
113
// return result;
114
// }
115
// JavaClass superClass = javaClass.getSuperClass();
116
// if (superClass == null){
117
// return null;
118
// }
119
// return getGetterMethod(superClass, fieldName);
120
// }
121
//
122

123     public void removeQuery(Query query) {
124         entity.removeQuery(query);
125         modelUpdatedFromUI();
126     }
127     
128     public boolean hasLocalInterface() {
129         return ejb.getLocal() != null;
130     }
131     
132     public boolean hasRemoteInterface() {
133         return ejb.getRemote() != null;
134     }
135     
136     public String JavaDoc getPrimkeyField() {
137         return entity.getPrimkeyField();
138     }
139     
140     public String JavaDoc getPrimKeyClass() {
141         return entity.getPrimKeyClass();
142     }
143     
144     public void setPrimkeyFieldName(String JavaDoc fieldName) {
145         entity.setPrimkeyField(fieldName);
146     }
147     
148     public void setPrimkeyField(String JavaDoc fieldName) throws ClassNotFoundException JavaDoc {
149         setPrimkeyFieldName(fieldName);
150         if (fieldName != null) {
151             CmpFieldHelper helper = cmpFields.getCmpFieldHelper(fieldName);
152 // helper.reloadType();
153
}
154         modelUpdatedFromUI();
155     }
156     
157 // public void setPrimKeyClass(Type newType) {
158
// List params = new LinkedList();
159
// params.add(JMIUtils.resolveType(entity.getPrimKeyClass()));
160
// changeFinderMethodParam(getLocalHomeInterfaceClass(), params, newType);
161
// changeFinderMethodParam(getHomeInterfaceClass(), params, newType);
162
// entity.setPrimKeyClass(newType.getName());
163
// JavaClass beanClass = getBeanClass();
164
// if (beanClass != null) {
165
// boolean rollback = true;
166
// JMIUtils.beginJmiTransaction(true);
167
// try {
168
// entityMethodController.registerClassForSave(beanClass);
169
// Method[] methods = JMIUtils.getMethods(beanClass);
170
// for (int i = 0; i < methods.length; i++) {
171
// Method method = methods[i];
172
// if ("ejbCreate".equals(method.getName())) {
173
// method.setType(newType);
174
// }
175
// }
176
// rollback = false;
177
// } finally {
178
// JMIUtils.endJmiTransaction(rollback);
179
// }
180
// }
181
// }
182
//
183
// private void changeFinderMethodParam(JavaClass javaClass, List params, Type newType) {
184
// if (javaClass != null) {
185
// entityMethodController.registerClassForSave(javaClass);
186
// Method method = javaClass.getMethod(PRIMARY_KEY_FINDER_METHOD, params, false);
187
// Utils.changeParameterType(method, newType);
188
// }
189
// }
190

191     //TODO: RETOUCHE
192
// protected EntityAndSessionGenerator getGenerator() {
193
// EntityGenerator generator = new EntityGenerator(entity.getPrimKeyClass());
194
// generator.setCMP(Entity.PERSISTENCE_TYPE_CONTAINER.equals(entity.getPersistenceType()));
195
// return generator;
196
// }
197

198     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
199         super.propertyChange(evt);
200         if (queries == null) {
201             return;
202         }
203         Object JavaDoc source = evt.getSource();
204         String JavaDoc propertyName = evt.getPropertyName();
205         Object JavaDoc oldValue = evt.getOldValue();
206         Object JavaDoc newValue = evt.getNewValue();
207         if (source == entity) {
208             if ((oldValue instanceof CmpField || newValue instanceof CmpField)) {
209                 cmpFields.change(source, propertyName, oldValue, newValue);
210             } else if ((oldValue instanceof Query || newValue instanceof Query)) {
211                 queries.change(source, propertyName, oldValue, newValue);
212             }
213         } else if (source instanceof CmpField) {
214             cmpFields.change(source, propertyName, oldValue, newValue);
215         } else if (source instanceof Query) {
216             queries.change(source, propertyName, oldValue, newValue);
217         }
218     }
219     
220     public EntityMethodController getEntityMethodController() {
221         return entityMethodController;
222     }
223     
224 // public void updateMethod(Method method, boolean local, boolean isComponent, boolean shouldExist) {
225
// entityMethodController.updateMethod(method, local, isComponent, shouldExist);
226
// }
227

228     public void updateFieldAccessor(String JavaDoc fieldName, boolean getter, boolean local, boolean shouldExist) {
229         if (local && hasLocalInterface() || !local && hasRemoteInterface()) {
230             entityMethodController.updateFieldAccessor(fieldName, getter, local, shouldExist);
231         }
232     }
233     
234     public class CmpFields implements PropertyChangeSource {
235         
236         private List JavaDoc listeners = new LinkedList JavaDoc();
237         private HashMap JavaDoc cmpFieldHelperMap = new HashMap JavaDoc();
238         private CmpFieldsTableModel cmpFieldsTableModel = new CmpFieldsTableModel(this);
239         
240         public int getCmpFieldCount() {
241             return entity.getCmpField().length;
242         }
243         
244         public CmpFieldHelper getCmpFieldHelper(int row) {
245             CmpField field = getCmpField(row);
246             CmpFieldHelper cmpFieldHelper = (CmpFieldHelper) cmpFieldHelperMap.get(field);
247             if (cmpFieldHelper == null) {
248                 cmpFieldHelper = createCmpFieldHelper(field);
249             }
250             return cmpFieldHelper;
251         }
252         
253         private CmpFieldHelper getCmpFieldHelper(String JavaDoc fieldName) {
254             CmpFieldHelper cmpFieldHelper = (CmpFieldHelper) cmpFieldHelperMap.get(fieldName);
255             if (cmpFieldHelper == null) {
256                 CmpField[] cmpFields = entity.getCmpField();
257                 for (int i = 0; i < cmpFields.length; i++) {
258                     CmpField field = cmpFields[i];
259                     if (fieldName.equals(field.getFieldName())) {
260                         cmpFieldHelper = createCmpFieldHelper(field);
261                     }
262                 }
263             }
264             return cmpFieldHelper;
265         }
266         
267         private CmpFieldHelper createCmpFieldHelper(CmpField field) {
268             CmpFieldHelper cmpFieldHelper;
269             cmpFieldHelper = new CmpFieldHelper(EntityHelper.this, field);
270             cmpFieldHelperMap.put(field, cmpFieldHelper);
271             return cmpFieldHelper;
272         }
273         
274         private CmpField getCmpField(int row) {
275             return getCmpFields()[row];
276         }
277         
278         public CmpField[] getCmpFields() {
279             CmpField[] cmpFields = entity.getCmpField();
280             Arrays.sort(cmpFields, new Comparator JavaDoc() {
281                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
282                     String JavaDoc s1 = ((CmpField) o1).getFieldName();
283                     String JavaDoc s2 = ((CmpField) o2).getFieldName();
284                     if (s1 == null) {
285                         s1 = "";
286                     }
287                     if (s2 == null) {
288                         s2 = "";
289                     }
290                     return s1.compareTo(s2);
291                 }
292             });
293             return cmpFields;
294         }
295         
296         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
297             listeners.add(listener);
298         }
299         
300         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
301             listeners.remove(listener);
302         }
303         
304         public void change(Object JavaDoc source, String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
305             if (source instanceof Entity) {
306                 cmpFieldHelperMap.keySet().retainAll(Arrays.asList(entity.getCmpField()));
307             }
308             firePropertyChange(new PropertyChangeEvent JavaDoc(source, propertyName, oldValue, newValue));
309         }
310         
311         protected void firePropertyChange(PropertyChangeEvent JavaDoc evt) {
312             for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();) {
313                 ((PropertyChangeListener JavaDoc) iterator.next()).propertyChange(evt);
314             }
315         }
316         
317         public void addCmpField() {
318 // CallEjb.addCmpField(getBeanClass(), ejbJarFile);
319
modelUpdatedFromUI();
320         }
321         
322         public CmpFieldsTableModel getCmpFieldsTableModel() {
323             return cmpFieldsTableModel;
324         }
325         
326         public int getFieldRow(CmpField cmpField) {
327             final CmpField[] fields = getCmpFields();
328             for (int i = 0; i < fields.length; i++) {
329                 if (fields[i].equals(cmpField)) {
330                     return i;
331                 }
332             }
333             return -1;
334         }
335     }
336     
337     public class Queries implements PropertyChangeSource {
338         private List JavaDoc listeners = new LinkedList JavaDoc();
339         private HashMap JavaDoc queryMethodHelperMap = new HashMap JavaDoc();
340         private Query[] selectMethods;
341         private Query[] finderMethods;
342         public static final String JavaDoc SELECT_PREFIX = "ejbSelect"; //NOI18N
343
public static final String JavaDoc FIND_PREFIX = "find"; //NOI18N
344
private FinderMethodsTableModel finderMethodsTableModel = new FinderMethodsTableModel(this);
345         private SelectMethodsTableModel selectMethodsTableModel = new SelectMethodsTableModel(this);
346         
347         public Queries() {
348             initQueryMethods();
349         }
350         
351         private void initQueryMethods() {
352             selectMethods = getQueries(SELECT_PREFIX);
353             finderMethods = getQueries(FIND_PREFIX);
354         }
355         
356         public QueryMethodHelper getQueryMethodHelper(Query query) {
357 // JMIUtils.beginJmiTransaction();
358
// try {
359
// QueryMethodHelper queryMethodHelper = (QueryMethodHelper) queryMethodHelperMap.get(query);
360
// if (queryMethodHelper == null) {
361
// queryMethodHelper = new QueryMethodHelper(EntityHelper.this, query);
362
// queryMethodHelperMap.put(query, queryMethodHelper);
363
// }
364
// return queryMethodHelper;
365
// } finally {
366
// JMIUtils.endJmiTransaction();
367
// }
368

369             return null;
370         }
371         
372         public QueryMethodHelper getFinderMethodHelper(int row) {
373             return getQueryMethodHelper(finderMethods[row]);
374         }
375         
376         public QueryMethodHelper getSelectMethodHelper(int row) {
377             return getQueryMethodHelper(selectMethods[row]);
378         }
379         
380         private Query[] getQueries(String JavaDoc s) {
381             List JavaDoc list = new LinkedList JavaDoc();
382             Query[] queries = entity.getQuery();
383             for (int i = 0; i < queries.length; i++) {
384                 Query query = queries[i];
385                 if (query.getQueryMethod().getMethodName().startsWith(s)) {
386                     list.add(query);
387                     final QueryMethodHelper helper = (QueryMethodHelper) queryMethodHelperMap.get(query);
388                     if (helper != null) {
389                         helper.init();
390                     }
391                     
392                 }
393             }
394             return (Query[]) list.toArray(new Query[0]);
395         }
396         
397 // public void addFinderMethod() {
398
// CallEjb.addFinderMethod(getBeanClass());
399
// modelUpdatedFromUI();
400
// }
401
//
402
// public void addSelectMethod() {
403
// CallEjb.addSelectMethod(getBeanClass());
404
// }
405
//
406
public int getFinderMethodCount() {
407             return finderMethods.length;
408         }
409         
410         public int getSelectMethodCount() {
411             return selectMethods.length;
412         }
413         
414         public Query getFinderMethod(int rowIndex) {
415             return finderMethods[rowIndex];
416         }
417         
418         public Query getSelecMethod(int rowIndex) {
419             return selectMethods[rowIndex];
420         }
421         
422         public void change(Object JavaDoc source, String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
423             initQueryMethods();
424             queryMethodHelperMap.keySet().retainAll(Arrays.asList(entity.getQuery()));
425             firePropertyChange(new PropertyChangeEvent JavaDoc(source, propertyName, oldValue, newValue));
426         }
427         
428         //todo
429
public Query[] getQuery() {
430             return entity.getQuery();
431         }
432         
433         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
434             listeners.add(listener);
435         }
436         
437         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
438             listeners.remove(listener);
439         }
440         
441         protected void firePropertyChange(PropertyChangeEvent JavaDoc evt) {
442             for (Iterator JavaDoc iterator = listeners.iterator(); iterator.hasNext();) {
443                 ((PropertyChangeListener JavaDoc) iterator.next()).propertyChange(evt);
444             }
445         }
446         
447         public String JavaDoc getLocal() {
448             return EntityHelper.this.getLocal();
449         }
450         
451         public String JavaDoc getRemote() {
452             return EntityHelper.this.getRemote();
453         }
454         
455         public FinderMethodsTableModel getFinderMethodsTableModel() {
456             return finderMethodsTableModel;
457         }
458         
459         public SelectMethodsTableModel getSelectMethodsTableModel() {
460             return selectMethodsTableModel;
461         }
462         
463         public int getSelectMethodRow(Query query) {
464             for (int i = 0; i < selectMethods.length; i++) {
465                 if (query.equals(selectMethods[i])) {
466                     return i;
467                 }
468             }
469             return -1;
470         }
471         
472         public int getFinderMethodRow(Query query) {
473             for (int i = 0; i < finderMethods.length; i++) {
474                 if (query.equals(finderMethods[i])) {
475                     return i;
476                 }
477             }
478             return -1;
479         }
480     }
481 }
482
Popular Tags