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 java.util.List;
022
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 * Abstract class to convert a chained vocabulary.
031 * <p>
032 * Only supports vocabularies with 2 levels for now
033 *
034 * @since 5.5
035 */
036public abstract class AbstractChainedVocabularyWidgetConverter extends AbstractWidgetDefinitionConverter {
037
038    protected boolean isAccepted(String wType) {
039        return "template".equals(wType) || WidgetDirectoryItemsConverter.isDirectoryWidget(wType);
040    }
041
042    protected abstract List<String> getAcceptedWidgetNames();
043
044    protected abstract String getParentDirectoryName();
045
046    protected abstract String getChildDirectoryName();
047
048    @Override
049    public WidgetDefinition getWidgetDefinition(WidgetDefinition widgetDef, LayoutConversionContext ctx) {
050        String wType = widgetDef.getType();
051        String wName = widgetDef.getName();
052        if (getAcceptedWidgetNames().contains(wName) && isAccepted(wType)) {
053            WidgetDefinition clone = getClonedWidget(widgetDef);
054            // change select options on new widget
055            WidgetSelectOption[] selectOptions = VocabularyHelper.getChainSelectVocabularySelectOptions(
056                    getParentDirectoryName(), getChildDirectoryName(), ctx.getLanguage()).toArray(
057                    new WidgetSelectOption[] {});
058            clone.setSelectOptions(selectOptions);
059            return clone;
060        }
061        return widgetDef;
062    }
063}