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