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.List;
023import java.util.Map;
024
025import javax.faces.view.facelets.FaceletContext;
026import javax.faces.view.facelets.TagConfig;
027
028import org.nuxeo.ecm.platform.forms.layout.api.Widget;
029import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOption;
030import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOptions;
031import org.nuxeo.ecm.platform.ui.web.directory.UIDirectorySelectItem;
032import org.nuxeo.ecm.platform.ui.web.directory.UIDirectorySelectItems;
033
034/**
035 * Helper class for options generation depending on the widget definition
036 *
037 * @since 5.4.2
038 */
039public abstract class AbstractDirectorySelectWidgetTypeHandler extends AbstractSelectWidgetTypeHandler {
040
041    protected enum DirectoryPropertyMappings {
042        directoryName, displayAll, displayObsoleteEntries, filter, localize, dbl10n;
043    }
044
045    public AbstractDirectorySelectWidgetTypeHandler(TagConfig config) {
046        super(config);
047    }
048
049    @Override
050    protected List<String> getExcludedProperties() {
051        List<String> res = super.getExcludedProperties();
052        for (DirectoryPropertyMappings mapping : DirectoryPropertyMappings.values()) {
053            res.add(mapping.name());
054        }
055        return res;
056    }
057
058    protected String getOptionComponentType(WidgetSelectOption selectOption) {
059        if (selectOption instanceof WidgetSelectOptions) {
060            return UIDirectorySelectItems.COMPONENT_TYPE;
061        } else {
062            return UIDirectorySelectItem.COMPONENT_TYPE;
063        }
064    }
065
066    // do not rely on selectOptions to be filled
067    protected boolean shouldAddWidgetPropsHandler(Widget widget) {
068        return true;
069    }
070
071    protected Map<String, Serializable> getOptionProperties(FaceletContext ctx, Widget widget,
072            WidgetSelectOption selectOption) {
073        Map<String, Serializable> props = super.getOptionProperties(ctx, widget, selectOption);
074        for (DirectoryPropertyMappings mapping : DirectoryPropertyMappings.values()) {
075            if (widget.getProperties().containsKey(mapping.name())) {
076                props.put(mapping.name(), widget.getProperty(mapping.name()));
077            }
078        }
079        // if selectOptions is filled on widget properties, force
080        // displayAll value to false to ensure filtering of presented
081        // items
082        if (props.containsKey(SelectPropertyMappings.selectOptions.name())
083                && !props.containsKey(DirectoryPropertyMappings.displayAll.name())) {
084            props.put(DirectoryPropertyMappings.displayAll.name(), Boolean.FALSE);
085        }
086        return props;
087    }
088
089}