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 Aug 24, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.ui.portlet;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javax.portlet.ActionRequest;
025: import javax.portlet.ActionResponse;
026: import javax.portlet.PortletException;
027: import javax.portlet.PortletPreferences;
028: import javax.portlet.PortletSession;
029: import javax.portlet.RenderRequest;
030: import javax.portlet.RenderResponse;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.pentaho.core.solution.HttpRequestParameterProvider;
035: import org.pentaho.core.solution.HttpSessionParameterProvider;
036: import org.pentaho.core.util.UIUtil;
037: import org.pentaho.core.util.XmlHelper;
038: import org.pentaho.messages.Messages;
039: import org.pentaho.ui.component.FilterDefinition;
040: import org.pentaho.ui.component.FilterPanelComponent;
041:
042: public class FilterPanelPortlet extends ViewPortlet {
043:
044: private static final String FILTERS = "filters"; //$NON-NLS-1$
045:
046: private static final String XSLNAME = "xsl"; //$NON-NLS-1$
047:
048: private String filterPanelDefinition = null;
049:
050: private static final Log portletLogger = LogFactory
051: .getLog(FilterPanelPortlet.class);
052:
053: public Log getLogger() {
054: return portletLogger;
055: }
056:
057: public void processPortletAction(ActionRequest request,
058: ActionResponse response, PentahoPortletSession userSession)
059: throws PortletException, IOException {
060:
061: // TODO get any changes in value and make it available through the
062: // session
063: if (filterPanelDefinition == null) {
064: return;
065: }
066: // see if this component is cached already
067: FilterPanelComponent filterPanel = (FilterPanelComponent) userSession
068: .getAttribute(filterPanelDefinition,
069: PortletSession.PORTLET_SCOPE);
070: if (filterPanel == null) {
071: return;
072: }
073: // iterate thru the list of filter objects
074: List filterList = filterPanel.getFilters();
075: Iterator filtersIterator = filterList.iterator();
076: while (filtersIterator.hasNext()) {
077: // get the filter definition
078: FilterDefinition filterDefinition = (FilterDefinition) filtersIterator
079: .next();
080: String filterName = filterDefinition.getName();
081: // get the new value of the filter from the requst
082: String[] values = request.getParameterValues(filterName);
083: XmlHelper.decode(values);
084:
085: if (values != null) {
086:
087: // see if this value is valid for the filter
088: if (filterDefinition.isValid(values)) {
089: filterDefinition.setDefaultValue(values);
090: userSession.removeAttribute(filterName,
091: PortletSession.APPLICATION_SCOPE);
092: userSession.setAttribute(filterName, values,
093: PortletSession.APPLICATION_SCOPE);
094: } else {
095: userSession.removeAttribute(filterName,
096: PortletSession.APPLICATION_SCOPE);
097:
098: }
099: }
100: }
101: }
102:
103: public void doPortletView(RenderRequest request,
104: RenderResponse response, PentahoPortletSession userSession)
105: throws PortletException, IOException {
106:
107: response.setContentType("text/html"); //$NON-NLS-1$
108:
109: PortletUrlFactory urlFactory = new PortletUrlFactory(response,
110: request.getWindowState(), request.getPortletMode());
111:
112: PortletRequestParameterProvider requestParameters = new PortletRequestParameterProvider(
113: request);
114: PortletSessionParameterProvider sessionParameters = new PortletSessionParameterProvider(
115: userSession);
116: PortletPreferences prefs = request.getPreferences();
117: PortletPreferencesParameterProvider portletPrefsParameters = new PortletPreferencesParameterProvider(
118: prefs);
119:
120: filterPanelDefinition = getSetting(FILTERS, null, request,
121: requestParameters);
122: String xslName = getSetting(XSLNAME, null, request,
123: requestParameters);
124:
125: if (filterPanelDefinition == null) {
126: response
127: .getWriter()
128: .print(
129: Messages
130: .getString("FilterPanel.ERROR_0001_PANELS_NOT_SPECIFIED")); //$NON-NLS-1$
131: return;
132: }
133:
134: ArrayList messages = new ArrayList();
135:
136: // see if this component is cached already
137: FilterPanelComponent filterPanel = (FilterPanelComponent) userSession
138: .getAttribute(filterPanelDefinition,
139: PortletSession.PORTLET_SCOPE);
140:
141: // NOTE to developers: if you are having trouble seeing your changes to your filter panel config file while debugging, it may be because your old filter panel has been cached in the session
142: if (filterPanel == null) {
143: // we need to create the filter panel and add it to the session
144: filterPanel = new FilterPanelComponent(
145: filterPanelDefinition, xslName, urlFactory,
146: messages);
147: userSession.setAttribute(filterPanelDefinition,
148: filterPanel, PortletSession.PORTLET_SCOPE);
149: } else {
150: filterPanel.setUrlFactory(urlFactory);
151: }
152:
153: filterPanel.validate(userSession, null);
154: filterPanel.init();
155: List filters = filterPanel.getFilters();
156: int index = 0;
157: while (index < filters.size()) {
158: FilterDefinition filter = (FilterDefinition) filters
159: .get(index);
160: filterPanel.setDefaultValue(filter.getName(),
161: (String[]) userSession.getAttribute(filter
162: .getName(),
163: PortletSession.APPLICATION_SCOPE));
164: index++;
165: }
166:
167: filterPanel.setParameterProvider(
168: HttpRequestParameterProvider.SCOPE_REQUEST,
169: requestParameters);
170: filterPanel.setParameterProvider(
171: HttpSessionParameterProvider.SCOPE_SESSION,
172: sessionParameters);
173: filterPanel
174: .setParameterProvider(
175: PortletPreferencesParameterProvider.SCOPE_PORTLET_PREFERENCES,
176: portletPrefsParameters);
177: String content = filterPanel.getContent("text/html"); //$NON-NLS-1$
178: filterPanel.setUrlFactory(null);
179: if (content == null || content.equals("")) { //$NON-NLS-1$
180: content = " "; //$NON-NLS-1$
181: }
182:
183: if (content == null) {
184: StringBuffer buffer = new StringBuffer();
185: UIUtil
186: .formatErrorMessage(
187: "text/html", Messages.getString("FilterPanel.ERROR_0002_COULD_NOT_CREATE_FILTERS"), messages, buffer); //$NON-NLS-1$ //$NON-NLS-2$
188: content = buffer.toString();
189: }
190:
191: response.getWriter().print(content);
192: }
193: }
|