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