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.etl.model.impl;
042:
043: import java.io.Serializable;
044: import java.util.Collection;
045: import java.util.HashMap;
046: import java.util.List;
047: import java.util.Map;
048: import org.netbeans.modules.etl.model.ETLDefinition;
049: import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
050: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
051: import org.netbeans.modules.sql.framework.model.SQLConstants;
052: import org.netbeans.modules.sql.framework.model.SQLDefinition;
053: import org.netbeans.modules.sql.framework.model.SQLFrameworkParentObject;
054: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
055: import org.netbeans.modules.sql.framework.model.SQLObject;
056: import org.netbeans.modules.sql.framework.model.SQLObjectListener;
057: import org.w3c.dom.Element;
058: import org.w3c.dom.NodeList;
059: import com.sun.sql.framework.exception.BaseException;
060: import com.sun.sql.framework.utils.Attribute;
061: import org.netbeans.modules.sql.framework.model.DBTable;
062: import org.netbeans.modules.sql.framework.model.DatabaseModel;
063: import org.netbeans.modules.sql.framework.model.SQLDBModel;
064: import org.netbeans.modules.sql.framework.model.ValidationInfo;
065:
066: /**
067: * Root container for holding ETL model objects.
068: *
069: * @author Jonathan Giron
070: * @author Ritesh Adval
071: * @author Ahimanikya Satapathy
072: * @version $Revision$
073: */
074: public class ETLDefinitionImpl implements ETLDefinition, Serializable {
075:
076: /** Attribute name: displayName */
077: public static final String ATTR_DISPLAYNAME = "displayName";
078: /** Attribute name: displayName */
079: public static final String ATTR_REFID = "refId";
080: /** Document version */
081: public static final String DOC_VERSION = "6.0";
082: /** XML formatting constant: indent prefix */
083: public static final String INDENT = " ";
084: /** TAG_DEFINITION is the tag for an ETL definition */
085: public static final String TAG_DEFINITION = "etlDefinition";
086: private static final String ATTR_VERSION = "version";
087: private Map<String, Attribute> attributes = new HashMap<String, Attribute>();
088: private SQLDefinition sqlDefinition;
089:
090: /**
091: * Creates a new default instance of ETLDefinitionImpl.
092: */
093: public ETLDefinitionImpl() {
094: init();
095: this .setVersion(DOC_VERSION);
096: }
097:
098: /**
099: * Creates a new instance of ETLDefinitionImpl, parsing the given DOM Element to
100: * retrieve its contents.
101: *
102: * @param xmlElement DOM element containing content information
103: * @param parent SQLFrameworkParentObject
104: * @exception com.sun.sql.framework.exception.BaseException if error occurs while
105: * parsing
106: */
107: public ETLDefinitionImpl(Element xmlElement,
108: SQLFrameworkParentObject parent) throws BaseException {
109: this (xmlElement, parent, false);
110: }
111:
112: /**
113: * Creates a new instance of ETLDefinitionImpl, parsing the given DOM Element to
114: * retrieve its contents, and optionally preserving the parsed version information.
115: *
116: * @param xmlElement DOM element containing content information
117: * @param parent SQLFrameworkParentObject
118: * @param preserveVersion true if version contained in xmlElement should be retained; false
119: * if version should be updated to the current value
120: * @exception com.sun.sql.framework.exception.BaseException if error occurs while
121: * parsing
122: */
123: public ETLDefinitionImpl(Element xmlElement,
124: SQLFrameworkParentObject parent, boolean preserveVersion)
125: throws BaseException {
126: init();
127: sqlDefinition.setSQLFrameworkParentObject(parent);
128: parseXML(xmlElement); // parseXML checks for old version
129: if (!preserveVersion) {
130: this .setVersion(DOC_VERSION); // after parsing set the version
131: }
132: }
133:
134: /**
135: * Creates a new instance of ETLDefinitionImpl with the given display name.
136: *
137: * @param displayName for this
138: */
139: public ETLDefinitionImpl(String displayName) {
140: this ();
141: this .setDisplayName(displayName);
142: sqlDefinition.setDisplayName(displayName);
143: }
144:
145: /**
146: * Adds given SQLObject instance to this SQLDefinition.
147: *
148: * @param newObject new instance to add
149: * @throws BaseException if add fails or instance implements an unrecognized object
150: * type.
151: */
152: public void addObject(SQLObject newObject) throws BaseException {
153: this .sqlDefinition.addObject(newObject);
154: }
155:
156: /**
157: * Adds an SQL object listener
158: *
159: * @param listener SQL object listener
160: */
161: public void addSQLObjectListener(SQLObjectListener listener) {
162: this .sqlDefinition.addSQLObjectListener(listener);
163: }
164:
165: /**
166: * Gets Collection of all SQLObjects in this model.
167: *
168: * @return Collection, possibly empty, of all SQLObjects
169: */
170: public Collection<SQLObject> getAllObjects() {
171: return this .sqlDefinition.getAllObjects();
172: }
173:
174: /**
175: * Gets the List of Databases
176: *
177: * @return java.util.List for this
178: */
179: public List<SQLDBModel> getAllDatabases() {
180: return this .sqlDefinition.getAllDatabases();
181: }
182:
183: /**
184: * Gets an attribute based on its name
185: *
186: * @param attrName attribute Name
187: * @return Attribute instance associated with attrName, or null if none exists
188: */
189: public Attribute getAttribute(String attrName) {
190: return attributes.get(attrName);
191: }
192:
193: /**
194: * @see SQLObject#getAttributeNames
195: */
196: public Collection<String> getAttributeNames() {
197: return attributes.keySet();
198: }
199:
200: /**
201: * @see SQLObject#getAttributeObject
202: */
203: public Object getAttributeValue(String attrName) {
204: Attribute attr = getAttribute(attrName);
205: return (attr != null) ? attr.getAttributeValue() : null;
206: }
207:
208: /**
209: * Getter for DatabaseModel
210: *
211: * @param modelName to be retrieved
212: * @return DatabaseModel for given Model Name
213: */
214: public DatabaseModel getDatabaseModel(String modelName) {
215: java.util.List list = sqlDefinition.getAllDatabases();
216: java.util.Iterator it = list.iterator();
217: while (it.hasNext()) {
218: SQLObject sqlObj = (SQLObject) it.next();
219: int type = sqlObj.getObjectType();
220: if (type == SQLConstants.SOURCE_DBMODEL
221: || type == SQLConstants.TARGET_DBMODEL) {
222: DatabaseModel dbModel = (DatabaseModel) sqlObj;
223: if (dbModel != null
224: && dbModel.getModelName().equals(modelName)) {
225: return dbModel;
226: }
227: }
228: }
229: return null;
230: }
231:
232: /**
233: * Gets display name.
234: *
235: * @return current display name
236: */
237: public String getDisplayName() {
238: return (String) this .getAttributeValue(ATTR_DISPLAYNAME);
239: }
240:
241: /**
242: * Gets execution strategy code set for this collaboration.
243: * @return execution strategy code
244: */
245: public Integer getExecutionStrategyCode() {
246: return this .sqlDefinition.getExecutionStrategyCode();
247: }
248:
249: /**
250: * get the parent repository object
251: *
252: * @return parent repository object
253: */
254: public Object getParent() {
255: return this .sqlDefinition.getParent();
256: }
257:
258: /**
259: * get runtime db model
260: *
261: * @return runtime db model
262: */
263: public RuntimeDatabaseModel getRuntimeDbModel() {
264: return this .sqlDefinition.getRuntimeDbModel();
265: }
266:
267: /**
268: * Gets a List of target DatabaseModels
269: *
270: * @return List, possibly empty, of source DatabaseModels
271: */
272: public List<SQLDBModel> getSourceDatabaseModels() {
273: return this .sqlDefinition.getSourceDatabaseModels();
274: }
275:
276: /**
277: * Gets the List of SourceTables
278: *
279: * @return List, possibly empty, of SourceTables
280: */
281: public List<DBTable> getSourceTables() {
282: return this .sqlDefinition.getSourceTables();
283: }
284:
285: /**
286: * Gets the SQL definition
287: *
288: * @return SQL definition
289: */
290: public SQLDefinition getSQLDefinition() {
291: return this .sqlDefinition;
292: }
293:
294: /**
295: * get the tag name for this ETLDefinitionImpl override at subclass level to return a
296: * different tag name
297: *
298: * @return tag name to be used in xml representation of this object
299: */
300: public String getTagName() {
301: return ETLDefinitionImpl.TAG_DEFINITION;
302: }
303:
304: /**
305: * Gets a List of target DatabaseModels
306: *
307: * @return List, possibly empty, of target DatabaseModels
308: */
309: public List<SQLDBModel> getTargetDatabaseModels() {
310: return this .sqlDefinition.getTargetDatabaseModels();
311: }
312:
313: /**
314: * Gets the List of TargetTables
315: *
316: * @return List, possibly empty, of TargetTables
317: */
318: public List<DBTable> getTargetTables() {
319: return this .sqlDefinition.getTargetTables();
320: }
321:
322: public String getVersion() {
323: return (String) this .getAttributeValue(ATTR_VERSION);
324: }
325:
326: /**
327: * Check if a java operator is used in the model.
328: *
329: * @return true if a java operator is used.
330: */
331: public boolean isContainsJavaOperators() {
332: return this .sqlDefinition.isContainsJavaOperators();
333: }
334:
335: /**
336: * Parses the XML content, if any, using the given Element as a source for
337: * reconstituting the member variables and collections of this instance.
338: *
339: * @param xmlElement DOM element containing XML marshalled version of a SQLDefinition
340: * instance
341: * @throws BaseException thrown while parsing XML, or if xmlElement is null
342: */
343: public void parseXML(Element xmlElement) throws BaseException {
344: NodeList list;
345:
346: if (xmlElement == null) {
347: throw new BaseException("xmlElement is null");
348: }
349:
350: list = xmlElement.getChildNodes();
351: TagParserUtility.parseAttributeList(attributes, list);
352: // check if we have a version less than 5.02
353: // for version less than 5.02 it is null
354: String version = getVersion();
355: if (version == null) {
356: sqlDefinition.parseXML(xmlElement);
357: } else {
358: list = xmlElement.getElementsByTagName(sqlDefinition
359: .getTagName());
360: if (list != null && list.getLength() > 0) {
361: // first element will be SQLDefinition
362: sqlDefinition.parseXML((Element) list.item(0));
363: }
364: }
365: }
366:
367: /**
368: * Removes given SQLObject instance from this SQLDefinition.
369: *
370: * @param sqlObj instance to remove
371: * @throws BaseException if error occurs during removal
372: */
373: public void removeObject(SQLObject sqlObj) throws BaseException {
374: this .sqlDefinition.removeObject(sqlObj);
375: }
376:
377: /**
378: * Removes SQL object listener
379: *
380: * @param listener SQL object listener
381: */
382: public void removeSQLObjectListener(SQLObjectListener listener) {
383: this .sqlDefinition.removeSQLObjectListener(listener);
384: }
385:
386: /**
387: * @see SQLObject#setAttribute
388: */
389: public void setAttribute(String attrName, Object val) {
390: Attribute attr = getAttribute(attrName);
391: if (attr != null) {
392: attr.setAttributeValue(val);
393: } else {
394: attr = new Attribute(attrName, val);
395: attributes.put(attrName, attr);
396: }
397: }
398:
399: /**
400: * Sets display name to given value.
401: *
402: * @param newName new display name
403: */
404: public void setDisplayName(String newName) {
405: this .setAttribute(ATTR_DISPLAYNAME, newName);
406: }
407:
408: /**
409: * Sets execution strategy code for this collaboration.
410: * @param code execution strategy code
411: */
412: public void setExecutionStrategyCode(Integer code) {
413: sqlDefinition.setExecutionStrategyCode(code);
414: }
415:
416: /**
417: * set the parent repository object
418: *
419: * @param parent parent repository object
420: */
421: public void setParent(Object parent) {
422: this .sqlDefinition.setParent(parent);
423: }
424:
425: public void setVersion(String ver) {
426: this .setAttribute(ATTR_VERSION, ver);
427: sqlDefinition.setVersion(ver);
428: }
429:
430: /**
431: * Returns the XML representation of collabSegment.
432: *
433: * @param prefix the xml.
434: * @return Returns the XML representation of colabSegment.
435: */
436: public String toXMLString(String prefix) throws BaseException {
437: if (prefix == null) {
438: prefix = "";
439: }
440:
441: StringBuilder xml = new StringBuilder(500);
442:
443: xml.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
444: xml.append(prefix + "<" + getTagName() + ">\n");
445: // write out attributes
446: xml.append(TagParserUtility.toXMLAttributeTags(attributes,
447: prefix));
448:
449: // write out SQL definition
450: if (sqlDefinition != null) {
451: xml.append(sqlDefinition.toXMLString(prefix + "\t"));
452: }
453:
454: xml.append("</" + getTagName() + ">\n");
455:
456: return xml.toString();
457: }
458:
459: /**
460: * validate the definition starting from the target tables.
461: * @return Map of invalid input object as keys and reason as value
462: */
463: public List<ValidationInfo> validate() {
464: return this .sqlDefinition.validate();
465: }
466:
467: public List<ValidationInfo> badgeValidate() {
468: return this .sqlDefinition.badgeValidate();
469: }
470:
471: /**
472: * Applies whatever rules are appropriate to migrate the current object model to the
473: * current version of ETLDefinition as implemented by the concrete class.
474: *
475: * @throws BaseException if error occurs during migration
476: */
477: public void migrateFromOlderVersions() throws BaseException {
478: sqlDefinition.migrateFromOlderVersions();
479: }
480:
481: protected void init() {
482: sqlDefinition = SQLModelObjectFactory.getInstance()
483: .createSQLDefinition();
484: }
485: }
|