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.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 Since 5.2. Injecting current document is not a good idea, should be fetched from navigationContext directly.
066     */
067    @Deprecated
068    protected DocumentModel currentDocument;
069
070    @In(create = true, required = false)
071    protected FacesMessages facesMessages;
072
073    @In(create = true)
074    // won't inject this because of seam problem after activation
075    // ::protected Map<String, String> messages;
076    protected ResourcesAccessor resourcesAccessor;
077
078    @In(create = true)
079    protected TypesTool typesTool;
080
081    @In(create = true, required = false)
082    protected NuxeoPrincipal currentUser;
083
084    /**
085     * Utility method that helps remove a {@link DocumentModel} from a list. The document models are compared on
086     * {@link DocumentRef}s.
087     *
088     * @param documentList
089     * @param document
090     */
091    public void removeDocumentFromList(List<DocumentModel> documentList, DocumentModel document) {
092        if (null == document) {
093            log.error("Received nul doc, not removing anything...");
094            return;
095        }
096
097        log.debug("Removing document " + document.getId() + " from list...");
098
099        for (int i = 0; i < documentList.size(); i++) {
100            if (documentList.get(i).getRef().equals(document.getRef())) {
101                documentList.remove(i);
102            }
103        }
104    }
105
106    /**
107     * Logs a {@link DocumentModel} title and the passed string (info).
108     */
109    public void logDocumentWithTitle(String someLogString, DocumentModel document) {
110        if (null != document) {
111            log.trace('[' + getClass().getSimpleName() + "] " + someLogString + ' ' + document.getId());
112            log.debug("CURRENT DOC PATH: " + document.getPathAsString());
113        } else {
114            log.trace('[' + getClass().getSimpleName() + "] " + someLogString + " NULL DOC");
115        }
116    }
117
118    /**
119     * Logs a {@link DocumentModel} name and the passed string (info).
120     */
121    public void logDocumentWithName(String someLogString, DocumentModel document) {
122        if (null != document) {
123            log.debug('[' + getClass().getSimpleName() + "] " + someLogString + ' ' + document.getName());
124        } else {
125            log.debug('[' + getClass().getSimpleName() + "] " + someLogString + " NULL DOC");
126        }
127    }
128
129    /**
130     * Extracts references from a list of document models.
131     */
132    protected List<DocumentRef> extractReferences(List<DocumentModel> documents) {
133        List<DocumentRef> references = new ArrayList<>();
134
135        for (DocumentModel docModel : documents) {
136            references.add(docModel.getRef());
137        }
138
139        return references;
140    }
141
142    protected void setFacesMessage(String msg) {
143        facesMessages.add(StatusMessage.Severity.INFO, resourcesAccessor.getMessages().get(msg));
144    }
145
146    /**
147     * Is the current logged user an administrator?
148     */
149    public boolean getAdministrator() {
150        return currentUser.isAdministrator();
151    }
152
153    /**
154     * Returns null.
155     * <p>
156     * Previous behavior was: Utility method to return non 'null' JSF outcome that do not change the current view. The
157     * problem with null outcome is that some seam components are not refetched and thus the JSF tree might hold
158     * references that are no longer up-to-date, esp. in search results views whose documents lists are computed by an
159     * EVENT scoped seam factory.
160     *
161     * @param actionOutcome a string that might be used in the future to compute the JSF outcome in a cleaner way
162     * @return the same view as previously based on the expectation that the 'outcome_name' match the view id
163     *         '/outcome_name.xhtml' faces-config.xml
164     * @deprecated returning a non-null outcome is now useless since our {@link FancyNavigationHandler} already performs
165     *             redirection to the right outcome when dealing with a null outcome. Plus assumptions on the
166     *             view/outcome names here was a buggy hack.
167     */
168    @Deprecated
169    public String computeOutcome(String actionOutcome) {
170        // actionOutcome is currently ignored on purpose but might be useful in
171        // the future
172        return null;
173    }
174
175}