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.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.netbeans.modules.etl.codegen.ETLStrategyBuilder;
026: import org.netbeans.modules.etl.codegen.ETLStrategyBuilderContext;
027: import org.netbeans.modules.etl.codegen.PatternFinder;
028: import org.netbeans.modules.etl.utils.MessageManager;
029: import org.netbeans.modules.sql.framework.model.SQLDefinition;
030: import org.netbeans.modules.sql.framework.model.TargetTable;
031: import net.java.hulp.i18n.Logger;
032: import com.sun.etl.engine.ETLEngine;
033: import com.sun.etl.engine.ETLTask;
034: import com.sun.etl.engine.ETLTaskNode;
035: import com.sun.sql.framework.exception.BaseException;
036: import org.netbeans.modules.etl.logger.Localizer;
037: import org.netbeans.modules.etl.logger.LogUtil;
038:
039: /**
040: * Builds ETLProcess Flow and delegate to appropriate ETLStrategy Builder as required
041: *
042: * @author Ahimanikya Satapathy
043: * @version $Revision$
044: */
045: public class DefaultProcessFlowGenerator extends BaseFlowGenerator {
046:
047: private static final String LOG_CATEGORY = DefaultProcessFlowGenerator.class
048: .getName();
049: private static transient final Logger mLogger = LogUtil
050: .getLogger(DefaultProcessFlowGenerator.class.getName());
051: private static transient final Localizer mLoc = Localizer.get();
052: private StringBuilder dependentsForThreadCollector = new StringBuilder();
053:
054: public DefaultProcessFlowGenerator(SQLDefinition sqlD)
055: throws BaseException {
056: super (sqlD);
057: this .builderModel.setUseInstanceDB(false);
058: }
059:
060: /**
061: * Called during Test run codegen.
062: */
063: public ETLEngine getScript() throws BaseException {
064: mLogger.infoNoloc(mLoc.t("PRSR003: In getScript(){0}",
065: LOG_CATEGORY));
066: generateScript();
067: return builderModel.getEngine();
068: }
069:
070: private void generateScript() throws BaseException {
071: final MessageManager dnLabelMgr = MessageManager
072: .getManager(ETLTaskNode.class);
073: createInitTask();
074:
075: // get target table List
076: List targetTables = builderModel.getSqlDefinition()
077: .getTargetTables();
078: if (targetTables == null || targetTables.size() == 0) {
079: throw new BaseException(
080: "Invalid eTL Collaboration: No target table defined.");
081: }
082:
083: // Loop Thru the target tables to generate:
084: ETLTaskNode commitTask = null;
085: ETLTaskNode threadCleanupTask = null;
086: List cleanupNodes = new ArrayList();
087: Iterator it = targetTables.iterator();
088:
089: ETLStrategyBuilderContext context = new ETLStrategyBuilderContext(
090: initTask, globalCleanupTask, this .statsUpdateTask,
091: this .builderModel);
092:
093: while (it.hasNext()) {
094: mLogger.infoNoloc(mLoc.t(
095: "PRSR004: Looping through target tables:{0}",
096: LOG_CATEGORY));
097: TargetTable tt = (TargetTable) it.next();
098: // Create commit node to collect transformer connections and commit/close
099: // them.
100:
101: commitTask = this .builderModel.getEngine()
102: .createETLTaskNode(ETLEngine.COMMIT);
103: commitTask.setDisplayName(dnLabelMgr.getString(
104: "TEMPLATE_dn", dnLabelMgr
105: .getString("LBL_dn_commit"), tt.getName()));
106:
107: threadCleanupTask = this .builderModel.getEngine()
108: .createETLTaskNode(ETLEngine.CLEANUP);
109: threadCleanupTask
110: .setDisplayName(dnLabelMgr.getString("TEMPLATE_dn",
111: dnLabelMgr.getString("LBL_dn_cleanup"), tt
112: .getName()));
113: cleanupNodes.add(threadCleanupTask);
114:
115: ETLStrategyBuilder tableScriptBuilder = PatternFinder
116: .createETLStrategyBuilder(tt, builderModel);
117: context.setTargetTable(tt);
118: context.setPredecessorTask(initTask);
119: context.setNextTaskOnSucess(commitTask);
120: context.setNextTaskOnException(threadCleanupTask);
121:
122: commitTask.addNextETLTaskNode(ETLTask.SUCCESS,
123: threadCleanupTask.getId());
124: commitTask.addNextETLTaskNode(ETLTask.EXCEPTION,
125: threadCleanupTask.getId());
126:
127: if (dependentsForThreadCollector.length() != 0) {
128: dependentsForThreadCollector.append(",");
129: }
130: dependentsForThreadCollector.append(threadCleanupTask
131: .getId());
132:
133: tableScriptBuilder.generateScriptForTable(context);
134:
135: } // end transformer Loop
136:
137: // set dependent list for collector wait node
138: Iterator iter = cleanupNodes.iterator();
139: while (iter.hasNext()) {
140: threadCleanupTask = (ETLTaskNode) iter.next();
141: threadCleanupTask.addNextETLTaskNode(ETLTask.SUCCESS,
142: threadCollectorWaitNode.getId());
143: threadCleanupTask.addNextETLTaskNode(ETLTask.EXCEPTION,
144: threadCollectorWaitNode.getId());
145: }
146:
147: threadCollectorWaitNode
148: .setDependsOn(dependentsForThreadCollector.toString());
149: threadCollectorWaitNode.addNextETLTaskNode(ETLTask.SUCCESS,
150: statsUpdateTask.getId());
151: threadCollectorWaitNode.addNextETLTaskNode(ETLTask.EXCEPTION,
152: statsUpdateTask.getId());
153: String waitDisplayName = dnLabelMgr
154: .getString("LBL_dn_transformerwait");
155: threadCollectorWaitNode.setDisplayName(waitDisplayName);
156:
157: statsUpdateTask.addNextETLTaskNode(ETLTask.SUCCESS,
158: globalCleanupTask.getId());
159: statsUpdateTask.addNextETLTaskNode(ETLTask.EXCEPTION,
160: globalCleanupTask.getId());
161:
162: globalCleanupTask.addNextETLTaskNode(ETLTask.SUCCESS,
163: this .endTask.getId());
164: globalCleanupTask.addNextETLTaskNode(ETLTask.EXCEPTION,
165: this .endTask.getId());
166:
167: // this to generate the engine xml and save for debugging use
168: this.builderModel.getEngine().toXMLString();
169: }
170: }
|