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