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.ColumnRef;
045: import org.netbeans.modules.sql.framework.model.GUIInfo;
046: import org.netbeans.modules.sql.framework.model.SQLCondition;
047: import org.netbeans.modules.sql.framework.model.SQLConstants;
048: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
049: import org.netbeans.modules.sql.framework.model.SQLDefinition;
050: import org.netbeans.modules.sql.framework.model.SQLObject;
051: import org.netbeans.modules.sql.framework.model.utils.GeneratorUtil;
052: import org.w3c.dom.Element;
053: import org.w3c.dom.Node;
054: import org.w3c.dom.NodeList;
055:
056: import com.sun.sql.framework.exception.BaseException;
057:
058: /**
059: * @author Ritesh Adval
060: */
061: public class ColumnRefImpl extends AbstractSQLObject implements
062: ColumnRef {
063:
064: /** Constant for column metadata name tag. */
065: static final String ELEMENT_TAG = "dbColumnRef"; // NOI18N
066:
067: /** String to use in prefixing each line of a generated XML document */
068: protected static final String INDENT = "\t";
069:
070: private SQLDBColumn columnRef;
071:
072: /** Contains UI state information */
073: private GUIInfo guiInfo = new GUIInfo();
074:
075: /** Creates a new instance of ColumnRefImpl */
076: public ColumnRefImpl() {
077: guiInfo = new GUIInfo();
078: init();
079: }
080:
081: /**
082: * New instance
083: *
084: * @param src - source
085: */
086: public ColumnRefImpl(ColumnRef src) {
087: this ();
088: if (src == null) {
089: throw new IllegalArgumentException(
090: "can not create ColumnRefImpl using copy constructor, src is null");
091: }
092:
093: copyFrom(src);
094: }
095:
096: /**
097: * New instance
098: *
099: * @param aColumnRef SQLDBColumn to be referenced by this instance
100: */
101: public ColumnRefImpl(SQLDBColumn aColumnRef) {
102: this ();
103: this .columnRef = aColumnRef;
104: }
105:
106: /**
107: * Clone
108: *
109: * @return cloned object
110: */
111: public Object clone() {
112: return new ColumnRefImpl(this );
113: }
114:
115: /**
116: * all SQL objects are cloneable
117: *
118: * @exception CloneNotSupportedException - exception
119: * @return cloned object
120: */
121: public Object cloneSQLObject() throws CloneNotSupportedException {
122: return this .clone();
123: }
124:
125: /**
126: * Copies contents of given ConditionColumn instance to this object.
127: *
128: * @param src ConditionColumn from which to copy contents
129: */
130: public void copyFrom(ColumnRef src) {
131: super .copyFromSource(src);
132:
133: GUIInfo gInfo = src.getGUIInfo();
134:
135: this .guiInfo = gInfo != null ? (GUIInfo) gInfo.clone() : null;
136: SQLDBColumn sCol = (SQLDBColumn) src.getColumn();
137: try {
138: this .columnRef = sCol != null ? (SQLDBColumn) sCol
139: .cloneSQLObject() : null;
140: } catch (CloneNotSupportedException e) {
141: throw new InternalError(e.toString());
142: }
143:
144: this .parentObject = src.getParentObject();
145: }
146:
147: /**
148: * @see org.netbeans.modules.sql.framework.model.impl.AbstractDBColumn#equals(java.lang.Object)
149: */
150: public boolean equals(Object refObj) {
151: if (!(refObj instanceof ColumnRef)) {
152: return false;
153: }
154:
155: ColumnRef refMeta = (ColumnRef) refObj;
156: boolean result = super .equals(refMeta);
157:
158: result &= (this .getColumn() != null) ? this .getColumn().equals(
159: refMeta.getColumn()) : (refMeta.getColumn() == null);
160:
161: return result;
162: }
163:
164: /**
165: * Get column
166: *
167: * @return SQLObject
168: */
169: public SQLObject getColumn() {
170: return columnRef;
171: }
172:
173: /**
174: * Get display name
175: *
176: * @return display name
177: */
178: public String getDisplayName() {
179: if (columnRef != null) {
180: return columnRef.getDisplayName();
181: }
182:
183: return "Undefined";
184: }
185:
186: /**
187: * Gets GUI-related attributes for this instance in the form of a GuiInfo instance.
188: *
189: * @return associated GuiInfo instance
190: * @see GUIInfo
191: */
192: public GUIInfo getGUIInfo() {
193: return guiInfo;
194: }
195:
196: /**
197: * Gets JDBC type of output, if any.
198: *
199: * @return JDBC type of output, or SQLConstants.JDBCSQL_TYPE_UNDEFINED if output is
200: * undefined for this instance
201: */
202: public int getJdbcType() {
203: return columnRef.getJdbcType();
204: }
205:
206: /**
207: * Gets reference to SQLObject corresponding to given argument name that can be linked
208: * to an SQLConnectableObject.
209: *
210: * @param argName argument name of linkable SQLObject
211: * @return linkable SQLObject corresponding to argName
212: * @throws BaseException if object cannot be linked to an SQLConnectableObject
213: */
214: public SQLObject getOutput(String argName) throws BaseException {
215: return this ;
216: }
217:
218: /**
219: * @see org.netbeans.modules.sql.framework.model.impl.AbstractDBColumn#hashCode
220: */
221: public int hashCode() {
222: return super .hashCode()
223: + ((this .getColumn() != null) ? this .getColumn()
224: .hashCode() : 0);
225: }
226:
227: /**
228: * Populates the member variables and collections of this SQLObject instance, parsing
229: * the given DOM Element as the source for reconstituting its contents.
230: *
231: * @param columnElement DOM element containing XML marshalled version of this
232: * SQLObject instance
233: * @throws BaseException if element is null or error occurs during parsing
234: */
235: public void parseXML(Element columnElement) throws BaseException {
236: super .parseXML(columnElement);
237:
238: NodeList childNodeList = columnElement.getChildNodes();
239: if (childNodeList != null && childNodeList.getLength() != 0) {
240: for (int i = 0; i < childNodeList.getLength(); i++) {
241: if (childNodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
242: Element childElement = (Element) childNodeList
243: .item(i);
244: String tagName = childElement.getTagName();
245:
246: if (TagParserUtility.TAG_OBJECTREF.equals(tagName)) {
247: secondPassParse(childElement);
248: } else if (GUIInfo.TAG_GUIINFO.equals(tagName)) {
249: this .guiInfo = new GUIInfo(childElement);
250: }
251: }
252: }
253: }
254: }
255:
256: /**
257: * Parses elements which require a second round of parsing to resolve their
258: * references.
259: *
260: * @param element DOM element containing XML marshalled version of this SQLObject
261: * instance
262: * @throws BaseException if element is null or error occurs during parsing
263: */
264: public void secondPassParse(Element element) throws BaseException {
265: SQLCondition sqlCond = (SQLCondition) this .getParentObject();
266: SQLDefinition definition = TagParserUtility
267: .getAncestralSQLDefinition((SQLObject) sqlCond
268: .getParentObject());
269:
270: SQLObject obj = TagParserUtility.parseXMLObjectRefTag(
271: definition, element);
272:
273: // If obj is null it may not be parsed yet so do a second parse...
274: // it registers this TargetColumn instance to be parsed a second time
275: // to resolve the value reference
276: if (obj == null) {
277: definition.addSecondPassSQLObject(this , element);
278: } else {
279: setColumn(obj);
280: }
281: }
282:
283: /**
284: * Set column
285: *
286: * @param column - column
287: */
288: public void setColumn(SQLObject column) {
289: this .columnRef = (SQLDBColumn) column;
290: }
291:
292: /**
293: * Set display name
294: *
295: * @param dispName - display name
296: */
297: public void setDisplayName(String dispName) {
298: }
299:
300: /**
301: * toString
302: *
303: * @return String
304: */
305: public String toString() {
306: if (columnRef != null) {
307: String cName = columnRef.getName();
308: try {
309: GeneratorUtil eval = GeneratorUtil.getInstance();
310: eval.setTableAliasUsed(true);
311: cName = eval.getEvaluatedString(this );
312: eval.setTableAliasUsed(false);
313:
314: return cName;
315: } catch (BaseException ignore) {
316: // ignore - should we log this?
317: }
318: }
319: return "Undefined";
320: }
321:
322: /**
323: * Gets XML representation of this SQLObject, appending the given String to the
324: * beginning of each new line.
325: *
326: * @param prefix String to append to each new line of the XML representation
327: * @return XML representation of this SQLObject instance
328: */
329: public String toXMLString(String prefix) {
330: StringBuilder buf = new StringBuilder(200);
331:
332: buf.append(prefix).append(getHeader());
333: buf.append(toXMLAttributeTags(prefix));
334:
335: if (this .getColumn() != null) {
336: try {
337: String refXml = TagParserUtility.toXMLObjectRefTag(this
338: .getColumn(), prefix + "\t");
339: buf.append(refXml);
340: } catch (BaseException e) {
341: // @TODO log this exception
342: }
343:
344: if (guiInfo != null) {
345: buf.append(guiInfo.toXMLString(prefix + INDENT));
346: }
347: }
348:
349: buf.append(prefix).append(getFooter());
350: return buf.toString();
351: }
352:
353: /*
354: * Performs sql framework initialization functions for constructors which cannot first
355: * call this().
356: */
357: private void init() {
358: type = SQLConstants.COLUMN_REF;
359: }
360: }
|