001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.etl.codegen;
020:
021: import java.io.File;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import org.netbeans.modules.etl.codegen.impl.InternalDBMetadata;
028: import org.netbeans.modules.sql.framework.common.jdbc.SQLDBConnectionDefinition;
029: import org.netbeans.modules.sql.framework.common.jdbc.SQLUtils;
030: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
031: import org.netbeans.modules.sql.framework.model.RuntimeInput;
032: import org.netbeans.modules.sql.framework.model.RuntimeOutput;
033: import org.netbeans.modules.sql.framework.model.SQLDBModel;
034: import org.netbeans.modules.sql.framework.model.SQLDBTable;
035: import org.netbeans.modules.sql.framework.model.SQLDefinition;
036: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
037: import com.sun.etl.engine.ETLEngine;
038: import com.sun.etl.engine.impl.ETLEngineImpl;
039: import com.sun.sql.framework.exception.BaseException;
040: import com.sun.sql.framework.jdbc.DBConstants;
041: import com.sun.sql.framework.utils.StringUtil;
042: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
043: import org.netbeans.modules.sql.framework.model.DBTable;
044: import org.netbeans.modules.sql.framework.model.DatabaseModel;
045:
046: /**
047: * @author Girish Patil
048: * @version $Revision 1.0$
049: */
050: public final class ETLScriptBuilderModel {
051:
052: // Folder under eTL working directory
053: public static final String ETL_DESIGN_WORK_FOLDER = getDefaultWorkingFolder();
054: // Pool name aka connection Definition name are for internal cross reference purposes.
055: public static final String ETL_INSTANCE_DB_CONN_DEF_NAME = "IDB_CONN_DEF";
056: public static final String ETL_INSTANCE_DB_FOLDER = "idb/";
057: public static final String ETL_INSTANCE_DB_NAME = "InstanceDB";
058: public static final String ETL_MONITOR_DB_CONN_DEF_NAME = "MDB_CONN_DEF";
059: public static final String ETL_MONITOR_DB_FOLDER = "mdb/";
060: public static final String ETL_MONITOR_DB_NAME = "MonitorDB";;
061: private boolean connectionDefinitionOverridesApplied = false;
062: private List<SQLDBConnectionDefinition> connectionDefinitions = null;
063: private ETLEngine engine = null;
064: private SQLDBConnectionDefinition instanceDb = null;
065: private String instanceDBFolder = ETL_DESIGN_WORK_FOLDER
066: + ETL_INSTANCE_DB_FOLDER;
067: private String instanceDBName = ETL_INSTANCE_DB_NAME;
068: private boolean memoryMonitorDB = true;
069: private SQLDBConnectionDefinition monitorDb = null;
070: private String monitorDBFolder = ETL_DESIGN_WORK_FOLDER
071: + ETL_MONITOR_DB_FOLDER;
072: private String monitorDBName = ETL_MONITOR_DB_NAME;
073: private Map<String, String> nameToDatabaseMap = null;
074: private Map oidToFFMetadata = new HashMap();
075: private Map<String, SQLDBConnectionDefinition> databaseToConnectionDefintionMap = null;
076: private Map<String, String> dbIdToNameMap = null;
077: private boolean shutdownMonitorDB = false;
078: private SQLDefinition sqlDefinition = null;
079: private boolean useInstanceDB = false;
080: private String workingFolder = ETL_DESIGN_WORK_FOLDER;
081:
082: private static final String getDefaultWorkingFolder() {
083: String nbUsrDir = System.getProperty("netbeans.user");
084: if ((nbUsrDir == null) || ("".equals(nbUsrDir))) {
085: nbUsrDir = ".." + File.separator + "usrdir";
086: }
087: return nbUsrDir + File.separator + "eTL" + File.separator
088: + "work/";
089: }
090:
091: public void applyConnectionDefinitions() throws BaseException {
092: // Get all connection definitions from SQLDefinition.
093: connectionDefinitions = new ArrayList<SQLDBConnectionDefinition>();
094: databaseToConnectionDefintionMap = new HashMap<String, SQLDBConnectionDefinition>();
095: dbIdToNameMap = new HashMap<String, String>();
096: nameToDatabaseMap = new HashMap<String, String>();
097:
098: SQLDBConnectionDefinition dbConnDef = null;
099: String key = null;
100: String qConnDefName = null;
101:
102: List dbModels = sqlDefinition.getAllDatabases();
103: Iterator itr = dbModels.iterator();
104: while (itr.hasNext()) {
105: DatabaseModel dbModel = (DatabaseModel) itr.next();
106: dbConnDef = SQLModelObjectFactory.getInstance()
107: .createDBConnectionDefinition(
108: dbModel.getConnectionDefinition());
109: key = ETLCodegenUtil
110: .getQualifiedObjectId((SQLDBModel) dbModel);
111:
112: if (key != null) {
113: databaseToConnectionDefintionMap.put(key, dbConnDef);
114: } else {
115: key = dbConnDef.getName();
116: databaseToConnectionDefintionMap.put(key, dbConnDef);
117: }
118:
119: dbIdToNameMap.put(key, qConnDefName);
120: nameToDatabaseMap.put(qConnDefName, key);
121: connectionDefinitions.add(dbConnDef);
122: }
123: addMonitorAndInstanceConnectionDefinitions(connectionDefinitions);
124: getEngine().setConnectionDefList(this .connectionDefinitions);
125: }
126:
127: /**
128: * ConnDefName to ConnDef. Note name is may not be same as in Conn Def oid to
129: * connDefName mapping.
130: */
131: public void applyConnectionDefinitions(Map name2connectionDefMap,
132: Map<String, String> connDefNameMap, Map intDbConfigParams)
133: throws BaseException {
134:
135: oidToFFMetadata = intDbConfigParams;
136:
137: nameToDatabaseMap = new HashMap<String, String>();
138: connectionDefinitions = new ArrayList<SQLDBConnectionDefinition>();
139:
140: databaseToConnectionDefintionMap = new HashMap<String, SQLDBConnectionDefinition>();
141: dbIdToNameMap = new HashMap<String, String>();
142:
143: nameToDatabaseMap = transposeMap(connDefNameMap);
144: addMonitorAndInstanceConnectionDefinitions(connectionDefinitions);
145:
146: Iterator itr = name2connectionDefMap.entrySet().iterator();
147: while (itr.hasNext()) {
148: Map.Entry entry = (Map.Entry) itr.next();
149: String connDefName = (String) entry.getKey();
150: SQLDBConnectionDefinition connDef = (SQLDBConnectionDefinition) entry
151: .getValue();
152: String qualifiedOid = nameToDatabaseMap.get(connDefName);
153:
154: if (SQLUtils.getSupportedDBType(connDef.getDBType()) == DBConstants.AXION) {
155: if (qualifiedOid != null) {
156: nameToDatabaseMap.remove(connDefName);
157: nameToDatabaseMap
158: .put(ETL_INSTANCE_DB_CONN_DEF_NAME,
159: qualifiedOid);
160: connDefNameMap.remove(qualifiedOid);
161: connDefNameMap.put(qualifiedOid,
162: ETL_INSTANCE_DB_CONN_DEF_NAME);
163: databaseToConnectionDefintionMap.put(qualifiedOid,
164: instanceDb);
165: }
166: itr.remove();
167: } else {
168: connectionDefinitions.add(connDef);
169: databaseToConnectionDefintionMap.put(qualifiedOid,
170: connDef);
171: }
172: }
173:
174: setOidToFFMetadataMap(intDbConfigParams);
175:
176: getEngine().setConnectionDefList(this .connectionDefinitions);
177: this .connectionDefinitionOverridesApplied = true;
178: }
179:
180: public void buildRuntimeDatabaseModel() {
181: // Create runtime attributes from runtime input, output tables (if they exist).
182: RuntimeDatabaseModel runtimeModel = this .sqlDefinition
183: .getRuntimeDbModel();
184: if (runtimeModel != null) {
185: RuntimeInput inputTable = runtimeModel.getRuntimeInput();
186: if (inputTable != null) {
187: getEngine().setInputAttrMap(
188: inputTable.getRuntimeAttributeMap());
189: }
190:
191: RuntimeOutput outputTable = runtimeModel.getRuntimeOutput();
192: if (outputTable != null) {
193: getEngine().setOutputAttrMap(
194: outputTable.getRuntimeAttributeMap());
195: }
196: }
197: }
198:
199: public DBConnectionDefinition getConnectionDefinition(DBTable table)
200: throws BaseException {
201: return table.getParent().getConnectionDefinition();
202: }
203:
204: public DBConnectionDefinition getConnectionDefinition(String name)
205: throws BaseException {
206: return (DBConnectionDefinition) databaseToConnectionDefintionMap
207: .get(name);
208: }
209:
210: public List getConnectionDefinitions() {
211: return this .connectionDefinitions;
212: }
213:
214: public ETLEngine getEngine() {
215: if (engine == null) {
216: engine = new ETLEngineImpl();
217: }
218: return engine;
219: }
220:
221: public String getInstanceDBFolder() {
222: return this .instanceDBFolder;
223: }
224:
225: public String getInstanceDBName() {
226: return this .instanceDBName;
227: }
228:
229: public InternalDBMetadata getInternalMetadata(DBTable tTable)
230: throws BaseException {
231: SQLDBModel element = (SQLDBModel) tTable.getParent();
232: if (element.getAttribute("refKey") == null) {
233: return null;
234: }
235: String oid = (String) element.getAttribute("refKey")
236: .getAttributeValue();
237: return (InternalDBMetadata) this .oidToFFMetadata.get(oid);
238: }
239:
240: public String getMonitorDBFolder() {
241: return this .monitorDBFolder;
242: }
243:
244: public String getMonitorDBName() {
245: return this .monitorDBName;
246: }
247:
248: public SQLDefinition getSqlDefinition() {
249: return this .sqlDefinition;
250: }
251:
252: public String getWorkingFolder() {
253: return this .workingFolder;
254: }
255:
256: public boolean isConnectionDefinitionOverridesApplied() {
257: return this .connectionDefinitionOverridesApplied;
258: }
259:
260: public boolean isMemoryMonitorDB() {
261: return this .memoryMonitorDB;
262: }
263:
264: public boolean isShutdownMonitorDB() {
265: return this .shutdownMonitorDB;
266: }
267:
268: public boolean isUseInstanceDB() {
269: return this .useInstanceDB;
270: }
271:
272: public void setConnectionDefinitionOverridesApplied(
273: boolean connectionDefinitionOverridesApplied) {
274: this .connectionDefinitionOverridesApplied = connectionDefinitionOverridesApplied;
275: }
276:
277: public void setConnectionDefinitions(
278: List<SQLDBConnectionDefinition> connectionDefinitions) {
279: this .connectionDefinitions = connectionDefinitions;
280: }
281:
282: public void setEngine(ETLEngine engine) {
283: this .engine = engine;
284: }
285:
286: public void setInstanceDBFolder(String instanceDBFolder) {
287: this .instanceDBFolder = instanceDBFolder;
288: }
289:
290: public void setInstanceDBName(String instanceDBName) {
291: this .instanceDBName = instanceDBName;
292: }
293:
294: public void setMemoryMonitorDB(boolean memoryMonitorDB) {
295: this .memoryMonitorDB = memoryMonitorDB;
296: }
297:
298: public void setMonitorDBFolder(String monitiorDBFolder) {
299: this .monitorDBFolder = monitiorDBFolder;
300: }
301:
302: public void setMonitorDBName(String monitorDBName) {
303: this .monitorDBName = monitorDBName;
304: }
305:
306: public void setOidToFFMetadataMap(Map dbToMetadata) {
307: //this.oidToFFMetadata.clear();
308: //this.oidToFFMetadata.putAll(dbToMetadata);
309: }
310:
311: public void setShutdownMonitorDB(boolean shutdownMonitorDB) {
312: this .shutdownMonitorDB = shutdownMonitorDB;
313: }
314:
315: public void setSqlDefinition(SQLDefinition sqlDefinition) {
316: this .sqlDefinition = sqlDefinition;
317: }
318:
319: public void setUseInstanceDB(boolean useInstanceDB) {
320: this .useInstanceDB = useInstanceDB;
321: }
322:
323: public void setWorkingFolder(String workingFolder) {
324: this .workingFolder = workingFolder;
325: }
326:
327: protected InternalDBMetadata getInternalMetadataFor(SQLDBTable table)
328: throws BaseException {
329: return (InternalDBMetadata) getOidToFFMetadataMap().get(
330: table.getName());
331: }
332:
333: private void addMonitorAndInstanceConnectionDefinitions(
334: List<SQLDBConnectionDefinition> connDefs)
335: throws BaseException {
336: DBConnectionDefinitionTemplate connTemplate = new DBConnectionDefinitionTemplate();
337: Map<String, String> args = new HashMap<String, String>(1);
338:
339: if (this .memoryMonitorDB) {
340: monitorDb = connTemplate
341: .getDBConnectionDefinition("AXIONMEMORYDB");
342: } else {
343: monitorDb = connTemplate
344: .getDBConnectionDefinition("STCDBADAPTER");
345: }
346:
347: // KEY_METADATA_DIR entry is ignored for memoryDatabase
348: args.put(DBConnectionDefinitionTemplate.KEY_DATABASE_NAME,
349: monitorDBName);
350: args.put(DBConnectionDefinitionTemplate.KEY_METADATA_DIR,
351: monitorDBFolder);
352: monitorDb.setConnectionURL(StringUtil.replace(monitorDb
353: .getConnectionURL(), args));
354: monitorDb.setName(ETL_MONITOR_DB_CONN_DEF_NAME);
355: this .databaseToConnectionDefintionMap.put(
356: ETL_MONITOR_DB_CONN_DEF_NAME, monitorDb);
357: connDefs.add(monitorDb);
358:
359: if (this .useInstanceDB) {
360: args.clear();
361: args.put(DBConnectionDefinitionTemplate.KEY_DATABASE_NAME,
362: instanceDBName);
363: args.put(DBConnectionDefinitionTemplate.KEY_METADATA_DIR,
364: instanceDBFolder);
365:
366: instanceDb = connTemplate
367: .getDBConnectionDefinition("STCDBADAPTER");
368: instanceDb.setConnectionURL(StringUtil.replace(instanceDb
369: .getConnectionURL(), args));
370: instanceDb
371: .setName(ETLScriptBuilderModel.ETL_INSTANCE_DB_CONN_DEF_NAME);
372: this .databaseToConnectionDefintionMap
373: .put(
374: ETLScriptBuilderModel.ETL_INSTANCE_DB_CONN_DEF_NAME,
375: instanceDb);
376: connDefs.add(instanceDb);
377: }
378: }
379:
380: private Map getOidToFFMetadataMap() {
381: return this .oidToFFMetadata;
382: }
383:
384: private Map<String, String> transposeMap(Map origMap) {
385: Map<String, String> trans = new HashMap<String, String>();
386:
387: if (origMap != null) {
388: Iterator itr = origMap.entrySet().iterator();
389: while (itr.hasNext()) {
390: Map.Entry mapEntry = (Map.Entry) itr.next();
391: trans.put((String) mapEntry.getValue(),
392: (String) mapEntry.getKey());
393: }
394: }
395: return trans;
396: }
397: }
|