001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
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 *     Thomas Roger <troger@nuxeo.com>
016 *     Florent Guillaume
017 */
018
019package org.nuxeo.ecm.quota;
020
021import org.apache.commons.lang.builder.ToStringBuilder;
022import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
023import org.nuxeo.ecm.core.work.AbstractWork;
024import org.nuxeo.runtime.api.Framework;
025
026/**
027 * Work doing an initial statistics computation for a defined {@link QuotaStatsUpdater}.
028 *
029 * @since 5.6
030 */
031public class QuotaStatsInitialWork extends AbstractWork {
032
033    private static final long serialVersionUID = 1L;
034
035    public static final String CATEGORY_QUOTA_INITIAL = "quotaInitialStatistics";
036
037    private final String updaterName;
038
039    public QuotaStatsInitialWork(String updaterName, String repositoryName) {
040        super(repositoryName + ":quotaInitialStatistics:" + updaterName);
041        setDocument(repositoryName, null);
042        this.updaterName = updaterName;
043    }
044
045    @Override
046    public String getCategory() {
047        return CATEGORY_QUOTA_INITIAL;
048    }
049
050    @Override
051    public String getTitle() {
052        return "Quota Statistics " + updaterName;
053    }
054
055    public void notifyProgress(float percent) {
056        setProgress(new Progress(percent));
057    }
058
059    public void notifyProgress(long current, long total) {
060        setProgress(new Progress(current, total));
061    }
062
063    @Override
064    public void work() {
065        final QuotaStatsInitialWork currentWorker = this;
066        new UnrestrictedSessionRunner(repositoryName) {
067            @Override
068            public void run() {
069                QuotaStatsService service = Framework.getLocalService(QuotaStatsService.class);
070                service.computeInitialStatistics(updaterName, session, currentWorker);
071            }
072        }.runUnrestricted();
073    }
074
075    @Override
076    public String toString() {
077        return new ToStringBuilder(this).append("updaterName", updaterName).append("repositoryName", repositoryName).toString();
078    }
079
080}