001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018package org.nuxeo.ecm.webapp.directory;
019
020import static org.jboss.seam.ScopeType.CONVERSATION;
021import static org.jboss.seam.annotations.Install.FRAMEWORK;
022
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Locale;
028import java.util.Map;
029
030import javax.faces.context.FacesContext;
031
032import org.apache.commons.lang.StringUtils;
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.jboss.seam.annotations.Install;
036import org.jboss.seam.annotations.Name;
037import org.jboss.seam.annotations.Observer;
038import org.jboss.seam.annotations.Scope;
039import org.nuxeo.ecm.core.api.DocumentModel;
040import org.nuxeo.ecm.directory.Directory;
041import org.nuxeo.ecm.directory.DirectoryException;
042import org.nuxeo.ecm.directory.Session;
043import org.nuxeo.ecm.directory.api.DirectoryService;
044import org.nuxeo.ecm.webapp.helpers.EventNames;
045import org.nuxeo.runtime.api.Framework;
046
047/**
048 * Manage localized vocabulary trees. These trees use {@code VocabularyTreeNode}
049 *
050 * @since 5.5
051 * @author <a href="mailto:qlamerand@nuxeo.com">Quentin Lamerand</a>
052 */
053@Scope(CONVERSATION)
054@Name("vocabularyTreeActions")
055@Install(precedence = FRAMEWORK)
056public class VocabularyTreeActions implements Serializable {
057
058    private static final long serialVersionUID = 1L;
059
060    private static final Log log = LogFactory.getLog(VocabularyTreeActions.class);
061
062    /**
063     * @deprecated since 5.9.2.
064     */
065    @Deprecated
066    public static final String L10NVOCABULARY_SCHEMA = "l10nvocabulary";
067
068    /**
069     * @deprecated since 5.9.2.
070     */
071    @Deprecated
072    public static final String L10NXVOCABULARY_SCHEMA = "l10nxvocabulary";
073
074    protected transient Map<String, VocabularyTreeNode> treeModels;
075
076    public VocabularyTreeNode get(String vocabularyName, boolean displayObsoleteEntries, char keySeparator,
077            String orderingField) {
078        if (treeModels == null) {
079            treeModels = new HashMap<String, VocabularyTreeNode>();
080        }
081        VocabularyTreeNode treeModel = treeModels.get(vocabularyName);
082        if (treeModel != null) {
083            // return cached model
084            return treeModel;
085        }
086        DirectoryService directoryService = Framework.getLocalService(DirectoryService.class);
087        try {
088            Directory directory = Framework.getLocalService(DirectoryService.class).getDirectory(vocabularyName);
089            if (directory == null) {
090                throw new DirectoryException(vocabularyName + " is not a registered directory");
091            }
092        } catch (DirectoryException e) {
093            throw new RuntimeException(e);
094        }
095
096        treeModel = new VocabularyTreeNode(0, "", "", "", vocabularyName, directoryService, displayObsoleteEntries,
097                keySeparator, orderingField);
098
099        treeModels.put(vocabularyName, treeModel);
100        return treeModel;
101    }
102
103    public List<VocabularyTreeNode> getRoots(String vocabularyName, boolean displayObsoleteEntries, char keySeparator,
104            String orderingField) {
105        return get(vocabularyName, displayObsoleteEntries, keySeparator, orderingField).getChildren();
106    }
107
108    public String getLabelFor(String vocabularyName, String path, char keySeparator) {
109        Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
110        String schemaName = null;
111        List<String> labels = new ArrayList<String>();
112        DirectoryService directoryService = Framework.getLocalService(DirectoryService.class);
113        try (Session session = directoryService.open(vocabularyName)) {
114            schemaName = directoryService.getDirectorySchema(vocabularyName);
115            for (String id : StringUtils.split(path, keySeparator)) {
116                DocumentModel entry = session.getEntry(id);
117                if (entry != null) {
118                    String computeLabel = VocabularyTreeNode.computeLabel(locale, entry, schemaName);
119                    if (computeLabel == null) {
120                        labels.add(id);
121                    } else {
122                        labels.add(computeLabel);
123                    }
124                } else {
125                    labels.add(id);
126                }
127            }
128        } catch (DirectoryException e) {
129            log.error("Error while accessing directory " + vocabularyName, e);
130        }
131
132        if (labels.isEmpty()) {
133            return null;
134        } else {
135            return StringUtils.join(labels, keySeparator);
136        }
137    }
138
139    @Observer(EventNames.DIRECTORY_CHANGED)
140    public void invalidate(String vocabularyName) {
141        if (treeModels != null) {
142            treeModels.remove(vocabularyName);
143        }
144    }
145
146}