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