KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > BaseUIComponent


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 Jul 22, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.ui;
19
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import org.apache.commons.logging.Log;
25 import org.pentaho.messages.Messages;
26 import org.pentaho.core.services.IActionRequestHandler;
27 import org.pentaho.core.session.IPentahoSession;
28 import org.pentaho.core.solution.IParameterProvider;
29 import org.pentaho.core.system.PentahoMessenger;
30 import org.pentaho.core.system.PentahoSystem;
31 import org.pentaho.core.ui.IPentahoUrlFactory;
32 import org.pentaho.messages.util.LocaleHelper;
33
34 /**
35  * @author James Dixon
36  *
37  * TODO To change the template for this generated type comment go to Window -
38  * Preferences - Java - Code Style - Code Templates
39  */

40 public abstract class BaseUIComponent extends PentahoMessenger {
41
42     public static final boolean debug = PentahoSystem.debug;
43
44     protected HashMap JavaDoc xslProperties;
45
46     protected HashMap JavaDoc contentTypes;
47
48     private IActionRequestHandler requestHandler;
49
50     private IPentahoSession userSession;
51
52     public abstract Log getLogger();
53
54     private HashMap JavaDoc parameterProviders;
55
56     protected IPentahoUrlFactory urlFactory;
57
58     private String JavaDoc sourcePath;
59
60     public void handleRequest(OutputStream JavaDoc outputStream, IActionRequestHandler actionRequestHandler, String JavaDoc contentType, HashMap JavaDoc requestParameterProviders) throws IOException JavaDoc {
61
62         this.parameterProviders = requestParameterProviders;
63         this.requestHandler = actionRequestHandler;
64         String JavaDoc content = getContent(contentType);
65         if (content != null) {
66             outputStream.write(content.getBytes(LocaleHelper.getSystemEncoding()));
67         } else {
68             error(Messages.getString("BaseUI.ERROR_0001_NO_CONTENT")); //$NON-NLS-1$
69
}
70     }
71
72     protected void setSourcePath(String JavaDoc sourcePath) {
73         this.sourcePath = sourcePath;
74     }
75
76     protected String JavaDoc getSourcePath() {
77         return sourcePath;
78     }
79
80     public void setUrlFactory(IPentahoUrlFactory urlFactory) {
81         this.urlFactory = urlFactory;
82     }
83
84     public void setRequestHandler(IActionRequestHandler actionRequestHandler) {
85         this.requestHandler = actionRequestHandler;
86     }
87
88     public BaseUIComponent(IPentahoUrlFactory urlFactory, List JavaDoc messages, String JavaDoc sourcePath) {
89         super();
90         this.urlFactory = urlFactory;
91         xslProperties = new HashMap JavaDoc();
92         contentTypes = new HashMap JavaDoc();
93         setMessages(messages);
94         parameterProviders = new HashMap JavaDoc();
95         this.sourcePath = sourcePath;
96     }
97
98     public void setParameterProvider(String JavaDoc name, IParameterProvider parameterProvider) {
99         if (parameterProviders == null) {
100             parameterProviders = new HashMap JavaDoc();
101         }
102         parameterProviders.put(name, parameterProvider);
103     }
104
105     public void setParameterProviders(HashMap JavaDoc parameterProviders) {
106         this.parameterProviders = parameterProviders;
107     }
108
109     protected IPentahoUrlFactory getUrlFactory() {
110         return urlFactory;
111     }
112
113     protected IActionRequestHandler getRequestHandler() {
114         return requestHandler;
115     }
116
117     public HashMap JavaDoc getParameterProviders() {
118         return parameterProviders;
119     }
120
121     public String JavaDoc getParameter(String JavaDoc name, String JavaDoc defaultValue) {
122         IParameterProvider parameterProvider = (IParameterProvider) parameterProviders.get("options"); //$NON-NLS-1$
123
String JavaDoc value = null;
124         if (parameterProvider != null) {
125             value = parameterProvider.getStringParameter(name, null);
126             if (value != null) {
127                 return value;
128             }
129         }
130         parameterProvider = (IParameterProvider) parameterProviders.get("request"); //$NON-NLS-1$
131
if (parameterProvider != null) {
132             value = parameterProvider.getStringParameter(name, null);
133             if (value != null) {
134                 return value;
135             }
136         }
137         parameterProvider = (IParameterProvider) parameterProviders.get("session"); //$NON-NLS-1$
138
if (parameterProvider != null) {
139             value = parameterProvider.getStringParameter(name, null);
140             if (value != null) {
141                 return value;
142             }
143         }
144         return defaultValue;
145     }
146
147     public String JavaDoc[] getParameterAsArray(String JavaDoc name) {
148         IParameterProvider parameterProvider = (IParameterProvider) parameterProviders.get("options"); //$NON-NLS-1$
149
Object JavaDoc value;
150         if (parameterProvider != null) {
151             value = parameterProvider.getParameter(name);
152             if (value != null) {
153                 return toStringArray( value );
154             }
155         }
156         parameterProvider = (IParameterProvider) parameterProviders.get("request"); //$NON-NLS-1$
157
if (parameterProvider != null) {
158             value = parameterProvider.getParameter(name);
159             if (value != null) {
160                 return toStringArray( value );
161             }
162         }
163         parameterProvider = (IParameterProvider) parameterProviders.get("session"); //$NON-NLS-1$
164
if (parameterProvider != null) {
165             value = parameterProvider.getParameter(name);
166             if (value != null) {
167                 return toStringArray( value );
168             }
169         }
170         return( new String JavaDoc[] {} );
171     }
172     
173     private String JavaDoc[] toStringArray( Object JavaDoc value ) {
174         if ( value == null ) {
175             return( new String JavaDoc[] {} );
176         }
177         
178         if ( value instanceof String JavaDoc[] ) {
179             return( (String JavaDoc[])value );
180         }
181         
182         return( new String JavaDoc[] { value.toString() } );
183     }
184
185     protected IPentahoSession getSession() {
186         return userSession;
187     }
188
189     public void setXsl(String JavaDoc mimeType, String JavaDoc xslName) {
190         contentTypes.put(mimeType, xslName);
191     }
192
193     public String JavaDoc getXsl(String JavaDoc mimeType) {
194         return (String JavaDoc) contentTypes.get(mimeType);
195     }
196
197     public abstract boolean validate();
198
199     public boolean validate(IPentahoSession session, IActionRequestHandler actionRequestHandler) {
200         this.userSession = session;
201         this.genLogIdFromSession(session);
202         this.requestHandler = actionRequestHandler;
203         return validate();
204     }
205
206     public void setXslProperty(String JavaDoc name, String JavaDoc value) {
207         xslProperties.put(name, value);
208     }
209
210     public HashMap JavaDoc getXslProperties() {
211         return xslProperties;
212     }
213
214     public abstract String JavaDoc getContent(String JavaDoc mimeType);
215
216     public void done() {
217
218     }
219
220 }
221
Popular Tags