001/*
002 * (C) Copyright 2006-2018 Nuxeo (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.lang3.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    protected final String path;
042
043    public QuotaStatsInitialWork(String updaterName, String repositoryName, String path) {
044        super(repositoryName + ":quotaInitialStatistics:" + updaterName);
045        setDocument(repositoryName, null);
046        this.updaterName = updaterName;
047        this.path = path;
048    }
049
050    @Override
051    public String getCategory() {
052        return CATEGORY_QUOTA_INITIAL;
053    }
054
055    @Override
056    public String getTitle() {
057        return "Quota Statistics " + updaterName;
058    }
059
060    public void notifyProgress(float percent) {
061        setProgress(new Progress(percent));
062    }
063
064    public void notifyProgress(long current, long total) {
065        setProgress(new Progress(current, total));
066    }
067
068    @Override
069    public void work() {
070        final QuotaStatsInitialWork currentWorker = this;
071        new UnrestrictedSessionRunner(repositoryName) {
072            @Override
073            public void run() {
074                QuotaStatsService service = Framework.getService(QuotaStatsService.class);
075                service.computeInitialStatistics(updaterName, session, currentWorker, path);
076            }
077        }.runUnrestricted();
078    }
079
080    @Override
081    public String toString() {
082        return new ToStringBuilder(this).append("updaterName", updaterName).append("repositoryName", repositoryName).toString();
083    }
084
085}