KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > solution > SolutionEngineAgent


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 Jan 28, 2006
14  * @author James Dixon
15  */

16
17 package org.pentaho.core.solution;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.pentaho.core.runtime.IActionParameter;
25 import org.pentaho.core.session.StandaloneSession;
26 import org.pentaho.core.system.PentahoSystem;
27 import org.pentaho.core.ui.IPentahoUrlFactory;
28 import org.pentaho.core.ui.SimpleUrlFactory;
29
30 public class SolutionEngineAgent {
31
32     private HashMap JavaDoc parameters;
33
34     private String JavaDoc userId;
35
36     private String JavaDoc actionSequence;
37
38     private String JavaDoc description;
39
40     private ByteArrayOutputStream JavaDoc outputStream;
41
42     private ISolutionEngine solutionEngine = null;
43
44     public SolutionEngineAgent() {
45         parameters = new HashMap JavaDoc();
46     }
47
48     public void setUserId(String JavaDoc userId) {
49         this.userId = userId;
50     }
51
52     public String JavaDoc getUserId() {
53         return userId;
54     }
55
56     public void setParamter(String JavaDoc name, String JavaDoc value) {
57         parameters.put(name, value);
58     }
59
60     public void setActionSequence(String JavaDoc actionSequence) {
61         this.actionSequence = actionSequence;
62     }
63
64     public String JavaDoc getActionSequence() {
65         return actionSequence;
66     }
67
68     public void setDescription(String JavaDoc description) {
69         this.description = description;
70     }
71
72     public String JavaDoc getDescription() {
73         return description;
74     }
75
76     public String JavaDoc getOutput(String JavaDoc name) {
77         if (name == null || "default".equals(name) || "".equals(name)) { //$NON-NLS-1$ //$NON-NLS-2$
78
return outputStream.toString();
79         } else {
80             IActionParameter output = solutionEngine.getExecutionContext().getOutputParameter(name);
81             return output.getStringValue();
82         }
83     }
84
85     public int execute() {
86         PentahoSystem.systemEntryPoint();
87         try {
88             // create a generic session object
89
StandaloneSession session = new StandaloneSession(userId);
90
91             solutionEngine = PentahoSystem.getSolutionEngineInstance(session);
92             solutionEngine.init(session);
93
94             SimpleParameterProvider parameterProvider = new SimpleParameterProvider(parameters);
95
96             String JavaDoc baseUrl = PentahoSystem.getApplicationContext().getBaseUrl();
97             HashMap JavaDoc parameterProviderMap = new HashMap JavaDoc();
98             parameterProviderMap.put("request", parameterProvider); //$NON-NLS-1$
99

100             IPentahoUrlFactory urlFactory = new SimpleUrlFactory(baseUrl);
101
102             String JavaDoc solutionRef[] = PentahoSystem.parseActionString(actionSequence);
103
104             String JavaDoc solution = solutionRef[0];
105             String JavaDoc solutionPath = solutionRef[1];
106             String JavaDoc action = solutionRef[2];
107             String JavaDoc processName = description;
108             boolean persisted = false;
109             List JavaDoc messages = new ArrayList JavaDoc();
110
111             outputStream = new ByteArrayOutputStream JavaDoc(0);
112             SimpleOutputHandler outputHandler = null;
113             if (outputStream != null) {
114                 outputHandler = new SimpleOutputHandler(outputStream, false);
115                 outputHandler.setOutputPreference(IOutputHandler.OUTPUT_TYPE_DEFAULT);
116             }
117             solutionEngine.execute(solution, solutionPath, action, processName, false, true, null, persisted, parameterProviderMap, outputHandler, null, urlFactory, messages);
118
119         } finally {
120             PentahoSystem.systemExitPoint();
121         }
122         return solutionEngine.getStatus();
123     }
124
125 }
126
Popular Tags