UIUtil.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.pentaho.core.util |
![]() |
![]() |
Pentaho |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Copyright 2006 Pentaho Corporation. All rights reserved.
* This software was developed by Pentaho Corporation and is provided under the terms
* of the Mozilla Public License, Version 1.1, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
* BI Platform. The Initial Developer is Pentaho Corporation.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*
* @created Jul 22, 2005 @author James Dixon
*
*/
package org.pentaho.core.util;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.pentaho.commons.connection.IPentahoResultSet;
import org.pentaho.core.repository.ISolutionRepository;
import org.pentaho.core.runtime.IRuntimeContext;
import org.pentaho.core.session.BaseSession;
import org.pentaho.core.session.IPentahoSession;
import org.pentaho.core.session.PentahoHttpSession;
import org.pentaho.core.solution.ActionResource;
import org.pentaho.core.solution.IActionResource;
import org.pentaho.core.system.PentahoMessenger;
import org.pentaho.core.system.PentahoSystem;
import org.pentaho.messages.Messages;
import org.pentaho.messages.util.LocaleHelper;
import org.pentaho.util.IVersionHelper;
import com.pentaho.security.SecurityUtils;
public class UIUtil implements IUITemplater {
//TODO sbarkdull, find occurances in code of these mime types, and where appropriate, use these static final value
public static final String HTML_MIME_TYPE = "text/html"; //$NON-NLS-1$
public static final String TEXT_MIME_TYPE = "text/plain"; //$NON-NLS-1$
public static final int MAX_RESULT_THRESHOLD = 100;
private static String footerTemplate = null;
private static final String FOOTER_TEMPLATE_FILENAME = "template-footer.html"; //$NON-NLS-1$
public String processTemplate(String template, String title, String content, IPentahoSession session) {
template = processTemplate(template, title, session);
template = template.replaceFirst("\\{content\\}", content); //$NON-NLS-1$
return template;
}
/*
* TODO: This needs to be architected to be more performant
*/
public String processTemplate(String template, String title, IPentahoSession session) {
ISolutionRepository repository = PentahoSystem.getSolutionRepository(session);
if (footerTemplate == null) {
footerTemplate = getTemplate(FOOTER_TEMPLATE_FILENAME, session);
}
template = template.replaceFirst("\\{footer\\}", footerTemplate); //$NON-NLS-1$
template = template.replaceAll("\\{title\\}", title); //$NON-NLS-1$
template = template.replaceAll("\\{home\\}", Messages.getString("UI.USER_HOME")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{navigate\\}", Messages.getString("UI.USER_NAVIGATE")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{solutions\\}", Messages.getString("UI.USER_SOLUTIONS")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{file-cache\\}", Messages.getString("UI.USER_FILE_CACHE")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{new-content\\}", Messages.getString("UI.USER_NEW_CONTENT")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{admin\\}", Messages.getString("UI.USER_ADMIN")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{about\\}", Messages.getString("UI.USER_ABOUT")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{pentaho.org\\}", Messages.getString("UI.USER_PENTAHO.COM")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{close\\}", Messages.getString("UI.USER_CLOSE")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{forums\\}", Messages.getString("UI.USER_FORUMS")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{demos\\}", Messages.getString("UI.USER_DEMOS")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{downloads\\}", Messages.getString("UI.USER_DOWNLOADS")); //$NON-NLS-1$ //$NON-NLS-2$
template = template
.replaceAll("text/html; charset=utf-8", "text/html; charset=" + LocaleHelper.getSystemEncoding()); //$NON-NLS-1$//$NON-NLS-2$
template = template.replaceAll("\\{text-direction\\}", LocaleHelper.getTextDirection()); //$NON-NLS-1$
template = template.replaceAll("\\{logout\\}", Messages.getString("UI.USER_LOGOUT")); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{portal\\}", Messages.getString("UI.USER_PORTAL")); //$NON-NLS-1$ //$NON-NLS-2$
IVersionHelper versionHelper = PentahoSystem.getVersionHelper(session);
template = template.replaceAll("\\{system\\}", PentahoSystem.getSystemName()); //$NON-NLS-1$
template = template.replaceAll("\\{version\\}", "Version: " + versionHelper.getVersionInformation()); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{isLoggedIn\\}", session.isAuthenticated() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
template = template.replaceAll("\\{background-alert\\}", session.getBackgroundExecutionAlert() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
template = template.replaceAll("\\{header-content\\}", ""); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{body-tag\\}", ""); //$NON-NLS-1$ //$NON-NLS-2$
template = template.replaceAll("\\{isAdmin\\}", SecurityUtils.isPentahoAdministrator(session) ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
template = template.replaceAll("\\{repository-name\\}", repository.getRepositoryName() ); //$NON-NLS-1$
template = template.replaceAll("\\{copyright\\}", Messages.getString("UI.USER_COPYRIGHT")); //$NON-NLS-1$ //$NON-NLS-2$
return template;
}
public String getTemplate(String templateName, IPentahoSession session) {
ActionResource resource = new ActionResource(
"", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", "system/custom/" + templateName); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String template = null;
try {
template = PentahoSystem.getSolutionRepository(session).getResourceAsString(resource);
} catch (Throwable t) {
}
if (template == null) {
return Messages.getString("UI.ERROR_0001_BAD_TEMPLATE", resource.getAddress()); //$NON-NLS-1$
} else {
return template;
}
}
public String[] breakTemplate(String templateName, String title, IPentahoSession session) {
String template = getTemplate(templateName, session);
return breakTemplateString(template, title, session);
}
public String[] breakTemplateString(String template, String title, IPentahoSession session) {
String token = "{content}"; //$NON-NLS-1$
template = processTemplate(template, title, session);
int index = template.indexOf(token);
if (index == -1) {
return new String[] { template };
} else {
String sections[] = new String[2];
sections[0] = template.substring(0, index);
sections[1] = template.substring(index + token.length());
return sections;
}
}
public static IPentahoSession getPentahoSession(HttpServletRequest request) {
HttpSession session = request.getSession();
IPentahoSession userSession = (IPentahoSession) session.getAttribute(BaseSession.PENTAHO_SESSION_KEY);
LocaleHelper.setLocale(request.getLocale());
if (userSession != null) {
return userSession;
}
userSession = new PentahoHttpSession(request.getRemoteUser(), request.getSession(), request.getLocale());
// TODO sbarkdull delete following line?
LocaleHelper.setLocale(request.getLocale());
session.setAttribute(BaseSession.PENTAHO_SESSION_KEY, userSession);
return userSession;
}
public static void formatErrorMessage(String mimeType, String title, String message, StringBuffer messageBuffer) {
ArrayList messages = new ArrayList(1);
messages.add(message);
formatErrorMessage(mimeType, title, messages, messageBuffer);
}
/**
* If PentahoMessenger.getUserString("ERROR") returns the string:
* "Error: {0} ({1})" (which is the case for English)
* Find the substring before the first "{". In this case, that
* would be: "Error: ".
* Return the first string in the messages list that contains
* the string "Error: ". If no string in the list contains
* "Error: ", return null;
* @param messages
* @return
*/
public static String getFirstError(List messages) {
// returns something like: "Error: {0} ({1})"
String errorStart = PentahoMessenger.getUserString("ERROR"); //$NON-NLS-1$
int pos = errorStart.indexOf('{');
if (pos != -1) {
errorStart = errorStart.substring(0, pos);
}
Iterator messageIterator = messages.iterator();
while (messageIterator.hasNext()) {
String message = (String) messageIterator.next();
if (message.indexOf(errorStart) == 0) {
return message;
}
}
return null;
}
public static void formatErrorMessage(String mimeType, String title, List messages, StringBuffer messageBuffer) {
// TODO make this template or XSL based
if ("text/html".equals(mimeType)) { //$NON-NLS-1$
messageBuffer
.append("<html><head><title>") //$NON-NLS-1$
.append(Messages.getString("UIUtil.ERROR_PAGE_TITLE")) //$NON-NLS-1$
.append(
"</title><link rel=\"stylesheet\" type=\"text/css\" href=\"/pentaho-style/active/default.css\"></head>") //$NON-NLS-1$
.append("<body dir=\"").append(LocaleHelper.getTextDirection()).append("\"><table cellspacing=\"10\"><tr><td class=\"portlet-section\" colspan=\"3\">") //$NON-NLS-1$ //$NON-NLS-2$
.append(title).append("<hr size=\"1\"/></td></tr><tr><td class=\"portlet-font\" valign=\"top\">"); //$NON-NLS-1$
Iterator messageIterator = messages.iterator();
String errorStart = PentahoMessenger.getUserString("ERROR"); //$NON-NLS-1$
int pos = errorStart.indexOf('{');
if (pos != -1) {
errorStart = errorStart.substring(0, pos);
}
String firstMessage = getFirstError(messages);
if (firstMessage != null) {
messageBuffer.append("<span style=\"color:red\">").append(firstMessage).append("</span><p/>"); //$NON-NLS-1$ //$NON-NLS-2$
}
messageIterator = messages.iterator();
while (messageIterator.hasNext()) {
messageBuffer.append((String) messageIterator.next()).append("<br/>"); //$NON-NLS-1$
}
messageBuffer.append("</td></tr></table><p>"); //$NON-NLS-1$
IVersionHelper versionHelper = PentahoSystem.getVersionHelper(null);
messageBuffer
.append(" " + Messages.getString("UIUtil.USER_SERVER_VERSION", versionHelper.getVersionInformation())); //$NON-NLS-1$ //$NON-NLS-2$
messageBuffer.append("</body></html>"); //$NON-NLS-1$
} else {
// TODO support other mime types
}
}
public static void formatFailureMessage(String mimeType, IRuntimeContext context, StringBuffer messageBuffer,
List defaultMessages) {
// TODO handle error messages from the runtime context
if ((context == null) && (defaultMessages == null)) {
// something went badly wrong
formatErrorMessage(
mimeType,
Messages.getString("UIUtil.ERROR_0001_REQUEST_FAILED"), Messages.getString("UIUtil.ERROR_0002_COULD_NOT_PROCESS"), messageBuffer); //$NON-NLS-1$ //$NON-NLS-2$
} else {
List theMessages = (context == null) ? defaultMessages : context.getMessages();
// TODO handle error messages from the runtime context
formatErrorMessage(mimeType, "Failed", theMessages, messageBuffer); //$NON-NLS-1$
// formatErrorMessage( mimeType,
// Messages.getString("UIUtil.ERROR_0001_REQUEST_FAILED"), messages,
// messageBuffer ); //$NON-NLS-1$
}
}
public static void formatFailureMessage(String mimeType, IRuntimeContext context, StringBuffer messageBuffer) {
formatFailureMessage(mimeType, context, messageBuffer, null);
}
public static void formatResultSetAsHTMLRows(IPentahoResultSet resultSet, StringBuffer messageBuffer) {
boolean hasColumnHeaders = false;
boolean hasRowHeaders = false;
Object[][] columnHeaders = null;
Object[][] rowHeaders = null;
if (resultSet.getMetaData() != null) {
columnHeaders = resultSet.getMetaData().getColumnHeaders();
rowHeaders = resultSet.getMetaData().getRowHeaders();
hasColumnHeaders = columnHeaders != null;
hasRowHeaders = rowHeaders != null;
if (hasColumnHeaders) {
for (int row = columnHeaders.length-1; row >= 0; row--) {
messageBuffer.append("<tr>"); //$NON-NLS-1$
if (hasRowHeaders) {
for (int indent = 0; indent < rowHeaders[0].length; indent++) {
messageBuffer.append("<th></th>"); //$NON-NLS-1$
}
}
for (int column = 0; column < columnHeaders[row].length; column++) {
messageBuffer.append("<th>").append(columnHeaders[row][column]).append("</th>"); //$NON-NLS-1$//$NON-NLS-2$
}
messageBuffer.append("</tr>"); //$NON-NLS-1$
}
}
}
Object[] dataRow = resultSet.next();
int currentRow = 0;
while (dataRow != null && currentRow < MAX_RESULT_THRESHOLD) {
messageBuffer.append("<tr>"); //$NON-NLS-1$
if (hasRowHeaders) {
for (int rowHeaderCol = rowHeaders[currentRow].length - 1; rowHeaderCol >= 0; rowHeaderCol--) {
messageBuffer.append("<th>").append(rowHeaders[currentRow][rowHeaderCol].toString()).append("</th>"); //$NON-NLS-1$//$NON-NLS-2$
}
}
NumberFormat nf = NumberFormat.getInstance();
for (int column = 0; column < dataRow.length; column++) {
if (dataRow[column] != null) {
Object value = dataRow[column];
if (value instanceof Number) {
Number numVal = (Number) value;
value = nf.format(numVal);
messageBuffer.append("<td align=\"right\">").append(value.toString()).append("</td>"); //$NON-NLS-1$//$NON-NLS-2$
} else {
messageBuffer.append("<td>").append(value.toString()).append("</td>"); //$NON-NLS-1$ //$NON-NLS-2$
}
} else {
messageBuffer.append("<td>---</td>"); //$NON-NLS-1$
}
}
messageBuffer.append("</tr>"); //$NON-NLS-1$
dataRow = resultSet.next();
currentRow++;
}
}
public static void formatSuccessMessage(String mimeType, IRuntimeContext context, StringBuffer messageBuffer,
boolean doMessages) {
formatSuccessMessage(mimeType, context, messageBuffer, doMessages, true);
}
public static void formatSuccessMessage(String mimeType, IRuntimeContext context, StringBuffer messageBuffer,
boolean doMessages, boolean doWrapper) {
if (context == null) {
// something went badly wrong
formatFailureMessage(mimeType, context, messageBuffer);
} else if (mimeType.equalsIgnoreCase(HTML_MIME_TYPE)) {
// TODO make this template or XSL based
if (doWrapper) {
messageBuffer
.append("<html><head><title>") //$NON-NLS-1$
.append(Messages.getString("UIUtil.USER_START_ACTION")) //$NON-NLS-1$
.append(
"</title><link rel=\"stylesheet\" type=\"text/css\" href=\"/pentaho-style/active/default.css\"></head>") //$NON-NLS-1$
.append("<body dir=\"").append(LocaleHelper.getTextDirection()).append("\"><table cellspacing=\"10\"><tr><td class=\"portlet-section\" colspan=\"3\">") //$NON-NLS-1$ //$NON-NLS-2$
.append(Messages.getString("UIUtil.USER_ACTION_SUCCESSFUL")) //$NON-NLS-1$
.append("<hr size=\"1\"/></td></tr><tr><td class=\"portlet-font\" valign=\"top\">"); //$NON-NLS-1$
}
// hmm do we need this to be ordered?
Set outputNames = context.getOutputNames();
Iterator outputNameIterator = outputNames.iterator();
while (outputNameIterator.hasNext()) {
String outputName = (String) outputNameIterator.next();
Object value = context.getOutputParameter(outputName).getValue();
if (value == null) {
value = ""; //$NON-NLS-1$
} else if (value instanceof IPentahoResultSet) {
formatResultSetAsHTMLRows((IPentahoResultSet) value, messageBuffer);
} else {
if (doWrapper) {
messageBuffer.append(outputName).append("="); //$NON-NLS-1$
}
messageBuffer.append(value.toString());
if (doWrapper) {
messageBuffer.append("<br/>"); //$NON-NLS-1$
}
}
}
if (doMessages) {
if (doWrapper) {
messageBuffer.append("<p><br size=\"1\">"); //$NON-NLS-1$
}
List messages = context.getMessages();
Iterator messageIterator = messages.iterator();
while (messageIterator.hasNext()) {
messageBuffer.append((String) messageIterator.next());
if (doWrapper) {
messageBuffer.append("<br/>"); //$NON-NLS-1$
}
}
}
if (doWrapper) {
messageBuffer.append("</td></tr></table></body></html>"); //$NON-NLS-1$
}
} else if (mimeType.equalsIgnoreCase(TEXT_MIME_TYPE)) {
messageBuffer.append(Messages.getString("UIUtil.USER_START_ACTION" + "\n")) //$NON-NLS-1$ //$NON-NLS-2$
.append(Messages.getString("UIUtil.USER_ACTION_SUCCESSFUL" + "\n")); //$NON-NLS-1$ //$NON-NLS-2$
// hmm do we need this to be ordered?
Set outputNames = context.getOutputNames();
Iterator outputNameIterator = outputNames.iterator();
while (outputNameIterator.hasNext()) {
String outputName = (String) outputNameIterator.next();
Object value = context.getOutputParameter(outputName).getValue();
if (value == null) {
value = ""; //$NON-NLS-1$
} else if (value instanceof IPentahoResultSet) {
IPentahoResultSet resultSet = (IPentahoResultSet) value;
Object[][] columnHeaders = resultSet.getMetaData().getColumnHeaders();
Object[][] rowHeaders = resultSet.getMetaData().getRowHeaders();
boolean hasColumnHeaders = columnHeaders != null;
boolean hasRowHeaders = rowHeaders != null;
if (hasColumnHeaders) {
for (int row = 0; row < columnHeaders.length; row++) {
for (int column = 0; column < columnHeaders[row].length; column++) {
if (hasRowHeaders) {
for (int indent = 0; indent < rowHeaders[0].length; indent++) {
messageBuffer.append("\t"); //$NON-NLS-1$
}
}
messageBuffer.append(columnHeaders[row][column]).append("\t"); //$NON-NLS-1$
}
messageBuffer.append("\n"); //$NON-NLS-1$
}
}
int headerRow = 0;
Object[] dataRow = resultSet.next();
int currentRow = 0;
while (dataRow != null && currentRow < MAX_RESULT_THRESHOLD) {
if (hasRowHeaders) {
for (int rowHeaderCol = 0; rowHeaderCol < rowHeaders[headerRow].length; rowHeaderCol++) {
messageBuffer.append(rowHeaders[headerRow][rowHeaderCol].toString()).append("\t"); //$NON-NLS-1$
}
}
for (int column = 0; column < dataRow.length; column++) {
messageBuffer.append(dataRow[column].toString()).append("\t"); //$NON-NLS-1$
}
dataRow = resultSet.next();
currentRow++;
}
} else {
messageBuffer.append(outputName).append("=").append(value.toString()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
if (doMessages) {
List messages = context.getMessages();
Iterator messageIterator = messages.iterator();
while (messageIterator.hasNext()) {
messageBuffer.append((String) messageIterator.next()).append("\n"); //$NON-NLS-1$
}
}
}
}
}
The table below shows all metrics for UIUtil.java.




