001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
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-2.1.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 */
017package org.nuxeo.ecm.collections.jsf.actions;
018
019import static org.jboss.seam.ScopeType.CONVERSATION;
020import static org.jboss.seam.annotations.Install.FRAMEWORK;
021import static org.nuxeo.ecm.collections.api.CollectionConstants.MAGIC_PREFIX_ID;
022
023import java.io.Serializable;
024import java.util.List;
025
026import org.jboss.seam.annotations.In;
027import org.jboss.seam.annotations.Install;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Observer;
030import org.jboss.seam.annotations.Scope;
031import org.nuxeo.ecm.collections.api.CollectionManager;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.IdRef;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * @since 6.0
039 */
040@Name("collectionBulkEditActions")
041@Scope(CONVERSATION)
042@Install(precedence = FRAMEWORK)
043public class CollectionBulkEditActions implements Serializable {
044
045    public static final String SELECTION_EDITED = "selectionEdited";
046
047    public static final String DOCUMENTS_IMPORTED = "documentImported";
048
049    @In(create = true, required = false)
050    protected transient CoreSession documentManager;
051
052    @SuppressWarnings("unchecked")
053    @Observer({ SELECTION_EDITED, DOCUMENTS_IMPORTED })
054    public void addCollectionsOnEvent(List<DocumentModel> documents, DocumentModel doc) {
055        List<String> collectionIds = (List<String>) doc.getContextData(org.nuxeo.common.collections.ScopeType.REQUEST,
056                "bulk_collections");
057        if (collectionIds != null && !collectionIds.isEmpty()) {
058            CollectionManager collectionManager = Framework.getService(CollectionManager.class);
059            for (String collectionId : collectionIds) {
060                if (collectionId.startsWith(MAGIC_PREFIX_ID)) {
061                    String title = collectionId.replaceAll("^" + MAGIC_PREFIX_ID, "");
062                    collectionManager.addToNewCollection(title, "", documents, documentManager);
063                } else {
064                    IdRef idRef = new IdRef(collectionId);
065                    if (documentManager.exists(idRef)) {
066                        DocumentModel collection = documentManager.getDocument(idRef);
067                        collectionManager.addToCollection(collection, documents, documentManager);
068                    }
069                }
070
071            }
072        }
073    }
074
075}