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.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.automation.features.SuggestConstants;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentModelList;
031import org.nuxeo.ecm.core.api.DocumentRef;
032import org.nuxeo.ecm.core.api.IdRef;
033import org.nuxeo.ecm.core.api.PathRef;
034import org.nuxeo.ecm.core.query.sql.NXQL;
035
036import net.sf.json.JSONArray;
037import net.sf.json.JSONObject;
038
039/**
040 * Group fields and methods used at initialization and runtime for select2 feature.
041 *
042 * @since 5.7.3
043 */
044public class Select2Common extends SuggestConstants {
045
046    private static final Log log = LogFactory.getLog(Select2Common.class);
047
048    // no instantiation
049    private Select2Common() {
050    }
051
052    public static final String LOCKED = "locked";
053
054    public static final String PLACEHOLDER = "placeholder";
055
056    public static final List<String> SELECT2_USER_WIDGET_TYPE_LIST = new ArrayList<String>(
057            Arrays.asList("singleUserSuggestion", "multipleUsersSuggestion"));
058
059    public static final List<String> SELECT2_DOC_WIDGET_TYPE_LIST = new ArrayList<String>(
060            Arrays.asList("singleDocumentSuggestion", "multipleDocumentsSuggestion"));
061
062    public static final String SUGGESTION_FORMATTER = "suggestionFormatter";
063
064    public static final String SELECTION_FORMATTER = "selectionFormatter";
065
066    public static final String USER_DEFAULT_SUGGESTION_FORMATTER = "userEntryDefaultFormatter";
067
068    public static final String DOC_DEFAULT_SUGGESTION_FORMATTER = "docEntryDefaultFormatter";
069
070    public static final List<String> SELECT2_DIR_WIDGET_TYPE_LIST = new ArrayList<String>(
071            Arrays.asList("suggestOneDirectory", "suggestManyDirectory"));
072
073    public static final List<String> SELECT2_DEFAULT_DOCUMENT_SCHEMAS = new ArrayList<String>(
074            Arrays.asList("dublincore", "common"));
075
076    public static final String DIR_DEFAULT_SUGGESTION_FORMATTER = "dirEntryDefaultFormatter";
077
078    public static final String READ_ONLY_PARAM = "readonly";
079
080    public static final String RERENDER_JS_FUNCTION_NAME = "reRenderFunctionName";
081
082    public static final String AJAX_RERENDER = "ajaxReRender";
083
084    public static final String USER_DEFAULT_SELECTION_FORMATTER = "userSelectionDefaultFormatter";
085
086    public static final String DOC_DEFAULT_SELECTION_FORMATTER = "docSelectionDefaultFormatter";
087
088    public static final String DIR_DEFAULT_SELECTION_FORMATTER = "dirSelectionDefaultFormatter";
089
090    public static final String WIDTH = "width";
091
092    public static final String DEFAULT_WIDTH = "300";
093
094    public static final String MIN_CHARS = "minChars";
095
096    public static final int DEFAULT_MIN_CHARS = 3;
097
098    public static final String TITLE = "title";
099
100    public static final String OPERATION_ID = "operationId";
101
102    /**
103     * @since 5.9.3
104     */
105    public static String[] getDefaultSchemas() {
106        return getSchemas(null);
107    }
108
109    /**
110     * Returns an array containing the given schema names plus the default ones if not included
111     *
112     * @param schemaNames
113     * @since 5.8
114     */
115    public static String[] getSchemas(final String schemaNames) {
116        List<String> result = new ArrayList<String>();
117        result.addAll(Select2Common.SELECT2_DEFAULT_DOCUMENT_SCHEMAS);
118        String[] temp = null;
119        if (schemaNames != null && !schemaNames.isEmpty()) {
120            temp = schemaNames.split(",");
121        }
122        if (temp != null) {
123            for (String s : temp) {
124                result.add(s);
125            }
126        }
127        return result.toArray(new String[result.size()]);
128    }
129
130    /**
131     * @since 6.0
132     */
133    public static String resolveDefaultEntries(final List<String> list) {
134        if (list == null || list.isEmpty()) {
135            return "[]";
136        } else {
137            JSONArray result = new JSONArray();
138            for (String l : list) {
139                JSONObject obj = new JSONObject();
140                obj.element(Select2Common.ID, l);
141                obj.element(Select2Common.LABEL, l);
142                result.add(obj);
143            }
144            return result.toString();
145        }
146    }
147
148    /**
149     * Finds a document by the given property and value. If the property is null or empty the value is considered as a
150     * {@link DocumentRef}.
151     *
152     * @since 9.2
153     */
154    public static DocumentModel resolveReference(String property, String value, CoreSession session) {
155        if (property != null && !property.isEmpty()) {
156            String query = "select * from Document where " + property + "=" + NXQL.escapeString(value);
157            DocumentModelList docs = session.query(query);
158            if (docs.size() > 0) {
159                return docs.get(0);
160            }
161            log.warn("Unable to resolve doc using property " + property + " and value " + value);
162            return null;
163        }
164        DocumentRef ref = null;
165        if (value.startsWith("/")) {
166            ref = new PathRef(value);
167        } else {
168            ref = new IdRef(value);
169        }
170        if (session.exists(ref)) {
171            return session.getDocument(ref);
172        }
173        log.warn("Unable to resolve reference on " + ref);
174        return null;
175    }
176
177}