001/* 002 * (C) Copyright 2006-20011 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018 019package org.nuxeo.ecm.platform.reporting.widget; 020 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.List; 024 025import javax.faces.component.html.HtmlInputText; 026import javax.faces.component.html.HtmlOutputText; 027import javax.faces.view.facelets.ComponentHandler; 028import javax.faces.view.facelets.CompositeFaceletHandler; 029import javax.faces.view.facelets.FaceletContext; 030import javax.faces.view.facelets.FaceletHandler; 031import javax.faces.view.facelets.TagAttribute; 032import javax.faces.view.facelets.TagAttributes; 033import javax.faces.view.facelets.TagConfig; 034 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes; 037import org.nuxeo.ecm.platform.forms.layout.api.Widget; 038import org.nuxeo.ecm.platform.forms.layout.api.exceptions.WidgetException; 039import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper; 040import org.nuxeo.ecm.platform.forms.layout.facelets.LeafFaceletHandler; 041import org.nuxeo.ecm.platform.forms.layout.facelets.RenderVariables; 042import org.nuxeo.ecm.platform.forms.layout.facelets.plugins.AbstractWidgetTypeHandler; 043import org.nuxeo.ecm.platform.reporting.api.ReportInstance; 044import org.nuxeo.ecm.platform.reporting.api.ReportModel; 045import org.nuxeo.ecm.platform.reporting.report.ReportParameter; 046 047/** 048 * Custom Widget Handler to managing Reports parameters configuration 049 * 050 * @author Tiry (tdelprat@nuxeo.com) 051 */ 052public class HtmlReportParameterWidgetTypeHandler extends AbstractWidgetTypeHandler { 053 054 private static final long serialVersionUID = 1L; 055 056 @Override 057 public FaceletHandler getFaceletHandler(FaceletContext ctx, TagConfig tagConfig, Widget widget, 058 FaceletHandler[] subHandlers) throws WidgetException { 059 060 FaceletHandlerHelper helper = new FaceletHandlerHelper(ctx, tagConfig); 061 String mode = widget.getMode(); 062 String widgetId = widget.getId(); 063 064 String docVar = RenderVariables.globalVariables.value.name(); 065 DocumentModel doc = (DocumentModel) ctx.getAttribute(docVar); 066 067 ReportInstance reportInstance = doc.getAdapter(ReportInstance.class); 068 List<ReportParameter> reportParams; 069 try { 070 if (reportInstance != null) { 071 reportParams = reportInstance.getReportParameters(); 072 } else { 073 ReportModel reportModel = doc.getAdapter(ReportModel.class); 074 reportParams = reportModel.getReportParameters(); 075 } 076 } catch (IOException e) { 077 throw new WidgetException("Unable to resolve report parameters", e); 078 } 079 080 TagAttributes attributes = helper.getTagAttributes(widgetId, widget); 081 FaceletHandler leaf = new LeafFaceletHandler(); 082 083 FaceletHandler[] handlers = new FaceletHandler[reportParams.size() * 4]; 084 int idx = 0; 085 int row = 0; 086 for (ReportParameter param : reportParams) { 087 088 // label 089 List<TagAttribute> attrs = new ArrayList<TagAttribute>(); 090 attrs.add(helper.createIdAttribute("paramName" + idx)); 091 attrs.add(helper.createAttribute("value", param.getName())); 092 ComponentHandler pName = helper.getHtmlComponentHandler(FaceletHandlerHelper.getTagAttributes(attrs), leaf, 093 HtmlOutputText.COMPONENT_TYPE, null); 094 095 // value 096 List<TagAttribute> attrs2 = new ArrayList<TagAttribute>(); 097 attrs2.add(helper.createIdAttribute("paramValue" + idx)); 098 String globalEL = attributes.get("value").getValue(); 099 100 String locator = "[" + row + "]['pValue']}"; 101 String value = globalEL.replace("}", locator); 102 if (value == null) { 103 value = "unset"; 104 } 105 ComponentHandler pValue; 106 if (BuiltinWidgetModes.EDIT.equals(mode) && param.isEditable()) { 107 attrs2.add(helper.createAttribute("value", value)); 108 pValue = helper.getHtmlComponentHandler(FaceletHandlerHelper.getTagAttributes(attrs2), leaf, 109 HtmlInputText.COMPONENT_TYPE, null); 110 } else { 111 if (!param.isEditable()) { 112 attrs2.add(helper.createAttribute("value", param.getStringValue())); 113 } else { 114 attrs2.add(helper.createAttribute("value", value)); 115 } 116 pValue = helper.getHtmlComponentHandler(FaceletHandlerHelper.getTagAttributes(attrs2), leaf, 117 HtmlOutputText.COMPONENT_TYPE, null); 118 } 119 120 // spacer 1 121 List<TagAttribute> attrs3 = new ArrayList<TagAttribute>(); 122 attrs3.add(helper.createIdAttribute("spacer1" + idx)); 123 attrs3.add(helper.createAttribute("value", " : ")); 124 attrs3.add(helper.createAttribute("escape", "false")); 125 ComponentHandler spacer = helper.getHtmlComponentHandler(FaceletHandlerHelper.getTagAttributes(attrs3), 126 leaf, HtmlOutputText.COMPONENT_TYPE, null); 127 128 // spacer 2 129 List<TagAttribute> attrs4 = new ArrayList<TagAttribute>(); 130 attrs4.add(helper.createIdAttribute("spacer2" + idx)); 131 attrs4.add(helper.createAttribute("value", "<br/>")); 132 attrs4.add(helper.createAttribute("escape", "false")); 133 ComponentHandler spacer2 = helper.getHtmlComponentHandler(FaceletHandlerHelper.getTagAttributes(attrs4), 134 leaf, HtmlOutputText.COMPONENT_TYPE, null); 135 136 handlers[idx] = pName; 137 handlers[idx + 1] = spacer; 138 handlers[idx + 2] = pValue; 139 handlers[idx + 3] = spacer2; 140 idx = idx + 4; 141 row += 1; 142 } 143 return new CompositeFaceletHandler(handlers); 144 } 145 146}