001/*
002 * (C) Copyright 2015 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
018 */
019
020package org.nuxeo.ecm.admin.permissions;
021
022import java.io.Serializable;
023
024import org.jboss.seam.ScopeType;
025import org.jboss.seam.annotations.In;
026import org.jboss.seam.annotations.Install;
027import org.jboss.seam.annotations.Name;
028import org.jboss.seam.annotations.Scope;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.work.api.Work;
031import org.nuxeo.ecm.core.work.api.WorkManager;
032import org.nuxeo.ecm.platform.contentview.jsf.ContentView;
033import org.nuxeo.ecm.platform.contentview.seam.ContentViewActions;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * @since 7.4
038 */
039@Name("adminPermissionsActions")
040@Scope(ScopeType.CONVERSATION)
041@Install(precedence = Install.FRAMEWORK)
042public class AdminPermissionsActions implements Serializable {
043
044    private static final long serialVersionUID = 1L;
045
046    public final static String PERMISSIONS_PURGE_CONTENT_VIEW = "PERMISSIONS_PURGE";
047
048    public static final String CATEGORY_PURGE_WORK = "permissionsPurge";
049
050    public static final String ACE_STATUS_ALL = "all";
051
052    @In(create = true)
053    protected ContentViewActions contentViewActions;
054
055    protected String purgeWorkId;
056
057    protected String selectedACEStatus = ACE_STATUS_ALL;
058
059    public String getSelectedACEStatus() {
060        return selectedACEStatus;
061    }
062
063    public void setSelectedACEStatus(String selectedACEStatus) {
064        this.selectedACEStatus = selectedACEStatus;
065    }
066
067    public String getACEStatusFixedPart() {
068        switch (selectedACEStatus) {
069            case ACE_STATUS_ALL:
070                return null;
071            case "0":
072                return "AND ecm:acl/*1/status = 0";
073            case "1":
074                return "AND (ecm:acl/*1/status IS NULL OR ecm:acl/*1/status = 1)";
075            case "2":
076                return "AND ecm:acl/*1/status = 2";
077            default:
078                return null;
079        }
080    }
081
082    public void doPurge() {
083        ContentView contentView = contentViewActions.getContentView(PERMISSIONS_PURGE_CONTENT_VIEW);
084        DocumentModel searchDocumentModel = contentView.getSearchDocumentModel();
085        PermissionsPurgeWork work = new PermissionsPurgeWork(searchDocumentModel);
086        purgeWorkId = work.getId();
087        WorkManager workManager = Framework.getLocalService(WorkManager.class);
088        workManager.schedule(work, WorkManager.Scheduling.IF_NOT_RUNNING_OR_SCHEDULED);
089    }
090
091    public void cancelPurge() {
092        ContentView contentView = contentViewActions.getContentView(PERMISSIONS_PURGE_CONTENT_VIEW);
093        contentView.resetSearchDocumentModel();
094        purgeWorkId = null;
095    }
096
097    public boolean canStartPurge() {
098        WorkManager workManager = Framework.getLocalService(WorkManager.class);
099        return workManager.getQueueSize("permissionsPurge", Work.State.RUNNING) <= 0;
100    }
101
102    public PurgeWorkStatus getPurgeStatus() {
103        if (purgeWorkId == null) {
104            return null;
105        }
106
107        WorkManager workManager = Framework.getLocalService(WorkManager.class);
108        Work.State workState = workManager.getWorkState(purgeWorkId);
109        if (workState == null) {
110            return null;
111        }
112        return new PurgeWorkStatus(workState);
113    }
114
115    public static class PurgeWorkStatus {
116
117        private final Work.State state;
118
119        public PurgeWorkStatus(Work.State state) {
120            this.state = state;
121        }
122
123        public Work.State getState() {
124            return state;
125        }
126
127        public boolean isScheduled() {
128            return state == Work.State.SCHEDULED;
129        }
130
131        public boolean isRunning() {
132            return state == Work.State.RUNNING;
133        }
134
135        public boolean isComplete() {
136            return state == Work.State.COMPLETED;
137        }
138    }
139}