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 31, 2005
014: * @author James Dixon
015: */
016:
017: package org.pentaho.ui.component;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.dom4j.Document;
028: import org.pentaho.core.solution.ActionResource;
029: import org.pentaho.core.solution.IActionResource;
030: import org.pentaho.core.system.PentahoSystem;
031: import org.pentaho.core.ui.IPentahoUrlFactory;
032: import org.pentaho.messages.Messages;
033: import org.pentaho.ui.UIException;
034: import org.pentaho.ui.XmlComponent;
035:
036: /**
037: * This class provides a user interface that lets users select items from lists
038: * or radio buttons. The selections are used by other components to filter
039: * queries used to populate data.
040: *
041: * @author James Dixon
042: */
043: public class FilterPanelComponent extends XmlComponent {
044: private static final Log log = LogFactory
045: .getLog(FilterPanelComponent.class);
046:
047: /**
048: *
049: */
050: private static final long serialVersionUID = 700681534058825526L;
051:
052: private static final Log logger = LogFactory
053: .getLog(FilterPanelComponent.class);
054:
055: /**
056: * repository relative path and filename to the filter panel definition file
057: */
058: private String definitionPath;
059:
060: private FilterPanel filterPanel;
061:
062: private String xslName;
063:
064: private Map defaultValues;
065:
066: public FilterPanelComponent(String definitionPath, String xslName,
067: IPentahoUrlFactory urlFactory, List messages) {
068: super (urlFactory, messages, null);
069: this .definitionPath = definitionPath;
070: if (xslName == null) {
071: // use a default XSL
072: xslName = "FilterPanelDefault.xsl"; //$NON-NLS-1$
073: }
074: PentahoSystem.ActionInfo info = PentahoSystem
075: .parseActionString(definitionPath);
076: if (info != null) {
077: setSourcePath(info.getSolutionName() + File.separator
078: + info.getPath());
079: }
080: this .xslName = xslName;
081: defaultValues = new HashMap();
082: }
083:
084: public void setDefaultValue(String filterName, String[] defaultValue) {
085: defaultValues.put(filterName, defaultValue);
086: }
087:
088: public Log getLogger() {
089: return logger;
090: }
091:
092: public boolean validate() {
093: return true;
094: }
095:
096: public List getFilters() {
097: return filterPanel.getFilters();
098: }
099:
100: public boolean init() {
101: if (filterPanel == null) {
102: // load the XML document that defines the dial
103: ActionResource resource = new ActionResource(
104: "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
105: definitionPath);
106: Document filterDocument = null;
107: try {
108: filterDocument = PentahoSystem.getSolutionRepository(
109: getSession()).getResourceAsDocument(resource);
110: } catch (IOException e) {
111: // TODO sbarkdull localize
112: logger
113: .error(
114: Messages
115: .getString("FilterPanelComponent.ERROR_0002_CREATE_XML"), e); //$NON-NLS-1$
116: return false;
117: }
118: try {
119: filterPanel = getFilterPanel(filterDocument);
120: } catch (FilterPanelException e) {
121: // TODO sbarkdull localize
122: logger
123: .error(
124: Messages
125: .getString("FilterPanelComponent.ERROR_0003_CREATE"), e); //$NON-NLS-1$
126: return false;
127: }
128: }
129: return true;
130: }
131:
132: public Document getXmlContent() {
133: // assert null != urlFactory : Messages.getString("FilterPanelComponent.ERROR_0000_FACTORY_CANNOT_BE_NULL"); //$NON-NLS-1$
134:
135: boolean ok = filterPanel.populate(getParameterProviders(),
136: defaultValues);
137:
138: if (!ok) {
139: String msg = Messages
140: .getString("FilterPanelComponent.ERROR_0001_POPULATE"); //$NON-NLS-1$
141: log.error(msg);
142: throw new UIException(msg);
143: }
144:
145: String actionUrl = urlFactory.getActionUrlBuilder().getUrl();
146: Document xForm = filterPanel.getXForm(actionUrl);
147:
148: setXsl("text/html", xslName); //$NON-NLS-1$
149:
150: return xForm;
151: }
152:
153: private FilterPanel getFilterPanel(Document filterDocument)
154: throws FilterPanelException {
155: FilterPanel newFilterPanel = new FilterPanel(getSession(),
156: filterDocument, this);
157: return newFilterPanel;
158: }
159:
160: }
|