KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > shark > SharkWorkflowComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jun 27, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.plugin.shark;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.pentaho.core.system.PentahoSystem;
26 import org.pentaho.messages.Messages;
27 import org.pentaho.plugin.ComponentBase;
28 import org.pentaho.repository.HibernateUtil;
29
30 /**
31  * @author James Dixon
32  *
33  * TODO To change the template for this generated type comment go to Window -
34  * Preferences - Java - Code Style - Code Templates
35  */

36 public class SharkWorkflowComponent extends ComponentBase {
37
38     /**
39      *
40      */

41
42     private static final String JavaDoc PACKAGE_NAME = "package-name"; //$NON-NLS-1$
43

44     private static final String JavaDoc PROCESS_NAME = "process-name"; //$NON-NLS-1$
45

46     private static final String JavaDoc NEW_INSTANCE = "new-instance"; //$NON-NLS-1$
47

48     private static final long serialVersionUID = -4522797650756773831L;
49
50     public Log getLogger() {
51         return LogFactory.getLog(SharkWorkflowComponent.class);
52     }
53
54     protected boolean validateSystemSettings() {
55         // TODO
56
return true;
57     }
58
59     public boolean validateAction() {
60
61         if (!isDefinedInput(PACKAGE_NAME)) {
62             error(Messages.getErrorString("SharkWorkflow.ERROR_0001_PACKAGE_NOT_DEFINED")); //$NON-NLS-1$
63
return false;
64         }
65         if (!isDefinedInput(PROCESS_NAME)) {
66             error(Messages.getErrorString("SharkWorkflow.ERROR_0002_PROCESS_NOT_DEFINED")); //$NON-NLS-1$
67
return false;
68         }
69
70         return true;
71     }
72
73     public boolean executeAction() {
74
75         String JavaDoc packageName = getInputStringValue(PACKAGE_NAME);
76         String JavaDoc processName = getInputStringValue(PROCESS_NAME);
77
78         if (debug)
79             debug(Messages.getString("SharkWorkflow.DEBUG_PACKAGE_AND_PROCESS", packageName, processName)); //$NON-NLS-1$
80
// Create a new Pentaho instance from the parent instace
81

82         // Initialize shark
83

84         SharkManager shark = SharkManager.getInstance(getSession());
85
86         boolean running = false;
87         int latestVersion = -1;
88         String JavaDoc processId = null;
89
90         String JavaDoc availableProcessNames[] = shark.processesToStart();
91         for (int i = 0; i < availableProcessNames.length; i++) {
92             if (debug)
93                 debug(Messages.getString("SharkWorkflow.DEBUG_AVAILABLE_PROCESS", availableProcessNames[i])); //$NON-NLS-1$
94
if ((availableProcessNames[i].indexOf(packageName) == 0) && (availableProcessNames[i].endsWith(processName))) {
95                 try {
96                     int idx1 = availableProcessNames[i].indexOf("#"); //$NON-NLS-1$
97
int idx2 = availableProcessNames[i].indexOf("#", idx1 + 1); //$NON-NLS-1$
98
if (debug)
99                         debug(Messages.getString("SharkWorkflow.DEBUG_AVAILABLE_PROCESS_VERSION", availableProcessNames[i].substring(idx1 + 1, idx2))); //$NON-NLS-1$
100
int thisVersion = new Integer JavaDoc(availableProcessNames[i].substring(idx1 + 1, idx2)).intValue();
101                     if (thisVersion > latestVersion) {
102                         latestVersion = thisVersion;
103                     }
104                     processId = availableProcessNames[i];
105                     running = true;
106                 } catch (Exception JavaDoc e) {
107                 }
108             }
109         }
110
111         if (!running || latestVersion == -1 || processId == null) {
112             if (!running)
113                 error(Messages.getErrorString("SharkWorkflow.ERROR_0003_PROCESS_NOT_RUNNING", packageName, processName)); //$NON-NLS-1$
114
if (processId == null)
115                 error(Messages.getErrorString("SharkWorkflow.ERROR_0004_INVALID_PROCESS_ID", packageName, processName)); //$NON-NLS-1$
116
if (latestVersion == -1)
117                 error(Messages.getErrorString("SharkWorkflow.ERROR_0005_INVALID_VERSION_NUMBER", packageName, processName)); //$NON-NLS-1$
118
return false;
119         }
120
121         HashMap JavaDoc processParams = new HashMap JavaDoc();
122         boolean newInstance = getInputBooleanValue(NEW_INSTANCE, false);
123         String JavaDoc instanceId;
124         if (newInstance) {
125             HashMap JavaDoc parameters = new HashMap JavaDoc();
126
127             Set JavaDoc parameterNames = getInputNames();
128             Iterator JavaDoc parameterNamesIterator = parameterNames.iterator();
129             String JavaDoc parameterName;
130             Object JavaDoc parameterValue;
131             while (parameterNamesIterator.hasNext()) {
132                 parameterName = (String JavaDoc) parameterNamesIterator.next();
133                 if (!parameterName.equals(PACKAGE_NAME) && !parameterName.equals(PROCESS_NAME)) {
134                     parameterValue = getInputValue(parameterName);
135                     parameters.put(parameterName, parameterValue);
136                 }
137             }
138
139             // Force an immediate write of the newly created object.
140
instanceId = createNewInstance(true, parameters, true);
141             // set any data elements
142
} else {
143             instanceId = getInstanceId();
144         }
145         processParams.put("Pentaho_Solution_Id_Param", getSolutionName()); //$NON-NLS-1$
146
processParams.put("Pentaho_Instance_Id_Param", instanceId); //$NON-NLS-1$
147
Set JavaDoc parameterNames = getInputNames();
148         Iterator JavaDoc parameterNamesIterator = parameterNames.iterator();
149         String JavaDoc parameterName;
150         Object JavaDoc parameterValue;
151         while (parameterNamesIterator.hasNext()) {
152             parameterName = (String JavaDoc) parameterNamesIterator.next();
153             if (!parameterName.equals(PACKAGE_NAME) && !parameterName.equals(PROCESS_NAME)) {
154                 parameterValue = getInputValue(parameterName);
155                 if (debug)
156                     debug(Messages.getString("SharkWorkflow.DEBUG_ADDING_PARAMETER", parameterName, parameterValue.toString())); //$NON-NLS-1$
157
processParams.put(parameterName, parameterValue);
158             }
159         }
160
161         try {
162             String JavaDoc userId = PentahoSystem.getSystemSetting("shark/shark.xml", "workflow-shark/workflow-shark-user-id", null); //$NON-NLS-1$ //$NON-NLS-2$
163
String JavaDoc password = PentahoSystem.getSystemSetting("shark/shark.xml", "workflow-shark/workflow-shark-password", null); //$NON-NLS-1$ //$NON-NLS-2$
164
if (debug)
165                 debug(Messages.getString("SharkWorkflow.DEBUG_STARTING_PROCESS", processId)); //$NON-NLS-1$
166
try {
167                 HibernateUtil.commitTransaction();
168                 shark.startProcess(processId, userId, password, processParams);
169             } finally {
170                 HibernateUtil.beginTransaction();
171             }
172         } catch (Exception JavaDoc e) {
173             error(Messages.getErrorString("SharkWorkflow.ERROR_0006_ERROR_STARTING_PROCESS", processId), e); //$NON-NLS-1$
174
return false;
175         }
176
177         return true;
178
179     }
180
181     /*
182      * (non-Javadoc)
183      *
184      * @see org.pentaho.component.ComponentBase#done()
185      */

186     public void done() {
187         // TODO Auto-generated method stub
188

189     }
190
191     /*
192      * (non-Javadoc)
193      *
194      * @see org.pentaho.component.ComponentBase#init()
195      */

196     public boolean init() {
197         // TODO Auto-generated method stub
198
return true;
199     }
200
201 }
202
Popular Tags