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