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