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 Jul 22, 2005
014: * @author James Dixon
015: *
016: */
017:
018: package org.pentaho.ui;
019:
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import java.util.HashMap;
023: import java.util.List;
024:
025: import org.apache.commons.logging.Log;
026: import org.pentaho.core.services.IActionRequestHandler;
027: import org.pentaho.core.session.IPentahoSession;
028: import org.pentaho.core.solution.HttpRequestParameterProvider;
029: import org.pentaho.core.solution.HttpSessionParameterProvider;
030: import org.pentaho.core.solution.IParameterProvider;
031: import org.pentaho.core.system.PentahoMessenger;
032: import org.pentaho.core.system.PentahoSystem;
033: import org.pentaho.core.ui.IPentahoUrlFactory;
034: import org.pentaho.messages.Messages;
035: import org.pentaho.messages.util.LocaleHelper;
036:
037: /**
038: * @author James Dixon
039: *
040: * TODO To change the template for this generated type comment go to Window -
041: * Preferences - Java - Code Style - Code Templates
042: */
043: public abstract class BaseUIComponent extends PentahoMessenger {
044:
045: public static final boolean debug = PentahoSystem.debug;
046:
047: protected HashMap xslProperties;
048:
049: protected HashMap contentTypes;
050:
051: private IActionRequestHandler requestHandler;
052:
053: private IPentahoSession userSession;
054:
055: public abstract Log getLogger();
056:
057: private HashMap parameterProviders;
058:
059: protected IPentahoUrlFactory urlFactory;
060:
061: private String sourcePath;
062:
063: public void handleRequest(OutputStream outputStream,
064: IActionRequestHandler actionRequestHandler,
065: String contentType, HashMap requestParameterProviders)
066: throws IOException {
067:
068: this .parameterProviders = requestParameterProviders;
069: this .requestHandler = actionRequestHandler;
070: String content = getContent(contentType);
071: if (content != null) {
072: outputStream.write(content.getBytes(LocaleHelper
073: .getSystemEncoding()));
074: } else {
075: error(Messages.getString("BaseUI.ERROR_0001_NO_CONTENT")); //$NON-NLS-1$
076: }
077: }
078:
079: protected void setSourcePath(String sourcePath) {
080: this .sourcePath = sourcePath;
081: }
082:
083: protected String getSourcePath() {
084: return sourcePath;
085: }
086:
087: public void setUrlFactory(IPentahoUrlFactory urlFactory) {
088: this .urlFactory = urlFactory;
089: }
090:
091: public void setRequestHandler(
092: IActionRequestHandler actionRequestHandler) {
093: this .requestHandler = actionRequestHandler;
094: }
095:
096: public BaseUIComponent(IPentahoUrlFactory urlFactory,
097: List messages, String sourcePath) {
098: super ();
099: this .urlFactory = urlFactory;
100: xslProperties = new HashMap();
101: contentTypes = new HashMap();
102: setMessages(messages);
103: parameterProviders = new HashMap();
104: this .sourcePath = sourcePath;
105: }
106:
107: public void setParameterProvider(String name,
108: IParameterProvider parameterProvider) {
109: if (parameterProviders == null) {
110: parameterProviders = new HashMap();
111: }
112: parameterProviders.put(name, parameterProvider);
113: }
114:
115: public void setParameterProviders(HashMap parameterProviders) {
116: this .parameterProviders = parameterProviders;
117: }
118:
119: protected IPentahoUrlFactory getUrlFactory() {
120: return urlFactory;
121: }
122:
123: protected IActionRequestHandler getRequestHandler() {
124: return requestHandler;
125: }
126:
127: public HashMap getParameterProviders() {
128: return parameterProviders;
129: }
130:
131: public String getParameter(String name, String defaultValue) {
132: IParameterProvider parameterProvider = (IParameterProvider) parameterProviders
133: .get("options"); //$NON-NLS-1$
134: String value = null;
135: if (parameterProvider != null) {
136: value = parameterProvider.getStringParameter(name, null);
137: if (value != null) {
138: return value;
139: }
140: }
141: parameterProvider = (IParameterProvider) parameterProviders
142: .get(HttpRequestParameterProvider.SCOPE_REQUEST);
143: if (parameterProvider != null) {
144: value = parameterProvider.getStringParameter(name, null);
145: if (value != null) {
146: return value;
147: }
148: }
149: parameterProvider = (IParameterProvider) parameterProviders
150: .get(HttpSessionParameterProvider.SCOPE_SESSION);
151: if (parameterProvider != null) {
152: value = parameterProvider.getStringParameter(name, null);
153: if (value != null) {
154: return value;
155: }
156: }
157: return defaultValue;
158: }
159:
160: public String[] getParameterAsArray(String name) {
161: IParameterProvider parameterProvider = (IParameterProvider) parameterProviders
162: .get("options"); //$NON-NLS-1$
163: Object value;
164: if (parameterProvider != null) {
165: value = parameterProvider.getParameter(name);
166: if (value != null) {
167: return toStringArray(value);
168: }
169: }
170: parameterProvider = (IParameterProvider) parameterProviders
171: .get(HttpRequestParameterProvider.SCOPE_REQUEST);
172: if (parameterProvider != null) {
173: value = parameterProvider.getParameter(name);
174: if (value != null) {
175: return toStringArray(value);
176: }
177: }
178: parameterProvider = (IParameterProvider) parameterProviders
179: .get(HttpSessionParameterProvider.SCOPE_SESSION);
180: if (parameterProvider != null) {
181: value = parameterProvider.getParameter(name);
182: if (value != null) {
183: return toStringArray(value);
184: }
185: }
186: return (new String[] {});
187: }
188:
189: private String[] toStringArray(Object value) {
190: if (value == null) {
191: return (new String[] {});
192: }
193:
194: if (value instanceof String[]) {
195: return ((String[]) value);
196: }
197:
198: return (new String[] { value.toString() });
199: }
200:
201: protected IPentahoSession getSession() {
202: return userSession;
203: }
204:
205: public void setXsl(String mimeType, String xslName) {
206: contentTypes.put(mimeType, xslName);
207: }
208:
209: public String getXsl(String mimeType) {
210: return (String) contentTypes.get(mimeType);
211: }
212:
213: public abstract boolean validate();
214:
215: /**
216: * Set the userSession member, generate a Log Id, set the requestHandler, and validate
217: * the component's configuration.
218: * NOTE: this method has several side effects not related to validation. could
219: * probably use some refactoring
220: *
221: * @param session
222: * @param actionRequestHandler
223: * @return boolean true if component configuration is valid, else false
224: */
225: public boolean validate(IPentahoSession session,
226: IActionRequestHandler actionRequestHandler) {
227: this .userSession = session;
228: this .genLogIdFromSession(session);
229: this .requestHandler = actionRequestHandler;
230: return validate();
231: }
232:
233: public void setXslProperty(String name, String value) {
234: xslProperties.put(name, value);
235: }
236:
237: public HashMap getXslProperties() {
238: return xslProperties;
239: }
240:
241: public abstract String getContent(String mimeType);
242:
243: public void done() {
244:
245: }
246:
247: }
|