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
018 */
019package org.nuxeo.ecm.quota.automation;
020
021import java.io.IOException;
022import java.io.StringWriter;
023import java.text.NumberFormat;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Locale;
027
028import org.codehaus.jackson.map.ObjectMapper;
029import org.nuxeo.common.utils.i18n.I18NUtils;
030import org.nuxeo.ecm.automation.core.annotations.Context;
031import org.nuxeo.ecm.automation.core.annotations.Operation;
032import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
033import org.nuxeo.ecm.automation.core.annotations.Param;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.Blobs;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.DocumentRef;
039import org.nuxeo.ecm.core.api.NuxeoException;
040import org.nuxeo.ecm.quota.size.QuotaAware;
041import org.nuxeo.ecm.quota.size.QuotaInfo;
042
043/**
044 * Returns a json representation of the quota info to be displayed in a pie chart
045 *
046 * @since 5.7
047 */
048@Operation(id = GetQuotaStatisticsOperation.ID, category = "Quotas", label = "Get Quota statistics", description = "Returns the Quota Infos (innerSize, totalSize and maxQuota) for a DocumentModel")
049public class GetQuotaStatisticsOperation {
050
051    public static final String ID = "Quotas.GetStatistics";
052
053    @Context
054    protected CoreSession session;
055
056    @Param(name = "documentRef", required = true)
057    protected DocumentRef documentRef;
058
059    @Param(name = "language", required = false)
060    protected String language;
061
062    @OperationMethod()
063    public Blob run() {
064        Locale locale = language != null && !language.isEmpty() ? new Locale(language) : Locale.ENGLISH;
065        DocumentModel doc = session.getDocument(documentRef);
066        QuotaAware qa = doc.getAdapter(QuotaAware.class);
067        if (qa == null) {
068            throw new NuxeoException("Quota not activated on doc");
069        }
070        String string = toJSON(qa.getQuotaInfo(), locale);
071        return Blobs.createJSONBlob(string);
072    }
073
074    public String toJSON(QuotaInfo quotaInfo, Locale locale) {
075        NumberFormat nf = NumberFormat.getInstance();
076        nf.setMaximumFractionDigits(2);
077        List<QuotaStat> stats = new ArrayList<QuotaStat>();
078        stats.add(new QuotaStat(quotaInfo.getLiveSize().getValue(), getI18nLabel("label.quota.liveSize", locale) + ":"
079                + nf.format(quotaInfo.getLiveSize().getValueInUnit()) + " " + getI18nLabel(quotaInfo.getLiveSize().getUnit(), locale)));
080        stats.add(new QuotaStat(quotaInfo.getTrashSize().getValue(), getI18nLabel("label.quota.trashSize", locale)
081                + ":" + nf.format(quotaInfo.getTrashSize().getValueInUnit()) + " "
082                + getI18nLabel(quotaInfo.getTrashSize().getUnit(), locale)));
083        stats.add(new QuotaStat(quotaInfo.getSizeVersions().getValue(),
084                getI18nLabel("label.quota.versionsSize", locale) + ":"
085                        + nf.format(quotaInfo.getSizeVersions().getValueInUnit()) + " "
086                        + getI18nLabel(quotaInfo.getSizeVersions().getUnit(), locale)));
087        ObjectMapper mapper = new ObjectMapper();
088        StringWriter writer = new StringWriter();
089        try {
090            mapper.writeValue(writer, stats);
091        } catch (IOException e) {
092            throw new NuxeoException(e);
093        }
094        return writer.toString();
095    }
096
097    protected String getI18nLabel(String label, Locale locale) {
098        if (label == null) {
099            label = "";
100        }
101        return I18NUtils.getMessageString("messages", label, null, locale);
102    }
103
104    class QuotaStat {
105        private String label;
106
107        private long data;
108
109        QuotaStat(long data, String label) {
110            this.data = data;
111            this.label = label;
112        }
113
114        public String getLabel() {
115            return label;
116        }
117
118        public long getData() {
119            return data;
120        }
121    }
122}