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.mashup.db.model.impl;
042:
043: import java.sql.Connection;
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Properties;
051: import java.util.Set;
052:
053: import org.netbeans.modules.mashup.db.common.FlatfileDBConnectionFactory;
054: import org.netbeans.modules.mashup.db.model.FlatfileDBConnectionDefinition;
055: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
056: import org.netbeans.modules.mashup.db.model.FlatfileDatabaseModel;
057: import org.netbeans.modules.mashup.db.model.FlatfileDefinition;
058: import org.netbeans.modules.sql.framework.common.utils.TagParserUtility;
059: import org.w3c.dom.Element;
060: import org.w3c.dom.Node;
061: import org.w3c.dom.NodeList;
062:
063: import com.sun.sql.framework.exception.BaseException;
064: import net.java.hulp.i18n.Logger;
065: import org.netbeans.modules.etl.logger.Localizer;
066: import org.netbeans.modules.etl.logger.LogUtil;
067: import org.netbeans.modules.etl.model.ETLObject;
068: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
069: import org.netbeans.modules.sql.framework.model.DBTable;
070: import org.netbeans.modules.sql.framework.model.DatabaseModel;
071: import org.netbeans.modules.sql.framework.model.impl.SQLDBModelImpl;
072:
073: /**
074: * Flatfile DB specific concrete implementation of DatabaseModel interface.
075: *
076: * @author Jonathan Giron
077: * @author Girish Patil
078: * @author Ahimanikya Satapathy
079: * @version $Revision$
080: */
081: public class FlatfileDatabaseModelImpl extends SQLDBModelImpl implements
082: FlatfileDatabaseModel, Cloneable {
083:
084: /** Constants used in XML tags * */
085: private static final String ATTR_MAJOR_VERSION = "majorVersion";
086: private static final String ATTR_MICRO_VERSION = "microVersion";
087: private static final String ATTR_MINOR_VERSION = "minorVersion";
088: private static final String ATTR_NAME = "name";
089: private static transient final Logger mLogger = LogUtil
090: .getLogger(FlatfileDatabaseModelImpl.class.getName());
091: private static transient final Localizer mLoc = Localizer.get();
092: private static final String DRIVER_NAME = "org.axiondb.jdbc.AxionDriver";
093: private static final List DRIVER_LIST;
094:
095: static {
096: List aList = new ArrayList(1);
097: aList.add(DRIVER_NAME);
098: DRIVER_LIST = Collections.unmodifiableList(aList);
099: }
100: private static final int DRIVER_TYPE = 4;
101: private static final String END_QUOTE_SPACE = "\" ";
102: private static final String EQUAL_START_QUOTE = "=\"";
103: /*
104: * String used to separate name, schema, and/or catalog Strings in a fully-qualified
105: * table name.
106: */
107: private static final String FQ_TBL_NAME_SEPARATOR = ".";
108: private static final String LOG_CATEGORY = FlatfileDatabaseModelImpl.class
109: .getName();
110: private static final String QUOTE = "\"";
111: private static final String TAB = "\t";
112: private static final String TAG_CONNECTION_DEFINITION = "connectionDefinition";
113: private static final String TAG_MODEL = "stcdbDatabaseModel";
114: private static final String TAG_STCDB_TABLE = "stcdbTable";
115: private static final String XML_DOC_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
116: /** Connection name */
117: protected volatile String connectionName;
118:
119: /* Major version number of DatabaseModel metadata */
120: private transient int majorVersion = 5;
121:
122: /* Micro (implementation) version number of DatabaseModel metadata */
123: private transient int microVersion = 0;
124:
125: /* Major version number of DatabaseModel metadata */
126: private transient int minorVersion = 1;
127:
128: /** Constructs a new default instance of FlatfileDatabaseModelImpl. */
129: public FlatfileDatabaseModelImpl() {
130: tables = new HashMap<String, FlatfileDBTable>();
131: }
132:
133: /**
134: * Creates a new instance of FlatfileDatabaseModelImpl, cloning the contents of the
135: * given DatabaseModel implementation instance.
136: *
137: * @param src DatabaseModel instance to be cloned
138: */
139: public FlatfileDatabaseModelImpl(FlatfileDatabaseModel src) {
140: this ();
141:
142: if (src == null) {
143: throw new IllegalArgumentException(
144: "Must supply non-null DatabseModel instance for src param.");
145: }
146:
147: copyFrom(src);
148: }
149:
150: /**
151: * Constructs a new instance of FlatfileDatabaseModelImpl of type SOURCE_DBMODEL,
152: * using the given name and DBConnectionDefinition.
153: *
154: * @param modelName name of new DatabaseModel
155: * @param connDef FlatfileDBConnectionDefinition for this new instance
156: */
157: public FlatfileDatabaseModelImpl(String modelName,
158: DBConnectionDefinition connDef) {
159: this ();
160:
161: if (connDef == null) {
162: throw new IllegalArgumentException(
163: "connDef must be non-null");
164: }
165:
166: String connName = connDef.getName();
167: if (connName == null || connName.trim().length() == 0) {
168: throw new IllegalArgumentException(
169: "connDef must have a name.");
170: }
171:
172: if (modelName == null || modelName.trim().length() == 0) {
173: throw new IllegalArgumentException(
174: "modelName must be a non-empty String");
175: }
176:
177: name = modelName;
178: connectionDefinition = new FlatfileDBConnectionDefinitionImpl(
179: connDef);
180: }
181:
182: /**
183: * Adds new SourceTable to the model.
184: *
185: * @param table new DBTable to add
186: */
187: public void addTable(FlatfileDBTable table) {
188: if (table != null) {
189: table.setParent(this );
190: tables.put(getFullyQualifiedTableName(table), table);
191: }
192: }
193:
194: /**
195: * Clones this object.
196: *
197: * @return shallow copy of this ETLDataSource
198: */
199: public Object clone() {
200: FlatfileDatabaseModelImpl myClone = (FlatfileDatabaseModelImpl) super
201: .clone();
202:
203: myClone.name = name;
204: myClone.description = description;
205: myClone.source = source;
206:
207: myClone.tables = new HashMap<String, FlatfileDBTable>();
208: tables.putAll(tables);
209:
210: myClone.connectionName = connectionName;
211:
212: return myClone;
213: }
214:
215: /**
216: * Copies member values from those contained in the given DatabaseModel instance.
217: */
218: public void copyFrom(DatabaseModel src) {
219: if (src == null || src == this ) {
220: return;
221: }
222:
223: copyPrimitivesFrom(src);
224: copyConnectionDefinitionFrom(src);
225: copyTablesFrom(src);
226: }
227:
228: /**
229: * Copies member values from those contained in the given FlatfileDatabaseModel
230: * instance.
231: *
232: * @param src DatabaseModel whose contents are to be copied into this instance
233: */
234: public void copyFrom(FlatfileDatabaseModel src) {
235: copyFrom((DatabaseModel) src);
236: }
237:
238: /**
239: * Create DBTable instance with the given table, schema, and catalog names.
240: *
241: * @param tableName table name of new table
242: * @param schemaName schema name of new table
243: * @param catalogName catalog name of new table
244: * @return an instance of ETLTable if successful, null if failed.
245: */
246: public DBTable createTable(String tableName, String schemaName,
247: String catalogName) {
248: FlatfileDBTableImpl table = null;
249:
250: if (tableName == null || tableName.length() == 0) {
251: throw new IllegalArgumentException(
252: "tableName cannot be null");
253: }
254:
255: table = new FlatfileDBTableImpl(tableName, schemaName,
256: catalogName);
257: addTable(table);
258:
259: return table;
260: }
261:
262: /**
263: * @see java.lang.Object#equals
264: */
265: public boolean equals(Object refObj) {
266: // Check for reflexivity.
267: if (this == refObj) {
268: return true;
269: }
270:
271: boolean result = false;
272:
273: // Ensure castability (also checks for null refObj)
274: if (refObj instanceof FlatfileDatabaseModelImpl) {
275: FlatfileDatabaseModelImpl aSrc = (FlatfileDatabaseModelImpl) refObj;
276:
277: result = ((aSrc.name != null) ? aSrc.name.equals(name)
278: : (name == null));
279: mLogger.infoNoloc(mLoc.t(
280: "PRSR063: equals(): Do model names match? {0}"
281: + result, LOG_CATEGORY));
282:
283: boolean connCheck = (aSrc.connectionName != null) ? aSrc.connectionName
284: .equals(connectionName)
285: : (connectionName == null);
286: mLogger.infoNoloc(mLoc.t(
287: "PRSR064: equals(): Do connection names match? {0}"
288: + connCheck, LOG_CATEGORY));
289: result &= connCheck;
290:
291: connCheck = ((aSrc.connectionDefinition != null) ? aSrc.connectionDefinition
292: .equals(connectionDefinition)
293: : (connectionDefinition == null));
294: mLogger.infoNoloc(mLoc.t(
295: "PRSR065: equals(): Do connection defs match? {0}"
296: + connCheck, LOG_CATEGORY));
297: result &= connCheck;
298:
299: if (tables != null && aSrc.tables != null) {
300: Set objTbls = aSrc.tables.keySet();
301: Set myTbls = tables.keySet();
302:
303: // Must be identical (no subsetting), hence the pair of tests.
304: boolean tblCheck = myTbls.containsAll(objTbls)
305: && objTbls.containsAll(myTbls);
306: mLogger.infoNoloc(mLoc.t(
307: "PRSR066: equals(): Do table names match? {0}"
308: + tblCheck, LOG_CATEGORY));
309: result &= tblCheck;
310: }
311: }
312:
313: mLogger.infoNoloc(mLoc.t(
314: "PRSR067: equals(): Is refObj equal to this? {0}"
315: + result, LOG_CATEGORY));
316: return result;
317: }
318:
319: public DBConnectionDefinition getFlatfileDBConnectionDefinition(
320: boolean download) {
321: if (download) {
322: return getConnectionDefinition();
323: } else {
324: return connectionDefinition;
325: }
326: }
327:
328: /**
329: * Gets name of DBConnectionDefinition associated with this database model.
330: *
331: * @return name of associated DBConnectionDefinition instance
332: */
333: public String getConnectionName() {
334: return this .connectionName;
335: }
336:
337: public Map getFlatfileTablePropertyMap(String flatfileName) {
338: FlatfileDBTable table = (FlatfileDBTable) this .getTableMap()
339: .get(flatfileName);
340: return table == null ? Collections.EMPTY_MAP : table
341: .getProperties();
342: }
343:
344: public Map getFlatfileTablePropertyMaps() {
345: Iterator iter = this .getTables().iterator();
346: Map propMap = Collections.EMPTY_MAP;
347: if (iter.hasNext()) {
348: propMap = new HashMap(this .getTables().size());
349: do {
350: FlatfileDBTable flatfileDBTable = (FlatfileDBTable) iter
351: .next();
352: propMap.put(flatfileDBTable.getName(), flatfileDBTable
353: .getProperties());
354: } while (iter.hasNext());
355: }
356:
357: return propMap;
358: }
359:
360: /**
361: * @see org.netbeans.modules.model.database.DatabaseModel#getFullyQualifiedTableName(DBTable)
362: */
363: public String getFullyQualifiedTableName(DBTable tbl) {
364: return (tbl != null) ? getFullyQualifiedTableName(
365: tbl.getName(), tbl.getSchema(), tbl.getCatalog()) : "";
366: }
367:
368: /**
369: * @see org.netbeans.modules.model.database.DatabaseModel#getFullyQualifiedTableName(String,
370: * String, String)
371: */
372: public String getFullyQualifiedTableName(String tblName,
373: String schName, String catName) {
374: if (tblName == null) {
375: throw new IllegalArgumentException(
376: "Must supply non-null String value for tblName.");
377: }
378:
379: StringBuilder buf = new StringBuilder(50);
380:
381: if (catName != null && catName.trim().length() != 0) {
382: buf.append(catName.trim());
383: buf.append(FQ_TBL_NAME_SEPARATOR);
384: }
385:
386: if (schName != null && schName.trim().length() != 0) {
387: buf.append(schName.trim());
388: buf.append(FQ_TBL_NAME_SEPARATOR);
389: }
390:
391: buf.append(tblName.trim());
392:
393: return buf.toString();
394: }
395:
396: public Connection getJDBCConnection() throws Exception {
397: return getJDBCConnection((ClassLoader) null);
398: }
399:
400: public Connection getJDBCConnection(ClassLoader cl)
401: throws Exception {
402: Connection conn = null;
403: String url = null;
404: String id = null;
405: String pswd = null;
406:
407: try {
408: DBConnectionDefinition cd = getConnectionDefinition();
409: url = cd.getConnectionURL();
410: id = cd.getUserName();
411: pswd = cd.getPassword();
412:
413: if ((id != null) && (!"".equals(id))) {
414: conn = FlatfileDBConnectionFactory.getInstance()
415: .getConnection(url, id, pswd, cl);
416: } else {
417: conn = FlatfileDBConnectionFactory.getInstance()
418: .getConnection(url, null, cl);
419: }
420: } catch (BaseException ex) {
421: Throwable cause = ex.getCause();
422: if (cause == null) {
423: cause = ex;
424: }
425: throw new Exception(cause);
426: }
427:
428: return conn;
429: }
430:
431: public Connection getJDBCConnection(Properties props)
432: throws Exception {
433: return getJDBCConnection(props, (ClassLoader) null);
434: }
435:
436: public Connection getJDBCConnection(Properties props, ClassLoader cl)
437: throws Exception {
438: Connection conn = null;
439: try {
440: DBConnectionDefinition cd = getConnectionDefinition();
441: String jdbcUrl = null;
442:
443: if (cd != null) {
444: jdbcUrl = cd.getConnectionURL();
445: }
446:
447: conn = FlatfileDBConnectionFactory.getInstance()
448: .getConnection(jdbcUrl, props, cl);
449: } catch (BaseException ex) {
450: Throwable cause = ex.getCause();
451: if (cause == null) {
452: cause = ex;
453: }
454: throw new Exception(cause);
455: }
456: return conn;
457: }
458:
459: public Connection getJDBCConnection(String jdbcUrl, String uid,
460: String passwd) throws Exception {
461: return getJDBCConnection(jdbcUrl, uid, passwd, null);
462: }
463:
464: public Connection getJDBCConnection(String jdbcUrl, String uid,
465: String passwd, ClassLoader cl) throws Exception {
466: try {
467: return FlatfileDBConnectionFactory.getInstance()
468: .getConnection(jdbcUrl, uid, passwd, cl);
469: } catch (BaseException ex) {
470: Throwable cause = ex.getCause();
471: if (cause == null) {
472: cause = ex;
473: }
474: throw new Exception(cause);
475: }
476: }
477:
478: public String getJDBCDriverClassName() throws Exception {
479: return FlatfileDBConnectionFactory.getInstance()
480: .getDriverClassName();
481: }
482:
483: public List getJDBCDriverClassNames() throws Exception {
484: return DRIVER_LIST;
485: }
486:
487: public int getJDBCDriverType() throws Exception {
488: return DRIVER_TYPE;
489: }
490:
491: public int getJDBCDriverTypes(String arg0) throws Exception {
492: return DRIVER_TYPE;
493: }
494:
495: public int getMajorVersion() {
496: return majorVersion;
497: }
498:
499: public int getMicroVersion() {
500: return microVersion;
501: }
502:
503: public int getMinorVersion() {
504: return minorVersion;
505: }
506:
507: /**
508: * Gets repository object, if any, providing underlying data for this DatabaseModel
509: * implementation.
510: *
511: * @return RepositoryObject hosting this object's metadata, or null if data are not
512: * held by a Object.
513: */
514: public ETLObject getSource() {
515: return source;
516: }
517:
518: public String getVersionString() {
519: return majorVersion + "." + minorVersion + "." + microVersion;
520: }
521:
522: /**
523: * Overrides default implementation to compute hashCode value for those members used
524: * in equals() for comparison.
525: *
526: * @return hash code for this object
527: * @see java.lang.Object#hashCode
528: */
529: public int hashCode() {
530: int myHash = (name != null) ? name.hashCode() : 0;
531:
532: // myHash += (connectionName != null) ? connectionName.hashCode() : 0;
533: myHash += (connectionDefinition != null) ? connectionDefinition
534: .hashCode() : 0;
535:
536: if (tables != null) {
537: myHash += tables.keySet().hashCode();
538: }
539:
540: return myHash;
541: }
542:
543: /**
544: * Unmarshal this object from XML element.
545: *
546: * @param xmlElement
547: */
548: public void parseXML(Element xmlElement) throws BaseException {
549: /*
550: * In order to be compliant with legacy JIBX generated XML following structure
551: * needs to be maintained. <FlatfileDatabaseModel majorVersion="5" .....> <map
552: * size="3"> // tables <entry key="PQ_EMPLOYEE_CSV"> <FlatfileTable
553: * name="PQ_EMPLOYEE_CSV" encoding="US-ASCII .... </FlatfileTable> </entry> </map>
554: * <connectionDefinition name="FlatfileDB" .../> </FlatfileDatabaseModel>
555: * </pre>
556: */
557: Element tmpElement = null;
558: parseAttributes(xmlElement);
559:
560: NodeList childNodes = xmlElement.getChildNodes();
561: int length = childNodes.getLength();
562: // "map"
563: for (int i = 0; i < length; i++) {
564: if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
565: tmpElement = (Element) (childNodes.item(i));
566:
567: if ("map".equals(tmpElement.getNodeName())) {
568: parseTablesMap(tmpElement);
569: }
570:
571: if (TAG_CONNECTION_DEFINITION.equals(tmpElement
572: .getNodeName())) {
573: this .connectionDefinition = new FlatfileDBConnectionDefinitionImpl();
574: ((FlatfileDBConnectionDefinition) connectionDefinition)
575: .parseXML(tmpElement);
576: }
577: }
578: }
579: }
580:
581: /**
582: * Setter for FlatfileDBConnectionDefinition
583: *
584: * @param theConnectionDefinition to be set
585: */
586: public void setConnectionDefinition(
587: DBConnectionDefinition theConnectionDefinition) {
588: this .connectionDefinition = theConnectionDefinition;
589: }
590:
591: /**
592: * Sets the Connection Name associated with connection name
593: *
594: * @param theConName associated with this DataSource
595: */
596: public void setConnectionName(String theConName) {
597: this .connectionName = theConName;
598: }
599:
600: /**
601: * Sets repository object, if any, providing underlying data for this DatabaseModel
602: * implementation.
603: *
604: * @param obj Object hosting this object's metadata, or null if data are not
605: * held by a Object.
606: */
607: public void setSource(FlatfileDefinition obj) {
608: source = obj;
609: }
610:
611: // Methods to enable Binding
612: // Castor expects these setters to be present
613: /**
614: * Setter for tables
615: *
616: * @param theTables to be part of Model
617: */
618: public void setTables(Map theTables) {
619: this .tables = theTables;
620: }
621:
622: /**
623: * Overrides default implementation to return name of this DatabaseModel.
624: *
625: * @return model name.
626: */
627: public String toString() {
628: // return getModelName();
629: return getFullyQualifiedName();
630: }
631:
632: /**
633: * Marshall this object to XML string.
634: *
635: * @param prefix
636: * @return XML string
637: */
638: public String toXMLString(String prefix) throws BaseException {
639: if (prefix == null) {
640: prefix = "";
641: }
642: StringBuilder sb = new StringBuilder(prefix);
643: sb.append(XML_DOC_HEADER);
644: sb.append(prefix);
645: sb.append("<");
646: sb.append(TAG_MODEL);
647: sb.append(getAttributeNameValues());
648: sb.append(">\n");
649: sb.append(getXMLTablesMap(prefix + TAB));
650: sb.append(getXMLConnectionDefition(prefix + TAB));
651: sb.append(prefix);
652: sb.append("</");
653: sb.append(TAG_MODEL);
654: sb.append(">\n");
655:
656: return sb.toString();
657: }
658:
659: /**
660: * Sets major version number.
661: *
662: * @param newMajor new major version number
663: */
664: void setMajorVersion(int newMajor) {
665: majorVersion = newMajor;
666: }
667:
668: /**
669: * Sets micro version number.
670: *
671: * @param newMicro new micro version number
672: */
673: void setMicroVersion(int newMicro) {
674: microVersion = newMicro;
675: }
676:
677: /**
678: * Sets minor version number.
679: *
680: * @param newMinor new minor version number
681: */
682: void setMinorVersion(int newMinor) {
683: minorVersion = newMinor;
684: }
685:
686: protected void parseAttributes(Element xmlElement) {
687: Map atts = TagParserUtility.getNodeAttributes(xmlElement);
688: String str = (String) atts.get(ATTR_MAJOR_VERSION);
689: if (str != null) {
690: try {
691: majorVersion = Integer.parseInt(str);
692: } catch (Exception ex) {
693: mLogger.infoNoloc(mLoc.t("PRSR068: LOG_CATEGORY {0}",
694: ATTR_MAJOR_VERSION), ex);
695: }
696: }
697:
698: str = (String) atts.get(ATTR_MINOR_VERSION);
699: if (str != null) {
700: try {
701: minorVersion = Integer.parseInt(str);
702: } catch (Exception ex) {
703: mLogger.infoNoloc(mLoc.t("PRSR069: LOG_CATEGORY {0}",
704: ATTR_MINOR_VERSION), ex);
705: }
706: }
707:
708: str = (String) atts.get(ATTR_MICRO_VERSION);
709: if (str != null) {
710: try {
711: microVersion = Integer.parseInt(str);
712: } catch (Exception ex) {
713: mLogger.infoNoloc(mLoc.t("PRSR070: LOG_CATEGORY {0}",
714: ATTR_MICRO_VERSION), ex);
715: }
716: }
717:
718: name = (String) atts.get(ATTR_NAME);
719: }
720:
721: protected void parseTablesMap(Element mapNode) throws BaseException {
722: // All child "entry" elements under "map" element
723: NodeList mapEntryNodeList = mapNode.getChildNodes();
724: Element entry = null;
725: Element tableElement = null;
726: FlatfileDBTableImpl table = null;
727: NodeList tableNodeList = null;
728:
729: int length = mapEntryNodeList.getLength();
730: for (int i = 0; i < length; i++) {
731: if (mapEntryNodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
732: entry = (Element) mapEntryNodeList.item(i);
733: table = new FlatfileDBTableImpl();
734: tableNodeList = entry
735: .getElementsByTagName(TAG_STCDB_TABLE);
736:
737: // Expect one child Entry
738: tableElement = (Element) (tableNodeList.item(0));
739: table.setParent(this );
740: table.parseXML(tableElement);
741: tables.put(table.getName(), table);
742: }
743: }
744: }
745:
746: private void copyConnectionDefinitionFrom(DatabaseModel src) {
747: DBConnectionDefinition connDef = src.getConnectionDefinition();
748: if (connDef instanceof FlatfileDBConnectionDefinition) {
749: connectionDefinition = new FlatfileDBConnectionDefinitionImpl(
750: (FlatfileDBConnectionDefinition) src
751: .getConnectionDefinition());
752: } else {
753: connectionDefinition = (connDef != null) ? new FlatfileDBConnectionDefinitionImpl(
754: connDef.getName(), connDef.getDriverClass(),
755: connDef.getConnectionURL(), connDef.getUserName(),
756: connDef.getPassword(), connDef.getDescription())
757: : new FlatfileDBConnectionDefinitionImpl(src
758: .getModelName(),
759: FlatfileDBConnectionFactory.DRIVER_NAME,
760: null, "", "", src.getModelName());
761: }
762: }
763:
764: private void copyPrimitivesFrom(DatabaseModel src) {
765: name = src.getModelName();
766: description = src.getModelDescription();
767: source = (FlatfileDefinition) src.getSource();
768: }
769:
770: private void copyTablesFrom(DatabaseModel src) {
771: tables.clear();
772: List srcTables = src.getTables();
773:
774: if (srcTables != null) {
775: Iterator iter = srcTables.iterator();
776: while (iter.hasNext()) {
777: DBTable tbl = (DBTable) iter.next();
778:
779: FlatfileDBTableImpl newTable = null;
780: if (tbl instanceof FlatfileDBTableImpl) {
781: newTable = (FlatfileDBTableImpl) ((FlatfileDBTable) tbl)
782: .clone();
783: } else {
784: newTable = new FlatfileDBTableImpl(tbl);
785: }
786:
787: addTable(newTable);
788: }
789: }
790: }
791:
792: private String getAttributeNameValues() {
793: StringBuilder sb = new StringBuilder(" ");
794: sb.append(ATTR_MAJOR_VERSION);
795: sb.append(EQUAL_START_QUOTE);
796: sb.append(this .majorVersion);
797: sb.append(END_QUOTE_SPACE);
798: sb.append(ATTR_MINOR_VERSION);
799: sb.append(EQUAL_START_QUOTE);
800: sb.append(this .minorVersion);
801: sb.append(END_QUOTE_SPACE);
802: sb.append(ATTR_MICRO_VERSION);
803: sb.append(EQUAL_START_QUOTE);
804: sb.append(this .microVersion);
805: sb.append(END_QUOTE_SPACE);
806: sb.append(ATTR_NAME);
807: sb.append(EQUAL_START_QUOTE);
808: sb.append(this .name);
809: sb.append(QUOTE);
810: return sb.toString();
811: }
812:
813: private String getFullyQualifiedName() {
814: return this .getModelName();
815: }
816:
817: private String getXMLConnectionDefition(String prefix) {
818: if (this .connectionDefinition != null) {
819: return ((FlatfileDBConnectionDefinition) connectionDefinition)
820: .toXMLString(prefix);
821: } else {
822: return "";
823: }
824: }
825:
826: private String getXMLTableMapEntries(String prefix)
827: throws BaseException {
828: StringBuilder sb = new StringBuilder();
829: FlatfileDBTable table = null;
830: if ((this .tables != null) && (this .tables.size() > 0)) {
831: Iterator itr = tables.keySet().iterator();
832: String key = null;
833: while (itr.hasNext()) {
834: key = (String) itr.next();
835: table = (FlatfileDBTable) tables.get(key);
836: sb.append(prefix);
837: sb.append("<entry key=\"");
838: sb.append(table.getName()); // Incase User changed the default table name
839: sb.append("\">\n");
840: sb.append(table.toXMLString(prefix + TAB));
841: sb.append(prefix);
842: sb.append("</entry>\n");
843: }
844: }
845:
846: return sb.toString();
847: }
848:
849: private String getXMLTablesMap(String prefix) throws BaseException {
850: StringBuilder sb = new StringBuilder(prefix);
851: sb.append("<map size=\"");
852: sb.append(this .tables.size());
853: sb.append("\">\n");
854: sb.append(getXMLTableMapEntries(prefix + TAB));
855: sb.append(prefix);
856: sb.append("</map>\n");
857: return sb.toString();
858: }
859:
860: /**
861: * Gets Flatfile instance, if any, whose table name matches the given String.
862: *
863: * @param tableName table name to search for
864: * @return matching instance, if any, or null if no Flatfile matches
865: * <code>aName</code>
866: */
867: public FlatfileDBTable getFileMatchingTableName(String tableName) {
868: if (tableName == null) {
869: return null;
870: }
871:
872: Iterator iter = getTables().iterator();
873: while (iter.hasNext()) {
874: FlatfileDBTable file = (FlatfileDBTable) iter.next();
875: if (tableName.equals(file.getTableName())) {
876: return file;
877: }
878: }
879:
880: return null;
881: }
882:
883: /**
884: * Gets Flatfile instance, if any, whose file name matches the given String
885: *
886: * @param aName file name to search for
887: * @return matching instance, if any, or null if no Flatfile matches
888: * <code>aName</code>
889: */
890: public FlatfileDBTable getFileMatchingFileName(String aName) {
891: if (aName == null) {
892: return null;
893: }
894:
895: Iterator iter = getTables().iterator();
896: while (iter.hasNext()) {
897: FlatfileDBTable file = (FlatfileDBTable) iter.next();
898: if (aName.equals(file.getFileName())) {
899: return file;
900: }
901: }
902:
903: return null;
904: }
905: }
|