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