001/*
002 * (C) Copyright 2006-2015 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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.webapp.base;
022
023import java.security.Principal;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.jboss.seam.annotations.In;
030import org.jboss.seam.faces.FacesMessages;
031import org.jboss.seam.international.StatusMessage;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentRef;
034import org.nuxeo.ecm.core.api.NuxeoPrincipal;
035import org.nuxeo.ecm.platform.actions.ejb.ActionManager;
036import org.nuxeo.ecm.platform.types.TypeManager;
037import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
038import org.nuxeo.ecm.platform.ui.web.rest.FancyNavigationHandler;
039import org.nuxeo.ecm.webapp.action.TypesTool;
040import org.nuxeo.ecm.webapp.helpers.EventManager;
041import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor;
042
043/**
044 * Contains generic functionality usable by all action listeners.
045 *
046 * @author <a href="mailto:rcaraghin@nuxeo.com">Razvan Caraghin</a>
047 */
048public abstract class InputController {
049
050    private static final Log log = LogFactory.getLog(InputController.class);
051
052    @In(create = true)
053    protected ActionManager actionManager;
054
055    @In(create = true)
056    protected TypeManager typeManager;
057
058    @In(create = true)
059    protected NavigationContext navigationContext;
060
061    @In(create = true)
062    protected EventManager eventManager;
063
064    @In(required = false, create = true)
065    /**
066     * @deprecated Since 5.2. Injecting current document is not a good idea, should be 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<>();
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}