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: import java.util.Map;
023:
024: import org.netbeans.modules.etl.codegen.ETLScriptBuilderModel;
025: import org.netbeans.modules.etl.codegen.ETLStrategyBuilderContext;
026: import org.netbeans.modules.sql.framework.common.jdbc.SQLDBConnectionDefinition;
027: import org.netbeans.modules.sql.framework.common.utils.MonitorUtil;
028: import org.netbeans.modules.sql.framework.codegen.StatementContext;
029: import org.netbeans.modules.sql.framework.model.SQLDBTable;
030: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
031: import org.netbeans.modules.sql.framework.model.TargetTable;
032:
033: import com.sun.etl.engine.ETLEngine;
034: import com.sun.etl.engine.ETLTaskNode;
035: import com.sun.sql.framework.exception.BaseException;
036: import com.sun.sql.framework.jdbc.SQLPart;
037: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
038:
039: /**
040: * Extends base pipelining strategy builder to override methods which affect conduct and
041: * execution of a validating collaboration.
042: *
043: * @author Jonathan Giron
044: * @version $Revision$
045: */
046: public class ValidatingStrategyBuilderImpl extends
047: PipelinedStrategyBuilderImpl {
048: public ValidatingStrategyBuilderImpl(ETLScriptBuilderModel model)
049: throws BaseException {
050: super (model);
051: }
052:
053: /**
054: * Adds statement, if appropriate, to the given ETLTaskNode in order to obtain count
055: * of rejected rows from rejection details table.
056: *
057: * @param updateStats ETLTaskNode to receive new statement
058: * @param tt TargetTable instance from which rejected row table information is derived
059: * @throws BaseException if error occurs during statement generation
060: */
061: @Override
062: public void addSelectRejectedRowCountStatement(
063: ETLTaskNode updateStats, TargetTable tt)
064: throws BaseException {
065: StatementContext context = new StatementContext();
066: context.setUsingFullyQualifiedTablePrefix(false);
067: context.setUsingUniqueTableName(true);
068:
069: // Add SelectRejectedRowsCountFromDetailsTableStatement
070: SQLPart selectRejectedCountPart = pipelineStmts
071: .getSelectRejectedRowsCountFromDetailsTableStatement(tt);
072: selectRejectedCountPart
073: .setConnectionPoolName(ETLScriptBuilderModel.ETL_MONITOR_DB_CONN_DEF_NAME);
074:
075: String tableName = db.getUnescapedName(db.getGeneratorFactory()
076: .generate(tt, context));
077: selectRejectedCountPart.setTableName(tableName);
078: updateStats.addTableSpecificStatement(tableName,
079: selectRejectedCountPart);
080: }
081:
082: @Override
083: public String getScriptToDisplay(ETLStrategyBuilderContext context)
084: throws BaseException {
085: super .checkTargetConnectionDefinition(context);
086:
087: StringBuilder buffer = new StringBuilder();
088: TargetTable targetTable = context.getTargetTable();
089:
090: StatementContext stmtContext = new StatementContext();
091: stmtContext.setUsingFullyQualifiedTablePrefix(false);
092:
093: DBConnectionDefinition tgtConnDef = context.getModel()
094: .getConnectionDefinition(targetTable);
095: buffer.append(MSG_MGR.getString(
096: "DISPLAY_TARGET_LOG_VALIDATING", targetTable
097: .getParent().getModelName(), tgtConnDef
098: .getDBType()));
099: buffer.append("\n");
100:
101: String targetLogSQL = getCreateLogDetailsTableSQL(targetTable);
102: buffer.append(targetLogSQL).append("\n\n");
103:
104: String transformSQL = createTransformStatement(targetTable,
105: stmtContext).getSQL();
106: buffer.append(getCommentForTransformer(targetTable)).append(
107: "\n");
108: buffer.append(transformSQL);
109:
110: return buffer.toString();
111: }
112:
113: @Override
114: protected void buildCleanupStatements(ETLTaskNode cleanupTask,
115: List targetTables) throws BaseException {
116: super .buildCleanupStatements(cleanupTask, targetTables);
117:
118: // Add statement to shut down the pipelining database.
119: SQLPart shutdownPipeline = new SQLPart("SHUTDOWN",
120: SQLPart.STMT_DEFRAG,
121: ETLScriptBuilderModel.ETL_INSTANCE_DB_CONN_DEF_NAME);
122: cleanupTask.addStatement(shutdownPipeline);
123: }
124:
125: @Override
126: protected void buildInitializationStatements(SQLDBTable table,
127: ETLTaskNode initTask, Map connDefToLinkName)
128: throws BaseException {
129: super .buildInitializationStatements(table, initTask,
130: connDefToLinkName);
131: if (table instanceof TargetTable) {
132: doBuildInitializationStatements((TargetTable) table,
133: initTask, connDefToLinkName);
134: }
135: }
136:
137: /**
138: * Implements construction of appropriate initialization statements for a validating
139: * engine task.
140: *
141: * @param table TargetTable for which to construct initialization statements
142: * @param initTask ETLTaskNode representing initialization node to hold generated
143: * statements
144: * @param connDefToLinkName Map of DBConnectionDefinition instances to DB link names
145: * @param pfGen PipelinedFlowGenerator instance to use in generating statements
146: * @throws BaseException if error occurs during statement generation
147: */
148: protected void doBuildInitializationStatements(TargetTable table,
149: ETLTaskNode initTask, Map connDefToLinkName)
150: throws BaseException {
151: StatementContext localContext = new StatementContext();
152: localContext.setUsingFullyQualifiedTablePrefix(false);
153: localContext.setUsingUniqueTableName(true);
154:
155: TargetTable detailsTable = getLogTableFor(table);
156:
157: if (!this .builderModel.isConnectionDefinitionOverridesApplied()) {
158: SQLPart dropLogPart = new SQLPart(getDropExternalTableSQL(
159: detailsTable, "", true, localContext),
160: SQLPart.STMT_INITIALIZESTATEMENTS,
161: ETLScriptBuilderModel.ETL_MONITOR_DB_CONN_DEF_NAME);
162: initTask.addOptionalTask(dropLogPart);
163: }
164:
165: SQLPart createLogPart = new SQLPart(
166: getCreateLogDetailsTableSQL(table),
167: SQLPart.STMT_INITIALIZESTATEMENTS,
168: ETLScriptBuilderModel.ETL_MONITOR_DB_CONN_DEF_NAME);
169: initTask.addOptionalTask(createLogPart);
170:
171: if (!this .builderModel.isConnectionDefinitionOverridesApplied()) {
172: SQLPart truncateLogPart = stmts.getTruncateStatement(
173: detailsTable, localContext);
174: truncateLogPart
175: .setConnectionPoolName(ETLScriptBuilderModel.ETL_MONITOR_DB_CONN_DEF_NAME);
176: truncateLogPart.setType(SQLPart.STMT_INITIALIZESTATEMENTS);
177: initTask.addOptionalTask(truncateLogPart);
178: }
179:
180: // Create DBLink for monitoring DB
181: DBConnectionDefinition monitorDef = builderModel
182: .getConnectionDefinition(ETLScriptBuilderModel.ETL_MONITOR_DB_CONN_DEF_NAME);
183: String linkName = monitorDef.getName();
184:
185: SQLDBConnectionDefinition linkConnDef = SQLModelObjectFactory
186: .getInstance().createDBConnectionDefinition(monitorDef);
187: linkConnDef.setDriverClass(monitorDef.getDriverClass());
188: SQLPart createMonitorDbLink = new SQLPart(super
189: .getCreateDBLinkSQL(linkConnDef, linkName),
190: SQLPart.STMT_INITIALIZESTATEMENTS,
191: ETLScriptBuilderModel.ETL_INSTANCE_DB_CONN_DEF_NAME);
192: initTask.addOptionalTask(createMonitorDbLink);
193:
194: // Add remote reference to table in monitoring DB from pipeline DB
195: SQLPart createRemoteRefPart = new SQLPart(pipelineStmts
196: .getCreateRemoteLogDetailsTableStatement(table,
197: linkName).getSQL(),
198: SQLPart.STMT_INITIALIZESTATEMENTS,
199: ETLScriptBuilderModel.ETL_INSTANCE_DB_CONN_DEF_NAME);
200: initTask.addOptionalTask(createRemoteRefPart);
201:
202: SQLPart remountPart = new SQLPart(pipelineStmts
203: .getRemountRemoteLogDetailsStatement(detailsTable)
204: .getSQL(), SQLPart.STMT_INITIALIZESTATEMENTS,
205: ETLScriptBuilderModel.ETL_INSTANCE_DB_CONN_DEF_NAME);
206: initTask.addOptionalTask(remountPart);
207: }
208:
209: /**
210: * Overrides parent implementation to indicate that design-time form of the create
211: * statement should be generated.
212: *
213: * @see org.netbeans.modules.etl.codegen.impl.ValidatingStrategyBuilderImpl#getCreateLogDetailsTableSQL(org.netbeans.modules.sql.framework.model.TargetTable)
214: */
215: protected String getCreateLogDetailsTableSQL(TargetTable table)
216: throws BaseException {
217: return pipelineStmts.getCreateLogDetailsTableStatement(table,
218: this .builderModel.isMemoryMonitorDB()).getSQL();
219: }
220:
221: protected TargetTable getLogTableFor(SQLDBTable table) {
222: TargetTable clone = SQLModelObjectFactory.getInstance()
223: .createTargetTable(table);
224: clone.setAliasName(table.getAliasName());
225: clone.setTablePrefix(MonitorUtil.LOG_DETAILS_TABLE_PREFIX);
226: return clone;
227: }
228:
229: @Override
230: protected String getTaskType() {
231: return ETLEngine.VALIDATING;
232: }
233:
234: }
|