PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/etl.editor/src/org/netbeans/modules/sql/framework/model/impl/ColumnRefImpl.java

https://bitbucket.org/rsaqc/netbeans-soa
Java | 353 lines | 149 code | 45 blank | 159 comment | 29 complexity | 903ffe1271575cf8bfac34e8a399a40f MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.sql.framework.model.impl;
  45. import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
  46. import org.netbeans.modules.sql.framework.model.ColumnRef;
  47. import org.netbeans.modules.sql.framework.model.GUIInfo;
  48. import org.netbeans.modules.sql.framework.model.SQLCondition;
  49. import org.netbeans.modules.sql.framework.model.SQLConstants;
  50. import org.netbeans.modules.sql.framework.model.SQLDBColumn;
  51. import org.netbeans.modules.sql.framework.model.SQLDefinition;
  52. import org.netbeans.modules.sql.framework.model.SQLObject;
  53. import org.netbeans.modules.sql.framework.model.utils.GeneratorUtil;
  54. import org.w3c.dom.Element;
  55. import org.w3c.dom.Node;
  56. import org.w3c.dom.NodeList;
  57. import com.sun.etl.exception.BaseException;
  58. /**
  59. * @author Ritesh Adval
  60. */
  61. public class ColumnRefImpl extends AbstractSQLObject implements ColumnRef {
  62. /** Constant for column metadata name tag. */
  63. static final String ELEMENT_TAG = "dbColumnRef"; // NOI18N
  64. /** String to use in prefixing each line of a generated XML document */
  65. protected static final String INDENT = "\t";
  66. private SQLDBColumn columnRef;
  67. /** Contains UI state information */
  68. private GUIInfo guiInfo = new GUIInfo();
  69. /** Creates a new instance of ColumnRefImpl */
  70. public ColumnRefImpl() {
  71. guiInfo = new GUIInfo();
  72. init();
  73. }
  74. /**
  75. * New instance
  76. *
  77. * @param src - source
  78. */
  79. public ColumnRefImpl(ColumnRef src) {
  80. this();
  81. if (src == null) {
  82. throw new IllegalArgumentException("can not create ColumnRefImpl using copy constructor, src is null");
  83. }
  84. copyFrom(src);
  85. }
  86. /**
  87. * New instance
  88. *
  89. * @param aColumnRef SQLDBColumn to be referenced by this instance
  90. */
  91. public ColumnRefImpl(SQLDBColumn aColumnRef) {
  92. this();
  93. this.columnRef = aColumnRef;
  94. }
  95. /**
  96. * Clone
  97. *
  98. * @return cloned object
  99. */
  100. public Object clone() {
  101. return new ColumnRefImpl(this);
  102. }
  103. /**
  104. * all SQL objects are cloneable
  105. *
  106. * @exception CloneNotSupportedException - exception
  107. * @return cloned object
  108. */
  109. public Object cloneSQLObject() throws CloneNotSupportedException {
  110. return this.clone();
  111. }
  112. /**
  113. * Copies contents of given ConditionColumn instance to this object.
  114. *
  115. * @param src ConditionColumn from which to copy contents
  116. */
  117. public void copyFrom(ColumnRef src) {
  118. super.copyFromSource(src);
  119. GUIInfo gInfo = src.getGUIInfo();
  120. this.guiInfo = gInfo != null ? (GUIInfo) gInfo.clone() : null;
  121. SQLDBColumn sCol = (SQLDBColumn) src.getColumn();
  122. try {
  123. this.columnRef = sCol != null ? (SQLDBColumn) sCol.cloneSQLObject() : null;
  124. } catch (CloneNotSupportedException e) {
  125. throw new InternalError(e.toString());
  126. }
  127. this.parentObject = src.getParentObject();
  128. }
  129. /**
  130. * @see org.netbeans.modules.sql.framework.model.impl.AbstractDBColumn#equals(java.lang.Object)
  131. */
  132. public boolean equals(Object refObj) {
  133. if (!(refObj instanceof ColumnRef)) {
  134. return false;
  135. }
  136. ColumnRef refMeta = (ColumnRef) refObj;
  137. boolean result = super.equals(refMeta);
  138. result &= (this.getColumn() != null) ? this.getColumn().equals(refMeta.getColumn()) : (refMeta.getColumn() == null);
  139. return result;
  140. }
  141. /**
  142. * Get column
  143. *
  144. * @return SQLObject
  145. */
  146. public SQLObject getColumn() {
  147. return columnRef;
  148. }
  149. /**
  150. * Get display name
  151. *
  152. * @return display name
  153. */
  154. public String getDisplayName() {
  155. if (columnRef != null) {
  156. return columnRef.getDisplayName();
  157. }
  158. return "Undefined";
  159. }
  160. /**
  161. * Gets GUI-related attributes for this instance in the form of a GuiInfo instance.
  162. *
  163. * @return associated GuiInfo instance
  164. * @see GUIInfo
  165. */
  166. public GUIInfo getGUIInfo() {
  167. return guiInfo;
  168. }
  169. /**
  170. * Gets JDBC type of output, if any.
  171. *
  172. * @return JDBC type of output, or SQLConstants.JDBCSQL_TYPE_UNDEFINED if output is
  173. * undefined for this instance
  174. */
  175. public int getJdbcType() {
  176. return columnRef.getJdbcType();
  177. }
  178. /**
  179. * Gets reference to SQLObject corresponding to given argument name that can be linked
  180. * to an SQLConnectableObject.
  181. *
  182. * @param argName argument name of linkable SQLObject
  183. * @return linkable SQLObject corresponding to argName
  184. * @throws BaseException if object cannot be linked to an SQLConnectableObject
  185. */
  186. public SQLObject getOutput(String argName) throws BaseException {
  187. return this;
  188. }
  189. /**
  190. * @see org.netbeans.modules.sql.framework.model.impl.AbstractDBColumn#hashCode
  191. */
  192. public int hashCode() {
  193. return super.hashCode() + ((this.getColumn() != null) ? this.getColumn().hashCode() : 0);
  194. }
  195. /**
  196. * Populates the member variables and collections of this SQLObject instance, parsing
  197. * the given DOM Element as the source for reconstituting its contents.
  198. *
  199. * @param columnElement DOM element containing XML marshalled version of this
  200. * SQLObject instance
  201. * @throws BaseException if element is null or error occurs during parsing
  202. */
  203. public void parseXML(Element columnElement) throws BaseException {
  204. super.parseXML(columnElement);
  205. NodeList childNodeList = columnElement.getChildNodes();
  206. if (childNodeList != null && childNodeList.getLength() != 0) {
  207. for (int i = 0; i < childNodeList.getLength(); i++) {
  208. if (childNodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
  209. Element childElement = (Element) childNodeList.item(i);
  210. String tagName = childElement.getTagName();
  211. if (TagParserUtility.TAG_OBJECTREF.equals(tagName)) {
  212. secondPassParse(childElement);
  213. } else if (GUIInfo.TAG_GUIINFO.equals(tagName)) {
  214. this.guiInfo = new GUIInfo(childElement);
  215. }
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * Parses elements which require a second round of parsing to resolve their
  222. * references.
  223. *
  224. * @param element DOM element containing XML marshalled version of this SQLObject
  225. * instance
  226. * @throws BaseException if element is null or error occurs during parsing
  227. */
  228. public void secondPassParse(Element element) throws BaseException {
  229. SQLCondition sqlCond = (SQLCondition) this.getParentObject();
  230. SQLDefinition definition = TagParserUtility.getAncestralSQLDefinition((SQLObject) sqlCond.getParentObject());
  231. SQLObject obj = TagParserUtility.parseXMLObjectRefTag(definition, element);
  232. // If obj is null it may not be parsed yet so do a second parse...
  233. // it registers this TargetColumn instance to be parsed a second time
  234. // to resolve the value reference
  235. if (obj == null) {
  236. definition.addSecondPassSQLObject(this, element);
  237. } else {
  238. setColumn(obj);
  239. }
  240. }
  241. /**
  242. * Set column
  243. *
  244. * @param column - column
  245. */
  246. public void setColumn(SQLObject column) {
  247. this.columnRef = (SQLDBColumn) column;
  248. }
  249. /**
  250. * Set display name
  251. *
  252. * @param dispName - display name
  253. */
  254. public void setDisplayName(String dispName) {
  255. }
  256. /**
  257. * toString
  258. *
  259. * @return String
  260. */
  261. public String toString() {
  262. if (columnRef != null) {
  263. String cName = columnRef.getName();
  264. try {
  265. GeneratorUtil eval = GeneratorUtil.getInstance();
  266. eval.setTableAliasUsed(true);
  267. cName = eval.getEvaluatedString(this);
  268. eval.setTableAliasUsed(false);
  269. return cName;
  270. } catch (BaseException ignore) {
  271. // ignore - should we log this?
  272. }
  273. }
  274. return "Undefined";
  275. }
  276. /**
  277. * Gets XML representation of this SQLObject, appending the given String to the
  278. * beginning of each new line.
  279. *
  280. * @param prefix String to append to each new line of the XML representation
  281. * @return XML representation of this SQLObject instance
  282. */
  283. public String toXMLString(String prefix) {
  284. StringBuilder buf = new StringBuilder(200);
  285. buf.append(prefix).append(getHeader());
  286. buf.append(toXMLAttributeTags(prefix));
  287. if (this.getColumn() != null) {
  288. try {
  289. String refXml = TagParserUtility.toXMLObjectRefTag(this.getColumn(), prefix + "\t");
  290. buf.append(refXml);
  291. } catch (BaseException e) {
  292. // @TODO log this exception
  293. }
  294. if (guiInfo != null) {
  295. buf.append(guiInfo.toXMLString(prefix + INDENT));
  296. }
  297. }
  298. buf.append(prefix).append(getFooter());
  299. return buf.toString();
  300. }
  301. /*
  302. * Performs sql framework initialization functions for constructors which cannot first
  303. * call this().
  304. */
  305. private void init() {
  306. type = SQLConstants.COLUMN_REF;
  307. }
  308. }