001/*
002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.ui.web.directory;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import javax.faces.component.UIComponent;
024import javax.faces.component.html.HtmlSelectOneListbox;
025import javax.faces.view.facelets.ComponentConfig;
026import javax.faces.view.facelets.FaceletContext;
027import javax.faces.view.facelets.FaceletHandler;
028import javax.faces.view.facelets.MetaRuleset;
029import javax.faces.view.facelets.MetaTagHandler;
030import javax.faces.view.facelets.TagAttribute;
031import javax.faces.view.facelets.TagAttributes;
032import javax.faces.view.facelets.TagConfig;
033
034import org.apache.commons.lang.StringUtils;
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037import org.nuxeo.ecm.platform.ui.web.tag.handler.GenericHtmlComponentHandler;
038import org.nuxeo.ecm.platform.ui.web.tag.handler.TagConfigFactory;
039
040import com.sun.faces.facelets.tag.TagAttributeImpl;
041import com.sun.faces.facelets.tag.TagAttributesImpl;
042
043/**
044 * Legacy class for nxdir:selectOneListbox tag
045 *
046 * @since 6.0
047 */
048public class DirectorySelectOneListboxHandler extends MetaTagHandler {
049
050    private static final Log log = LogFactory.getLog(DirectorySelectOneListboxHandler.class);
051
052    protected enum DeprecatedPropertyKeys {
053        displayValueOnly;
054    }
055
056    static final List<String> deprecatedProps = new ArrayList<>();
057    static {
058        for (DeprecatedPropertyKeys item : DeprecatedPropertyKeys.values()) {
059            deprecatedProps.add(item.name());
060        }
061    }
062
063    protected enum OptionPropertyKeys {
064        directoryName, localize, displayIdAndLabel, ordering, caseSensistive,
065        //
066        displayObsoleteEntries, notDisplayDefaultOption, filter;
067    }
068
069    static List<String> optionProps = new ArrayList<>();
070    static {
071        for (OptionPropertyKeys item : OptionPropertyKeys.values()) {
072            optionProps.add(item.name());
073        }
074    }
075
076    protected final TagConfig tagConfig;
077
078    protected List<TagAttribute> select;
079
080    protected List<TagAttribute> options;
081
082    public DirectorySelectOneListboxHandler(TagConfig config) {
083        super(config);
084        tagConfig = config;
085        initAttributes(tag.getAttributes().getAll());
086    }
087
088    protected void initAttributes(TagAttribute[] attrs) {
089        select = new ArrayList<>();
090        options = new ArrayList<>();
091        boolean orderingFound = false;
092        if (attrs != null) {
093            for (TagAttribute attr : attrs) {
094                String name = attr.getLocalName();
095                if (StringUtils.equals(name, OptionPropertyKeys.ordering.name())) {
096                    orderingFound = true;
097                }
098                if (optionProps.contains(name)) {
099                    options.add(attr);
100                } else {
101                    if (deprecatedProps.contains(name)) {
102                        log.error(String.format("Property %s is not taken into account "
103                                + "anymore on tag nxdir:selectOneListbox", name));
104                    }
105                    select.add(attr);
106                }
107            }
108        }
109        // add default needed values for lookup to work ok
110        options.add(getTagAttribute("var", "item"));
111        options.add(getTagAttribute("itemValue", "#{item.id}"));
112        // Check if the attribute "ordering" has been defined, otherwise, the default value is added
113        if (!orderingFound) {
114            options.add(getTagAttribute(OptionPropertyKeys.ordering.name(), "label"));
115        }
116    }
117
118    protected TagAttribute getTagAttribute(String name, String value) {
119        return new TagAttributeImpl(tagConfig.getTag().getLocation(), tagConfig.getTag().getNamespace(), name, name,
120                value);
121    }
122
123    @Override
124    public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
125        // generate component handlers to be used instead
126        TagAttributes optionAttributes = new TagAttributesImpl(options.toArray(new TagAttribute[] {}));
127        ComponentConfig optionsConfig = TagConfigFactory.createComponentConfig(tagConfig, tagConfig.getTagId(),
128                optionAttributes, new FaceletHandler() {
129                    @Override
130                    public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
131                        // do nothing
132                    }
133                }, UIDirectorySelectItems.COMPONENT_TYPE, null);
134        FaceletHandler optionsHandler = new GenericHtmlComponentHandler(optionsConfig);
135        TagAttributes selectAttributes = new TagAttributesImpl(select.toArray(new TagAttribute[] {}));
136        ComponentConfig selectConfig = TagConfigFactory.createComponentConfig(tagConfig, tagConfig.getTagId(),
137                selectAttributes, optionsHandler, getSelectComponentType(), null);
138        new GenericHtmlComponentHandler(selectConfig).apply(ctx, parent);
139    }
140
141    protected String getSelectComponentType() {
142        return HtmlSelectOneListbox.COMPONENT_TYPE;
143    }
144
145    @Override
146    @SuppressWarnings("rawtypes")
147    protected MetaRuleset createMetaRuleset(Class type) {
148        return null;
149    }
150
151}