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.lang.reflect.Constructor; 020import java.util.List; 021 022import org.nuxeo.ecm.collections.core.listener.CollectionAsynchrnonousQuery; 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.work.AbstractWork; 025import org.nuxeo.ecm.core.work.api.WorkManager; 026import org.nuxeo.ecm.platform.query.nxql.NXQLQueryBuilder; 027import org.nuxeo.runtime.api.Framework; 028 029/** 030 * @since 5.9.3 031 */ 032public abstract class RemovedAbstractWork extends AbstractWork { 033 034 private static final long serialVersionUID = 5240954386944920642L; 035 036 protected abstract String getQuery(); 037 038 protected long offset = 0; 039 040 public RemovedAbstractWork() { 041 this(0); 042 } 043 044 protected RemovedAbstractWork(final long offset) { 045 super(); 046 this.offset = offset; 047 } 048 049 @Override 050 public String getId() { 051 return repositoryName + ":" + docId + ":" + offset; 052 } 053 054 @Override 055 public void work() { 056 setStatus("Updating"); 057 if (docId != null) { 058 initSession(); 059 final List<DocumentModel> results = getNextResults(); 060 final int nbResult = results.size(); 061 setProgress(new Progress(0, results.size())); 062 for (int i = 0; i < nbResult; i++) { 063 updateDocument(results.get(i)); 064 setProgress(new Progress(0, nbResult)); 065 } 066 067 if (nbResult == CollectionAsynchrnonousQuery.MAX_RESULT) { 068 setStatus("Rescheduling next work"); 069 RemovedAbstractWork nextWork; 070 try { 071 Constructor<? extends RemovedAbstractWork> c = this.getClass().getDeclaredConstructor(long.class); 072 nextWork = c.newInstance(offset + CollectionAsynchrnonousQuery.MAX_RESULT); 073 } catch (ReflectiveOperationException e) { 074 throw new RuntimeException(e); 075 } 076 nextWork.setDocument(repositoryName, docId); 077 WorkManager workManager = Framework.getLocalService(WorkManager.class); 078 workManager.schedule(nextWork, WorkManager.Scheduling.IF_NOT_SCHEDULED, true); 079 setStatus("Rescheduling Done"); 080 } 081 } 082 setStatus("Updating Done"); 083 } 084 085 protected abstract void updateDocument(final DocumentModel d); 086 087 private List<DocumentModel> getNextResults() { 088 List<DocumentModel> results; 089 Object[] parameters = new Object[1]; 090 parameters[0] = docId; 091 092 String query = NXQLQueryBuilder.getQuery(getQuery(), parameters, true, false, null); 093 094 results = session.query(query, null, CollectionAsynchrnonousQuery.MAX_RESULT, 0, 095 CollectionAsynchrnonousQuery.MAX_RESULT); 096 return results; 097 } 098 099}