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.utils.MessageManager;
026: import org.netbeans.modules.sql.framework.codegen.DB;
027: import org.netbeans.modules.sql.framework.codegen.DBFactory;
028: import org.netbeans.modules.sql.framework.codegen.StatementContext;
029: import org.netbeans.modules.sql.framework.model.SQLConstants;
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.sql.framework.exception.BaseException;
037: import net.java.hulp.i18n.Logger;
038: import org.netbeans.modules.etl.logger.Localizer;
039: import org.netbeans.modules.etl.logger.LogUtil;
040:
041: /**
042: * Strategy: If extractionRequired is true we will extract all the source table to target
043: * database and then do the transformation. If extractionRequired is false we assume
044: * source tables and target table are from same database so we directly jump to
045: * transformation.
046: * <p>
047: * Note: In some cases where the source tables and target table are flat file we will also
048: * jump directly transformation, but the caller has to make sure the table names are
049: * unique since there might be situation where two Database was created and both have same
050: * table name, in that case we just prefix the table name with database name
051: * <p>
052: *
053: * @author Ahimanikya Satapathy
054: */
055: public class SimpleETLStrategyBuilderImpl extends
056: BaseETLStrategyBuilder {
057:
058: private static final String LOG_CATEGORY = SimpleETLStrategyBuilderImpl.class
059: .getName();
060: private static final String SQL_INDENT = "";
061: private static transient final Logger mLogger = LogUtil
062: .getLogger(SimpleETLStrategyBuilderImpl.class.getName());
063: private static transient final Localizer mLoc = Localizer.get();
064:
065: public SimpleETLStrategyBuilderImpl(ETLScriptBuilderModel model)
066: throws BaseException {
067: super (model);
068: }
069:
070: /**
071: * Before calling apply appropriate applyConnections
072: */
073: public void generateScriptForTable(ETLStrategyBuilderContext context)
074: throws BaseException {
075: mLogger.infoNoloc(mLoc.t(
076: "PRSR009: Looping through target tables:{0}",
077: LOG_CATEGORY));
078: populateInitTask(context.getInitTask(), context
079: .getGlobalCleanUpTask(), context.getTargetTable());
080:
081: checkTargetConnectionDefinition(context);
082:
083: createTransformerTask(context);
084:
085: //RIT commented summary table and statistics related code for now
086: // Add statements to create execution summary table if it does not exist, and
087: // update the assocaited execution record upon successful execution
088: addCreateIfNotExistsSummaryTableStatement(context.getInitTask());
089: addUpdateExecutionRecordPreparedStatement(context
090: .getStatsUpdateTask(), context.getTargetTable());
091: }
092:
093: public String getScriptToDisplay(ETLStrategyBuilderContext context)
094: throws BaseException {
095: super .checkTargetConnectionDefinition(context);
096: StringBuilder buffer = new StringBuilder();
097: TargetTable targetTable = context.getTargetTable();
098: DB targetDB = getDBFor(context.getModel()
099: .getConnectionDefinition(targetTable));
100:
101: List sourceTables = context.getTargetTable()
102: .getSourceTableList();
103:
104: String transformSQL = getTransformerSQL(targetTable, targetDB,
105: sourceTables, true);
106: buffer.append(SQL_INDENT).append(
107: getCommentForTransformer(targetTable)).append("\n");
108: buffer.append(transformSQL);
109:
110: return buffer.toString();
111: }
112:
113: protected void createTransformerTask(
114: ETLStrategyBuilderContext context) throws BaseException {
115: TargetTable targetTable = context.getTargetTable();
116: DB targetDB = getDBFor(this .builderModel
117: .getConnectionDefinition(targetTable));
118: DB statsDB = DBFactory.getInstance().getDatabase(DB.AXIONDB);
119: int statementType = targetTable.getStatementType();
120: ETLTaskNode transformerTask = null;
121:
122: MessageManager msgMgr = MessageManager
123: .getManager(ETLTaskNode.class);
124: String displayName = msgMgr.getString("TEMPLATE_dn", msgMgr
125: .getString("LBL_dn_transformer"), context
126: .getTargetTable().getName());
127:
128: // TODO Need to refactor/redesign interfaces between sql-codegen and etl-codgen framework
129: // such that we will avoid code like "IF ELSE" like below.
130: if ((targetDB.getDBType() == DB.JDBCDB)
131: && (targetTable.getSourceTableList().size() != 0)
132: && ((statementType == SQLConstants.UPDATE_STATEMENT) || (statementType == SQLConstants.INSERT_UPDATE_STATEMENT))) {
133: transformerTask = builderModel.getEngine()
134: .createETLTaskNode(
135: ETLEngine.CORRELATED_QUERY_EXECUTOR);
136: transformerTask.addNextETLTaskNode(ETLTask.SUCCESS, context
137: .getNextTaskOnSuccess().getId());
138: transformerTask.addNextETLTaskNode(ETLTask.EXCEPTION,
139: context.getNextTaskOnException().getId());
140:
141: transformerTask.setDisplayName(displayName);
142:
143: if (context.getDependentTasksForNextTask().length() > 0) {
144: context.getDependentTasksForNextTask().append(",");
145: }
146: context.getDependentTasksForNextTask().append(
147: transformerTask.getId());
148:
149: createCorrelatedUpdateSQLParts(
150: targetTable,
151: transformerTask,
152: getTargetConnName(),
153: targetDB,
154: statsDB,
155: (statementType == SQLConstants.INSERT_UPDATE_STATEMENT));
156: } else {
157: // for each target table create a transformer task node
158: transformerTask = builderModel.getEngine()
159: .createETLTaskNode(ETLEngine.TRANSFORMER);
160: transformerTask.addNextETLTaskNode(ETLTask.SUCCESS, context
161: .getNextTaskOnSuccess().getId());
162: transformerTask.addNextETLTaskNode(ETLTask.EXCEPTION,
163: context.getNextTaskOnException().getId());
164:
165: transformerTask.setDisplayName(displayName);
166:
167: if (context.getDependentTasksForNextTask().length() > 0) {
168: context.getDependentTasksForNextTask().append(",");
169: }
170: context.getDependentTasksForNextTask().append(
171: transformerTask.getId());
172:
173: createTransformerSQLPart(targetTable, true,
174: transformerTask, getTargetConnName(), context
175: .getNextTaskOnException(), targetDB,
176: statsDB);
177: }
178:
179: // Reference to collector task node that immediately precedes the set
180: // of transform nodes; ordinarily the extractor wait node, but could
181: // be START if no extractor nodes exist.
182: ETLTaskNode xformPredecessor = context.getPredecessorTask();
183:
184: // Set dependent list for predecessor to transform nodes
185: xformPredecessor.addNextETLTaskNode(ETLTask.SUCCESS,
186: transformerTask.getId());
187:
188: }
189:
190: protected StatementContext getTargetTableBaseContext() {
191: StatementContext sc = new StatementContext();
192: if (ETLScriptBuilderModel.ETL_INSTANCE_DB_CONN_DEF_NAME
193: .equals(getTargetConnName())) {
194: sc.setUsingUniqueTableName(true);
195: }
196: return sc;
197: }
198:
199: @Override
200: protected boolean isExtractionRequired(SourceTable sourceTable,
201: TargetTable targetTable) throws BaseException {
202: return false;
203: }
204: }
|