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.CollectionConstants; 027import org.nuxeo.ecm.collections.api.CollectionManager; 028import org.nuxeo.ecm.collections.core.adapter.CollectionMember; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.IdRef; 031import org.nuxeo.ecm.core.api.versioning.VersioningService; 032import org.nuxeo.ecm.core.work.AbstractWork; 033import org.nuxeo.ecm.platform.dublincore.listener.DublinCoreListener; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * @since 5.9.3 038 */ 039public class DuplicateCollectionMemberWork extends AbstractWork { 040 041 private static final Log log = LogFactory.getLog(DuplicateCollectionMemberWork.class); 042 043 public DuplicateCollectionMemberWork(final String repoName, final String newCollectionId, 044 final List<String> collectionMemberIds, final int offset) { 045 super(CATEGORY + ":" + repoName + ":" + newCollectionId + ":" + offset); 046 repositoryName = repoName; 047 this.newCollectionId = newCollectionId; 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.getService(CollectionManager.class); 084 setProgress(new Progress(0, collectionMemberIds.size())); 085 openSystemSession(); 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(CollectionConstants.DISABLE_NOTIFICATION_SERVICE, true); 097 collectionMember.putContextData(CollectionConstants.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}