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 boolean isIdempotent() {
058        return false;
059    }
060
061    @Override
062    public void work() {
063        setStatus("Updating");
064        if (docId != null) {
065            openSystemSession();
066            final List<DocumentModel> results = getNextResults();
067            final int nbResult = results.size();
068            setProgress(new Progress(0, results.size()));
069            for (int i = 0; i < nbResult; i++) {
070                updateDocument(results.get(i));
071                setProgress(new Progress(0, nbResult));
072            }
073
074            if (nbResult == CollectionAsynchrnonousQuery.MAX_RESULT) {
075                setStatus("Rescheduling next work");
076                RemovedAbstractWork nextWork;
077                try {
078                    Constructor<? extends RemovedAbstractWork> c = this.getClass().getDeclaredConstructor(long.class);
079                    nextWork = c.newInstance(offset + CollectionAsynchrnonousQuery.MAX_RESULT);
080                } catch (ReflectiveOperationException e) {
081                    throw new RuntimeException(e);
082                }
083                nextWork.setDocument(repositoryName, docId);
084                WorkManager workManager = Framework.getService(WorkManager.class);
085                workManager.schedule(nextWork, WorkManager.Scheduling.IF_NOT_SCHEDULED, true);
086                setStatus("Rescheduling Done");
087            }
088        }
089        setStatus("Updating Done");
090    }
091
092    protected abstract void updateDocument(final DocumentModel d);
093
094    private List<DocumentModel> getNextResults() {
095        List<DocumentModel> results;
096        Object[] parameters = new Object[1];
097        parameters[0] = docId;
098
099        String query = NXQLQueryBuilder.getQuery(getQuery(), parameters, true, false, null);
100
101        results = session.query(query, null, CollectionAsynchrnonousQuery.MAX_RESULT, 0,
102                CollectionAsynchrnonousQuery.MAX_RESULT);
103        return results;
104    }
105
106}