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 repositoryName = repoName; 050 this.collectionMemberIds = new ArrayList<>(collectionMemberIds); 051 } 052 053 public static final String CATEGORY = "duplicateCollectionMember"; 054 055 protected static final long serialVersionUID = 4985374651436954280L; 056 057 protected static final String TITLE = "Duplicate CollectionMember Work"; 058 059 protected String newCollectionId; 060 061 protected List<String> collectionMemberIds; 062 063 @Override 064 public String getCategory() { 065 return CATEGORY; 066 } 067 068 public String getNewCollectionId() { 069 return newCollectionId; 070 } 071 072 @Override 073 public String getTitle() { 074 return TITLE; 075 } 076 077 public void setNewCollectionId(String newCollectionId) { 078 this.newCollectionId = newCollectionId; 079 } 080 081 @Override 082 public void work() { 083 setStatus("Duplicating"); 084 if (collectionMemberIds != null) { 085 CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class); 086 setProgress(new Progress(0, collectionMemberIds.size())); 087 openSystemSession(); 088 for (int i = 0; i < collectionMemberIds.size(); i++) { 089 log.trace(String.format("Worker %s, populating Collection %s, processing CollectionMember %s", getId(), 090 newCollectionId, collectionMemberIds.get(i))); 091 if (collectionMemberIds.get(i) != null) { 092 DocumentModel collectionMember = session.getDocument(new IdRef(collectionMemberIds.get(i))); 093 if (collectionManager.isCollectable(collectionMember)) { 094 095 // We want to disable the following listener on a 096 // collection member when it is added to a collection 097 collectionMember.putContextData(DublinCoreListener.DISABLE_DUBLINCORE_LISTENER, true); 098 collectionMember.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, true); 099 collectionMember.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, true); 100 collectionMember.putContextData(VersioningService.DISABLE_AUTO_CHECKOUT, true); 101 102 CollectionMember collectionMemberAdapter = collectionMember.getAdapter(CollectionMember.class); 103 collectionMemberAdapter.addToCollection(newCollectionId); 104 session.saveDocument(collectionMember); 105 } 106 } 107 setProgress(new Progress(i, collectionMemberIds.size())); 108 } 109 } 110 setStatus("Done"); 111 } 112 113}