001/* 002 * (C) Copyright 2006-2015 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 * Nuxeo - initial API and implementation 016 * 017 */ 018 019package org.nuxeo.ecm.webapp.base; 020 021import java.security.Principal; 022import java.util.ArrayList; 023import java.util.List; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.jboss.seam.annotations.In; 028import org.jboss.seam.faces.FacesMessages; 029import org.jboss.seam.international.StatusMessage; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.DocumentRef; 032import org.nuxeo.ecm.core.api.NuxeoPrincipal; 033import org.nuxeo.ecm.platform.actions.ejb.ActionManager; 034import org.nuxeo.ecm.platform.types.TypeManager; 035import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 036import org.nuxeo.ecm.platform.ui.web.rest.FancyNavigationHandler; 037import org.nuxeo.ecm.webapp.action.TypesTool; 038import org.nuxeo.ecm.webapp.helpers.EventManager; 039import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor; 040 041/** 042 * Contains generic functionality usable by all action listeners. 043 * 044 * @author <a href="mailto:rcaraghin@nuxeo.com">Razvan Caraghin</a> 045 */ 046public abstract class InputController { 047 048 private static final Log log = LogFactory.getLog(InputController.class); 049 050 @In(create = true) 051 protected ActionManager actionManager; 052 053 @In(create = true) 054 protected TypeManager typeManager; 055 056 @In(create = true) 057 protected NavigationContext navigationContext; 058 059 @In(create = true) 060 protected EventManager eventManager; 061 062 @In(required = false, create = true) 063 /** 064 * @deprecated Since 5.2. Injecting current document is not a good idea, should be fetched from navigationContext directly. 065 */ 066 @Deprecated 067 protected DocumentModel currentDocument; 068 069 @In(create = true, required = false) 070 protected FacesMessages facesMessages; 071 072 @In(create = true) 073 // won't inject this because of seam problem after activation 074 // ::protected Map<String, String> messages; 075 protected ResourcesAccessor resourcesAccessor; 076 077 @In(create = true) 078 protected TypesTool typesTool; 079 080 @In(create = true, required = false) 081 protected Principal currentUser; 082 083 /** 084 * Utility method that helps remove a {@link DocumentModel} from a list. The document models are compared on 085 * {@link DocumentRef}s. 086 * 087 * @param documentList 088 * @param document 089 */ 090 public void removeDocumentFromList(List<DocumentModel> documentList, DocumentModel document) { 091 if (null == document) { 092 log.error("Received nul doc, not removing anything..."); 093 return; 094 } 095 096 log.debug("Removing document " + document.getId() + " from list..."); 097 098 for (int i = 0; i < documentList.size(); i++) { 099 if (documentList.get(i).getRef().equals(document.getRef())) { 100 documentList.remove(i); 101 } 102 } 103 } 104 105 /** 106 * Logs a {@link DocumentModel} title and the passed string (info). 107 */ 108 public void logDocumentWithTitle(String someLogString, DocumentModel document) { 109 if (null != document) { 110 log.trace('[' + getClass().getSimpleName() + "] " + someLogString + ' ' + document.getId()); 111 log.debug("CURRENT DOC PATH: " + document.getPathAsString()); 112 } else { 113 log.trace('[' + getClass().getSimpleName() + "] " + someLogString + " NULL DOC"); 114 } 115 } 116 117 /** 118 * Logs a {@link DocumentModel} name and the passed string (info). 119 */ 120 public void logDocumentWithName(String someLogString, DocumentModel document) { 121 if (null != document) { 122 log.debug('[' + getClass().getSimpleName() + "] " + someLogString + ' ' + document.getName()); 123 } else { 124 log.debug('[' + getClass().getSimpleName() + "] " + someLogString + " NULL DOC"); 125 } 126 } 127 128 /** 129 * Extracts references from a list of document models. 130 */ 131 protected List<DocumentRef> extractReferences(List<DocumentModel> documents) { 132 List<DocumentRef> references = new ArrayList<>(); 133 134 for (DocumentModel docModel : documents) { 135 references.add(docModel.getRef()); 136 } 137 138 return references; 139 } 140 141 protected void setFacesMessage(String msg) { 142 facesMessages.add(StatusMessage.Severity.INFO, resourcesAccessor.getMessages().get(msg)); 143 } 144 145 /** 146 * Is the current logged user an administrator? 147 */ 148 public boolean getAdministrator() { 149 boolean administrator = false; 150 if (currentUser instanceof NuxeoPrincipal) { 151 administrator = ((NuxeoPrincipal) currentUser).isAdministrator(); 152 } 153 return administrator; 154 } 155 156 /** 157 * Returns null. 158 * <p> 159 * Previous behavior was: Utility method to return non 'null' JSF outcome that do not change the current view. The 160 * problem with null outcome is that some seam components are not refetched and thus the JSF tree might hold 161 * references that are no longer up-to-date, esp. in search results views whose documents lists are computed by an 162 * EVENT scoped seam factory. 163 * 164 * @param actionOutcome a string that might be used in the future to compute the JSF outcome in a cleaner way 165 * @return the same view as previously based on the expectation that the 'outcome_name' match the view id 166 * '/outcome_name.xhtml' faces-config.xml 167 * @deprecated returning a non-null outcome is now useless since our {@link FancyNavigationHandler} already performs 168 * redirection to the right outcome when dealing with a null outcome. Plus assumptions on the 169 * view/outcome names here was a buggy hack. 170 */ 171 @Deprecated 172 public String computeOutcome(String actionOutcome) { 173 // actionOutcome is currently ignored on purpose but might be useful in 174 // the future 175 return null; 176 } 177 178}