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: package org.netbeans.modules.sql.framework.model.impl;
042:
043: import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
044: import org.netbeans.modules.sql.framework.model.SQLCondition;
045: import org.netbeans.modules.sql.framework.model.SQLConstants;
046: import org.netbeans.modules.sql.framework.model.SQLInputObject;
047: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
048: import org.netbeans.modules.sql.framework.model.SQLObject;
049: import org.netbeans.modules.sql.framework.model.SQLPredicate;
050: import org.netbeans.modules.sql.framework.model.SQLWhen;
051: import org.netbeans.modules.sql.framework.model.visitors.SQLVisitor;
052: import org.w3c.dom.Element;
053: import org.w3c.dom.NodeList;
054:
055: import com.sun.sql.framework.exception.BaseException;
056:
057: /**
058: * This class is part of When. An addendum to SQLCase and always used with SQLCase
059: *
060: * @author Sudhendra Seshachala
061: * @version $Revision$
062: */
063: public class SQLWhenImpl extends SQLConnectableObjectImpl implements
064: SQLWhen {
065:
066: /** Key constant: condition input */
067: public static final String CONDITION = "condition";
068:
069: /** Key constant: return output */
070: public static final String RETURN = "return";
071:
072: private SQLPredicate oldPredicate;
073:
074: private SQLCondition whenCondition;
075:
076: /** Creates a new default instance of SQLWhenImpl. */
077: public SQLWhenImpl() {
078: super ();
079:
080: type = SQLConstants.WHEN;
081: whenCondition = SQLModelObjectFactory.getInstance()
082: .createSQLCondition(WHEN_CONDITION);
083: whenCondition.setParent(this );
084: whenCondition.setConditionText("");
085:
086: SQLInputObject inputObject = new SQLInputObjectImpl(RETURN,
087: RETURN, null);
088: this .inputMap.put(RETURN, inputObject);
089: }
090:
091: /**
092: * Creates a new instance of SQLWhen with the given display name
093: *
094: * @param newDisplayName display name for the new instance
095: */
096: public SQLWhenImpl(String newDisplayName) {
097: this ();
098: setDisplayName(newDisplayName);
099: }
100:
101: /**
102: * @see org.netbeans.modules.sql.framework.model.SQLConnectableObject#addInput
103: */
104: public void addInput(String argName, SQLObject newInput)
105: throws BaseException {
106: if (CONDITION.equals(argName)) {
107: oldPredicate = (SQLPredicate) newInput;
108: } else {
109: super .addInput(argName, newInput);
110: }
111: }
112:
113: /**
114: * @see org.netbeans.modules.sql.framework.model.SQLWhen#getCondition()
115: */
116: public SQLCondition getCondition() {
117: return whenCondition;
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.netbeans.modules.sql.framework.model.SQLConnectableObject#getInput(java.lang.String)
124: */
125: public SQLInputObject getInput(String argName) {
126: if (CONDITION.equals(argName)) {
127: return new SQLInputObjectImpl(CONDITION, CONDITION,
128: oldPredicate);
129: } else {
130: return super .getInput(argName);
131: }
132: }
133:
134: /**
135: * Overrides default implementation to return JDBC type of the associated return
136: * input, if any.
137: *
138: * @return JDBC type of return input, or default value if no return input is currently
139: * linked.
140: * @see org.netbeans.modules.sql.framework.model.impl.AbstractSQLObject#getJdbcType
141: */
142: public int getJdbcType() {
143: SQLObject value = this .getSQLObject(RETURN);
144:
145: // Return either the associated return object's type, or the default
146: // type as defined in AbstractSQLObject.
147: return (value != null) ? value.getJdbcType() : super
148: .getJdbcType();
149: }
150:
151: /**
152: * @see org.netbeans.modules.sql.framework.model.SQLConnectableObject#isInputValid
153: */
154: public boolean isInputValid(String argName, SQLObject input) {
155: if (input == null || argName == null) {
156: return false;
157: }
158:
159: switch (input.getObjectType()) {
160: case SQLConstants.GENERIC_OPERATOR:
161: case SQLConstants.CUSTOM_OPERATOR:
162: case SQLConstants.CAST_OPERATOR:
163: case SQLConstants.DATE_DIFF_OPERATOR:
164: case SQLConstants.DATE_ADD_OPERATOR:
165: case SQLConstants.LITERAL:
166: case SQLConstants.VISIBLE_LITERAL:
167: case SQLConstants.CASE:
168: case SQLConstants.SOURCE_COLUMN:
169: return RETURN.equals(argName.trim());
170:
171: default:
172: return false;
173: }
174: }
175:
176: /**
177: * Populates the member variables and collections of this SQLWhen instance, parsing
178: * the given DOM Element as the source for reconstituting its contents.
179: *
180: * @param xmlElement DOM element containing XML marshaled version of this SQLWhen
181: * instance
182: * @throws BaseException if element is null or error occurs during parsing
183: */
184: public void parseXML(Element xmlElement) throws BaseException {
185: super .parseXML(xmlElement);
186: this .objectType = xmlElement
187: .getAttribute(SQLObject.OBJECT_TYPE);
188:
189: NodeList conditionNodeList = xmlElement
190: .getElementsByTagName(SQLCondition.TAG_CONDITION);
191: if (conditionNodeList != null
192: && conditionNodeList.getLength() != 0) {
193: Element elem = (Element) conditionNodeList.item(0);
194: whenCondition = SQLModelObjectFactory.getInstance()
195: .createSQLCondition(WHEN_CONDITION);
196: whenCondition.setParent(this );
197: whenCondition.parseXML(elem);
198: }
199:
200: NodeList list = xmlElement.getChildNodes();
201: if (list != null && list.getLength() != 0) {
202: TagParserUtility.parseInputChildNodes(this , list);
203: }
204: }
205:
206: /**
207: * Resolves object reference contained in given DOM element; called in second pass of
208: * SQLDefinition parsing process.
209: *
210: * @param element to be parsed
211: * @exception BaseException thrown while parsing
212: */
213: public void secondPassParse(Element element) throws BaseException {
214: TagParserUtility.parseInputTag(this , element);
215: }
216:
217: /*
218: * (non-Javadoc)
219: *
220: * @see org.netbeans.modules.sql.framework.model.SQLWhen#setCondition(org.netbeans.modules.sql.framework.model.SQLCondition)
221: */
222: public void setCondition(SQLCondition cond) {
223: whenCondition = cond;
224: }
225:
226: /**
227: * Overrides parent implementation to append when condition information.
228: *
229: * @param prefix String to append to each new line of the XML representation
230: * @return XML representation of this SQLObject instance
231: * @throws BaseException if error occurs during XML creation
232: */
233: public String toXMLString(String prefix) throws BaseException {
234: StringBuilder buffer = new StringBuilder();
235: if (prefix == null) {
236: prefix = "";
237: }
238:
239: buffer.append(prefix).append(getHeader());
240: buffer.append(toXMLAttributeTags(prefix));
241:
242: if (whenCondition != null) {
243: buffer.append(whenCondition.toXMLString(prefix + "\t"));
244: }
245:
246: buffer.append(TagParserUtility.toXMLInputTag(prefix + "\t",
247: inputMap));
248: buffer.append(prefix).append(getFooter());
249:
250: return buffer.toString();
251: }
252:
253: public void visit(SQLVisitor visitor) {
254: visitor.visit(this);
255: }
256: }
|