001/*
002 * (C) Copyright 2014 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-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 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
016 */
017package org.nuxeo.ecm.collections.core.worker;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.collections.api.CollectionManager;
025import org.nuxeo.ecm.collections.core.adapter.CollectionMember;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.IdRef;
028import org.nuxeo.ecm.core.versioning.VersioningService;
029import org.nuxeo.ecm.core.work.AbstractWork;
030import org.nuxeo.ecm.platform.audit.service.NXAuditEventsService;
031import org.nuxeo.ecm.platform.dublincore.listener.DublinCoreListener;
032import org.nuxeo.ecm.platform.ec.notification.NotificationConstants;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @since 5.9.3
037 */
038public class DuplicateCollectionMemberWork extends AbstractWork {
039
040    private static final Log log = LogFactory.getLog(DuplicateCollectionMemberWork.class);
041
042    public DuplicateCollectionMemberWork(final String repoName, final String newCollectionId,
043            final List<String> collectionMemberIds, final int offset) {
044        super(CATEGORY + ":" + repoName + ":" + newCollectionId + ":" + offset);
045        repositoryName = repoName;
046        this.newCollectionId = newCollectionId;
047        repositoryName = repoName;
048        this.collectionMemberIds = new ArrayList<>(collectionMemberIds);
049    }
050
051    public static final String CATEGORY = "duplicateCollectionMember";
052
053    protected static final long serialVersionUID = 4985374651436954280L;
054
055    protected static final String TITLE = "Duplicate CollectionMember Work";
056
057    protected String newCollectionId;
058
059    protected List<String> collectionMemberIds;
060
061    @Override
062    public String getCategory() {
063        return CATEGORY;
064    }
065
066    public String getNewCollectionId() {
067        return newCollectionId;
068    }
069
070    @Override
071    public String getTitle() {
072        return TITLE;
073    }
074
075    public void setNewCollectionId(String newCollectionId) {
076        this.newCollectionId = newCollectionId;
077    }
078
079    @Override
080    public void work() {
081        setStatus("Duplicating");
082        if (collectionMemberIds != null) {
083            CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
084            setProgress(new Progress(0, collectionMemberIds.size()));
085            initSession();
086            for (int i = 0; i < collectionMemberIds.size(); i++) {
087                log.trace(String.format("Worker %s, populating Collection %s, processing CollectionMember %s", getId(),
088                        newCollectionId, collectionMemberIds.get(i)));
089                if (collectionMemberIds.get(i) != null) {
090                    DocumentModel collectionMember = session.getDocument(new IdRef(collectionMemberIds.get(i)));
091                    if (collectionManager.isCollectable(collectionMember)) {
092
093                        // We want to disable the following listener on a
094                        // collection member when it is added to a collection
095                        collectionMember.putContextData(DublinCoreListener.DISABLE_DUBLINCORE_LISTENER, true);
096                        collectionMember.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, true);
097                        collectionMember.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, true);
098                        collectionMember.putContextData(VersioningService.DISABLE_AUTO_CHECKOUT, true);
099
100                        CollectionMember collectionMemberAdapter = collectionMember.getAdapter(CollectionMember.class);
101                        collectionMemberAdapter.addToCollection(newCollectionId);
102                        session.saveDocument(collectionMember);
103                    }
104                }
105                setProgress(new Progress(i, collectionMemberIds.size()));
106            }
107        }
108        setStatus("Done");
109    }
110
111}