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