001/*
002 * (C) Copyright 2016 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 */
018package org.nuxeo.ecm.platform.rendition.action;
019
020import static org.jboss.seam.ScopeType.EVENT;
021
022import java.io.Serializable;
023import java.util.Arrays;
024import java.util.LinkedHashMap;
025import java.util.List;
026import java.util.Map;
027import java.util.Map.Entry;
028
029import org.jboss.seam.annotations.Name;
030import org.jboss.seam.annotations.Scope;
031import org.nuxeo.ecm.platform.rendition.automation.SuggestRenditionDefinitionEntry;
032import org.nuxeo.ecm.platform.ui.select2.common.Select2Common;
033
034/**
035 * Helper component for rendition name widget relying on select2.
036 *
037 * @since 8.3
038 */
039@Name("renditionDefinitionSelect2Support")
040@Scope(EVENT)
041public class RenditionDefinitionSelect2Support {
042
043    protected String label;
044
045    public String resolveRenditionDefinitions(List<String> list) {
046        return Select2Common.resolveDefaultEntries(list);
047    }
048
049    public String resolveRenditionDefinitions(String[] array) {
050        if (array == null || array.length == 0) {
051            return Select2Common.resolveDefaultEntries(null);
052        }
053        return Select2Common.resolveDefaultEntries(Arrays.asList(array));
054    }
055
056    protected void reset() {
057        label = null;
058    }
059
060    public String encodeParameters(Map<String, Serializable> widgetProperties) {
061        return encodeCommonParameters(widgetProperties).toString();
062    }
063
064    protected Map<String, Object> encodeCommonParameters(Map<String, Serializable> widgetProperties) {
065        return encodeCommonParameters(widgetProperties, null);
066    }
067
068    protected Map<String, Object> encodeCommonParameters(Map<String, Serializable> widgetProperties,
069            Map<String, String> additionalParameters) {
070        Map<String, Object> obj = new LinkedHashMap<>();
071        obj.put("multiple", "true");
072        obj.put(Select2Common.MIN_CHARS, "1");
073        obj.put(Select2Common.READ_ONLY_PARAM, "false");
074        obj.put(Select2Common.OPERATION_ID, SuggestRenditionDefinitionEntry.ID);
075        obj.put(Select2Common.WIDTH, "300px");
076        obj.put(Select2Common.SELECTION_FORMATTER, "formatSelectedRenditionDefinitions");
077        obj.put(Select2Common.SUGGESTION_FORMATTER, "formatSuggestedRenditionDefinitions");
078        obj.put("tokenSeparators", Arrays.asList(",", " "));
079        if (additionalParameters != null) {
080            for (Entry<String, String> entry : additionalParameters.entrySet()) {
081                obj.put(entry.getKey(), entry.getValue());
082            }
083        }
084        for (Entry<String, Serializable> entry : widgetProperties.entrySet()) {
085            obj.put(entry.getKey(), entry.getValue().toString());
086        }
087        return obj;
088    }
089
090    public String getLabel() {
091        return label;
092    }
093
094    public void setLabel(String label) {
095        this.label = label;
096    }
097
098}