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