001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jun 27, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.plugin.shark;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Set;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.pentaho.core.system.PentahoSystem;
025: import org.pentaho.messages.Messages;
026: import org.pentaho.plugin.ComponentBase;
027: import org.pentaho.repository.HibernateUtil;
028:
029: /**
030: * @author James Dixon
031: *
032: * TODO To change the template for this generated type comment go to Window -
033: * Preferences - Java - Code Style - Code Templates
034: */
035: public class SharkWorkflowComponent extends ComponentBase {
036:
037: /**
038: *
039: */
040:
041: private static final String PACKAGE_NAME = "package-name"; //$NON-NLS-1$
042:
043: private static final String PROCESS_NAME = "process-name"; //$NON-NLS-1$
044:
045: private static final String NEW_INSTANCE = "new-instance"; //$NON-NLS-1$
046:
047: private static final long serialVersionUID = -4522797650756773831L;
048:
049: public Log getLogger() {
050: return LogFactory.getLog(SharkWorkflowComponent.class);
051: }
052:
053: protected boolean validateSystemSettings() {
054: // TODO
055: return true;
056: }
057:
058: public boolean validateAction() {
059:
060: if (!isDefinedInput(PACKAGE_NAME)) {
061: error(Messages
062: .getErrorString("SharkWorkflow.ERROR_0001_PACKAGE_NOT_DEFINED")); //$NON-NLS-1$
063: return false;
064: }
065: if (!isDefinedInput(PROCESS_NAME)) {
066: error(Messages
067: .getErrorString("SharkWorkflow.ERROR_0002_PROCESS_NOT_DEFINED")); //$NON-NLS-1$
068: return false;
069: }
070:
071: return true;
072: }
073:
074: public boolean executeAction() {
075:
076: String packageName = getInputStringValue(PACKAGE_NAME);
077: String processName = getInputStringValue(PROCESS_NAME);
078:
079: if (debug)
080: debug(Messages
081: .getString(
082: "SharkWorkflow.DEBUG_PACKAGE_AND_PROCESS", packageName, processName)); //$NON-NLS-1$
083: // Create a new Pentaho instance from the parent instace
084:
085: // Initialize shark
086:
087: SharkManager shark = SharkManager.getInstance(getSession());
088:
089: boolean running = false;
090: int latestVersion = -1;
091: String processId = null;
092:
093: String availableProcessNames[] = shark.processesToStart();
094: for (int i = 0; i < availableProcessNames.length; i++) {
095: if (debug)
096: debug(Messages
097: .getString(
098: "SharkWorkflow.DEBUG_AVAILABLE_PROCESS", availableProcessNames[i])); //$NON-NLS-1$
099: if ((availableProcessNames[i].indexOf(packageName) == 0)
100: && (availableProcessNames[i].endsWith(processName))) {
101: try {
102: int idx1 = availableProcessNames[i].indexOf("#"); //$NON-NLS-1$
103: int idx2 = availableProcessNames[i].indexOf(
104: "#", idx1 + 1); //$NON-NLS-1$
105: if (debug)
106: debug(Messages
107: .getString(
108: "SharkWorkflow.DEBUG_AVAILABLE_PROCESS_VERSION", availableProcessNames[i].substring(idx1 + 1, idx2))); //$NON-NLS-1$
109: int this Version = new Integer(
110: availableProcessNames[i].substring(
111: idx1 + 1, idx2)).intValue();
112: if (this Version > latestVersion) {
113: latestVersion = this Version;
114: }
115: processId = availableProcessNames[i];
116: running = true;
117: } catch (Exception e) {
118: }
119: }
120: }
121:
122: if (!running || latestVersion == -1 || processId == null) {
123: if (!running)
124: error(Messages
125: .getErrorString(
126: "SharkWorkflow.ERROR_0003_PROCESS_NOT_RUNNING", packageName, processName)); //$NON-NLS-1$
127: if (processId == null)
128: error(Messages
129: .getErrorString(
130: "SharkWorkflow.ERROR_0004_INVALID_PROCESS_ID", packageName, processName)); //$NON-NLS-1$
131: if (latestVersion == -1)
132: error(Messages
133: .getErrorString(
134: "SharkWorkflow.ERROR_0005_INVALID_VERSION_NUMBER", packageName, processName)); //$NON-NLS-1$
135: return false;
136: }
137:
138: HashMap processParams = new HashMap();
139: boolean newInstance = getInputBooleanValue(NEW_INSTANCE, false);
140: String instanceId;
141: if (newInstance) {
142: HashMap parameters = new HashMap();
143:
144: Set parameterNames = getInputNames();
145: Iterator parameterNamesIterator = parameterNames.iterator();
146: String parameterName;
147: Object parameterValue;
148: while (parameterNamesIterator.hasNext()) {
149: parameterName = (String) parameterNamesIterator.next();
150: if (!parameterName.equals(PACKAGE_NAME)
151: && !parameterName.equals(PROCESS_NAME)) {
152: parameterValue = getInputValue(parameterName);
153: parameters.put(parameterName, parameterValue);
154: }
155: }
156:
157: // Force an immediate write of the newly created object.
158: instanceId = createNewInstance(true, parameters, true);
159: // set any data elements
160: } else {
161: instanceId = getInstanceId();
162: }
163: processParams.put(
164: "Pentaho_Solution_Id_Param", getSolutionName()); //$NON-NLS-1$
165: processParams.put("Pentaho_Instance_Id_Param", instanceId); //$NON-NLS-1$
166: Set parameterNames = getInputNames();
167: Iterator parameterNamesIterator = parameterNames.iterator();
168: String parameterName;
169: Object parameterValue;
170: while (parameterNamesIterator.hasNext()) {
171: parameterName = (String) parameterNamesIterator.next();
172: if (!parameterName.equals(PACKAGE_NAME)
173: && !parameterName.equals(PROCESS_NAME)) {
174: parameterValue = getInputValue(parameterName);
175: if (debug)
176: debug(Messages
177: .getString(
178: "SharkWorkflow.DEBUG_ADDING_PARAMETER", parameterName, parameterValue.toString())); //$NON-NLS-1$
179: processParams.put(parameterName, parameterValue);
180: }
181: }
182:
183: try {
184: String userId = PentahoSystem
185: .getSystemSetting(
186: "shark/shark.xml", "workflow-shark/workflow-shark-user-id", null); //$NON-NLS-1$ //$NON-NLS-2$
187: String password = PentahoSystem
188: .getSystemSetting(
189: "shark/shark.xml", "workflow-shark/workflow-shark-password", null); //$NON-NLS-1$ //$NON-NLS-2$
190: if (debug)
191: debug(Messages
192: .getString(
193: "SharkWorkflow.DEBUG_STARTING_PROCESS", processId)); //$NON-NLS-1$
194: try {
195: HibernateUtil.commitTransaction();
196: shark.startProcess(processId, userId, password,
197: processParams);
198: } finally {
199: HibernateUtil.beginTransaction();
200: }
201: } catch (Exception e) {
202: error(
203: Messages
204: .getErrorString(
205: "SharkWorkflow.ERROR_0006_ERROR_STARTING_PROCESS", processId), e); //$NON-NLS-1$
206: return false;
207: }
208:
209: return true;
210:
211: }
212:
213: /*
214: * (non-Javadoc)
215: *
216: * @see org.pentaho.component.ComponentBase#done()
217: */
218: public void done() {
219: // TODO Auto-generated method stub
220:
221: }
222:
223: /*
224: * (non-Javadoc)
225: *
226: * @see org.pentaho.component.ComponentBase#init()
227: */
228: public boolean init() {
229: // TODO Auto-generated method stub
230: return true;
231: }
232:
233: }
|