001/* 002 * (C) Copyright 2006-2012 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 * Thomas Roger <troger@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.quota.count; 019 020import static org.jboss.seam.annotations.Install.FRAMEWORK; 021 022import java.io.Serializable; 023import java.util.List; 024import java.util.Map; 025 026import javax.faces.application.FacesMessage; 027import javax.faces.component.UIComponent; 028import javax.faces.context.FacesContext; 029import javax.faces.validator.ValidatorException; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.jboss.seam.ScopeType; 034import org.jboss.seam.annotations.Create; 035import org.jboss.seam.annotations.Factory; 036import org.jboss.seam.annotations.In; 037import org.jboss.seam.annotations.Install; 038import org.jboss.seam.annotations.Name; 039import org.jboss.seam.annotations.Scope; 040import org.nuxeo.ecm.core.api.CoreSession; 041import org.nuxeo.ecm.core.api.DocumentModel; 042import org.nuxeo.ecm.core.work.api.Work; 043import org.nuxeo.ecm.core.work.api.Work.State; 044import org.nuxeo.ecm.core.work.api.WorkManager; 045import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 046import org.nuxeo.ecm.quota.QuotaStatsService; 047import org.nuxeo.ecm.quota.QuotaStatsUpdater; 048import org.nuxeo.ecm.quota.size.QuotaAware; 049import org.nuxeo.ecm.quota.size.QuotaDisplayValue; 050import org.nuxeo.launcher.config.ConfigurationGenerator; 051import org.nuxeo.runtime.api.Framework; 052 053/** 054 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 055 * @since 5.5 056 */ 057@Name("quotaStatsActions") 058@Scope(ScopeType.CONVERSATION) 059@Install(precedence = FRAMEWORK) 060public class QuotaStatsActions implements Serializable { 061 062 protected Log log = LogFactory.getLog(QuotaStatsActions.class); 063 064 private static final long serialVersionUID = -1L; 065 066 @In(create = true) 067 protected transient CoreSession documentManager; 068 069 @In(create = true) 070 protected transient NavigationContext navigationContext; 071 072 @In(create = true) 073 protected Map<String, String> messages; 074 075 private transient ConfigurationGenerator setupConfigGenerator; 076 077 protected QuotaStatsService quotaStatsService; 078 079 protected boolean activateQuotaOnUsersWorkspaces; 080 081 protected long maxQuotaOnUsersWorkspaces = -1; 082 083 protected WorkManager workManager; 084 085 @Create 086 public void initialize() { 087 initQuotaActivatedOnUserWorkspaces(); 088 } 089 090 public List<QuotaStatsUpdater> getQuotaStatsUpdaters() { 091 QuotaStatsService quotaStatsService = Framework.getLocalService(QuotaStatsService.class); 092 return quotaStatsService.getQuotaStatsUpdaters(); 093 } 094 095 public void launchInitialComputation(String updaterName) { 096 launchInitialComputation(updaterName, documentManager.getRepositoryName()); 097 } 098 099 public void launchInitialComputation(String updaterName, String repositoryName) { 100 QuotaStatsService quotaStatsService = Framework.getLocalService(QuotaStatsService.class); 101 quotaStatsService.launchInitialStatisticsComputation(updaterName, repositoryName); 102 } 103 104 public String getStatus(String updaterName) { 105 QuotaStatsService quotaStatsService = Framework.getLocalService(QuotaStatsService.class); 106 return quotaStatsService.getProgressStatus(updaterName, documentManager.getRepositoryName()); 107 } 108 109 @Factory(value = "currentQuotaDoc", scope = ScopeType.EVENT) 110 public QuotaAware getQuotaDoc() { 111 DocumentModel doc = navigationContext.getCurrentDocument(); 112 return doc.getAdapter(QuotaAware.class); 113 } 114 115 public void validateQuotaSize(FacesContext context, UIComponent component, Object value) { 116 String strValue = value.toString(); 117 Long quotaValue = -1L; 118 boolean quotaAllowed = true; 119 try { 120 quotaValue = Long.parseLong(strValue); 121 } catch (NumberFormatException e) { 122 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, messages.get("wrong format"), null); 123 // also add global message 124 context.addMessage(null, message); 125 throw new ValidatorException(message); 126 } 127 128 quotaAllowed = getQuotaStatsService().canSetMaxQuota(quotaValue, navigationContext.getCurrentDocument(), 129 documentManager); 130 if (quotaAllowed) { 131 return; 132 } 133 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, 134 messages.get("label.quotaException.QuotaCanNotBeSet"), null); 135 // also add global message 136 context.addMessage(null, message); 137 throw new ValidatorException(message); 138 } 139 140 public QuotaDisplayValue formatQuota(long value, long max) { 141 QuotaDisplayValue qdv = new QuotaDisplayValue(value, max); 142 return qdv; 143 } 144 145 public double getMinQuotaSliderValue(long totalSize) { 146 long minSize = 100 * 1024; 147 // 11.528 148 if (totalSize > minSize) { 149 return Math.log(totalSize + minSize); 150 } else { 151 return Math.log(minSize); 152 } 153 } 154 155 public long getMinQuotaSliderValue() { 156 return 102400;// 100KB 157 } 158 159 public long getMaxQuotaSliderValue() { 160 long maxQuotaSize = -1L; 161 DocumentModel doc = navigationContext.getCurrentDocument(); 162 if (doc != null) { 163 maxQuotaSize = getQuotaStatsService().getQuotaFromParent(doc, documentManager); 164 } 165 return maxQuotaSize > 0 ? maxQuotaSize : 1072668082176L; // 999GB 166 } 167 168 /** 169 * @since 5.7 170 */ 171 public void saveQuotaActivatedOnUsersWorkspaces() { 172 long maxSize = -1; 173 if (isActivateQuotaOnUsersWorkspaces()) { 174 maxSize = getMaxQuotaOnUsersWorkspaces(); 175 } 176 getQuotaStatsService().activateQuotaOnUserWorkspaces(maxSize, documentManager); 177 getQuotaStatsService().launchSetMaxQuotaOnUserWorkspaces(maxSize, documentManager.getRootDocument(), 178 documentManager); 179 } 180 181 /** 182 * @since 5.7 183 */ 184 public void initQuotaActivatedOnUserWorkspaces() { 185 long quota = getQuotaStatsService().getQuotaSetOnUserWorkspaces(documentManager); 186 setActivateQuotaOnUsersWorkspaces(quota == -1 ? false : true); 187 setMaxQuotaOnUsersWorkspaces(quota); 188 } 189 190 public boolean workQueuesInProgess() { 191 WorkManager workManager = getWorkManager(); 192 int running = workManager.getQueueSize("quota", State.RUNNING); 193 int scheduled = workManager.getQueueSize("quota", State.SCHEDULED); 194 return running + scheduled > 0; 195 } 196 197 public boolean isQuotaSetOnCurrentDocument() { 198 DocumentModel doc = navigationContext.getCurrentDocument(); 199 // the quota info set on the userworkspaces root should be ignored 200 if ("UserWorkspacesRoot".equals(doc.getType())) { 201 return true; 202 } 203 QuotaAware qa = doc.getAdapter(QuotaAware.class); 204 if (qa == null) { 205 return false; 206 } 207 long maxSize = qa.getMaxQuota(); 208 return maxSize > 0; 209 } 210 211 public boolean isActivateQuotaOnUsersWorkspaces() { 212 return activateQuotaOnUsersWorkspaces; 213 } 214 215 public void setActivateQuotaOnUsersWorkspaces(boolean activateQuotaOnUsersWorkspaces) { 216 this.activateQuotaOnUsersWorkspaces = activateQuotaOnUsersWorkspaces; 217 } 218 219 public long getMaxQuotaOnUsersWorkspaces() { 220 return maxQuotaOnUsersWorkspaces; 221 } 222 223 public void setMaxQuotaOnUsersWorkspaces(long maxQuotaOnUsersWorkspaces) { 224 this.maxQuotaOnUsersWorkspaces = maxQuotaOnUsersWorkspaces; 225 } 226 227 QuotaStatsService getQuotaStatsService() { 228 if (quotaStatsService == null) { 229 quotaStatsService = Framework.getLocalService(QuotaStatsService.class); 230 } 231 return quotaStatsService; 232 } 233 234 protected WorkManager getWorkManager() { 235 if (workManager == null) { 236 workManager = Framework.getLocalService(WorkManager.class); 237 } 238 return workManager; 239 } 240}