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: package org.netbeans.modules.sql.framework.codegen;
043:
044: import java.util.Iterator;
045: import java.util.LinkedHashMap;
046: import java.util.List;
047: import org.netbeans.modules.sql.framework.model.SQLConstants;
048: import org.netbeans.modules.sql.framework.model.SQLObject;
049: import org.netbeans.modules.sql.framework.model.SQLOperator;
050: import org.netbeans.modules.sql.framework.model.SQLOperatorArg;
051: import org.netbeans.modules.sql.framework.model.SQLOperatorDefinition;
052: import com.sun.sql.framework.exception.BaseException;
053: import com.sun.sql.framework.utils.StringUtil;
054: import java.util.Map;
055:
056: /**
057: * @author Ritesh Adval
058: * @author Ahimanikya Satapathy
059: * @version $Revision$
060: */
061: public class OperatorGenerator extends AbstractGenerator {
062:
063: public String generate(SQLObject obj, StatementContext context)
064: throws BaseException {
065: String result;
066: SQLOperator operator = (SQLOperator) obj;
067: //String defaultCustomFuncId = "userFx";
068: // get all necessary data.
069: LinkedHashMap params = (LinkedHashMap) operator
070: .getSQLObjectMap();
071:
072: //String operatorType = (operator.isCustomOperator()) ? defaultCustomFuncId : operator.getOperatorType();
073: SQLOperatorDefinition defn = this .getDB().getOperatorFactory()
074: .getSQLOperatorDefinition(operator.getOperatorType());
075:
076: if (defn.getArgCountType() == SQLConstants.OPERATOR_ARGS_VARIABLE) {
077: if (operator.isCustomOperator()) {
078: context.putClientProperty(
079: StatementContext.USER_FUNCTION_NAME, operator
080: .getCustomOperatorName());
081: }
082: result = genVariableExpression(params, defn, context);
083: } else {
084: result = genFixedExpression(params, defn, context);
085: }
086:
087: boolean appendParenthesis = operator.isShowParenthesis();
088:
089: result = (appendParenthesis ? "(" : "") + result
090: + (appendParenthesis ? ")" : "");
091:
092: return result;
093: }
094:
095: /**
096: * genFixedExpression
097: *
098: * @param params - SequencedHashMap
099: * @param defn - SQLOperatorDefinition
100: * @return String
101: * @throws BaseException - exception
102: */
103: protected String genFixedExpression(LinkedHashMap params,
104: SQLOperatorDefinition defn, StatementContext context)
105: throws BaseException {
106: String result;
107: List args = defn.getArgList();
108: if (params.size() != args.size()) {
109: throw new BaseException("Operator "
110: + defn.getOperatorName() + " requires "
111: + args.size() + " number of input arguments.");
112: }
113:
114: Map<String, String> resolvedparams = new LinkedHashMap<String, String>();
115: for (int i = 0; i < args.size(); i++) {
116: SQLOperatorArg arg = (SQLOperatorArg) args.get(i);
117: String key = arg.getArgName();
118:
119: Object obj = params.get(key);
120: if (obj instanceof SQLObject) {
121: SQLObject val = (SQLObject) obj;
122: if (val == null) {
123: throw new BaseException(
124: "No input argument for variable " + key);
125: }
126:
127: resolvedparams.put(key, this .getGeneratorFactory()
128: .generate(val, context));
129: }
130: }
131:
132: result = StringUtil.replace(defn.getScript(), resolvedparams,
133: SQLConstants.OPERATOR_VARIABLE_PREFIX);
134: return result;
135: }
136:
137: /**
138: * genVariableExpression
139: *
140: * @param params - SequencedHashMap
141: * @param defn - SQLOperatorDefinition
142: * @return String
143: * @throws BaseException - exception
144: */
145: protected String genVariableExpression(LinkedHashMap params,
146: SQLOperatorDefinition defn, StatementContext context)
147: throws BaseException {
148: String result = "";
149: Map<String, String> resolvedparams = new LinkedHashMap<String, String>();
150:
151: for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
152: String key = (String) iter.next();
153: SQLObject val = (SQLObject) params.get(key);
154:
155: if (val == null) {
156: throw new BaseException(
157: "No input arguments for variable " + key);
158: }
159:
160: resolvedparams.put(key, this .getGeneratorFactory()
161: .generate(val, context));
162: }
163:
164: String script = defn.getScript();
165: if (script.indexOf("[") == -1 || script.indexOf("]") == -1) {
166: throw new BaseException("Bad variable operator script.");
167: }
168:
169: String operator = script.substring(script.indexOf("]") + 1);
170:
171: Iterator i = (resolvedparams.values()).iterator();
172: int count = 0;
173: while (i.hasNext()) {
174: result += (count > 0) ? " " + operator + " " : "";
175: result += (String) i.next();
176: count++;
177: }
178: return result + "";
179: }
180: }
|