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