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