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