KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > portlet > FilterPanelPortlet


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

16
17 package org.pentaho.ui.portlet;
18
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import javax.portlet.ActionRequest;
24 import javax.portlet.ActionResponse;
25 import javax.portlet.PortletException;
26 import javax.portlet.PortletSession;
27 import javax.portlet.RenderRequest;
28 import javax.portlet.RenderResponse;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.pentaho.core.util.UIUtil;
32 import org.pentaho.core.util.XmlHelper;
33 import org.pentaho.messages.Messages;
34 import org.pentaho.ui.component.FilterDefinition;
35 import org.pentaho.ui.component.FilterPanelComponent;
36
37 public class FilterPanelPortlet extends ViewPortlet {
38
39     private String JavaDoc FILTERS = "filters"; //$NON-NLS-1$
40

41     private String JavaDoc XSLNAME = "xsl"; //$NON-NLS-1$
42

43     private String JavaDoc filterPanelDefinition = null;
44
45     private static final Log portletLogger = LogFactory.getLog(FilterPanelPortlet.class);
46
47     public Log getLogger() {
48         return portletLogger;
49     }
50
51     public void processPortletAction(ActionRequest request, ActionResponse response, PentahoPortletSession userSession) throws PortletException, IOException JavaDoc {
52
53         // TODO get any changes in value and make it available through the
54
// session
55
if (filterPanelDefinition == null) {
56             return;
57         }
58         // see if this component is cached already
59
FilterPanelComponent filterPanel = (FilterPanelComponent) userSession.getAttribute(filterPanelDefinition, PortletSession.PORTLET_SCOPE);
60         if (filterPanel == null) {
61             return;
62         }
63         // iterate thru the list of filter objects
64
List JavaDoc filterList = filterPanel.getFilters();
65         Iterator JavaDoc filtersIterator = filterList.iterator();
66         while (filtersIterator.hasNext()) {
67             // get the filter definition
68
FilterDefinition filterDefinition = (FilterDefinition) filtersIterator.next();
69             String JavaDoc filterName = filterDefinition.getName();
70             // get the new value of the filter from the requst
71
String JavaDoc[] values = request.getParameterValues(filterName);
72             XmlHelper.decode(values);
73             
74             if (values != null) {
75
76                 // see if this value is valid for the filter
77
if (filterDefinition.isValid(values)) {
78                     filterDefinition.setDefaultValue(values);
79                     userSession.removeAttribute(filterName, PortletSession.APPLICATION_SCOPE);
80                     userSession.setAttribute(filterName, values, PortletSession.APPLICATION_SCOPE);
81                 } else {
82                     userSession.removeAttribute(filterName, PortletSession.APPLICATION_SCOPE);
83
84                 }
85             }
86         }
87     }
88
89     public void doPortletView(RenderRequest request, RenderResponse response, PentahoPortletSession userSession) throws PortletException, IOException JavaDoc {
90
91         response.setContentType("text/html"); //$NON-NLS-1$
92

93         PortletUrlFactory urlFactory = new PortletUrlFactory(response, request.getWindowState(), request.getPortletMode());
94
95         PortletRequestParameterProvider requestParameters = new PortletRequestParameterProvider(request);
96         PortletSessionParameterProvider sessionParameters = new PortletSessionParameterProvider(userSession);
97
98         filterPanelDefinition = getSetting(FILTERS, null, request, requestParameters);
99         String JavaDoc xslName = getSetting(XSLNAME, null, request, requestParameters);
100
101         if (filterPanelDefinition == null) {
102             response.getWriter().print(Messages.getString("FilterPanel.ERROR_0001_PANELS_NOT_SPECIFIED")); //$NON-NLS-1$
103
return;
104         }
105
106         ArrayList JavaDoc messages = new ArrayList JavaDoc();
107
108         // see if this component is cached already
109
FilterPanelComponent filterPanel = (FilterPanelComponent) userSession.getAttribute(filterPanelDefinition, PortletSession.PORTLET_SCOPE);
110
111         if (filterPanel == null) {
112             // we need to create the filter panel and add it to the session
113
filterPanel = new FilterPanelComponent(filterPanelDefinition, xslName, urlFactory, messages);
114             userSession.setAttribute(filterPanelDefinition, filterPanel, PortletSession.PORTLET_SCOPE);
115         } else {
116             filterPanel.setUrlFactory(urlFactory);
117         }
118
119         filterPanel.validate(userSession, null);
120         filterPanel.init();
121         List JavaDoc filters = filterPanel.getFilters();
122         int index = 0;
123         while (index < filters.size()) {
124             FilterDefinition filter = (FilterDefinition) filters.get(index);
125             filterPanel.setDefaultValue(filter.getName(), (String JavaDoc[]) userSession.getAttribute(filter.getName(), PortletSession.APPLICATION_SCOPE));
126             index++;
127         }
128
129         filterPanel.setParameterProvider("request", requestParameters); //$NON-NLS-1$
130
filterPanel.setParameterProvider("session", sessionParameters); //$NON-NLS-1$
131
String JavaDoc content = filterPanel.getContent("text/html"); //$NON-NLS-1$
132
filterPanel.setUrlFactory(null);
133         if (content == null || content.equals("")) { //$NON-NLS-1$
134
content = "&nbsp;"; //$NON-NLS-1$
135
}
136
137         response.getWriter().print(content);
138
139         if (content == null) {
140             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
141             UIUtil.formatErrorMessage("text/html", Messages.getString("FilterPanel.ERROR_0002_COULD_NOT_CREATE_FILTERS"), messages, buffer); //$NON-NLS-1$ //$NON-NLS-2$
142
content = buffer.toString();
143         }
144
145     }
146 }
147
Popular Tags