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.io.plugins;
020
021import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
022import org.nuxeo.ecm.platform.forms.layout.api.BuiltinWidgetModes;
023import org.nuxeo.ecm.platform.forms.layout.api.WidgetDefinition;
024import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOption;
025import org.nuxeo.ecm.platform.forms.layout.api.converters.AbstractWidgetDefinitionConverter;
026import org.nuxeo.ecm.platform.forms.layout.api.converters.LayoutConversionContext;
027import org.nuxeo.ecm.platform.forms.layout.io.plugins.helpers.VocabularyHelper;
028
029/**
030 * Converter that adds translated select options to widgets rendering directory items
031 *
032 * @since 5.5
033 */
034public class WidgetDirectoryItemsConverter extends AbstractWidgetDefinitionConverter {
035
036    public static final String DIR_NAME_PROPERTY = "directoryName";
037
038    protected static enum SUPPORTED_DIR_TYPES {
039        selectOneDirectory, selectManyDirectory, selectOneRadioDirectory, selectManyCheckboxDirectory, suggestOneDirectory, suggestManyDirectory
040    }
041
042    /**
043     * @since 7.3
044     */
045    public static boolean isDirectoryWidget(String wType) {
046        for (SUPPORTED_DIR_TYPES item : SUPPORTED_DIR_TYPES.values()) {
047            if (item.name().equals(wType)) {
048                return true;
049            }
050        }
051        return false;
052    }
053
054    @Override
055    public WidgetDefinition getWidgetDefinition(WidgetDefinition widgetDef, LayoutConversionContext ctx) {
056        String wType = widgetDef.getType();
057        if (isDirectoryWidget(wType)) {
058            String dirName = (String) widgetDef.getProperties(BuiltinModes.ANY, BuiltinModes.ANY).get(DIR_NAME_PROPERTY);
059            if (dirName == null) {
060                dirName = (String) widgetDef.getProperties(BuiltinModes.ANY, BuiltinWidgetModes.EDIT).get(
061                        DIR_NAME_PROPERTY);
062            }
063            if (dirName == null) {
064                dirName = (String) widgetDef.getProperties(BuiltinModes.ANY, BuiltinWidgetModes.VIEW).get(
065                        DIR_NAME_PROPERTY);
066            }
067            if (dirName != null) {
068                WidgetDefinition clone = getClonedWidget(widgetDef);
069                // change select options on new widget
070                WidgetSelectOption[] selectOptions = VocabularyHelper.getVocabularySelectOptions(dirName,
071                        ctx.getLanguage()).toArray(new WidgetSelectOption[] {});
072                clone.setSelectOptions(selectOptions);
073                return clone;
074            }
075        }
076        return widgetDef;
077    }
078}