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