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