001/*
002 * (C) Copyright 2016-2018 Nuxeo (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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.automation.features;
020
021import java.util.Map;
022
023import org.apache.commons.lang3.StringUtils;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.schema.types.Schema;
027import org.nuxeo.ecm.platform.usermanager.UserConfig;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.services.config.ConfigurationService;
030
031/**
032 * Constants used to generate JSON for suggestion in operations.
033 */
034public class SuggestConstants {
035
036    private static final Log log = LogFactory.getLog(SuggestConstants.class);
037
038    public static final String LANG_TOKEN = "{lang}";
039
040    private static final String FORCE_DISPLAY_EMAIL_IN_SUGGESTION = "nuxeo.ui.displayEmailInUserSuggestion";
041
042    public static final String ID = "id";
043
044    public static final String LABEL = "displayLabel";
045
046    public static final String ICON = "icon";
047
048    public static final String DEFAULT_LANG = "en";
049
050    public static final String USER_TYPE = "USER_TYPE";
051
052    public static final String GROUP_TYPE = "GROUP_TYPE";
053
054    public static final String TYPE_KEY_NAME = "type";
055
056    public static final String PREFIXED_ID_KEY_NAME = "prefixed_id";
057
058    protected static Boolean forceDisplayEmailInSuggestion = null;
059
060    public static final String DISPLAY_ICON = "displayIcon";
061
062    public static final String PARENT_FIELD_ID = "parent";
063
064    public static final String OBSOLETE_FIELD_ID = "obsolete";
065
066    public static final String ABSOLUTE_LABEL = "absoluteLabel";
067
068    public static final String COMPUTED_ID = "computedId";
069
070    public static final String WARN_MESSAGE_LABEL = "warn_message";
071
072    public static final String DIRECTORY_ORDER_FIELD_NAME = "ordering";
073
074    public static final String DIRECTORY_DEFAULT_LABEL_COL_NAME = "label";
075
076    public static final String DEFAULT_KEY_SEPARATOR = "/";
077
078    public static void computeGroupLabel(final Map<String, Object> obj, final String groupId, final String groupLabelField,
079            final boolean hideFirstLabel) {
080        String label = null;
081        if (hideFirstLabel) {
082            label = groupId;
083        } else {
084            String groupLabelValue = (String) obj.get(groupLabelField);
085            if (StringUtils.isNotBlank(groupLabelValue)) {
086                label = groupLabelValue;
087            } else {
088                label = groupId;
089            }
090        }
091        obj.put(LABEL, label);
092    }
093
094    public static void computeUserGroupIcon(final Map<String, Object> obj, final boolean hideIcon) {
095        if (obj != null) {
096            if (!hideIcon) {
097                String userGroupType = (String) obj.get(TYPE_KEY_NAME);
098                obj.put(DISPLAY_ICON, StringUtils.isNotBlank(userGroupType)
099                        && (userGroupType.equals(USER_TYPE) || userGroupType.equals(GROUP_TYPE)));
100            }
101        }
102    }
103
104    public static void computeUserLabel(final Map<String, Object> obj, final String firstLabelField,
105            final String secondLabelField, final String thirdLabelField, final boolean hideFirstLabel,
106            final boolean hideSecondLabel, final boolean hideThirdLabel, boolean displayEmailInSuggestion,
107            final String userId) {
108        String result = "";
109        if (obj != null) {
110
111            if (StringUtils.isNotBlank(firstLabelField) && !hideFirstLabel) {
112                // If firtLabelField given and first label not hidden
113                final String firstLabel = (String) obj.get(firstLabelField);
114                result += StringUtils.isNotBlank(firstLabel) ? firstLabel : "";
115            } else if (!hideFirstLabel) {
116                // Else we use firstname
117                final String firstname = (String) obj.get(UserConfig.FIRSTNAME_COLUMN);
118                result += StringUtils.isNotBlank(firstname) ? firstname : "";
119            }
120
121            if (StringUtils.isNotBlank(secondLabelField) && !hideSecondLabel) {
122                // If secondLabelField given and second label not hidden
123                final String secondLabel = (String) obj.get(firstLabelField);
124                if (StringUtils.isNotBlank(secondLabel)) {
125                    if (StringUtils.isNotBlank(result)) {
126                        result += " ";
127                    }
128                    result += secondLabel;
129                }
130            } else if (!hideSecondLabel) {
131                // Else we use lastname
132                final String lastname = (String) obj.get(UserConfig.LASTNAME_COLUMN);
133                if (StringUtils.isNotBlank(lastname)) {
134                    if (StringUtils.isNotBlank(result)) {
135                        result += " ";
136                    }
137                    result += lastname;
138                }
139            }
140            if (StringUtils.isBlank(result)) {
141                // At this point, if returned label is empty, we use user id
142                result += StringUtils.isNotBlank(userId) ? userId : "";
143            }
144
145            if (isForceDisplayEmailInSuggestion() || (displayEmailInSuggestion && !hideThirdLabel)) {
146                if (StringUtils.isNotBlank(thirdLabelField)) {
147                    final String thirdLabel = (String) obj.get(thirdLabelField);
148                    if (StringUtils.isNotBlank(thirdLabel)) {
149                        if (StringUtils.isNotBlank(result)) {
150                            result += " ";
151                        }
152                        result += thirdLabel;
153                    }
154                } else {
155                    // Else we use email
156                    String email = (String) obj.get(UserConfig.EMAIL_COLUMN);
157                    if (StringUtils.isNotBlank(email)) {
158                        if (StringUtils.isNotBlank(result)) {
159                            result += " ";
160                        }
161                        result += email;
162                    }
163                }
164            }
165
166            obj.put(LABEL, result);
167        }
168    }
169
170    /**
171     * Compute the field name of the directory that holds the value that we want to display.
172     *
173     * @param schema the directory schema
174     * @param dbl10n are translations carried by directory fields
175     * @param labelFieldName the name or pattern of the fields that held values
176     * @param lang the current language
177     * @throws IllegalArgumentException when cannot compute label field name
178     * @return the final field name where we pick up the value
179     * @since 5.7.3
180     */
181    public static String getLabelFieldName(final Schema schema, boolean dbl10n, String labelFieldName,
182            final String lang) {
183        if (labelFieldName == null || labelFieldName.isEmpty()) {
184            // No labelFieldName provided, we assume it is 'label'
185            labelFieldName = DIRECTORY_DEFAULT_LABEL_COL_NAME;
186        }
187        if (dbl10n) {
188            int i = labelFieldName.indexOf(LANG_TOKEN);
189            if (i >= 0) {
190                // a pattern is provided, let's compute the field name
191                // according
192                // to the current lang
193                StringBuilder sb = new StringBuilder();
194                sb.append(labelFieldName.substring(0, i));
195                sb.append(lang);
196                sb.append(labelFieldName.substring(i + LANG_TOKEN.length()));
197                String result = sb.toString();
198                if (schema.getField(result) != null) {
199                    return result;
200                } else {
201                    // there is no field for the current lang, let's pick
202                    // english by default
203                    sb = new StringBuilder();
204                    sb.append(labelFieldName.substring(0, i));
205                    sb.append(DEFAULT_LANG);
206                    sb.append(labelFieldName.substring(i + LANG_TOKEN.length()));
207                    return sb.toString();
208                }
209            } else {
210                // No pattern
211                String result = labelFieldName + "_" + lang;
212                if (schema.getField(result) != null) {
213                    // we assume that fields are named like 'xxx_en',
214                    // 'xxx_fr', etc.
215                    return result;
216                }
217
218                log.warn(String.format(
219                        "Unable to find field %s in directory schema %s. Trying to fallback on default one.",
220                        labelFieldName, schema.getName()));
221
222                result = DIRECTORY_DEFAULT_LABEL_COL_NAME + "_" + DEFAULT_LANG;
223                if (schema.getField(result) != null) {
224                    // no available locale, fallback to english by default
225                    return result;
226                }
227                result = DIRECTORY_DEFAULT_LABEL_COL_NAME;
228                if (schema.getField(result) != null) {
229                    // no available default locale, fallback to label
230                    return result;
231                }
232
233                if (schema.getField(labelFieldName) != null) {
234                    // let's pretend this is not dbl10n
235                    return labelFieldName;
236                }
237
238                throw new IllegalArgumentException(String.format("Unable to find field %s in directory schema %s",
239                        labelFieldName, schema.getName()));
240            }
241        } else {
242            if (schema.getField(labelFieldName) != null) {
243                return labelFieldName;
244            } else {
245                throw new IllegalArgumentException(String.format("Unable to find field %s in directory schema %s",
246                        labelFieldName, schema.getName()));
247            }
248        }
249    }
250
251    protected static boolean isForceDisplayEmailInSuggestion() {
252        if (forceDisplayEmailInSuggestion == null) {
253            ConfigurationService cs = Framework.getService(ConfigurationService.class);
254            forceDisplayEmailInSuggestion = cs.isBooleanTrue(FORCE_DISPLAY_EMAIL_IN_SUGGESTION);
255        }
256        return forceDisplayEmailInSuggestion;
257    }
258
259    // no instantiation
260    protected SuggestConstants() {
261    }
262
263}