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.impl;
020:
021: import java.util.List;
022:
023: import org.netbeans.modules.etl.codegen.ETLScriptBuilderModel;
024: import org.netbeans.modules.etl.codegen.ETLStrategyBuilderContext;
025: import org.netbeans.modules.etl.codegen.PatternFinder;
026: import org.netbeans.modules.etl.utils.MessageManager;
027: import org.netbeans.modules.sql.framework.codegen.DB;
028: import org.netbeans.modules.sql.framework.codegen.DBFactory;
029: import org.netbeans.modules.sql.framework.codegen.StatementContext;
030: import org.netbeans.modules.sql.framework.model.SourceTable;
031: import org.netbeans.modules.sql.framework.model.TargetTable;
032:
033: import com.sun.etl.engine.ETLEngine;
034: import com.sun.etl.engine.ETLTask;
035: import com.sun.etl.engine.ETLTaskNode;
036: import com.sun.etl.engine.impl.Extractor;
037: import com.sun.sql.framework.exception.BaseException;
038: import com.sun.sql.framework.jdbc.DBConstants;
039: import com.sun.sql.framework.jdbc.SQLPart;
040: import com.sun.sql.framework.utils.AttributeMap;
041: import net.java.hulp.i18n.Logger;
042: import org.netbeans.modules.etl.logger.Localizer;
043: import org.netbeans.modules.etl.logger.LogUtil;
044: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
045:
046: /**
047: * If all source table are from same DB and statement type is Insert
048: *
049: * @author Ahimanikya Satapathy
050: * @version $Revision$
051: */
052: public class OnePassETLStrategyBuilderImpl extends
053: BaseETLStrategyBuilder {
054:
055: private static final String LOG_CATEGORY = OnePassETLStrategyBuilderImpl.class
056: .getName();
057: private static transient final Logger mLogger = LogUtil
058: .getLogger(OnePassETLStrategyBuilderImpl.class.getName());
059: private static transient final Localizer mLoc = Localizer.get();
060:
061: public OnePassETLStrategyBuilderImpl(ETLScriptBuilderModel model)
062: throws BaseException {
063: super (model);
064: }
065:
066: public void createTransformerTask(ETLStrategyBuilderContext context)
067: throws BaseException {
068: DBConnectionDefinition targetConnDef = context.getModel()
069: .getConnectionDefinition(context.getTargetTable());
070: DB targetDB = this .getDBFor(targetConnDef);
071: MessageManager msgMgr = MessageManager
072: .getManager(ETLTaskNode.class);
073: ETLTaskNode onePassETLNode = context.getModel().getEngine()
074: .createETLTaskNode(ETLEngine.EXTRACTOR);
075: String displayName = msgMgr.getString("TEMPLATE_dn", msgMgr
076: .getString("LBL_dn_onepass"), context.getTargetTable()
077: .getName());
078: onePassETLNode.setDisplayName(displayName);
079:
080: context.getPredecessorTask().addNextETLTaskNode(
081: ETLTask.SUCCESS, onePassETLNode.getId());
082:
083: TargetTable tgtTable = context.getTargetTable();
084: StatementContext tgtStmtContext = new StatementContext();
085: if ((this .builderModel.isConnectionDefinitionOverridesApplied())
086: && (PatternFinder.isInternalDBTable(tgtTable))) {
087: tgtStmtContext.setUsingUniqueTableName(tgtTable, true);
088: }
089: createTargetTableIfNotExists(context.getTargetTable(),
090: onePassETLNode, targetConnDef.getName(), targetDB,
091: tgtStmtContext);
092: truncateTargetTableIfExists(context.getTargetTable(),
093: onePassETLNode, targetConnDef.getName(), targetDB
094: .getStatements(), tgtStmtContext);
095:
096: List sourceTables = context.getTargetTable()
097: .getSourceTableList();
098: if (sourceTables != null && !sourceTables.isEmpty()) {
099: StatementContext stmtContext = new StatementContext();
100: SourceTable srcTable = (SourceTable) sourceTables.get(0);
101: DBConnectionDefinition srcConDef = context.getModel()
102: .getConnectionDefinition(srcTable);
103: String sourceConnName = getConDefnName(srcConDef);
104: int dbType = connFactory
105: .getDatabaseVersion(BaseETLStrategyBuilder
106: .getConnectionPropertiesFrom(srcConDef));
107:
108: DB db = DBFactory.getInstance().getDatabase(dbType);
109:
110: if ((this .builderModel
111: .isConnectionDefinitionOverridesApplied())
112: && (dbType == DBConstants.AXION)) {
113: stmtContext.setUsingUniqueTableName(true);
114: }
115:
116: SQLPart selectPart = db.getStatements()
117: .getOnePassSelectStatement(tgtTable, stmtContext);
118: stmtContext.setUsingUniqueTableName(false);
119: selectPart.setConnectionPoolName(sourceConnName);
120: onePassETLNode.addStatement(selectPart);
121:
122: // Insert new execution record for target table, tt, and add query to obtain
123: // execution id assigned to new execution record.
124: super .addInsertNewExecutionRecordStatement(onePassETLNode,
125: tgtTable);
126: super .addSelectExecutionIdForNewExecutionRecordStatement(
127: onePassETLNode, tgtTable);
128:
129: DB statsDb = DBFactory.getInstance()
130: .getDatabase(DB.AXIONDB);
131: stmtContext.setUsingFullyQualifiedTablePrefix(false);
132: stmtContext.setUsingUniqueTableName(true);
133: String statsTable = statsDb.getUnescapedName(statsDb
134: .getGeneratorFactory().generate(tgtTable,
135: stmtContext));
136: onePassETLNode.setTableName(statsTable); //NOI18N
137: } else {
138: throw new BaseException(
139: "Must have source table for One-Pass eTL Strategy.");
140: }
141:
142: SQLPart insertPart = targetDB.getStatements()
143: .getPreparedInsertStatement(tgtTable, tgtStmtContext);
144: insertPart.setConnectionPoolName(getTargetConnName());
145: onePassETLNode.addStatement(insertPart);
146:
147: onePassETLNode.addNextETLTaskNode(ETLTask.SUCCESS, context
148: .getNextTaskOnSuccess().getId());
149: onePassETLNode.addNextETLTaskNode(ETLTask.EXCEPTION, context
150: .getNextTaskOnException().getId());
151:
152: if (context.getDependentTasksForNextTask().length() > 0) {
153: context.getDependentTasksForNextTask().append(",");
154: }
155: context.getDependentTasksForNextTask().append(
156: onePassETLNode.getId());
157:
158: AttributeMap attrMap = new AttributeMap();
159: attrMap.put("batchSize", context.getTargetTable()
160: .getBatchSize()
161: + ""); //NOI18N
162:
163: // Set flag in extractor indicating it is operating in one-pass mode.
164: attrMap.put(Extractor.KEY_ISONEPASS, Boolean.TRUE);
165:
166: onePassETLNode.setAttributeMap(attrMap);
167: }
168:
169: /**
170: * Before calling apply appropriate applyConnections
171: */
172: public void generateScriptForTable(ETLStrategyBuilderContext context)
173: throws BaseException {
174: mLogger.infoNoloc(mLoc.t(
175: "PRSR005: Looping through target tables:{0}",
176: LOG_CATEGORY));
177: populateInitTask(context.getInitTask(), context
178: .getGlobalCleanUpTask(), context.getTargetTable());
179:
180: // Create cleanup task for this execution thread.
181: ETLTaskNode threadCleanupTask = context
182: .getNextTaskOnException();
183:
184: // Create commit node to collect transformer connections and commit/close
185: // them.
186: ETLTaskNode commitTask = context.getNextTaskOnSuccess();
187:
188: checkTargetConnectionDefinition(context);
189:
190: createTransformerTask(context);
191:
192: // Add statements to create execution summary table if it does not exist, and
193: // update the associated execution record upon successful execution
194: addCreateIfNotExistsSummaryTableStatement(context.getInitTask());
195: addUpdateExecutionRecordPreparedStatement(context
196: .getStatsUpdateTask(), context.getTargetTable());
197:
198: // --------------------------------------------------------------------
199: commitTask.addNextETLTaskNode(ETLTask.SUCCESS,
200: threadCleanupTask.getId());
201: commitTask.addNextETLTaskNode(ETLTask.EXCEPTION,
202: threadCleanupTask.getId());
203: }
204:
205: public String getScriptToDisplay(ETLStrategyBuilderContext context)
206: throws BaseException {
207: super .checkTargetConnectionDefinition(context);
208: StringBuilder buffer = new StringBuilder();
209:
210: TargetTable tgtTable = context.getTargetTable();
211: List sourceTables = tgtTable.getSourceTableList();
212:
213: // generate one-pass select part.
214: if (sourceTables != null && !sourceTables.isEmpty()) {
215: SourceTable srcTable = (SourceTable) sourceTables.get(0);
216: DBConnectionDefinition srcConDefn = this .builderModel
217: .getConnectionDefinition(srcTable);
218:
219: String modelName = srcTable.getParent().getModelName();
220: buffer.append(
221: MSG_MGR.getString("DISPLAY_SELECT", srcTable
222: .getName(), modelName, srcConDefn
223: .getDBType())).append("\n");
224:
225: int dbType = connFactory
226: .getDatabaseVersion(BaseETLStrategyBuilder
227: .getConnectionPropertiesFrom(srcConDefn));
228: DB db = DBFactory.getInstance().getDatabase(dbType);
229: StatementContext stmtContext = new StatementContext();
230: SQLPart selectPart = db.getStatements()
231: .getOnePassSelectStatement(tgtTable, stmtContext);
232:
233: buffer.append(selectPart.getSQL());
234: buffer.append("\n\n");
235: }
236:
237: // generate one-pass insert/load part.
238: StatementContext stmtContext = new StatementContext();
239: SQLPart insertPart = getStatementsForTargetTableDB(context)
240: .getPreparedInsertStatement(tgtTable, stmtContext);
241:
242: if (insertPart != null) {
243: buffer.append(getCommentForTransformer(tgtTable)).append(
244: "\n");
245: buffer.append(insertPart.getSQL());
246: buffer.append("\n\n");
247: }
248: return buffer.toString();
249: }
250: }
|