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: package org.netbeans.modules.form.j2ee;
042:
043: import java.lang.reflect.InvocationTargetException;
044: import org.netbeans.modules.form.CreationDescriptor;
045: import org.netbeans.modules.form.FormProperty;
046: import org.netbeans.modules.form.codestructure.CodeExpression;
047: import org.netbeans.modules.form.codestructure.CodeExpressionOrigin;
048:
049: /**
050: * Creator for query obtained from entity manager.
051: *
052: * @author Jan Stola
053: */
054: class QueryCreator implements CreationDescriptor.Creator {
055: /** Parameter types. */
056: private Class[] paramTypes = new Class[] {
057: Object.class/*EntityManager.class*/, String.class,
058: int.class, int.class };
059: /** Exception types. */
060: private Class[] exTypes = new Class[0];
061: /** Property names. */
062: private String[] propNames = new String[] { "entityManager",
063: "query", "firstResult", "maxResults" }; // NOI18N
064:
065: /**
066: * Returns number of parameters of the creator.
067: *
068: * @return number of parameters of the creator.
069: */
070: public int getParameterCount() {
071: return 4;
072: }
073:
074: /**
075: * Returns parameter types of the creator.
076: *
077: * @return parameter types of the creator.
078: */
079: public Class[] getParameterTypes() {
080: return paramTypes;
081: }
082:
083: /**
084: * Returns exception types of the creator.
085: *
086: * @return exception types of the creator.
087: */
088: public Class[] getExceptionTypes() {
089: return exTypes;
090: }
091:
092: /**
093: * Returns property names of the creator.
094: *
095: * @return property names of the creator.
096: */
097: public String[] getPropertyNames() {
098: return propNames;
099: }
100:
101: /**
102: * Creates instance according to given properties.
103: *
104: * @param props properties describing the instance to create.
105: * @return instance that reflects values of the given properties.
106: */
107: public Object createInstance(FormProperty[] props)
108: throws InstantiationException, IllegalAccessException,
109: IllegalArgumentException, InvocationTargetException {
110: return new Object(); // Hack
111: }
112:
113: /**
114: * Creates instance according to given parameter values.
115: *
116: * @param paramValues parameter values describing the instance to create.
117: * @return instance that reflects values of the given parameters.
118: */
119: public Object createInstance(Object[] paramValues)
120: throws InstantiationException, IllegalAccessException,
121: IllegalArgumentException, InvocationTargetException {
122: return new Object(); // Hack
123: }
124:
125: /**
126: * Returns creation code according to given properties.
127: *
128: * @param props properties describing the instance whose creation code should be returned.
129: * @param expressionType type of the expression to create.
130: * @return creation code that reflects values of the given properties.
131: */
132: public String getJavaCreationCode(FormProperty[] props,
133: Class expressionType, String genericTypes) {
134: assert (props.length == 4);
135:
136: String entityManager = null;
137: String query = null;
138: String firstResult = null;
139: String maxResults = null;
140: for (int i = 0; i < props.length; i++) {
141: String propName = props[i].getName();
142: if (propNames[0].equals(propName)) {
143: entityManager = props[i].getJavaInitializationString();
144: } else if (propNames[1].equals(propName)) {
145: query = props[i].getJavaInitializationString();
146: } else if (propNames[2].equals(propName)) {
147: if (props[i].isChanged()) {
148: firstResult = props[i]
149: .getJavaInitializationString();
150: }
151: } else if (propNames[3].equals(propName)) {
152: if (props[i].isChanged()) {
153: maxResults = props[i].getJavaInitializationString();
154: }
155: } else {
156: assert false;
157: }
158: }
159:
160: StringBuilder sb = new StringBuilder();
161: sb.append("java.beans.Beans.isDesignTime() ? null : "); // NOI18N
162: if ("null".equals("" + entityManager)) { // NOI18N
163: sb.append("((javax.persistence.EntityManager)null)"); // NOI18N
164: } else {
165: sb.append(entityManager);
166: }
167: sb.append(".createQuery(").append(query).append(')'); // NOI18N
168: if (firstResult != null) {
169: sb.append(".setFirstResult(").append(firstResult).append(
170: ')'); // NOI18N
171: }
172: if (maxResults != null) {
173: sb.append(".setMaxResults(").append(maxResults).append(')'); // NOI18N
174: }
175: return sb.toString();
176: }
177:
178: public CodeExpressionOrigin getCodeOrigin(CodeExpression[] params) {
179: return null; // PENDING how is this used?
180: }
181:
182: }
|