001/*
002 * (C) Copyright 2009 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.webapp.bulkedit;
021
022import static org.jboss.seam.ScopeType.CONVERSATION;
023import static org.nuxeo.ecm.webapp.documentsLists.DocumentsListsManager.CURRENT_DOCUMENT_SELECTION;
024
025import java.io.Serializable;
026import java.util.Collections;
027import java.util.List;
028import java.util.Map;
029
030import org.jboss.seam.annotations.In;
031import org.jboss.seam.annotations.Install;
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Observer;
034import org.jboss.seam.annotations.Scope;
035import org.jboss.seam.core.Events;
036import org.jboss.seam.faces.FacesMessages;
037import org.jboss.seam.international.StatusMessage;
038import org.nuxeo.ecm.core.api.CoreSession;
039import org.nuxeo.ecm.core.api.DocumentModel;
040import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel;
041import org.nuxeo.ecm.core.api.security.SecurityConstants;
042import org.nuxeo.ecm.platform.types.TypeManager;
043import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
044import org.nuxeo.ecm.webapp.documentsLists.DocumentsListsManager;
045import org.nuxeo.ecm.webapp.helpers.EventNames;
046import org.nuxeo.runtime.api.Framework;
047
048/**
049 * Handles Bulk Edit actions.
050 *
051 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
052 */
053@Name("bulkEditActions")
054@Scope(CONVERSATION)
055@Install(precedence = Install.FRAMEWORK)
056public class BulkEditActions implements Serializable {
057
058    private static final long serialVersionUID = 1L;
059
060    public static final String SELECTION_EDITED = "selectionEdited";
061
062    @In(create = true)
063    protected transient DocumentsListsManager documentsListsManager;
064
065    @In(create = true)
066    protected transient TypeManager typeManager;
067
068    @In(create = true)
069    protected transient CoreSession documentManager;
070
071    @In(create = true)
072    protected transient NavigationContext navigationContext;
073
074    @In(create = true, required = false)
075    protected FacesMessages facesMessages;
076
077    @In(create = true)
078    protected Map<String, String> messages;
079
080    protected DocumentModel fictiveDocumentModel;
081
082    /**
083     * Returns the common layouts of the current selected documents for the {@code edit} mode.
084     */
085    public List<String> getCommonsLayouts() {
086        if (documentsListsManager.isWorkingListEmpty(CURRENT_DOCUMENT_SELECTION)) {
087            return Collections.emptyList();
088        }
089
090        List<DocumentModel> selectedDocuments = documentsListsManager.getWorkingList(CURRENT_DOCUMENT_SELECTION);
091        return BulkEditHelper.getCommonLayouts(typeManager, selectedDocuments);
092    }
093
094    /**
095     * Returns the common schemas for the current selected documents.
096     *
097     * @deprecated not yet used since 5.7
098     */
099    protected List<String> getCommonSchemas() {
100        if (documentsListsManager.isWorkingListEmpty(CURRENT_DOCUMENT_SELECTION)) {
101            return Collections.emptyList();
102        }
103
104        List<DocumentModel> selectedDocuments = documentsListsManager.getWorkingList(CURRENT_DOCUMENT_SELECTION);
105        return BulkEditHelper.getCommonSchemas(selectedDocuments);
106    }
107
108    public DocumentModel getBulkEditDocumentModel() {
109        if (fictiveDocumentModel == null) {
110            fictiveDocumentModel = new SimpleDocumentModel();
111        }
112        return fictiveDocumentModel;
113    }
114
115    public String bulkEditSelection() {
116        if (fictiveDocumentModel != null) {
117            List<DocumentModel> selectedDocuments = documentsListsManager.getWorkingList(CURRENT_DOCUMENT_SELECTION);
118            Framework.getLocalService(BulkEditService.class).updateDocuments(documentManager, fictiveDocumentModel,
119                    selectedDocuments);
120
121            for (DocumentModel doc : selectedDocuments) {
122                Events.instance().raiseEvent(EventNames.DOCUMENT_CHANGED, doc);
123            }
124
125            facesMessages.add(StatusMessage.Severity.INFO, messages.get("label.bulk.edit.documents.updated"),
126                    selectedDocuments.size());
127
128            Events.instance().raiseEvent(SELECTION_EDITED, selectedDocuments, fictiveDocumentModel);
129            fictiveDocumentModel = null;
130        }
131        return null;
132    }
133
134    /**
135     * @deprecated since 5.7. Use {@link org.nuxeo.ecm.webapp.bulkedit.BulkEditActions#bulkEditSelection()} .
136     */
137    @Deprecated
138    public void bulkEditSelectionNoRedirect() {
139        bulkEditSelection();
140    }
141
142    public boolean getCanEdit() {
143        if (documentsListsManager.isWorkingListEmpty(CURRENT_DOCUMENT_SELECTION)) {
144            return false;
145        }
146
147        List<DocumentModel> docs = documentsListsManager.getWorkingList(CURRENT_DOCUMENT_SELECTION);
148        for (DocumentModel doc : docs) {
149            if (!documentManager.hasPermission(doc.getRef(), SecurityConstants.WRITE)) {
150                return false;
151            }
152        }
153        return true;
154    }
155
156    @Observer(CURRENT_DOCUMENT_SELECTION + "Updated")
157    public void cancel() {
158        fictiveDocumentModel = null;
159    }
160
161}