001/* 002 * (C) Copyright 2006-2007 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 * $Id: DirectoryEntryOutputRenderer.java 29611 2008-01-24 16:51:03Z gracinet $ 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.directory; 021 022import java.io.IOException; 023import java.util.Locale; 024import java.util.Map; 025 026import javax.faces.component.UIComponent; 027import javax.faces.context.FacesContext; 028import javax.faces.context.ResponseWriter; 029import javax.faces.render.Renderer; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.common.utils.i18n.I18NUtils; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.core.api.PropertyException; 036import org.nuxeo.ecm.directory.DirectoryException; 037 038import com.sun.faces.renderkit.Attribute; 039import com.sun.faces.renderkit.AttributeManager; 040import com.sun.faces.renderkit.RenderKitUtils; 041 042/** 043 * Renderer for directory entry. 044 * 045 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 046 */ 047public class DirectoryEntryOutputRenderer extends Renderer { 048 049 private static final Log log = LogFactory.getLog(DirectoryHelper.class); 050 051 private static final Attribute[] OUTPUT_ATTRIBUTES = AttributeManager.getAttributes(AttributeManager.Key.OUTPUTTEXT); 052 053 @Override 054 public void encodeBegin(FacesContext context, UIComponent component) throws IOException { 055 String toWrite = getEntryLabel(context, component); 056 057 ResponseWriter writer = context.getResponseWriter(); 058 boolean isOutput = false; 059 060 String style = (String) component.getAttributes().get("style"); 061 String styleClass = (String) component.getAttributes().get("styleClass"); 062 String dir = (String) component.getAttributes().get("dir"); 063 String lang = (String) component.getAttributes().get("lang"); 064 String title = (String) component.getAttributes().get("title"); 065 Map<String, Object> passthroughAttributes = component.getPassThroughAttributes(false); 066 boolean hasPassthroughAttributes = null != passthroughAttributes && !passthroughAttributes.isEmpty(); 067 068 boolean renderSpan = styleClass != null || style != null || dir != null || lang != null || title != null 069 || hasPassthroughAttributes; 070 if (renderSpan) { 071 writer.startElement("span", component); 072 if (null != styleClass) { 073 writer.writeAttribute("class", styleClass, "styleClass"); 074 } 075 // style is rendered as a passthru attribute 076 RenderKitUtils.renderPassThruAttributes(context, writer, component, OUTPUT_ATTRIBUTES); 077 078 } 079 if (toWrite != null) { 080 writer.write(toWrite); 081 } 082 083 if (renderSpan) { 084 writer.endElement("span"); 085 } 086 087 } 088 089 @SuppressWarnings("deprecation") 090 protected String getEntryLabel(FacesContext context, UIComponent component) { 091 DirectoryEntryOutputComponent dirComponent = (DirectoryEntryOutputComponent) component; 092 String entryId = (String) dirComponent.getValue(); 093 if (entryId == null) { 094 // BBB 095 entryId = dirComponent.getEntryId(); 096 } 097 String directoryName = dirComponent.getDirectoryName(); 098 String toWrite = null; 099 if (directoryName != null) { 100 // get the entry information 101 String keySeparator = (String) dirComponent.getAttributes().get("keySeparator"); 102 String schema; 103 try { 104 schema = DirectoryHelper.getDirectoryService().getDirectorySchema(directoryName); 105 } catch (DirectoryException de) { 106 log.error("Unable to get directory schema for " + directoryName, de); 107 schema = keySeparator != null ? "xvocabulary" : "vocabulary"; 108 } 109 if (keySeparator != null && entryId != null) { 110 entryId = entryId.substring(entryId.lastIndexOf(keySeparator) + 1, entryId.length()); 111 } 112 DocumentModel entry = DirectoryHelper.getEntry(directoryName, entryId); 113 114 if (entry != null) { 115 Boolean displayIdAndLabel = dirComponent.getDisplayIdAndLabel(); 116 if (displayIdAndLabel == null) { 117 displayIdAndLabel = Boolean.FALSE; // unboxed later 118 } 119 Boolean translate = dirComponent.getLocalize(); 120 121 String label; 122 try { 123 label = (String) entry.getProperty(schema, "label"); 124 } catch (PropertyException e) { 125 label = null; 126 } 127 String display = (String) dirComponent.getAttributes().get("display"); 128 if (label == null || "".equals(label)) { 129 label = entryId; 130 } 131 if (Boolean.TRUE.equals(translate)) { 132 label = translate(context, label); 133 } 134 toWrite = DirectoryHelper.getOptionValue(entryId, label, display, displayIdAndLabel.booleanValue(), " "); 135 } 136 } 137 if (toWrite == null) { 138 // default rendering: the entry id itself 139 toWrite = entryId; 140 } 141 return toWrite; 142 } 143 144 protected static String translate(FacesContext context, String label) { 145 String bundleName = context.getApplication().getMessageBundle(); 146 Locale locale = context.getViewRoot().getLocale(); 147 label = I18NUtils.getMessageString(bundleName, label, null, locale); 148 return label; 149 } 150 151}