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 java.sql.Types;
044: import java.util.ArrayList;
045: import java.util.Iterator;
046: import java.util.List;
047:
048: import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
049: import org.netbeans.modules.sql.framework.model.GUIInfo;
050: import org.netbeans.modules.sql.framework.model.SQLCaseOperator;
051: import org.netbeans.modules.sql.framework.model.SQLConstants;
052: import org.netbeans.modules.sql.framework.model.SQLInputObject;
053: import org.netbeans.modules.sql.framework.model.SQLObject;
054: import org.netbeans.modules.sql.framework.model.SQLWhen;
055: import org.netbeans.modules.sql.framework.model.utils.GeneratorUtil;
056: import org.netbeans.modules.sql.framework.model.visitors.SQLVisitor;
057: import org.w3c.dom.Element;
058: import org.w3c.dom.Node;
059: import org.w3c.dom.NodeList;
060:
061: import com.sun.sql.framework.exception.BaseException;
062: import net.java.hulp.i18n.Logger;
063: import org.netbeans.modules.etl.logger.Localizer;
064: import org.netbeans.modules.etl.logger.LogUtil;
065:
066: /**
067: * Case operator used when we want to do a join and lookup
068: *
069: * @author Sudhi Seshachala
070: * @author Jonathan Giron
071: * @version $Revision$
072: */
073: public class SQLCaseOperatorImpl extends SQLConnectableObjectImpl
074: implements SQLCaseOperator {
075:
076: private static final String DEFAULT = "default"; // NOI18N
077: private static transient final Logger mLogger = LogUtil
078: .getLogger(SQLCaseOperatorImpl.class.getName());
079: private static transient final Localizer mLoc = Localizer.get();
080:
081: /**
082: * List of when for this case expression
083: */
084: protected List whenList = new ArrayList();
085:
086: private GUIInfo guiInfo = new GUIInfo();
087:
088: /**
089: * Constructor
090: */
091: public SQLCaseOperatorImpl() {
092: super ();
093: this .type = SQLConstants.CASE;
094:
095: String nbBundle1 = mLoc.t("PRSR001: default");
096: String dispName = Localizer.parse(nbBundle1);
097: SQLInputObject input = new SQLInputObjectImpl(DEFAULT,
098: dispName, null);
099: this .inputMap.put(DEFAULT, input);
100: }
101:
102: /**
103: * @see org.netbeans.modules.sql.framework.model.SQLConnectableObject#addInput
104: */
105: public void addInput(String argName, SQLObject newInput)
106: throws BaseException {
107: super .addInput(argName, newInput);
108: }
109:
110: /**
111: * Adds an SQLWhen object to the when list.
112: *
113: * @param when is the SQLWhen to be added.
114: * @return boolean true when added.
115: * @throws BaseException when the input is null.
116: */
117: public boolean addSQLWhen(SQLWhen when) throws BaseException {
118: if (when == null) {
119: throw new BaseException("When cannot be null for this Case");
120: }
121: when.setParentObject(this );
122: this .whenList.add(when);
123:
124: return true;
125: }
126:
127: /**
128: * Compares two objects for object sorting purposes.
129: *
130: * @param refObj is the object to compare to this object.
131: * @return int result of the comparison.
132: */
133: public int compareTo(Object refObj) {
134: SQLCaseOperator refCase = (SQLCaseOperator) refObj;
135:
136: if (refCase != null) {
137: return this .id.compareTo(refCase.getId());
138: }
139: return -1;
140: }
141:
142: /**
143: * Creates a new SQLWhen object and returns it.
144: *
145: * @return SQLWhen as the newly created object.
146: * @throws BaseException if any input params are passed in as null.
147: */
148: public SQLWhen createSQLWhen() throws BaseException {
149: SQLWhen when = new SQLWhenImpl(generateNewWhenName());
150: // no need to generate id for when as it is not referenced anywhere
151: return when;
152: }
153:
154: /**
155: * Compares two objects for equivalence.
156: *
157: * @param refObj is the object to be compared against this.
158: * @return boolean true if equal, false if not equal.
159: */
160: public boolean equals(Object refObj) {
161: if (refObj == null) {
162: return false;
163: }
164:
165: if (!(refObj instanceof SQLCaseOperator)) {
166: return false;
167: }
168:
169: SQLCaseOperator caseStatement = (SQLCaseOperator) refObj;
170:
171: if ((caseStatement.getId() == null && this .id != null)
172: || (caseStatement.getId() != null && this .id == null)) {
173: return false;
174: }
175:
176: if (caseStatement.getId() != null && this .id != null
177: && !this .id.equals(caseStatement.getId())) {
178: return false;
179: }
180:
181: return true;
182: }
183:
184: /**
185: * @see org.netbeans.modules.sql.framework.model.SQLCaseOperator#generateNewWhenName()
186: */
187: public String generateNewWhenName() {
188: String whenName = "when";
189: String newName = whenName;
190: int count = 0;
191: while (isWhenExist(newName)) {
192: newName = whenName + "_" + count++;
193: }
194:
195: return newName;
196: }
197:
198: /**
199: * Gets list of child sql objects.
200: *
201: * @return List of child SQLObjects
202: */
203: public List getChildSQLObjects() {
204: return getWhenList();
205: }
206:
207: /**
208: * Gets GUI-related attributes for this instance in the form of a GuiInfo instance.
209: *
210: * @return associated GuiInfo instance
211: * @see GUIInfo
212: */
213: public GUIInfo getGUIInfo() {
214: return this .guiInfo;
215: }
216:
217: /**
218: * @see org.netbeans.modules.sql.framework.model.SQLObject#getJdbcType()
219: */
220: public int getJdbcType() {
221: // Use VARCHAR as the "widest" possible default value for JDBC type
222: return Types.VARCHAR;
223: }
224:
225: /**
226: * Gets a specific SQLWhen by name.
227: *
228: * @param whenName of the SQLWhen object to return.
229: * @return SQLWhen instance with the given name.
230: */
231: public SQLWhen getWhen(String whenName) {
232: if (this .whenList == null) {
233: return (null);
234: }
235:
236: for (int i = 0; i < whenList.size(); i++) {
237: SQLWhen when = (SQLWhen) whenList.get(i);
238: if (when.getDisplayName().equals(whenName)) {
239: return when;
240: }
241: }
242:
243: return null;
244: }
245:
246: /**
247: * getWhenCount returns the size of the when list.
248: *
249: * @return int the size of the list.
250: */
251: public int getWhenCount() {
252: return this .whenList.size();
253: }
254:
255: /**
256: * @see org.netbeans.modules.sql.framework.model.SQLCaseOperator#getWhenList()
257: */
258: public List getWhenList() {
259: return this .whenList;
260: }
261:
262: /**
263: * Gets the hashCode for this object.
264: *
265: * @return int hash code value.
266: */
267: public int hashCode() {
268: return super .hashCode();
269: }
270:
271: /**
272: * @see org.netbeans.modules.sql.framework.model.SQLConnectableObject#isInputValid
273: */
274: public boolean isInputValid(String argName, SQLObject input) {
275: if (input == null) {
276: return false;
277: }
278:
279: switch (input.getObjectType()) {
280: case SQLConstants.VISIBLE_LITERAL:
281: case SQLConstants.LITERAL:
282: case SQLConstants.SOURCE_COLUMN:
283: case SQLConstants.GENERIC_OPERATOR:
284: case SQLConstants.CUSTOM_OPERATOR:
285: case SQLConstants.CAST_OPERATOR:
286: case SQLConstants.DATE_DIFF_OPERATOR:
287: case SQLConstants.DATE_ADD_OPERATOR:
288: return DEFAULT.equals(argName);
289:
290: default:
291: return false;
292: }
293: }
294:
295: /**
296: * Parses the XML element data and populates a new SQLCase object.
297: *
298: * @param xmlElement which represents SQLCase
299: * @throws BaseException when the xmlElement is null.
300: */
301: public void parseXML(Element xmlElement) throws BaseException {
302: super .parseXML(xmlElement);
303:
304: NodeList list = xmlElement.getChildNodes();
305: TagParserUtility.parseInputChildNodes(this , list);
306: parseSQLWhens(list);
307:
308: // There may be multiple guiInfo descendants - we're only interested in the
309: // instance
310: // associated with this case operator.
311: NodeList guiInfoList = xmlElement
312: .getElementsByTagName(GUIInfo.TAG_GUIINFO);
313: if (guiInfoList != null && guiInfoList.getLength() != 0) {
314: for (int i = 0; i < guiInfoList.getLength(); i++) {
315: Element elem = (Element) guiInfoList.item(i);
316: if (elem.getParentNode() == xmlElement) {
317: guiInfo = new GUIInfo(elem);
318: break;
319: }
320: }
321: }
322: }
323:
324: /**
325: * Removes an SQLWhen instance from the when list.
326: *
327: * @param when SQLWhen instance to be removed.
328: * @return boolean true when removed.
329: * @throws BaseException when the input is null.
330: */
331: public boolean removeSQLWhen(SQLWhen when) throws BaseException {
332: if (when == null) {
333: throw new BaseException(
334: "Cannot remove null when for this Case");
335: }
336: this .whenList.remove(when);
337:
338: return true;
339: }
340:
341: /**
342: * Second parse. Called if not found in first pass.
343: *
344: * @param element to be parsed
345: * @exception BaseException thrown while secondparsing
346: */
347: public void secondPassParse(Element element) throws BaseException {
348: TagParserUtility.parseInputTag(this , element);
349: }
350:
351: /**
352: * @see java.lang.Object#toString()
353: */
354: public String toString() {
355: try {
356: return GeneratorUtil.getInstance().getEvaluatedString(this );
357: } catch (BaseException ignore) {
358: return "Unknown";
359: }
360: }
361:
362: /**
363: * Generates this SQLCase object as an XML String and returns it
364: *
365: * @param prefix is the prefix to use for the XML string.
366: * @return String in XML of the object.
367: * @throws BaseException if error occurs during generation
368: */
369: public String toXMLString(String prefix) throws BaseException {
370: StringBuilder buffer = new StringBuilder();
371:
372: buffer.append(prefix).append(getHeader());
373: buffer.append(toXMLAttributeTags(prefix));
374: buffer.append(TagParserUtility.toXMLInputTag(prefix + "\t",
375: this .inputMap));
376: buffer.append(TagParserUtility.toXMLInputTag(prefix + "\t",
377: this .whenList));
378: buffer.append(this .guiInfo.toXMLString(prefix + "\t"));
379: buffer.append(prefix + super .getFooter());
380:
381: return buffer.toString();
382: }
383:
384: public void visit(SQLVisitor visitor) {
385: visitor.visit(this );
386: }
387:
388: private boolean isWhenExist(String whenName) {
389: Iterator it = this .whenList.iterator();
390: while (it.hasNext()) {
391: SQLWhen when = (SQLWhen) it.next();
392: if (when.getDisplayName().equals(whenName)) {
393: return true;
394: }
395: }
396:
397: return false;
398: }
399:
400: private void parseSQLWhens(NodeList list) throws BaseException {
401: for (int i = 0; i < list.getLength(); i++) {
402: if (list.item(i).getNodeType() == Node.ELEMENT_NODE
403: && list.item(i).getNodeName().equals(
404: SQLObject.TAG_SQLOBJECT)) {
405: Element elem = (Element) list.item(i);
406: String objType = elem
407: .getAttribute(SQLObject.OBJECT_TYPE);
408: int obType = TagParserUtility.getIntType(objType);
409: if (obType == SQLConstants.WHEN) {
410: SQLWhen obj = new SQLWhenImpl();
411: obj.setParentObject(this);
412: obj.parseXML(elem);
413: this.addSQLWhen(obj);
414: }
415: }
416: }
417: }
418: }
|