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