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.util.Iterator;
044: import java.util.List;
045:
046: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
047: import org.netbeans.modules.sql.framework.model.RuntimeInput;
048: import org.netbeans.modules.sql.framework.model.RuntimeOutput;
049: import org.netbeans.modules.sql.framework.model.SQLConstants;
050: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
051: import org.netbeans.modules.sql.framework.model.SQLObject;
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: * Runtime DbModel is one instance per sql definition and it contains only one instance of
060: * RuntimeInputImpl and only one instance of RuntimeOutputImpl
061: *
062: * @author Ritesh Adval
063: */
064: public class RuntimeDatabaseModelImpl extends SQLDBModelImpl implements
065: RuntimeDatabaseModel {
066:
067: /** Creates a new instance of RuntimeDatabaseModelImpl */
068: public RuntimeDatabaseModelImpl() {
069: super ();
070: type = SQLConstants.RUNTIME_DBMODEL;
071: this .setModelName("RuntimeDbModel");
072: this .setDisplayName("Runtime DbModel");
073: }
074:
075: /**
076: * Get runtime input
077: *
078: * @return RuntimeInput
079: */
080: public RuntimeInput getRuntimeInput() {
081: List tbls = this .getTables();
082: Iterator it = tbls.iterator();
083:
084: while (it.hasNext()) {
085: SQLObject sqlObj = (SQLObject) it.next();
086: if (sqlObj.getObjectType() == SQLConstants.RUNTIME_INPUT) {
087: return (RuntimeInput) sqlObj;
088: }
089: }
090:
091: return null;
092: }
093:
094: /**
095: * Get runtime output
096: *
097: * @return RuntimeOutput
098: */
099: public RuntimeOutput getRuntimeOutput() {
100: List tbls = this .getTables();
101: Iterator it = tbls.iterator();
102:
103: while (it.hasNext()) {
104: SQLObject sqlObj = (SQLObject) it.next();
105: if (sqlObj.getObjectType() == SQLConstants.RUNTIME_OUTPUT) {
106: return (RuntimeOutput) sqlObj;
107: }
108: }
109:
110: return null;
111: }
112:
113: /**
114: * Parses the XML content, if any, using the given Element as a source for
115: * reconstituting the member variables and collections of this instance.
116: *
117: * @param dbElement DOM element containing XML marshalled version of a
118: * @exception BaseException thrown while parsing XML, or if member variable element is
119: * null
120: */
121: public void parseXML(Element dbElement) throws BaseException {
122: if (dbElement == null) {
123: throw new BaseException(
124: "Must supply non-null org.w3c.dom.Element ref for element.No <"
125: + RUNTIME_MODEL_TAG + "> element found.");
126: }
127:
128: if (!RUNTIME_MODEL_TAG.equals(dbElement.getNodeName())) {
129: throw new BaseException("Invalid root element; expected "
130: + RUNTIME_MODEL_TAG + ", got "
131: + dbElement.getNodeName());
132: }
133:
134: super .parseCommonAttributesAndTags(dbElement);
135: name = dbElement.getAttribute(NAME);
136: String typeStr = dbElement.getAttribute(TYPE);
137:
138: NodeList childNodeList = null;
139:
140: childNodeList = dbElement.getChildNodes();
141:
142: if (STRTYPE_RUNTIME.equals(typeStr)) {
143: type = SQLConstants.RUNTIME_DBMODEL;
144: childNodeList = dbElement
145: .getElementsByTagName(RuntimeInput.TAG_RUNTIME_INPUT);
146: parseRuntimeInput(childNodeList);
147: childNodeList = dbElement
148: .getElementsByTagName(RuntimeOutput.TAG_RUNTIME_OUTPUT);
149: parseRuntimeOutput(childNodeList);
150: } else {
151: throw new BaseException(
152: "Missing or invalid modelType attribute: "
153: + typeStr);
154: }
155: }
156:
157: /**
158: * Gets xml representation of this DatabaseModel instance.
159: *
160: * @param prefix for this xml.
161: * @return Return the xml representation of data source metadata.
162: * @exception BaseException - exception
163: */
164: public String toXMLString(String prefix) throws BaseException {
165: StringBuilder xml = new StringBuilder(INIT_XMLBUF_SIZE);
166: if (prefix == null) {
167: prefix = "";
168: }
169:
170: xml.append(prefix).append('<').append(RUNTIME_MODEL_TAG)
171: .append(" ").append(NAME).append("=\"").append(
172: name.trim()).append("\"");
173:
174: if (id != null && id.trim().length() != 0) {
175: xml.append(" ").append(ID).append("=\"").append(id.trim())
176: .append("\"");
177: }
178:
179: if (displayName != null && displayName.trim().length() != 0) {
180: xml.append(" ").append(DISPLAY_NAME).append("=\"").append(
181: displayName.trim()).append('"');
182: }
183:
184: if (type == SQLConstants.RUNTIME_DBMODEL) {
185: xml.append(' ').append(TYPE).append("=\"").append(
186: STRTYPE_RUNTIME).append('"');
187: }
188:
189: xml.append(">\n");
190:
191: // write out tables
192: writeTables(prefix, xml);
193:
194: xml.append(prefix).append("</").append(RUNTIME_MODEL_TAG)
195: .append(">\n");
196: return xml.toString();
197: }
198:
199: /**
200: * Extracts SourceTable instances from the given NodeList.
201: *
202: * @param tableNodeList Nodes to be unmarshalled
203: * @throws BaseException if error occurs while parsing
204: */
205: protected void parseRuntimeInput(NodeList tableNodeList)
206: throws BaseException {
207: for (int i = 0; i < tableNodeList.getLength(); i++) {
208: if (tableNodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
209: Element tableElement = (Element) tableNodeList.item(i);
210:
211: RuntimeInput runtime = SQLModelObjectFactory
212: .getInstance().createRuntimeInput();
213: runtime.setParentObject(this );
214:
215: runtime.parseXML(tableElement);
216: addTable(runtime);
217: }
218: }
219: }
220:
221: /**
222: * Extracts TargetTable instances from the given NodeList.
223: *
224: * @param tableNodeList Nodes to be unmarshalled
225: * @throws BaseException if error occurs while parsing
226: */
227: protected void parseRuntimeOutput(NodeList tableNodeList)
228: throws BaseException {
229: for (int i = 0; i < tableNodeList.getLength(); i++) {
230: if (tableNodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
231: Element tableElement = (Element) tableNodeList.item(i);
232:
233: RuntimeOutput runtime = SQLModelObjectFactory
234: .getInstance().createRuntimeOutput();
235: runtime.setParentObject(this);
236:
237: runtime.parseXML(tableElement);
238: this.addTable(runtime);
239: }
240: }
241: }
242: }
|