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.List;
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.ecm.collections.api.CollectionManager;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.DocumentRef;
025import org.nuxeo.ecm.core.api.IdRef;
026import org.nuxeo.ecm.core.work.AbstractWork;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * @since 7.3
031 */
032public class RemoveFromCollectionWork extends AbstractWork {
033
034    private static final long serialVersionUID = 1L;
035
036    private static final Log log = LogFactory.getLog(RemoveFromCollectionWork.class);
037
038    public static final String CATEGORY = "removeFromCollection";
039
040    protected static final String TITLE = "Remove From Collection Work";
041
042    protected List<String> collectionMemberIds;
043
044    protected String collectionId;
045
046    public RemoveFromCollectionWork(final String repoName, String collectionId, List<String> collectionMemberIds,
047            final int offset) {
048        super(CATEGORY + ":" + repoName + ":" + collectionId + ":" + offset);
049        this.repositoryName = repoName;
050        this.collectionId = collectionId;
051        this.collectionMemberIds = collectionMemberIds;
052    }
053
054    @Override
055    public String getCategory() {
056        return CATEGORY;
057    }
058
059    @Override
060    public String getTitle() {
061        return TITLE;
062    }
063
064    @Override
065    public void work() {
066        setStatus("Removing");
067        if (collectionMemberIds != null) {
068            setProgress(new Progress(0, collectionMemberIds.size()));
069            if (session == null) {
070                initSession();
071            }
072            final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class);
073            for (int i = 0; i < collectionMemberIds.size(); i++) {
074                log.trace(String.format("Worker %s, deleting from Collection %s, processing CollectionMember %s",
075                        getId(), collectionId, collectionMemberIds.get(i)));
076                if (collectionMemberIds.get(i) != null) {
077                    final DocumentRef collectionMemberRef = new IdRef(collectionMemberIds.get(i));
078                    if (session.exists(collectionMemberRef)) {
079                        final DocumentModel collectionMember = session.getDocument(
080                                new IdRef(collectionMemberIds.get(i)));
081                        if (collectionManager.isCollectable(collectionMember)) {
082                            collectionManager.doRemoveFromCollection(collectionMember, collectionId, session);
083                        }
084                    }
085                }
086                setProgress(new Progress(i + 1, collectionMemberIds.size()));
087            }
088        }
089        setStatus("Done");
090    }
091
092}