001/*
002 * (C) Copyright 2017 Nuxeo (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 *     arameshkumar
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.quota.automation;
021
022import org.nuxeo.ecm.automation.OperationException;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
030import org.nuxeo.ecm.quota.QuotaStatsService;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Recomputes Quota size statistics.
035 *
036 * @since 10.1
037 */
038@Operation(id = RecomputeQuotaStatistics.ID, category = "Quotas", //
039        label = "Recompute quota statistics on documents, optionally only for a tenant, user or path")
040public class RecomputeQuotaStatistics {
041
042    public static final String ID = "Quotas.RecomputeStatistics";
043
044    public static final String SIZE_UPDATER = "documentsSizeUpdater";
045
046    @Param(name = "tenantId", required = false)
047    protected String tenantId;
048
049    @Param(name = "username", required = false)
050    protected String username;
051
052    @Param(name = "path", required = false)
053    protected String path;
054
055    @Param(name = "updaterName", required = false)
056    protected String updaterName;
057
058    @Context
059    protected CoreSession session;
060
061    @Context
062    protected QuotaStatsService quotaStatsService;
063
064    @OperationMethod
065    public String run() throws OperationException {
066        String docPath;
067        if (tenantId != null) {
068            if (username != null || path != null) {
069                throw new OperationException("Only one of tenantId, username or path can be defined");
070            }
071            docPath = getTenantPath();
072        } else if (username != null) {
073            if (path != null) {
074                throw new OperationException("Only one of tenantId, username or path can be defined");
075            }
076            docPath = getUserPersonalWorkspacePath();
077        } else {
078            docPath = path; // may be null
079        }
080        if (updaterName == null) {
081            updaterName = SIZE_UPDATER;
082        }
083        String repositoryName = session.getRepositoryName();
084        quotaStatsService.launchInitialStatisticsComputation(updaterName, repositoryName, docPath);
085        return quotaStatsService.getProgressStatus(updaterName, repositoryName);
086    }
087
088    protected String getTenantPath() throws OperationException {
089        // TODO this should use the multi-tenant service instead of assuming that tenants are per-domain
090        if (tenantId.contains("/")) {
091            throw new OperationException("Invalid tenantId: " + tenantId);
092        }
093        return "/" + tenantId;
094    }
095
096    protected String getUserPersonalWorkspacePath() throws OperationException {
097        UserWorkspaceService uws = Framework.getService(UserWorkspaceService.class);
098        DocumentModel userWorkspace = uws.getUserPersonalWorkspace(username, session.getRootDocument());
099        if (userWorkspace == null) {
100            throw new OperationException("Invalid username or missing user workspace: " + username);
101        }
102        return userWorkspace.getPathAsString();
103    }
104
105}