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