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