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    @In(create = true)
049    protected ContentViewActions contentViewActions;
050
051    protected String purgeWorkId;
052
053    public void doPurge() {
054        ContentView contentView = contentViewActions.getContentView(PERMISSIONS_PURGE_CONTENT_VIEW);
055        DocumentModel searchDocumentModel = contentView.getSearchDocumentModel();
056        PermissionsPurgeWork work = new PermissionsPurgeWork(searchDocumentModel);
057        purgeWorkId = work.getId();
058        WorkManager workManager = Framework.getLocalService(WorkManager.class);
059        workManager.schedule(work, WorkManager.Scheduling.IF_NOT_RUNNING_OR_SCHEDULED);
060    }
061
062    public void cancelPurge() {
063        ContentView contentView = contentViewActions.getContentView(PERMISSIONS_PURGE_CONTENT_VIEW);
064        contentView.resetSearchDocumentModel();
065        purgeWorkId = null;
066    }
067
068    public boolean canStartPurge() {
069        WorkManager workManager = Framework.getLocalService(WorkManager.class);
070        return workManager.getQueueSize("permissionsPurge", Work.State.RUNNING) <= 0;
071    }
072
073    public PurgeWorkStatus getPurgeStatus() {
074        if (purgeWorkId == null) {
075            return null;
076        }
077
078        WorkManager workManager = Framework.getLocalService(WorkManager.class);
079        Work.State workState = workManager.getWorkState(purgeWorkId);
080        if (workState == null) {
081            return null;
082        }
083        return new PurgeWorkStatus(workState);
084    }
085
086    public static class PurgeWorkStatus {
087
088        private final Work.State state;
089
090        public PurgeWorkStatus(Work.State state) {
091            this.state = state;
092        }
093
094        public Work.State getState() {
095            return state;
096        }
097
098        public boolean isScheduled() {
099            return state == Work.State.SCHEDULED;
100        }
101
102        public boolean isRunning() {
103            return state == Work.State.RUNNING;
104        }
105
106        public boolean isComplete() {
107            return state == Work.State.COMPLETED;
108        }
109    }
110}