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