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.model.DBColumn;
044: import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
045: import org.netbeans.modules.sql.framework.model.SQLConstants;
046: import org.netbeans.modules.sql.framework.model.SQLDBModel;
047: import org.netbeans.modules.sql.framework.model.SQLDefinition;
048: import org.netbeans.modules.sql.framework.model.SQLObject;
049: import org.netbeans.modules.sql.framework.model.TargetColumn;
050: import org.netbeans.modules.sql.framework.model.TargetTable;
051: import org.w3c.dom.Element;
052: import org.w3c.dom.Node;
053: import org.w3c.dom.NodeList;
054:
055: import com.sun.sql.framework.exception.BaseException;
056:
057: /**
058: * Concrete implementation of DBColumn describing column metadata for target columns.
059: *
060: * @author Sudhendra Seshachala, Jonathan Giron
061: * @version $Revision$
062: */
063: public class TargetColumnImpl extends AbstractDBColumn implements
064: TargetColumn {
065:
066: /** SQLObject supplying input value for this target column */
067: private SQLObject inputValue;
068:
069: /**
070: * Constructs a default instance of TargetColumnImpl.
071: */
072: public TargetColumnImpl() {
073: super ();
074: init();
075: }
076:
077: /**
078: * Creates a new instance of TargetColumn, cloning the contents of the given DBColumn
079: * implementation instance.
080: *
081: * @param src DBColumn instance to be cloned
082: */
083: public TargetColumnImpl(DBColumn src) {
084: this ();
085:
086: if (src == null) {
087: throw new IllegalArgumentException(
088: "Must supply non-null DBColumn instance for src.");
089: }
090:
091: copyFrom(src);
092: }
093:
094: /**
095: * Constructs a new instance of TargetColumn using the given parameters and assuming
096: * that the column is not part of a foreign key or primary key, and that it accepts
097: * null values.
098: *
099: * @param colName name of this column
100: * @param sqlJdbcType JDBC type of this column
101: * @param colScale scale of this column
102: * @param colPrecision precision of this column
103: * @param isNullable true if nullable, false otherwise
104: * @see java.sql.Types
105: */
106: public TargetColumnImpl(String colName, int sqlJdbcType,
107: int colScale, int colPrecision, boolean isNullable) {
108: super (colName, sqlJdbcType, colScale, colPrecision, isNullable);
109: init();
110: }
111:
112: /**
113: * @see org.netbeans.modules.sql.framework.model.impl.AbstractDBColumn
114: */
115: public TargetColumnImpl(String colName, int sqlJdbcType,
116: int colScale, int colPrecision, boolean isPrimaryKey,
117: boolean isForeignKey, boolean isIndexed, boolean isNullable) {
118: super (colName, sqlJdbcType, colScale, colPrecision,
119: isPrimaryKey, isForeignKey, isIndexed, isNullable);
120: init();
121: }
122:
123: /**
124: * Clone a copy of DBColumn.
125: *
126: * @return a copy of DBColumn.
127: */
128: public Object clone() {
129: try {
130: TargetColumnImpl column = (TargetColumnImpl) super .clone();
131: column.parentObject = parentObject;
132: column.inputValue = inputValue;
133:
134: return column;
135: } catch (CloneNotSupportedException e) {
136: throw new InternalError(e.toString());
137: }
138: }
139:
140: /**
141: * Sets the various member variables and collections using the given DBColumn instance
142: * as a source object.
143: *
144: * @param source DBColumn from which to obtain values for member variables and
145: * collections
146: */
147: public void copyFrom(DBColumn source) {
148: super .copyFrom(source);
149:
150: if (source instanceof TargetColumn) {
151: inputValue = ((TargetColumn) source).getValue();
152: }
153: }
154:
155: /**
156: * Overrides default implementation to return value based on memberwise comparison.
157: *
158: * @param refObj Object against which we compare this instance
159: * @return true if refObj is functionally identical to this instance; false otherwise
160: */
161: public boolean equals(Object refObj) {
162: if (!(refObj instanceof TargetColumn)) {
163: return false;
164: } else if (this == refObj) {
165: return true;
166: }
167:
168: TargetColumn refMeta = (TargetColumn) refObj;
169:
170: boolean result = super .equals(refObj);
171: result &= (inputValue != null) ? inputValue.equals(refMeta
172: .getValue()) : (refMeta.getValue() == null);
173:
174: return result;
175: }
176:
177: /**
178: * @see SQLObject#getOutput(java.lang.String)
179: */
180: public SQLObject getOutput(String argName) throws BaseException {
181: throw new BaseException(
182: "TargetColumnImpl cannot be an output SQLObject.");
183: }
184:
185: /**
186: * Gets associated input SQLObject, if any, for this column.
187: *
188: * @return input SQLObject, or null if none was set
189: */
190: public SQLObject getValue() {
191: return inputValue;
192: }
193:
194: /**
195: * Returns the hashCode for this object.
196: *
197: * @return the hashCode of this object.
198: */
199: public int hashCode() {
200: return super .hashCode()
201: + ((inputValue != null) ? inputValue.hashCode() : 0);
202: }
203:
204: /**
205: * @see org.netbeans.modules.sql.framework.model.impl.AbstractDBColumn#parseXML(org.w3c.dom.Element)
206: */
207: public void parseXML(Element columnElement) throws BaseException {
208: super .parseXML(columnElement);
209:
210: NodeList childNodeList = columnElement.getChildNodes();
211: if (childNodeList != null && childNodeList.getLength() != 0) {
212: for (int i = 0; i < childNodeList.getLength(); i++) {
213: if (childNodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
214: Element childElement = (Element) childNodeList
215: .item(i);
216: String tagName = childElement.getTagName();
217:
218: if (TagParserUtility.TAG_OBJECTREF.equals(tagName)) {
219: secondPassParse(childElement);
220: }
221: }
222: }
223: }
224: }
225:
226: /**
227: * Parses elements which require a second pass to resolve their values.
228: *
229: * @param element DOM element containing XML marshalled version of this SQLObject
230: * instance
231: * @throws BaseException if element is null or error occurs during parsing
232: */
233: public void secondPassParse(Element element) throws BaseException {
234: TargetTable parentTable = (TargetTable) parentObject;
235: SQLDBModel parentDbModel = (SQLDBModel) parentTable
236: .getParentObject();
237: SQLDefinition definition = (SQLDefinition) parentDbModel
238: .getParentObject();
239:
240: SQLObject obj = TagParserUtility.parseXMLObjectRefTag(
241: definition, element);
242:
243: // If obj is null it may not be parsed yet so do a second parse...
244: // it registers this TargetColumn instance to be parsed a second time
245: // to resolve the value reference
246: if (obj == null) {
247: definition.addSecondPassSQLObject(this , element);
248: } else {
249: setValue(obj);
250: }
251: }
252:
253: /**
254: * Sets associated SQLObject as input to this column.
255: *
256: * @param newInput new input SQLObject; may be null
257: */
258: public void setValue(SQLObject newInput) {
259: inputValue = newInput;
260: }
261:
262: /**
263: * Overrides default implementation to return evaluated column name.
264: *
265: * @return evaluated column name.
266: */
267: public String toString() {
268: return super .toString();
269: }
270:
271: /**
272: * @see SQLObject#toXMLString
273: */
274: public String toXMLString(String prefix) {
275: StringBuilder xml = new StringBuilder(50);
276: xml.append(prefix).append("<").append(ELEMENT_TAG);
277:
278: // Allow superclass to write its attributes out first.
279: appendXMLAttributes(xml);
280:
281: xml.append(" >\n");
282:
283: // write out attributes
284: xml.append(super .toXMLAttributeTags(prefix));
285:
286: if (inputValue != null) {
287: try {
288: String refXml = TagParserUtility.toXMLObjectRefTag(
289: inputValue, prefix + "\t");
290: xml.append(refXml);
291: } catch (BaseException e) {
292: // TODO log this exception
293: }
294: }
295:
296: xml.append(prefix).append("</").append(ELEMENT_TAG).append(
297: ">\n");
298: return xml.toString();
299: }
300:
301: /**
302: * @see org.netbeans.modules.sql.framework.model.impl.AbstractDBColumn#getElementTagName
303: */
304: protected String getElementTagName() {
305: return ELEMENT_TAG;
306: }
307:
308: /*
309: * Performs sql framework initialization functions for constructors which cannot first
310: * call this().
311: */
312: private void init() {
313: type = SQLConstants.TARGET_COLUMN;
314: }
315:
316: }
|