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 boolean isIdempotent() { 070 return false; 071 } 072 073 @Override 074 public void work() { 075 setStatus("Removing"); 076 if (collectionMemberIds != null) { 077 setProgress(new Progress(0, collectionMemberIds.size())); 078 if (session == null) { 079 openSystemSession(); 080 } 081 final CollectionManager collectionManager = Framework.getService(CollectionManager.class); 082 for (int i = 0; i < collectionMemberIds.size(); i++) { 083 log.trace(String.format("Worker %s, deleting from Collection %s, processing CollectionMember %s", 084 getId(), collectionId, collectionMemberIds.get(i))); 085 if (collectionMemberIds.get(i) != null) { 086 final DocumentRef collectionMemberRef = new IdRef(collectionMemberIds.get(i)); 087 if (session.exists(collectionMemberRef)) { 088 final DocumentModel collectionMember = session.getDocument( 089 new IdRef(collectionMemberIds.get(i))); 090 if (collectionManager.isCollectable(collectionMember)) { 091 collectionManager.doRemoveFromCollection(collectionMember, collectionId, session); 092 } 093 } 094 } 095 setProgress(new Progress(i + 1L, collectionMemberIds.size())); 096 } 097 } 098 setStatus("Done"); 099 } 100 101}