001/*
002 * (C) Copyright 2013 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 *     Mariana Cedica <mcedica@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.quota;
021
022import java.util.List;
023
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.IdRef;
026import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
027import org.nuxeo.ecm.core.work.AbstractWork;
028import org.nuxeo.ecm.quota.size.QuotaAware;
029import org.nuxeo.ecm.quota.size.QuotaAwareDocumentFactory;
030
031/**
032 * Work to set the maxSize on a list of documents
033 *
034 * @since 5.7
035 */
036public class QuotaMaxSizeSetterWork extends AbstractWork {
037
038    private static final long serialVersionUID = 1L;
039
040    public long maxSize;
041
042    public static final String QUOTA_MAX_SIZE_UPDATE_WORK = "quotaMaxSizeSetter";
043
044    public QuotaMaxSizeSetterWork(long maxSize, List<String> docIds, String repositoryName) {
045        super(); // random id, for unique job
046        setDocuments(repositoryName, docIds);
047        this.maxSize = maxSize;
048    }
049
050    @Override
051    public String getTitle() {
052        return QUOTA_MAX_SIZE_UPDATE_WORK;
053    }
054
055    @Override
056    public String getCategory() {
057        return QUOTA_MAX_SIZE_UPDATE_WORK;
058    }
059
060    public void notifyProgress(long current) {
061        setProgress(new Progress(current, docIds.size()));
062    }
063
064    @Override
065    public void work() {
066        new UnrestrictedSessionRunner(repositoryName) {
067
068            @Override
069            public void run() {
070                for (String docId : docIds) {
071                    DocumentModel doc = session.getDocument(new IdRef(docId));
072                    QuotaAware qa = QuotaAwareDocumentFactory.make(doc);
073                    // skip validation on other children quotas
074                    qa.setMaxQuota(maxSize, true);
075                    qa.save();
076                }
077            }
078        }.runUnrestricted();
079    }
080}