001/*
002 * (C) Copyright 2014 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.ui.web.directory;
020
021import javax.faces.model.SelectItem;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.directory.DirectoryException;
027import org.nuxeo.ecm.directory.Session;
028import org.nuxeo.ecm.directory.api.DirectoryService;
029import org.nuxeo.ecm.platform.ui.web.component.SelectItemFactory;
030
031/**
032 * @since 6.0
033 */
034public abstract class DirectorySelectItemFactory extends SelectItemFactory {
035
036    private static final Log log = LogFactory.getLog(DirectorySelectItemFactory.class);
037
038    protected abstract String getDirectoryName();
039
040    protected Session getDirectorySession() {
041        String dirName = getDirectoryName();
042        return getDirectorySession(dirName);
043    }
044
045    protected static Session getDirectorySession(String dirName) {
046        Session directorySession = null;
047        if (dirName != null) {
048            try {
049                DirectoryService service = DirectoryHelper.getDirectoryService();
050                directorySession = service.open(dirName);
051            } catch (DirectoryException e) {
052                log.error(String.format("Error when retrieving directory %s", dirName), e);
053            }
054        }
055        return directorySession;
056    }
057
058    /**
059     * @deprecated since 7.4. Directory sessions are now AutoCloseable.
060     */
061    @Deprecated
062    protected static void closeDirectorySession(Session directorySession) {
063        if (directorySession != null) {
064            try {
065                directorySession.close();
066            } catch (DirectoryException e) {
067            }
068        }
069    }
070
071    @Override
072    public SelectItem createSelectItem(Object value) {
073        SelectItem item = null;
074        if (value instanceof SelectItem) {
075            Object varValue = saveRequestMapVarValue();
076            try {
077                putIteratorToRequestParam(value);
078                item = createSelectItem();
079                removeIteratorFromRequestParam();
080            } finally {
081                restoreRequestMapVarValue(varValue);
082            }
083        } else if (value instanceof String) {
084            Object varValue = saveRequestMapVarValue();
085            try (Session directorySession = getDirectorySession()) {
086                String entryId = (String) value;
087                if (directorySession != null) {
088                    try {
089                        DocumentModel entry = directorySession.getEntry(entryId);
090                        if (entry != null) {
091                            putIteratorToRequestParam(entry);
092                            item = createSelectItem();
093                            removeIteratorFromRequestParam();
094                        }
095                    } catch (DirectoryException e) {
096                    }
097                } else {
098                    log.error("No session provided for directory, returning empty selection");
099                }
100            } finally {
101                restoreRequestMapVarValue(varValue);
102            }
103        }
104        return item;
105    }
106
107}