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