1 19 package org.netbeans.modules.j2ee.ddloaders.multiview; 20 21 import java.util.Collections ; 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 ; 30 import java.beans.PropertyChangeListener ; 31 import java.util.Arrays ; 32 import java.util.Comparator ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.LinkedList ; 36 import java.util.List ; 37 38 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 PRIMARY_KEY_FINDER_METHOD = "findByPrimaryKey"; 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 super.abstractMethodController = entityMethodController; 59 } 60 61 102 110 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 getPrimkeyField() { 137 return entity.getPrimkeyField(); 138 } 139 140 public String getPrimKeyClass() { 141 return entity.getPrimKeyClass(); 142 } 143 144 public void setPrimkeyFieldName(String fieldName) { 145 entity.setPrimkeyField(fieldName); 146 } 147 148 public void setPrimkeyField(String fieldName) throws ClassNotFoundException { 149 setPrimkeyFieldName(fieldName); 150 if (fieldName != null) { 151 CmpFieldHelper helper = cmpFields.getCmpFieldHelper(fieldName); 152 } 154 modelUpdatedFromUI(); 155 } 156 157 191 198 public void propertyChange(PropertyChangeEvent evt) { 199 super.propertyChange(evt); 200 if (queries == null) { 201 return; 202 } 203 Object source = evt.getSource(); 204 String propertyName = evt.getPropertyName(); 205 Object oldValue = evt.getOldValue(); 206 Object 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 228 public void updateFieldAccessor(String 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 listeners = new LinkedList (); 237 private HashMap cmpFieldHelperMap = new HashMap (); 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 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 () { 281 public int compare(Object o1, Object o2) { 282 String s1 = ((CmpField) o1).getFieldName(); 283 String 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 listener) { 297 listeners.add(listener); 298 } 299 300 public void removePropertyChangeListener(PropertyChangeListener listener) { 301 listeners.remove(listener); 302 } 303 304 public void change(Object source, String propertyName, Object oldValue, Object newValue) { 305 if (source instanceof Entity) { 306 cmpFieldHelperMap.keySet().retainAll(Arrays.asList(entity.getCmpField())); 307 } 308 firePropertyChange(new PropertyChangeEvent (source, propertyName, oldValue, newValue)); 309 } 310 311 protected void firePropertyChange(PropertyChangeEvent evt) { 312 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 313 ((PropertyChangeListener ) iterator.next()).propertyChange(evt); 314 } 315 } 316 317 public void addCmpField() { 318 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 listeners = new LinkedList (); 339 private HashMap queryMethodHelperMap = new HashMap (); 340 private Query[] selectMethods; 341 private Query[] finderMethods; 342 public static final String SELECT_PREFIX = "ejbSelect"; public static final String FIND_PREFIX = "find"; 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 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 s) { 381 List list = new LinkedList (); 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 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 source, String propertyName, Object oldValue, Object newValue) { 423 initQueryMethods(); 424 queryMethodHelperMap.keySet().retainAll(Arrays.asList(entity.getQuery())); 425 firePropertyChange(new PropertyChangeEvent (source, propertyName, oldValue, newValue)); 426 } 427 428 public Query[] getQuery() { 430 return entity.getQuery(); 431 } 432 433 public void addPropertyChangeListener(PropertyChangeListener listener) { 434 listeners.add(listener); 435 } 436 437 public void removePropertyChangeListener(PropertyChangeListener listener) { 438 listeners.remove(listener); 439 } 440 441 protected void firePropertyChange(PropertyChangeEvent evt) { 442 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 443 ((PropertyChangeListener ) iterator.next()).propertyChange(evt); 444 } 445 } 446 447 public String getLocal() { 448 return EntityHelper.this.getLocal(); 449 } 450 451 public String 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 |