001/*
002 * (C) Copyright 2013 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 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
018 */
019package org.nuxeo.ecm.platform.ui.select2.common;
020
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.List;
024
025import org.nuxeo.ecm.automation.features.SuggestConstants;
026
027import net.sf.json.JSONArray;
028import net.sf.json.JSONObject;
029
030/**
031 * Group fields and methods used at initialization and runtime for select2 feature.
032 *
033 * @since 5.7.3
034 */
035public class Select2Common extends SuggestConstants {
036    
037    // no instantiation
038    private Select2Common() {
039    }
040    
041    public static final String LOCKED = "locked";
042
043    public static final String PLACEHOLDER = "placeholder";
044
045    public static final List<String> SELECT2_USER_WIDGET_TYPE_LIST = new ArrayList<String>(Arrays.asList(
046            "singleUserSuggestion", "multipleUsersSuggestion"));
047
048    public static final List<String> SELECT2_DOC_WIDGET_TYPE_LIST = new ArrayList<String>(Arrays.asList(
049            "singleDocumentSuggestion", "multipleDocumentsSuggestion"));
050
051    public static final String SUGGESTION_FORMATTER = "suggestionFormatter";
052
053    public static final String SELECTION_FORMATTER = "selectionFormatter";
054
055    public static final String USER_DEFAULT_SUGGESTION_FORMATTER = "userEntryDefaultFormatter";
056
057    public static final String DOC_DEFAULT_SUGGESTION_FORMATTER = "docEntryDefaultFormatter";
058
059    public static final List<String> SELECT2_DIR_WIDGET_TYPE_LIST = new ArrayList<String>(Arrays.asList(
060            "suggestOneDirectory", "suggestManyDirectory"));
061
062    public static final List<String> SELECT2_DEFAULT_DOCUMENT_SCHEMAS = new ArrayList<String>(Arrays.asList(
063            "dublincore", "common"));
064
065    public static final String DIR_DEFAULT_SUGGESTION_FORMATTER = "dirEntryDefaultFormatter";
066
067    public static final String READ_ONLY_PARAM = "readonly";
068
069    public static final String RERENDER_JS_FUNCTION_NAME = "reRenderFunctionName";
070
071    public static final String AJAX_RERENDER = "ajaxReRender";
072
073    public static final String USER_DEFAULT_SELECTION_FORMATTER = "userSelectionDefaultFormatter";
074
075    public static final String DOC_DEFAULT_SELECTION_FORMATTER = "docSelectionDefaultFormatter";
076
077    public static final String DIR_DEFAULT_SELECTION_FORMATTER = "dirSelectionDefaultFormatter";
078
079    public static final String WIDTH = "width";
080
081    public static final String DEFAULT_WIDTH = "300";
082
083    public static final String MIN_CHARS = "minChars";
084
085    public static final int DEFAULT_MIN_CHARS = 3;
086
087    public static final String TITLE = "title";
088
089    public static final String OPERATION_ID = "operationId";
090
091    /**
092     * @since 5.9.3
093     */
094    public static String[] getDefaultSchemas() {
095        return getSchemas(null);
096    }
097
098    /**
099     * Returns an array containing the given schema names plus the default ones if not included
100     *
101     * @param schemaNames
102     * @since 5.8
103     */
104    public static String[] getSchemas(final String schemaNames) {
105        List<String> result = new ArrayList<String>();
106        result.addAll(Select2Common.SELECT2_DEFAULT_DOCUMENT_SCHEMAS);
107        String[] temp = null;
108        if (schemaNames != null && !schemaNames.isEmpty()) {
109            temp = schemaNames.split(",");
110        }
111        if (temp != null) {
112            for (String s : temp) {
113                result.add(s);
114            }
115        }
116        return result.toArray(new String[result.size()]);
117    }
118
119    /**
120     * @since 6.0
121     */
122    public static String resolveDefaultEntries(final List<String> list) {
123        if (list == null || list.isEmpty()) {
124            return "[]";
125        } else {
126            JSONArray result = new JSONArray();
127            for (String l : list) {
128                JSONObject obj = new JSONObject();
129                obj.element(Select2Common.ID, l);
130                obj.element(Select2Common.LABEL, l);
131                result.add(obj);
132            }
133            return result.toString();
134        }
135    }
136
137}