001/*
002 * (C) Copyright 2011 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 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.platform.forms.layout.io.plugins.helpers;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.DocumentModelList;
032import org.nuxeo.ecm.core.api.PropertyException;
033import org.nuxeo.ecm.directory.DirectoryException;
034import org.nuxeo.ecm.directory.Session;
035import org.nuxeo.ecm.directory.api.DirectoryService;
036import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOption;
037import org.nuxeo.ecm.platform.forms.layout.api.impl.WidgetSelectOptionImpl;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Exports Vocabularies as translated SelectOptions
042 *
043 * @author Tiry (tdelprat@nuxeo.com)
044 * @since 5.5
045 */
046public class VocabularyHelper {
047
048    private static final Log log = LogFactory.getLog(VocabularyHelper.class);
049
050    public static final String PARENT_PROPERTY_NAME = "parent";
051
052    public static final String LABEL_PROPERTY_NAME = "label";
053
054    public static final String SUBDIRECTORY_SEPARATOR = "/";
055
056    public static List<WidgetSelectOption> getVocabularySelectOptions(String dirName, String lang) {
057        DirectoryService ds = Framework.getLocalService(DirectoryService.class);
058        try (Session session = ds.open(dirName)) {
059            String schema = ds.getDirectory(dirName).getSchema();
060            DocumentModelList entries = session.getEntries();
061            return convertToSelectOptions(entries, schema, dirName, lang);
062        } catch (DirectoryException e) {
063            log.error("Error while getting content of directory " + dirName, e);
064            return Collections.emptyList();
065        }
066    }
067
068    public static List<WidgetSelectOption> getChainSelectVocabularySelectOptions(String parentDirName,
069            String childDirName, String lang) {
070        DirectoryService ds = Framework.getLocalService(DirectoryService.class);
071        List<WidgetSelectOption> result = new ArrayList<WidgetSelectOption>();
072        try (Session session = ds.open(parentDirName); Session subSession = ds.open(childDirName)) {
073            String schema = ds.getDirectory(parentDirName).getSchema();
074            String subSchema = ds.getDirectory(childDirName).getSchema();
075
076            DocumentModelList entries = session.getEntries();
077            for (DocumentModel entry : entries) {
078                String itemValue = entry.getId();
079                String itemLabel = (String) entry.getProperty(schema, LABEL_PROPERTY_NAME);
080                if (lang != null) {
081                    itemLabel = TranslationHelper.getTranslation(itemLabel, lang);
082                }
083                WidgetSelectOption selectOption = new WidgetSelectOptionImpl(itemLabel, itemValue);
084                result.add(selectOption);
085
086                Map<String, Serializable> filter = new HashMap<String, Serializable>();
087                filter.put(PARENT_PROPERTY_NAME, itemValue);
088                DocumentModelList subEntries = subSession.query(filter, null);
089                for (DocumentModel subEntry : subEntries) {
090                    String subItemValue = itemValue + SUBDIRECTORY_SEPARATOR + subEntry.getId();
091                    String subItemLabel = (String) subEntry.getProperty(subSchema, LABEL_PROPERTY_NAME);
092                    if (lang != null) {
093                        subItemLabel = TranslationHelper.getTranslation(subItemLabel, lang);
094                    }
095                    String subItemCompleteLabel = itemLabel + SUBDIRECTORY_SEPARATOR + subItemLabel;
096                    WidgetSelectOption subSelectOption = new WidgetSelectOptionImpl(subItemCompleteLabel, subItemValue);
097                    result.add(subSelectOption);
098                }
099            }
100            return result;
101        }
102    }
103
104    public static List<WidgetSelectOption> convertToSelectOptions(DocumentModelList entries, String schema,
105            String directoryName, String lang) {
106        List<WidgetSelectOption> res = new ArrayList<WidgetSelectOption>();
107        for (DocumentModel entry : entries) {
108            String itemValue = entry.getId();
109            String itemLabel = itemValue;
110            try {
111                itemLabel = (String) entry.getProperty(schema, LABEL_PROPERTY_NAME);
112                if (lang != null) {
113                    itemLabel = TranslationHelper.getTranslation(itemLabel, lang);
114                }
115            } catch (PropertyException e) {
116                if (lang != null) {
117                    // try out l10n vocabulary structure
118                    itemLabel = (String) entry.getProperty(schema, LABEL_PROPERTY_NAME + "_" + lang);
119                }
120            }
121            WidgetSelectOption selectOption = new WidgetSelectOptionImpl(itemLabel, itemValue);
122            res.add(selectOption);
123        }
124        return res;
125    }
126
127}