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-2007 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: /*
043: * DesignInfoGenerator.java
044: *
045: * Created on March 8, 2005, 12:16 AM
046: */
047:
048: package org.netbeans.modules.visualweb.ejb.load;
049:
050: import org.netbeans.modules.visualweb.ejb.datamodel.MethodInfo;
051: import org.netbeans.modules.visualweb.ejb.util.Util;
052: import java.io.File;
053: import java.io.FileOutputStream;
054: import java.io.PrintWriter;
055: import java.util.Date;
056:
057: /**
058: *
059: * @author cao
060: */
061: public class DataProviderDesignInfoGenerator {
062:
063: private String clientWrapperName;
064: private String fullBeanClassName;
065: private MethodInfo method;
066:
067: public DataProviderDesignInfoGenerator(String clientWrapperName,
068: String fullBeanClassName, MethodInfo method) {
069: this .clientWrapperName = clientWrapperName;
070: this .fullBeanClassName = fullBeanClassName;
071: this .method = method;
072: }
073:
074: public ClassDescriptor[] generateClass(String srcDir)
075: throws EjbLoadException {
076: // Two class will be generated- DesignInfo and DataClassInstanceCustomizer
077: ClassDescriptor[] classDescriptors = new ClassDescriptor[2];
078:
079: // Declare it outside the try-catch so that the file name can be logged in case of exception
080: File javaFile = null;
081:
082: try {
083: // Figure out the package name, class name and directory/file name
084:
085: String beanClassName = Util.getClassName(fullBeanClassName);
086: String packageName = Util.getPackageName(fullBeanClassName);
087: String designInfoClassName = beanClassName + "DesignInfo";
088:
089: String classDir = packageName.replace('.',
090: File.separatorChar);
091: File dirF = new File(srcDir + File.separator + classDir);
092: if (!dirF.exists()) {
093: if (!dirF.mkdirs())
094: System.out.println(".....failed to make dir"
095: + srcDir + File.separator + classDir);
096: }
097:
098: String designInfoClassFile = designInfoClassName + ".java";
099: javaFile = new File(dirF, designInfoClassFile);
100: javaFile.createNewFile();
101:
102: classDescriptors[0] = new ClassDescriptor(
103: designInfoClassName, packageName, javaFile
104: .getAbsolutePath(), classDir
105: + File.separator + designInfoClassFile);
106:
107: // For the inner class
108: classDescriptors[1] = new ClassDescriptor(
109: designInfoClassName
110: + "$DataClassInstanceCustomizer",
111: packageName, new File(dirF, designInfoClassName
112: + "$DataClassInstanceCustomizer.class")
113: .getAbsolutePath(), classDir
114: + File.separator + designInfoClassName
115: + "$DataClassInstanceCustomizer.class",
116: true);
117:
118: // Generate java code
119:
120: PrintWriter out = new PrintWriter(new FileOutputStream(
121: javaFile));
122:
123: // pacage
124: if (packageName != null && packageName.length() != 0) {
125: out.println("package " + packageName + ";");
126: out.println();
127: }
128:
129: // comments
130: out.println("/**");
131: out.println(" * Source code created on " + new Date());
132: out.println(" */");
133: out.println();
134:
135: // Import
136: out.println("import com.sun.rave.designtime.*;");
137: out.println("import com.sun.rave.designtime.impl.*;");
138: out.println("import com.sun.rave.designtime.faces.*;");
139: out.println("import java.util.ArrayList;");
140: out.println();
141:
142: // Start class
143: out.println("public class " + designInfoClassName
144: + " extends BasicDesignInfo {");
145: out.println();
146:
147: // Constructor
148: out.println(" public " + designInfoClassName + "() {");
149: out.println(" super( " + clientWrapperName
150: + ".class );");
151: out.println(" } ");
152: out.println();
153:
154: // Method - beanCreatedSetup()
155:
156: String fullPackageWrapperClassName = packageName + "."
157: + clientWrapperName;
158:
159: String clientWrapperBeanPropName = clientWrapperName;
160: if (!Util.isAcronyn(clientWrapperName))
161: clientWrapperBeanPropName = Util
162: .decapitalize(clientWrapperName);
163:
164: out
165: .println(" public Result beanCreatedSetup( DesignBean designBean ) {");
166: out
167: .println(" String currentScope = (String)designBean.getDesignContext().getContextData( Constants.ContextData.SCOPE );");
168: //out.println( " System.out.println( \"++++++++++ currentScope: \" + currentScope );" );
169: out
170: .println(" ArrayList clientBeans = new ArrayList();");
171: //out.println( " DesignContext[] contexts = designBean.getDesignContext().getProject().getDesignContexts();" );
172: // For performance improvement. No need to get all the contexts in the project
173: out
174: .println(" DesignProject designProject = designBean.getDesignContext().getProject();");
175: out.println(" DesignContext[] ctxs;");
176: out
177: .println(" if (designProject instanceof FacesDesignProject) {");
178: out
179: .println(" ctxs = ((FacesDesignProject)designProject).findDesignContexts(new String[] {");
180: out.println(" \"request\",");
181: out.println(" \"session\",");
182: out.println(" \"application\"");
183: out.println(" });");
184: out.println(" } else {");
185: out.println(" ctxs = new DesignContext[0];");
186: out.println(" }");
187:
188: // fix duplicate beans in customizer when dropping DataProvider onto a Session/Request/Application bean
189: out.println("");
190: out.println(" boolean duplicate = false;");
191: out
192: .println(" DesignContext currentDesignContext = designBean.getDesignContext();");
193: out
194: .println(" for ( int i = 0; i < ctxs.length; i++) {");
195: out
196: .println(" if (ctxs[i] == currentDesignContext) {");
197: out.println(" duplicate = true;");
198: out.println(" break;");
199: out.println(" }");
200: out.println(" }");
201: out.println(" DesignContext[] contexts;");
202: out.println(" if (duplicate) {");
203: out.println(" contexts = ctxs;");
204: out.println(" }else {");
205: out
206: .println(" contexts = new DesignContext[ctxs.length + 1];");
207: out
208: .println(" contexts[0] = designBean.getDesignContext();");
209: out
210: .println(" System.arraycopy(ctxs, 0, contexts, 1, ctxs.length);");
211: out.println(" }");
212: out.println("");
213:
214: out
215: .println(" for( int i = 0; i < contexts.length; i ++ ) {");
216: out
217: .println(" DesignBean[] beans = contexts[i].getBeansOfType( "
218: + fullPackageWrapperClassName + ".class );");
219: out
220: .println(" for( int bi = 0; bi < beans.length; bi ++ ) { ");
221: out.println();
222: out
223: .println(" // Filter out the ones in the same scope unless it is in the same backing bean");
224: out
225: .println(" String sourceBeanScope = (String)beans[bi].getDesignContext().getContextData( Constants.ContextData.SCOPE );");
226: //out.println( " System.out.println( \"++++++++++ sourceBeanScope: \" + sourceBeanScope );" );
227: out
228: .println(" if( currentScope.equals( sourceBeanScope ) && !designBean.getBeanParent().getInstanceName().equals( beans[bi].getBeanParent().getInstanceName() ) )");
229: //out.println( " {" );
230: //out.println( " System.out.println( \"++++++++++ skipping bean from sourceBeanScope: \" + sourceBeanScope + \"-\" + beans[bi].getBeanParent().getInstanceName() );" );
231: out.println(" continue;");
232: //out.println( " }" );
233: out.println();
234: out
235: .println(" // Filter out the ones in smaller scopes.");
236: out
237: .println(" // For example, if the current scope is in application, then only the ones in the application scope can be referred ");
238: out
239: .println(" if( currentScope.equals( \"application\" ) && !sourceBeanScope.equals( \"application\" ) )");
240: out.println(" continue;");
241: out.println();
242: out
243: .println(" // If the current scope is in session, then only the ones in application and session can be referred.");
244: out
245: .println(" // In another words, the ones from request are not good");
246: out
247: .println(" if( currentScope.equals( \"session\" ) && sourceBeanScope.equals( \"request\" ) )");
248: out.println(" continue;");
249: out.println();
250: //out.println( " System.out.println( \"++++++++++ adding source bean from scope: \" + sourceBeanScope + \"-\" + beans[bi].getBeanParent().getInstanceName() );" );
251: out
252: .println(" clientBeans.add( beans[bi] );");
253: out.println(" }");
254: out.println(" }");
255: //out.println( " System.out.println( \"++++++++++ num of DesignBean of type " + clientWrapperName + ": \" + clientBeans.size() );" );
256: out.println(" if( clientBeans.size() == 0 ) { ");
257: out
258: .println(" if( designBean.getDesignContext().canCreateBean( \""
259: + fullPackageWrapperClassName
260: + "\", designBean.getBeanParent(), null ) ) {");
261: out
262: .println(" DesignBean clientBean = designBean.getDesignContext().createBean( \""
263: + fullPackageWrapperClassName
264: + "\", designBean.getBeanParent(), null );");
265: out.println(" designBean.getProperty( \""
266: + clientWrapperBeanPropName
267: + "\").setValue( clientBean.getInstance() );");
268: out.println(" return Result.SUCCESS;");
269: out.println(" }");
270: out.println(" else {");
271: out.println();
272: out
273: .println(" ResultMessage message = new ResultMessage( ResultMessage.TYPE_CRITICAL, \"No "
274: + fullPackageWrapperClassName
275: + " instance\", \"No "
276: + fullPackageWrapperClassName
277: + " instance found and failed to create one\" );");
278: out
279: .println(" return new Result( true, message );");
280: out.println(" }");
281: out
282: .println(" } else if( clientBeans.size() == 1 ) {");
283: out
284: .println(" DesignBean selectedBean = (DesignBean)clientBeans.get(0);");
285: out
286: .println(" designBean.getProperty( \""
287: + clientWrapperBeanPropName
288: + "\").setValue( ((DesignBean)clientBeans.get(0)).getInstance() );");
289: out.println(" return Result.SUCCESS;");
290: out.println(" } else {// More than one found");
291: out
292: .println(" DesignProperty prop = designBean.getProperty( \""
293: + clientWrapperBeanPropName + "\" );");
294: out
295: .println(" DesignBean defaultBean = (DesignBean)clientBeans.get(0);");
296: out
297: .println(" prop.setValue( defaultBean.getInstance() );");
298: out.println();
299: out
300: .println(" Customizer2 dpCustomizer = new DataClassInstanceCustomizer( \"DataProvider customizer\", designBean, prop, (DesignBean[])clientBeans.toArray(new DesignBean[0]) ); ");
301: out
302: .println(" return new CustomizerResult( designBean, dpCustomizer );");
303: out.println(" }");
304: out.println(" }");
305: out.println();
306: out
307: .println(" private String capitalize(String name) {");
308: out
309: .println(" if( name == null || name.length() == 0 )");
310: out.println(" return name;");
311: out.println();
312: out.println(" char chars[] = name.toCharArray();");
313: out
314: .println(" chars[0] = Character.toUpperCase(chars[0]);");
315: out.println(" return new String(chars);");
316: out.println(" }");
317: out.println();
318:
319: // TODO for now, have this class in each DesignInfo class. Need to move to a jar file
320: createDataClassInstanceCustomizer(out);
321:
322: // End of class
323: out.println("}");
324:
325: out.flush();
326: out.close();
327:
328: return classDescriptors;
329: } catch (java.io.FileNotFoundException ex) {
330: // Log error
331: /*String errMsg = "Error occurred when trying to generate the wrapper bean class for EJB " + ejbName
332: + ". Could not find file " + javaFile.getAbsolutePath();
333: ErrorManager.getDefault().getInstance( "org.netbeans.modules.visualweb.ejb.load.ClientBeanInfoGenerator" ).log( errMsg );*/
334: ex.printStackTrace();
335:
336: // Throw up as a SYSTEM_ERROR
337: throw new EjbLoadException(ex.getMessage());
338: } catch (java.io.IOException ex) {
339: // Log error
340: /*String errMsg = "Error occurred when trying to generate the wrapper bean class for EJB " + ejbName
341: + ". Could not create file " + javaFile.getAbsolutePath();
342: ErrorManager.getDefault().getInstance( "org.netbeans.modules.visualweb.ejb.load.ClientBeanInfoGenerator" ).log( errMsg );*/
343: ex.printStackTrace();
344:
345: // Throw up as a SYSTEM_ERROR
346: throw new EjbLoadException(ex.getMessage());
347: }
348: }
349:
350: private void createDataClassInstanceCustomizer(PrintWriter out) {
351: out.println(" /**");
352: out
353: .println(" * This customizer for setting the data class instance on a data provider");
354: out.println(" */");
355: out
356: .println(" public class DataClassInstanceCustomizer extends BasicCustomizer2 {");
357: out.println(" // The bean where the property from");
358: out.println(" protected DesignBean targetBean;");
359: out.println();
360: out.println(" // The design property to be set");
361: out.println(" protected DesignProperty targetProperty;");
362: out.println();
363: out
364: .println(" // The bean instance from one the following design beans will be set to the target property");
365: out.println(" protected DesignBean[] sourceBeans;");
366: out.println();
367: out
368: .println(" // The List to display the bean instance to be selected from");
369: out
370: .println(" private javax.swing.JList beanInstancesList;");
371: out.println();
372: out.println(" /**");
373: out.println(" * Constructor");
374: out.println(" *");
375: out
376: .println(" * @param name The title of the customizer");
377: out
378: .println(" * @param targetProperty The property this customizer is designed to set");
379: out.println(" * @param sourceBeans");
380: out.println(" */");
381: out
382: .println(" public DataClassInstanceCustomizer(String name, DesignBean targetBean, DesignProperty targetProperty, DesignBean[] sourceBeans) { ");
383: out.println(" super( null, name ); ");
384: out.println(" setApplyCapable( false );");
385: out
386: .println(" this.targetProperty = targetProperty;");
387: out.println(" this.targetBean = targetBean;");
388: out.println(" this.sourceBeans = sourceBeans;");
389: out.println(" }");
390: out.println();
391: out
392: .println(" protected java.awt.Component createCustomizerPanel() {");
393: out.println();
394: out
395: .println(" javax.swing.JPanel panel = new javax.swing.JPanel();");
396: out.println();
397: out
398: .println(" java.awt.GridBagConstraints gridBagConstraints;");
399: out.println();
400: out
401: .println(" javax.swing.JScrollPane listScrollPane = new javax.swing.JScrollPane();");
402: out
403: .println(" beanInstancesList = new javax.swing.JList();");
404: out
405: .println(" javax.swing.JLabel label = new javax.swing.JLabel();");
406: out.println();
407: out
408: .println(" panel.setLayout(new java.awt.GridBagLayout());");
409: out.println();
410: out
411: .println(" beanInstancesList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);");
412: out
413: .println(" beanInstancesList.setVisibleRowCount(4);");
414: out
415: .println(" listScrollPane.setViewportView(beanInstancesList);");
416: out.println();
417: out
418: .println(" gridBagConstraints = new java.awt.GridBagConstraints();");
419: out.println(" gridBagConstraints.gridx = 0;");
420: out.println(" gridBagConstraints.gridy = 1;");
421: out
422: .println(" gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;");
423: out
424: .println(" gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 12);");
425: out
426: .println(" gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;");
427: out.println(" gridBagConstraints.weightx = 1.0;");
428: out.println(" gridBagConstraints.weighty = 1.0;");
429: out
430: .println(" panel.add(listScrollPane, gridBagConstraints);");
431: out.println();
432: out
433: .println(" label.setText(\"Select a "
434: + clientWrapperName
435: + " instance to set on data provider instance \" + targetBean.getInstanceName() );");
436: out
437: .println(" gridBagConstraints = new java.awt.GridBagConstraints();");
438: out.println(" gridBagConstraints.gridx = 0;");
439: out.println(" gridBagConstraints.gridy = 0;");
440: out
441: .println(" gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;");
442: out
443: .println(" gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 12);");
444: out
445: .println(" gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;");
446: out
447: .println(" panel.add(label, gridBagConstraints);");
448: out.println();
449: out
450: .println(" String[] beanInstanceNames = new String[ sourceBeans.length ];");
451: out
452: .println(" for( int i = 0; i < sourceBeans.length; i ++ ) {");
453: out
454: .println(" String parentInstance = sourceBeans[i].getBeanParent().getInstanceName();");
455: out
456: .println(" beanInstanceNames[i] = sourceBeans[i].getInstanceName() + \"(\" + parentInstance + \")\";");
457: out.println(" }");
458: out
459: .println(" beanInstancesList.setListData( beanInstanceNames );");
460: out.println();
461: out.println("// Have the first one selected by default");
462: out
463: .println(" beanInstancesList.setSelectedIndex(0);");
464: out.println();
465: out.println(" return panel;");
466: out.println(" }");
467: out.println();
468: out.println(" public Result applyChanges() {");
469: out.println();
470: out
471: .println(" int index = beanInstancesList.getSelectedIndex();");
472: out
473: .println(" DesignBean selectedBean = sourceBeans[index];");
474: out
475: .println(" targetProperty.setValue( sourceBeans[index].getInstance() );");
476: out.println();
477: out.println(" return Result.SUCCESS;");
478: out.println(" }");
479: out.println(" }");
480: }
481:
482: }
|