001/*
002 * (C) Copyright 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.admin.work;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.jboss.seam.ScopeType;
029import org.jboss.seam.annotations.Name;
030import org.jboss.seam.annotations.Scope;
031import org.jboss.seam.annotations.web.RequestParameter;
032import org.nuxeo.ecm.core.work.SleepWork;
033import org.nuxeo.ecm.core.work.api.Work;
034import org.nuxeo.ecm.core.work.api.Work.State;
035import org.nuxeo.ecm.core.work.api.WorkManager;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Seam bean that wraps the {@link WorkManager} service to provide a JSF admin UI.
040 */
041@Name("workManagerAdmin")
042@Scope(ScopeType.PAGE)
043public class WorkManagerAdminBean implements Serializable {
044
045    private static final long serialVersionUID = 1L;
046
047    @RequestParameter("queueId")
048    protected String queueId;
049
050    protected WorkManager getWorkManager() {
051        return Framework.getLocalService(WorkManager.class);
052    }
053
054    public List<Map<String, Object>> getWorkQueuesInfo() {
055        List<Map<String, Object>> info = new ArrayList<Map<String, Object>>();
056        WorkManager workManager = getWorkManager();
057        List<String> workQueueIds = workManager.getWorkQueueIds();
058        Collections.sort(workQueueIds);
059        for (String queueId : workQueueIds) {
060            List<Work> running = workManager.listWork(queueId, State.RUNNING);
061            int scheduled = workManager.getQueueSize(queueId, State.SCHEDULED);
062            int completed = workManager.getQueueSize(queueId, State.COMPLETED);
063            Map<String, Object> map = new HashMap<String, Object>();
064            info.add(map);
065            map.put("id", queueId);
066            map.put("scheduled", scheduled);
067            map.put("running", running.size());
068            map.put("completed", completed);
069            map.put("runningWorks", running);
070        }
071        return info;
072    }
073
074    public long getCurrentTimeMillis() {
075        return System.currentTimeMillis();
076    }
077
078    public String clearQueueCompletedWork() {
079        getWorkManager().clearCompletedWork(queueId);
080        return null;
081    }
082
083    public String clearAllCompletedWork() {
084        getWorkManager().clearCompletedWork(0);
085        return null;
086    }
087
088    public String startTestWork() {
089        getWorkManager().schedule(new SleepWork(10000));
090        return null;
091    }
092
093}