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 Sep 21, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.ui.component;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.File;
021: import java.util.List;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.pentaho.messages.Messages;
025: import org.pentaho.core.runtime.IRuntimeContext;
026: import org.pentaho.core.session.IPentahoSession;
027: import org.pentaho.core.solution.ISolutionEngine;
028: import org.pentaho.core.solution.SimpleOutputHandler;
029: import org.pentaho.core.system.PentahoSystem;
030: import org.pentaho.core.ui.IPentahoUrlFactory;
031: import org.pentaho.messages.util.LocaleHelper;
032: import org.pentaho.ui.BaseUIComponent;
033:
034: public class ActionComponent extends BaseUIComponent {
035:
036: private static final long serialVersionUID = 1217363866006312765L;
037:
038: private static final Log logger = LogFactory
039: .getLog(ActionComponent.class);
040:
041: private String solutionName;
042:
043: private String actionPath;
044:
045: private String actionName;
046:
047: private String instanceId;
048:
049: private int outputPreference;
050:
051: public ActionComponent(String solutionName, String actionPath,
052: String actionName, String instanceId, int outputPreference,
053: IPentahoUrlFactory urlFactory, List messages) {
054: super (urlFactory, messages, solutionName + File.separator
055: + actionPath);
056: this .solutionName = solutionName;
057: this .actionName = actionName;
058: this .actionPath = actionPath;
059: this .instanceId = instanceId;
060: this .outputPreference = outputPreference;
061: }
062:
063: public ActionComponent(String actionString, String instanceId,
064: int outputPreference, IPentahoUrlFactory urlFactory,
065: List messages) {
066: super (urlFactory, messages, null);
067: PentahoSystem.ActionInfo info = PentahoSystem
068: .parseActionString(actionString);
069: if (info != null) {
070: solutionName = info.getSolutionName();
071: actionPath = info.getPath();
072: actionName = info.getActionName();
073: }
074: setSourcePath(solutionName + File.separator + actionPath);
075: this .instanceId = instanceId;
076: this .outputPreference = outputPreference;
077: }
078:
079: public Log getLogger() {
080: return logger;
081: }
082:
083: public boolean validate() {
084: return true;
085: }
086:
087: public String getContent(String mimeType) {
088: IPentahoSession userSession = getSession();
089:
090: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
091:
092: SimpleOutputHandler outputHandler = new SimpleOutputHandler(
093: outputStream, true);
094: outputHandler.setOutputPreference(outputPreference);
095:
096: ISolutionEngine solutionEngine = PentahoSystem
097: .getSolutionEngineInstance(getSession());
098: solutionEngine.setLoggingLevel(getLoggingLevel());
099: solutionEngine.init(userSession);
100:
101: IRuntimeContext context = null;
102: try {
103: context = solutionEngine
104: .execute(
105: solutionName,
106: actionPath,
107: actionName,
108: Messages
109: .getString("BaseTest.DEBUG_JUNIT_TEST"), false, true, instanceId, false, getParameterProviders(), outputHandler, null, urlFactory, getMessages()); //$NON-NLS-1$
110: } finally {
111: if (context != null) {
112: context.dispose();
113: }
114: }
115:
116: // TODO test the return result
117: String result = ""; //$NON-NLS-1$
118: try {
119: result = outputStream.toString(LocaleHelper
120: .getSystemEncoding());
121: } catch (Exception e) {
122: return outputStream.toString();
123: }
124: return result;
125:
126: }
127:
128: }
|