001/*
002 * (C) Copyright 2011 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.forms.layout.facelets.plugins;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026import javax.faces.view.facelets.FaceletContext;
027import javax.faces.view.facelets.TagAttributes;
028import javax.faces.view.facelets.TagConfig;
029
030import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
031import org.nuxeo.ecm.platform.forms.layout.api.Widget;
032import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOption;
033import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOptions;
034import org.nuxeo.ecm.platform.forms.layout.facelets.FaceletHandlerHelper;
035import org.nuxeo.ecm.platform.ui.web.directory.UIDirectorySelectItem;
036import org.nuxeo.ecm.platform.ui.web.directory.UIDirectorySelectItems;
037
038/**
039 * Helper class for options generation depending on the widget definition
040 *
041 * @since 5.4.2
042 */
043public abstract class AbstractDirectorySelectWidgetTypeHandler extends AbstractSelectWidgetTypeHandler {
044
045    protected enum DirectoryPropertyMappings {
046        directoryName, displayAll, displayObsoleteEntries, filter, localize, dbl10n;
047    }
048
049    public AbstractDirectorySelectWidgetTypeHandler(TagConfig config) {
050        super(config);
051    }
052
053    @Override
054    protected List<String> getExcludedProperties() {
055        List<String> res = super.getExcludedProperties();
056        for (DirectoryPropertyMappings mapping : DirectoryPropertyMappings.values()) {
057            res.add(mapping.name());
058        }
059        return res;
060    }
061
062    protected String getOptionComponentType(WidgetSelectOption selectOption) {
063        if (selectOption instanceof WidgetSelectOptions) {
064            return UIDirectorySelectItems.COMPONENT_TYPE;
065        } else {
066            return UIDirectorySelectItem.COMPONENT_TYPE;
067        }
068    }
069
070    // do not rely on selectOptions to be filled
071    protected boolean shouldAddWidgetPropsHandler(Widget widget) {
072        return true;
073    }
074
075    protected Map<String, Serializable> getOptionProperties(FaceletContext ctx, Widget widget,
076            WidgetSelectOption selectOption) {
077        Map<String, Serializable> props = super.getOptionProperties(ctx, widget, selectOption);
078        for (DirectoryPropertyMappings mapping : DirectoryPropertyMappings.values()) {
079            if (widget.getProperties().containsKey(mapping.name())) {
080                props.put(mapping.name(), widget.getProperty(mapping.name()));
081            }
082        }
083        // if selectOptions is filled on widget properties, force
084        // displayAll value to false to ensure filtering of presented
085        // items
086        if (props.containsKey(SelectPropertyMappings.selectOptions.name())
087                && !props.containsKey(DirectoryPropertyMappings.displayAll.name())) {
088            props.put(DirectoryPropertyMappings.displayAll.name(), Boolean.FALSE);
089        }
090        return props;
091    }
092
093    /**
094     * Get tag attributes for a specific mode.
095     * @param widget The widget to generate tag attributes for.
096     * @param mode The given mode like PLAIN, CSV.
097     * @param helper An instance of FaceletHandlerHelper.
098     * @param widgetId The widget id.
099     * @return
100     *
101     * @since 9.3
102     */
103    protected TagAttributes getTagAttributesForMode(Widget widget, String mode, FaceletHandlerHelper helper,
104            String widgetId) {
105        TagAttributes result;
106        if (BuiltinWidgetModes.isLikePlainMode(mode)) {
107            // use attributes without id and with
108            List<String> excludedProperties = new ArrayList<String>();
109            // In case of plain mode css style attributes are to be excluded
110            excludedProperties.add("styleClass");
111            result = helper.getTagAttributes(widget, excludedProperties, true);
112        } else {
113            result = helper.getTagAttributes(widgetId, widget);
114        }
115        return result;
116    }
117
118}