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; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.collections.api.CollectionManager; 024import org.nuxeo.ecm.collections.core.adapter.CollectionMember; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.IdRef; 027import org.nuxeo.ecm.core.versioning.VersioningService; 028import org.nuxeo.ecm.core.work.AbstractWork; 029import org.nuxeo.ecm.platform.audit.service.NXAuditEventsService; 030import org.nuxeo.ecm.platform.dublincore.listener.DublinCoreListener; 031import org.nuxeo.ecm.platform.ec.notification.NotificationConstants; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * @since 5.9.3 036 */ 037public class DuplicateCollectionMemberWork extends AbstractWork { 038 039 private static final Log log = LogFactory.getLog(DuplicateCollectionMemberWork.class); 040 041 public DuplicateCollectionMemberWork(final String repoName, final String newCollectionId, 042 final List<String> collectionMemberIds, final int offset) { 043 super(CATEGORY + ":" + repoName + ":" + newCollectionId + ":" + offset); 044 this.repositoryName = repoName; 045 this.newCollectionId = newCollectionId; 046 this.repositoryName = repoName; 047 this.collectionMemberIds = collectionMemberIds; 048 } 049 050 public static final String CATEGORY = "duplicateCollectionMember"; 051 052 protected static final long serialVersionUID = 4985374651436954280L; 053 054 protected static final String TITLE = "Duplicate CollectionMember Work"; 055 056 protected String newCollectionId; 057 058 protected List<String> collectionMemberIds; 059 060 @Override 061 public String getCategory() { 062 return CATEGORY; 063 } 064 065 public String getNewCollectionId() { 066 return newCollectionId; 067 } 068 069 @Override 070 public String getTitle() { 071 return TITLE; 072 } 073 074 public void setNewCollectionId(String newCollectionId) { 075 this.newCollectionId = newCollectionId; 076 } 077 078 @Override 079 public void work() { 080 setStatus("Duplicating"); 081 if (collectionMemberIds != null) { 082 CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class); 083 setProgress(new Progress(0, collectionMemberIds.size())); 084 initSession(); 085 for (int i = 0; i < collectionMemberIds.size(); i++) { 086 log.trace(String.format("Worker %s, populating Collection %s, processing CollectionMember %s", getId(), 087 newCollectionId, collectionMemberIds.get(i))); 088 if (collectionMemberIds.get(i) != null) { 089 DocumentModel collectionMember = session.getDocument(new IdRef(collectionMemberIds.get(i))); 090 if (collectionManager.isCollectable(collectionMember)) { 091 092 // We want to disable the following listener on a 093 // collection member when it is added to a collection 094 collectionMember.putContextData(DublinCoreListener.DISABLE_DUBLINCORE_LISTENER, true); 095 collectionMember.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, true); 096 collectionMember.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, true); 097 collectionMember.putContextData(VersioningService.DISABLE_AUTO_CHECKOUT, true); 098 099 CollectionMember collectionMemberAdapter = collectionMember.getAdapter(CollectionMember.class); 100 collectionMemberAdapter.addToCollection(newCollectionId); 101 session.saveDocument(collectionMember); 102 } 103 } 104 setProgress(new Progress(i, collectionMemberIds.size())); 105 } 106 } 107 setStatus("Done"); 108 } 109 110}