001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.sun.share.configbean;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: import javax.enterprise.deploy.model.DDBean;
050:
051: import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
052: import org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException;
053: import org.netbeans.modules.j2ee.sun.dd.api.ejb.Ejb;
054: import org.netbeans.modules.j2ee.sun.dd.api.ejb.FlushAtEndOfMethod;
055: import org.netbeans.modules.j2ee.sun.dd.api.ejb.Cmp;
056: import org.netbeans.modules.j2ee.sun.dd.api.ejb.Finder;
057: import org.netbeans.modules.j2ee.sun.dd.api.ejb.Method;
058: import org.netbeans.modules.j2ee.sun.dd.api.ejb.OneOneFinders;
059: import org.netbeans.modules.j2ee.sun.dd.api.ejb.PrefetchDisabled;
060: import org.netbeans.modules.j2ee.sun.dd.api.ejb.QueryMethod;
061:
062: import org.netbeans.modules.j2ee.sun.share.configbean.ConfigQuery;
063:
064: /**
065: *
066: * @author vkraemer
067: */
068: public class CmpEntityEjb extends EntityEjb {
069:
070: /** Holds value of property cmp. */
071: private Cmp cmp;
072:
073: /** Holds value of property flush-at-end-of-method. */
074: private FlushAtEndOfMethod flushAtEndOfMethod;
075:
076: /** Holds value of property schema. */
077: private String schema;
078:
079: /** Holds value of property tableName. */
080: private String tableName;
081:
082: /** Holds value of property consistency. */
083: private String consistency;
084:
085: /** Holds value of property secondaryTables. */
086:
087: /** Holds value of property beanName. */
088: //private String beanName;
089: /** Creates a new instance of CmpEntityEjb */
090: public CmpEntityEjb() {
091: }
092:
093: /** -----------------------------------------------------------------------
094: * Validation implementation
095: */
096:
097: // relative xpaths (double as field id's)
098: public static final String FIELD_CMP_MAPPINGPROPERTIES = "cmp/mapping-properties"; // NOI18N
099:
100: protected void updateValidationFieldList() {
101: super .updateValidationFieldList();
102:
103: validationFieldList.add(FIELD_CMP_MAPPINGPROPERTIES);
104: }
105:
106: public boolean validateField(String fieldId) {
107: boolean result = super .validateField(fieldId);
108:
109: Collection/*ValidationError*/errors = new ArrayList();
110:
111: // !PW use visitor pattern to get rid of switch/if statement for validation
112: // field -- data member mapping.
113: //
114: // ValidationSupport can return multiple errors for a single field. We only want
115: // to display one error per field, so we'll pick the first error rather than adding
116: // them all. As the user fixes each error, the remainder will display until all of
117: // them are handled. (Hopefully the errors are generated in a nice order, e.g.
118: // check blank first, then content, etc. If not, we may have to reconsider this.)
119: //
120: String absoluteFieldXpath = getAbsoluteXpath(fieldId);
121: if (fieldId.equals(FIELD_CMP_MAPPINGPROPERTIES)) {
122: String value = (cmp != null) ? cmp.getMappingProperties()
123: : null;
124: errors.add(executeValidator(
125: ValidationError.PARTITION_EJB_GLOBAL, value,
126: absoluteFieldXpath, bundle
127: .getString("LBL_Mapping_Properties"))); // NOI18N
128: }
129:
130: boolean noErrors = true;
131: Iterator errorIter = errors.iterator();
132:
133: while (errorIter.hasNext()) {
134: ValidationError error = (ValidationError) errorIter.next();
135: getMessageDB().updateError(error);
136:
137: if (Utils.notEmpty(error.getMessage())) {
138: noErrors = false;
139: }
140: }
141:
142: // return true if there was no error added
143: return noErrors || result;
144: }
145:
146: /** Getter for property schema.
147: * @return Value of property schema.
148: *
149: */
150: public String getSchema() {
151: return this .schema;
152: }
153:
154: /** Setter for property schema.
155: * @param schema New value of property schema.
156: *
157: * @throws PropertyVetoException
158: *
159: */
160: public void setSchema(String schema)
161: throws java.beans.PropertyVetoException {
162: String oldSchema = this .schema;
163: getVCS().fireVetoableChange("schema", oldSchema, schema);
164: this .schema = schema;
165: getPCS().firePropertyChange("schema", oldSchema, schema);
166: }
167:
168: /** Getter for property tableName.
169: * @return Value of property tableName.
170: *
171: */
172: public String getTableName() {
173: return this .tableName;
174: }
175:
176: /** Setter for property tableName.
177: * @param tableName New value of property tableName.
178: *
179: * @throws PropertyVetoException
180: *
181: */
182: public void setTableName(String tableName)
183: throws java.beans.PropertyVetoException {
184: String oldTableName = this .tableName;
185: getVCS().fireVetoableChange("tableName", oldTableName,
186: tableName);
187: this .tableName = tableName;
188: getPCS().firePropertyChange("tableName", oldTableName,
189: tableName);
190: }
191:
192: /** Getter for property consistency.
193: * @return Value of property consistency.
194: *
195: */
196: public String getConsistency() {
197: return this .consistency;
198: }
199:
200: /** Setter for property consistency.
201: * @param consistency New value of property consistency.
202: *
203: * @throws PropertyVetoException
204: *
205: */
206: public void setConsistency(String consistency)
207: throws java.beans.PropertyVetoException {
208: String oldConsistency = this .consistency;
209: getVCS().fireVetoableChange("consistency", oldConsistency,
210: consistency);
211: this .consistency = consistency;
212: getPCS().firePropertyChange("consistency", oldConsistency,
213: consistency);
214: }
215:
216: /** Getter for property beanName.
217: * @return Value of property beanName.
218: *
219: */
220: public String getBeanName() {
221: return cleanDDBeanText(getDDBean());
222: }
223:
224: /* ------------------------------------------------------------------------
225: * Persistence support. Loads DConfigBeans from previously saved Deployment
226: * plan file.
227: */
228: protected class CmpEntityEjbSnippet extends
229: EntityEjb.EntityEjbSnippet {
230:
231: public CommonDDBean getDDSnippet() {
232: Ejb ejb = (Ejb) super .getDDSnippet();
233: String version = getAppServerVersion()
234: .getEjbJarVersionAsString();
235:
236: Cmp c = getCmp();
237: if (hasContent(c)) {
238: ejb.setCmp((Cmp) c.cloneVersion(version));
239: }
240:
241: try {
242: FlushAtEndOfMethod feom = getFlushAtEndOfMethod();
243: if (feom != null && feom.sizeMethod() > 0) {
244: ejb.setFlushAtEndOfMethod((FlushAtEndOfMethod) feom
245: .cloneVersion(version));
246: }
247: } catch (VersionNotSupportedException ex) {
248: }
249:
250: return ejb;
251: }
252:
253: public boolean hasDDSnippet() {
254: if (super .hasDDSnippet()) {
255: return true;
256: }
257:
258: if (hasContent(getCmp())) {
259: return true;
260: }
261:
262: FlushAtEndOfMethod feom = getFlushAtEndOfMethod();
263: if (feom != null && feom.sizeMethod() > 0) {
264: return true;
265: }
266:
267: return false;
268: }
269:
270: private boolean hasContent(Cmp c) {
271: if (c == null) {
272: return false;
273: }
274:
275: if (Utils.notEmpty(c.getIsOneOneCmp())
276: || Utils.notEmpty(c.getMappingProperties())) {
277: return true;
278: }
279:
280: OneOneFinders finders = c.getOneOneFinders();
281: if (finders != null && finders.sizeFinder() > 0) {
282: return true;
283: }
284:
285: try {
286: PrefetchDisabled pd = c.getPrefetchDisabled();
287: if (pd != null && pd.sizeQueryMethod() > 0) {
288: return true;
289: }
290: } catch (VersionNotSupportedException ex) {
291: }
292:
293: return false;
294: }
295: }
296:
297: java.util.Collection getSnippets() {
298: Collection snippets = new ArrayList();
299: snippets.add(new CmpEntityEjbSnippet());
300:
301: // FIXME create snippet for sun-cmp-mappings.xml here as well.
302:
303: return snippets;
304: }
305:
306: protected void loadEjbProperties(Ejb savedEjb) {
307: super .loadEjbProperties(savedEjb);
308: Cmp cmp = savedEjb.getCmp();
309: if (null != cmp) {
310: this .cmp = cmp;
311: }
312:
313: FlushAtEndOfMethod flushAtEndOfMethod = null;
314: try {
315: flushAtEndOfMethod = savedEjb.getFlushAtEndOfMethod();
316: } catch (org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException e) {
317: //System.out.println("Not Supported Version"); //NOI18N
318: }
319:
320: if (null != flushAtEndOfMethod) {
321: this .flushAtEndOfMethod = flushAtEndOfMethod;
322: }
323: }
324:
325: protected void clearProperties() {
326: super .clearProperties();
327: StorageBeanFactory beanFactory = getConfig()
328: .getStorageFactory();
329:
330: cmp = beanFactory.createCmp();
331: flushAtEndOfMethod = beanFactory.createFlushAtEndOfMethod();
332: schema = null;
333: tableName = null;
334: consistency = null;
335: }
336:
337: /** Getter for property cmp.
338: * @return Value of property cmp.
339: *
340: */
341: public Cmp getCmp() {
342: return this .cmp;
343: }
344:
345: /** Setter for property cmp.
346: * @param cmp New value of property cmp.
347: *
348: * @throws PropertyVetoException
349: *
350: */
351: public void setCmp(Cmp cmp) throws java.beans.PropertyVetoException {
352: Cmp oldCmp = this .cmp;
353: getVCS().fireVetoableChange("cmp", oldCmp, cmp);
354: this .cmp = cmp;
355: getPCS().firePropertyChange("cmp", oldCmp, cmp);
356: }
357:
358: /** Getter for property flushAtEndOfMethod.
359: * @return Value of property flushAtEndOfMethod.
360: */
361: public FlushAtEndOfMethod getFlushAtEndOfMethod() {
362: return this .flushAtEndOfMethod;
363: }
364:
365: /** Setter for property flushAtEndOfMethod.
366: * @param flushAtEndOfMethod New value of property flushAtEndOfMethod.
367: *
368: * @throws PropertyVetoException
369: */
370: public void setFlushAtEndOfMethod(
371: FlushAtEndOfMethod flushAtEndOfMethod)
372: throws java.beans.PropertyVetoException {
373: FlushAtEndOfMethod oldFlushAtEndOfMethod = this .flushAtEndOfMethod;
374: getVCS().fireVetoableChange("flushAtEndOfMethod",
375: oldFlushAtEndOfMethod, flushAtEndOfMethod); //NOI18N
376: this .flushAtEndOfMethod = flushAtEndOfMethod;
377: getPCS().firePropertyChange("flushAtEndOfMethod",
378: oldFlushAtEndOfMethod, flushAtEndOfMethod); //NOI18N
379: }
380:
381: //methods called by the customizer model
382: public void addFinder(Finder finder) {
383: if (null == cmp) {
384: cmp = getConfig().getStorageFactory().createCmp();
385: }
386: OneOneFinders oneOneFinders = cmp.getOneOneFinders();
387: if (null == oneOneFinders) {
388: oneOneFinders = cmp.newOneOneFinders();
389: cmp.setOneOneFinders(oneOneFinders);
390: }
391: oneOneFinders.addFinder(finder);
392: }
393:
394: public void addMethod(Method method) {
395: /*
396: System.out.println("CmpEntityEjb addMethod ddMethod:" + method); //NOI18N
397: System.out.println("CmpEntityEjb addMethod name :" + method.getMethodName() ); //NOI18N
398: System.out.println("CmpEntityEjb addMethod interface :" + method.getMethodIntf() ); //NOI18N
399: System.out.println("CmpEntityEjb addMethod ejb name :" + method.getEjbName() ); //NOI18N
400: System.out.println("CmpEntityEjb addMethod params :" + method.getMethodParams() ); //NOI18N
401: */
402: if (null == flushAtEndOfMethod) {
403: flushAtEndOfMethod = getConfig().getStorageFactory()
404: .createFlushAtEndOfMethod();
405: }
406: flushAtEndOfMethod.addMethod(method);
407: }
408:
409: public void addQueryMethod(QueryMethod queryMethod) {
410: try {
411: if (null == cmp) {
412: cmp = getConfig().getStorageFactory().createCmp();
413: }
414:
415: PrefetchDisabled prefetchDisabled = cmp
416: .getPrefetchDisabled();
417: if (null == prefetchDisabled) {
418: prefetchDisabled = cmp.newPrefetchDisabled();
419: cmp.setPrefetchDisabled(prefetchDisabled);
420: }
421: prefetchDisabled.addQueryMethod(queryMethod);
422: } catch (org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException ex) {
423: //System.out.println("Not Supported Version"); //NOI18N
424: }
425: }
426:
427: public void removeFinder(Finder finder) {
428: if (null != cmp) {
429: OneOneFinders oneOneFinders = cmp.getOneOneFinders();
430: if (null != oneOneFinders) {
431: oneOneFinders.removeFinder(finder);
432: try {
433: if (oneOneFinders.sizeFinder() < 1) {
434: setCmp(null);
435: }
436: } catch (java.beans.PropertyVetoException ex) {
437: }
438: }
439: }
440: }
441:
442: public void removeMethod(Method method) {
443: if (null != flushAtEndOfMethod) {
444: flushAtEndOfMethod.removeMethod(method);
445: }
446: }
447:
448: public void removeQueryMethod(QueryMethod queryMethod) {
449: try {
450: if (null != cmp) {
451: PrefetchDisabled prefetchDisabled = cmp
452: .getPrefetchDisabled();
453: if (null != prefetchDisabled) {
454: prefetchDisabled.removeQueryMethod(queryMethod);
455: }
456: }
457: } catch (org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException ex) {
458: //System.out.println("Not Supported Version"); //NOI18N
459: }
460: }
461:
462: //List of all the finder methods of cmp bean
463: public List getFinderMethods() {
464: ArrayList methods = new ArrayList();
465: DDBean ddBean = getDDBean();
466:
467: //xpath - ejb-jar/enterprise-beans/entity
468: DDBean[] childBeans = ddBean.getChildBean("./query"); //NOI18N
469: ConfigQuery.MethodData methodData = null;
470: DDBean queryMethods[];
471: DDBean queryMethod;
472: DDBean methodNameBean;
473: String methodName;
474: DDBean methodParams;
475: DDBean methodParam[];
476: for (int i = 0; i < childBeans.length; i++) {
477: queryMethods = childBeans[i].getChildBean("./query-method"); //NOI18N
478: if (queryMethods.length > 0) {
479: queryMethod = queryMethods[0];
480: methodNameBean = queryMethod
481: .getChildBean("./method-name")[0]; //NOI18N
482: methodName = methodNameBean.getText();
483: if ((methodName != null) && (methodName.length() > 0)) {
484: methodParams = queryMethod
485: .getChildBean("./method-params")[0]; //NOI18N
486: methodParam = methodParams
487: .getChildBean("./method-param"); //NOI18N
488: ArrayList params = new ArrayList();
489: if (methodParam != null) {
490: for (int j = 0; j < methodParam.length; j++) {
491: params.add(methodParam[j].getText());
492: }
493: }
494: methodData = new ConfigQuery.MethodData(methodName,
495: params);
496: }
497: }
498: methods.add((Object) methodData);
499: }
500: return methods;
501: }
502:
503: //List of all the QueryMethod elements(elements from DD)
504: public List getPrefetchedMethods() {
505: List prefetchedMethodList = new ArrayList();
506:
507: try {
508: if (cmp != null) {
509: PrefetchDisabled prefetchDisabled = cmp
510: .getPrefetchDisabled();
511: if (prefetchDisabled != null) {
512: QueryMethod[] queryMethods = prefetchDisabled
513: .getQueryMethod();
514: if (queryMethods != null) {
515: for (int i = 0; i < queryMethods.length; i++) {
516: prefetchedMethodList.add(queryMethods[i]);
517: }
518: }
519: }
520: }
521: } catch (org.netbeans.modules.j2ee.sun.dd.api.VersionNotSupportedException ex) {
522: //System.out.println("Not Supported Version"); //NOI18N
523: }
524: return prefetchedMethodList;
525: }
526:
527: public String getHelpId() {
528: return "AS_CFG_CmpEntityEjb"; //NOI18N
529: }
530: }
|