KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > util > UIUtil


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

16
17 package org.pentaho.core.util;
18
19 import java.text.NumberFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.pentaho.core.connection.IPentahoResultSet;
29 import org.pentaho.core.runtime.IRuntimeContext;
30 import org.pentaho.core.session.IPentahoSession;
31 import org.pentaho.core.session.PentahoHttpSession;
32 import org.pentaho.core.solution.ActionResource;
33 import org.pentaho.core.solution.IActionResource;
34 import org.pentaho.core.system.PentahoMessenger;
35 import org.pentaho.core.system.PentahoSystem;
36 import org.pentaho.messages.Messages;
37 import org.pentaho.messages.util.LocaleHelper;
38 import org.pentaho.util.VersionHelper;
39
40 public class UIUtil implements IUITemplater {
41     public static final String JavaDoc HTML_MIME_TYPE = "text/html"; //$NON-NLS-1$
42

43     public static final String JavaDoc TEXT_MIME_TYPE = "text/plain"; //$NON-NLS-1$
44

45     public static final int MAX_RESULT_THRESHOLD = 100;
46
47     public String JavaDoc processTemplate( String JavaDoc template, String JavaDoc title, String JavaDoc content, IPentahoSession session ) {
48         
49         template = processTemplate( template, title, session );
50         template = template.replaceFirst( "\\{content\\}", content ); //$NON-NLS-1$
51

52         return template;
53     }
54     
55     public String JavaDoc processTemplate( String JavaDoc template, String JavaDoc title, IPentahoSession session ) {
56         
57         template = template.replaceAll( "\\{title\\}", title ); //$NON-NLS-1$
58
template = template.replaceAll( "\\{home\\}", Messages.getString("UI.USER_HOME") ); //$NON-NLS-1$ //$NON-NLS-2$
59
template = template.replaceAll( "\\{navigate\\}", Messages.getString("UI.USER_NAVIGATE") ); //$NON-NLS-1$ //$NON-NLS-2$
60
template = template.replaceAll( "\\{solutions\\}", Messages.getString("UI.USER_SOLUTIONS") ); //$NON-NLS-1$ //$NON-NLS-2$
61
template = template.replaceAll( "\\{file-cache\\}", Messages.getString("UI.USER_FILE_CACHE") ); //$NON-NLS-1$ //$NON-NLS-2$
62
template = template.replaceAll( "\\{new-content\\}", Messages.getString("UI.USER_NEW_CONTENT") ); //$NON-NLS-1$ //$NON-NLS-2$
63

64         template = template.replaceAll( "\\{admin\\}", Messages.getString("UI.USER_ADMIN") ); //$NON-NLS-1$ //$NON-NLS-2$
65
template = template.replaceAll( "\\{about\\}", Messages.getString("UI.USER_ABOUT") ); //$NON-NLS-1$ //$NON-NLS-2$
66
template = template.replaceAll( "\\{pentaho.org\\}", Messages.getString("UI.USER_PENTAHO.ORG") ); //$NON-NLS-1$ //$NON-NLS-2$
67
template = template.replaceAll( "\\{close\\}", Messages.getString("UI.USER_CLOSE") ); //$NON-NLS-1$ //$NON-NLS-2$
68
template = template.replaceAll( "\\{forums\\}", Messages.getString("UI.USER_FORUMS") ); //$NON-NLS-1$ //$NON-NLS-2$
69
template = template.replaceAll( "\\{demos\\}", Messages.getString("UI.USER_DEMOS") ); //$NON-NLS-1$ //$NON-NLS-2$
70
template = template.replaceAll( "\\{downloads\\}", Messages.getString("UI.USER_DOWNLOADS") ); //$NON-NLS-1$ //$NON-NLS-2$
71
template = template.replaceAll( "\\{charset\\}", LocaleHelper.getSystemEncoding() ); //$NON-NLS-1$
72
template = template.replaceAll( "\\{text-direction\\}", LocaleHelper.getTextDirection() ); //$NON-NLS-1$
73
template = template.replaceAll( "\\{logout\\}", Messages.getString("UI.USER_LOGOUT") ); //$NON-NLS-1$ //$NON-NLS-2$
74
template = template.replaceAll( "\\{portal\\}", Messages.getString("UI.USER_PORTAL") ); //$NON-NLS-1$ //$NON-NLS-2$
75

76         template = template.replaceAll( "\\{system\\}", PentahoSystem.getSystemName() ); //$NON-NLS-1$
77
template = template.replaceAll( "\\{version\\}", "V"+VersionHelper.getVersion()+" build "+VersionHelper.getBuild() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
78
template = template.replaceAll( "\\{isLoggedIn\\}", session.isAuthenticated() ? "true" : "false" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
79
template = template.replaceAll( "\\{background-alert\\}", session.getBackgroundExecutionAlert() ? "true" : "false" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
80
template = template.replaceAll( "\\{header-content\\}", "" ); //$NON-NLS-1$ //$NON-NLS-2$
81
template = template.replaceAll( "\\{body-tag\\}", "" ); //$NON-NLS-1$ //$NON-NLS-2$
82

83         return template;
84     }
85     
86     public String JavaDoc getTemplate( String JavaDoc templateName, IPentahoSession session ) {
87         
88             ActionResource resource = new ActionResource( "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", "system/custom/" + templateName ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
89

90         String JavaDoc template = null;
91         try {
92                 template = PentahoSystem.getSolutionRepository(session).getResourceAsString( resource );
93             } catch (Throwable JavaDoc t) {
94             }
95
96             if( template == null ) {
97                 return Messages.getString( "UI.ERROR_0001_BAD_TEMPLATE", resource.getAddress() ); //$NON-NLS-1$
98
} else {
99                 return template;
100             }
101         }
102     
103     public String JavaDoc[] breakTemplate( String JavaDoc templateName, String JavaDoc title, IPentahoSession session ) {
104         String JavaDoc template = getTemplate( templateName, session );
105         return breakTemplateString( template, title, session );
106     }
107     
108     public String JavaDoc[] breakTemplateString( String JavaDoc template, String JavaDoc title, IPentahoSession session ) {
109         String JavaDoc token = "{content}"; //$NON-NLS-1$
110
template = processTemplate( template, title, session );
111         int index = template.indexOf( token );
112         if( index == -1 ) {
113             return new String JavaDoc[] {template};
114         } else {
115             String JavaDoc sections[] = new String JavaDoc[2];
116             sections[0] = template.substring( 0, index );
117             sections[1] = template.substring( index + token.length() );
118             return sections;
119         }
120     }
121     
122     public static IPentahoSession getPentahoSession(HttpServletRequest JavaDoc request) {
123
124         HttpSession JavaDoc session = request.getSession();
125         IPentahoSession userSession = (IPentahoSession) session.getAttribute("pentaho-session"); //$NON-NLS-1$
126
LocaleHelper.setLocale(request.getLocale());
127         if (userSession != null) {
128             return userSession;
129         }
130         userSession = new PentahoHttpSession(request.getRemoteUser(), request.getSession(), request.getLocale());
131         LocaleHelper.setLocale(request.getLocale());
132
133         session.setAttribute("pentaho-session", userSession); //$NON-NLS-1$
134
return userSession;
135     }
136
137     public static void formatErrorMessage(String JavaDoc mimeType, String JavaDoc title, String JavaDoc message, StringBuffer JavaDoc messageBuffer) {
138         ArrayList JavaDoc messages = new ArrayList JavaDoc(1);
139         messages.add(message);
140         formatErrorMessage(mimeType, title, messages, messageBuffer);
141     }
142
143     public static String JavaDoc getFirstError(List JavaDoc messages) {
144         String JavaDoc errorStart = PentahoMessenger.getUserString("ERROR"); //$NON-NLS-1$
145
int pos = errorStart.indexOf('{');
146         if (pos != -1) {
147             errorStart = errorStart.substring(0, pos);
148         }
149         Iterator JavaDoc messageIterator = messages.iterator();
150         while (messageIterator.hasNext()) {
151             String JavaDoc message = (String JavaDoc) messageIterator.next();
152             if (message.indexOf(errorStart) == 0) {
153                 return message;
154             }
155         }
156         return null;
157     }
158
159     public static void formatErrorMessage(String JavaDoc mimeType, String JavaDoc title, List JavaDoc messages, StringBuffer JavaDoc messageBuffer) {
160         // TODO make this template or XSL based
161
if ("text/html".equals(mimeType)) { //$NON-NLS-1$
162
messageBuffer.append("<html><head><title>") //$NON-NLS-1$
163
.append(Messages.getString("UIUtil.USER_START_ACTION")) //$NON-NLS-1$
164
.append("</title><link rel=\"stylesheet\" type=\"text/css\" HREF=\"/pentaho-style/active/default.css\"></head>") //$NON-NLS-1$
165
.append("<body dir=\"").append(LocaleHelper.getTextDirection()).append("\"><table cellspacing=\"10\"><tr><td class=\"portlet-section\" colspan=\"3\">") //$NON-NLS-1$ //$NON-NLS-2$
166
.append(title).append("<hr size=\"1\"/></td></tr><tr><td class=\"portlet-font\" valign=\"top\">"); //$NON-NLS-1$
167
Iterator JavaDoc messageIterator = messages.iterator();
168             String JavaDoc errorStart = PentahoMessenger.getUserString("ERROR"); //$NON-NLS-1$
169
int pos = errorStart.indexOf('{');
170             if (pos != -1) {
171                 errorStart = errorStart.substring(0, pos);
172             }
173             String JavaDoc firstMessage = getFirstError(messages);
174             if (firstMessage != null) {
175                 messageBuffer.append("<span style=\"color:red\">").append(firstMessage).append("</span><p/>"); //$NON-NLS-1$ //$NON-NLS-2$
176
}
177             messageIterator = messages.iterator();
178             while (messageIterator.hasNext()) {
179                 messageBuffer.append((String JavaDoc) messageIterator.next()).append("<br/>"); //$NON-NLS-1$
180
}
181             messageBuffer.append("</td></tr></table><p>"); //$NON-NLS-1$
182
messageBuffer.append( "&nbsp;&nbsp;" + Messages.getString("UIUtil.USER_SERVER_VERSION", VersionHelper.getVersion(), VersionHelper.getBuild()) ); //$NON-NLS-1$ //$NON-NLS-2$
183
messageBuffer.append("</body></html>"); //$NON-NLS-1$
184
} else {
185             // TODO support other mime types
186
}
187
188     }
189
190     public static void formatFailureMessage(String JavaDoc mimeType, IRuntimeContext context, StringBuffer JavaDoc messageBuffer, List JavaDoc defaultMessages) {
191
192         // TODO handle error messages from the runtime context
193

194         if ((context == null) && (defaultMessages == null)) {
195             // something went badly wrong
196
formatErrorMessage(mimeType, Messages.getString("UIUtil.ERROR_0001_REQUEST_FAILED"), Messages.getString("UIUtil.ERROR_0002_COULD_NOT_PROCESS"), messageBuffer); //$NON-NLS-1$ //$NON-NLS-2$
197
} else {
198             List JavaDoc theMessages = (context == null) ? defaultMessages : context.getMessages();
199             // TODO handle error messages from the runtime context
200
formatErrorMessage(mimeType, "Failed", theMessages, messageBuffer); //$NON-NLS-1$
201
// formatErrorMessage( mimeType,
202
// Messages.getString("UIUtil.ERROR_0001_REQUEST_FAILED"), messages,
203
// messageBuffer ); //$NON-NLS-1$
204
}
205     }
206
207     public static void formatFailureMessage(String JavaDoc mimeType, IRuntimeContext context, StringBuffer JavaDoc messageBuffer) {
208         formatFailureMessage(mimeType, context, messageBuffer, null);
209     }
210     
211     public static void formatResultSetAsHTMLRows( IPentahoResultSet resultSet, StringBuffer JavaDoc messageBuffer ) {
212         boolean hasColumnHeaders = false;
213         boolean hasRowHeaders = false;
214         Object JavaDoc[][] columnHeaders = null;
215         Object JavaDoc[][] rowHeaders = null;
216         if (resultSet.getMetaData() != null) {
217             columnHeaders = resultSet.getMetaData().getColumnHeaders();
218             rowHeaders = resultSet.getMetaData().getRowHeaders();
219             hasColumnHeaders = columnHeaders != null;
220             hasRowHeaders = rowHeaders != null;
221             if (hasColumnHeaders) {
222                 for (int row = 0; row < columnHeaders.length; row++) {
223                     messageBuffer.append("<tr>"); //$NON-NLS-1$
224
if (hasRowHeaders) {
225                         for (int indent = 0; indent < rowHeaders[0].length; indent++) {
226                             messageBuffer.append("<th></th>"); //$NON-NLS-1$
227
}
228                     }
229                     for (int column = 0; column < columnHeaders[row].length; column++) {
230                         messageBuffer.append("<th>").append(columnHeaders[row][column]).append("</th>"); //$NON-NLS-1$//$NON-NLS-2$
231
}
232                     messageBuffer.append("</tr>"); //$NON-NLS-1$
233
}
234             }
235             
236         }
237         Object JavaDoc[] dataRow = resultSet.next();
238         int currentRow = 0;
239         while (dataRow != null && currentRow < MAX_RESULT_THRESHOLD) {
240             messageBuffer.append("<tr>"); //$NON-NLS-1$
241
if (hasRowHeaders) {
242                 for (int rowHeaderCol = rowHeaders[currentRow].length - 1; rowHeaderCol >= 0; rowHeaderCol--) {
243                     messageBuffer.append("<th>").append(rowHeaders[currentRow][rowHeaderCol].toString()).append("</th>"); //$NON-NLS-1$//$NON-NLS-2$
244
}
245             }
246             NumberFormat JavaDoc nf = NumberFormat.getInstance();
247             for (int column = 0; column < dataRow.length; column++) {
248                 if (dataRow[column] != null) {
249                     Object JavaDoc value = dataRow[column];
250                     if (value instanceof Number JavaDoc) {
251                         Number JavaDoc numVal = (Number JavaDoc)value;
252                         value = nf.format(numVal);
253                         messageBuffer.append("<td align=\"right\">").append(value.toString()).append("</td>"); //$NON-NLS-1$//$NON-NLS-2$
254
} else {
255                         messageBuffer.append("<td>").append(value.toString()).append("</td>"); //$NON-NLS-1$ //$NON-NLS-2$
256
}
257                 } else {
258                     messageBuffer.append("<td>---</td>"); //$NON-NLS-1$
259
}
260             }
261             messageBuffer.append("</tr>"); //$NON-NLS-1$
262
dataRow = resultSet.next();
263             currentRow++;
264         }
265     }
266
267     public static void formatSuccessMessage(String JavaDoc mimeType, IRuntimeContext context, StringBuffer JavaDoc messageBuffer, boolean doMessages) {
268             formatSuccessMessage( mimeType, context, messageBuffer, doMessages, true );
269     }
270
271     public static void formatSuccessMessage(String JavaDoc mimeType, IRuntimeContext context, StringBuffer JavaDoc messageBuffer, boolean doMessages, boolean doWrapper) {
272
273         if (context == null) {
274             // something went badly wrong
275
formatFailureMessage(mimeType, context, messageBuffer);
276         } else if (mimeType.equalsIgnoreCase(HTML_MIME_TYPE)) {
277             // TODO make this template or XSL based
278
if( doWrapper ) {
279                     messageBuffer.append("<html><head><title>") //$NON-NLS-1$
280
.append(Messages.getString("UIUtil.USER_START_ACTION")) //$NON-NLS-1$
281
.append("</title><link rel=\"stylesheet\" type=\"text/css\" HREF=\"/pentaho-style/active/default.css\"></head>") //$NON-NLS-1$
282
.append("<body dir=\"").append(LocaleHelper.getTextDirection()).append("\"><table cellspacing=\"10\"><tr><td class=\"portlet-section\" colspan=\"3\">") //$NON-NLS-1$ //$NON-NLS-2$
283
.append(Messages.getString("UIUtil.USER_ACTION_SUCCESSFUL")) //$NON-NLS-1$
284
.append("<hr size=\"1\"/></td></tr><tr><td class=\"portlet-font\" valign=\"top\">"); //$NON-NLS-1$
285
}
286
287             // hmm do we need this to be ordered?
288
Set JavaDoc outputNames = context.getOutputNames();
289             Iterator JavaDoc outputNameIterator = outputNames.iterator();
290             while (outputNameIterator.hasNext()) {
291                 String JavaDoc outputName = (String JavaDoc) outputNameIterator.next();
292                 Object JavaDoc value = context.getOutputParameter(outputName).getValue();
293                 if (value == null) {
294                     value = ""; //$NON-NLS-1$
295
} else if (value instanceof IPentahoResultSet) {
296                     formatResultSetAsHTMLRows( (IPentahoResultSet)value, messageBuffer );
297                 } else {
298                     if( doWrapper ) {
299                         messageBuffer.append(outputName).append("="); //$NON-NLS-1$
300
}
301                     messageBuffer.append(value.toString());
302                     if( doWrapper ) {
303                         messageBuffer.append("<br/>"); //$NON-NLS-1$
304
}
305                 }
306             }
307             if (doMessages) {
308                     if( doWrapper ) {
309                         messageBuffer.append("<p><br size=\"1\">"); //$NON-NLS-1$
310
}
311                 List JavaDoc messages = context.getMessages();
312                 Iterator JavaDoc messageIterator = messages.iterator();
313                 while (messageIterator.hasNext()) {
314                     messageBuffer.append((String JavaDoc) messageIterator.next());
315                     if( doWrapper ) {
316                             messageBuffer.append("<br/>"); //$NON-NLS-1$
317
}
318                 }
319             }
320
321             if( doWrapper ) {
322                     messageBuffer.append("</td></tr></table></body></html>"); //$NON-NLS-1$
323
}
324         } else if (mimeType.equalsIgnoreCase(TEXT_MIME_TYPE)) {
325             messageBuffer.append(Messages.getString("UIUtil.USER_START_ACTION" + "\n")) //$NON-NLS-1$ //$NON-NLS-2$
326
.append(Messages.getString("UIUtil.USER_ACTION_SUCCESSFUL" + "\n")); //$NON-NLS-1$ //$NON-NLS-2$
327

328             // hmm do we need this to be ordered?
329
Set JavaDoc outputNames = context.getOutputNames();
330             Iterator JavaDoc outputNameIterator = outputNames.iterator();
331             while (outputNameIterator.hasNext()) {
332                 String JavaDoc outputName = (String JavaDoc) outputNameIterator.next();
333                 Object JavaDoc value = context.getOutputParameter(outputName).getValue();
334                 if (value == null) {
335                     value = ""; //$NON-NLS-1$
336
} else if (value instanceof IPentahoResultSet) {
337                     IPentahoResultSet resultSet = (IPentahoResultSet) value;
338                     Object JavaDoc[][] columnHeaders = resultSet.getMetaData().getColumnHeaders();
339                     Object JavaDoc[][] rowHeaders = resultSet.getMetaData().getRowHeaders();
340                     boolean hasColumnHeaders = columnHeaders != null;
341                     boolean hasRowHeaders = rowHeaders != null;
342                     if (hasColumnHeaders) {
343                         for (int row = 0; row < columnHeaders.length; row++) {
344                             for (int column = 0; column < columnHeaders[row].length; column++) {
345                                 if (hasRowHeaders) {
346                                     for (int indent = 0; indent < rowHeaders[0].length; indent++) {
347                                         messageBuffer.append("\t"); //$NON-NLS-1$
348
}
349                                 }
350                                 messageBuffer.append(columnHeaders[row][column]).append("\t"); //$NON-NLS-1$
351
}
352                             messageBuffer.append("\n"); //$NON-NLS-1$
353
}
354                     }
355                     int headerRow = 0;
356                     Object JavaDoc[] dataRow = resultSet.next();
357                     int currentRow = 0;
358                     while (dataRow != null && currentRow < MAX_RESULT_THRESHOLD) {
359                         if (hasRowHeaders) {
360                             for (int rowHeaderCol = 0; rowHeaderCol < rowHeaders[headerRow].length; rowHeaderCol++) {
361                                 messageBuffer.append(rowHeaders[headerRow][rowHeaderCol].toString()).append("\t"); //$NON-NLS-1$
362
}
363                         }
364                         for (int column = 0; column < dataRow.length; column++) {
365                             messageBuffer.append(dataRow[column].toString()).append("\t"); //$NON-NLS-1$
366
}
367                         dataRow = resultSet.next();
368                         currentRow++;
369                     }
370                 } else {
371                     messageBuffer.append(outputName).append("=").append(value.toString()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
372
}
373             }
374             if (doMessages) {
375                 List JavaDoc messages = context.getMessages();
376                 Iterator JavaDoc messageIterator = messages.iterator();
377                 while (messageIterator.hasNext()) {
378                     messageBuffer.append((String JavaDoc) messageIterator.next()).append("\n"); //$NON-NLS-1$
379
}
380             }
381         }
382     }
383 }
384
Popular Tags