001/*
002 *  Copyright 2016 Anyware Services
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 */
016package org.ametys.workspaces.repository.maintenance;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.logger.AbstractLogEnabled;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.commons.lang.StringUtils;
027
028import org.ametys.core.ui.Callable;
029import org.ametys.workspaces.repository.maintenance.MaintenanceTaskManager.MaintenanceTaskType;
030
031/**
032 * Component to launch and follow the maintenance tasks on the repository
033 */
034public class MaintenanceTaskComponent extends AbstractLogEnabled implements Component, Serviceable
035{
036    /** The maintenance task manager */
037    private MaintenanceTaskManager _taskManager;
038
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        _taskManager = (MaintenanceTaskManager) smanager.lookup(MaintenanceTaskManager.ROLE);
043    }
044
045    /**
046     * Start a Maintenance Task
047     * @param taskName the name of the task
048     * @return a JSon object
049     */
050    @Callable
051    public Map<String, Object> startTask(String taskName)
052    {
053        Map<String, Object> result = new HashMap<>();
054
055        try
056        {
057            boolean launched = _taskManager.launch(MaintenanceTaskType.valueOf(taskName));
058            result.put("launched", launched);
059        }
060        catch (Exception e)
061        {
062            getLogger().error(e.getMessage(), e);
063            result.put("launched", false);
064        }
065        
066        result.put("running", _taskManager.isTaskRunning());
067        
068        return result;
069    }
070
071    /**
072     * Check if a task is running or not
073     * @param taskName The task name
074     * @return true if the task is running
075     */
076    @Callable
077    public Map<String, Object> isRunning(String taskName)
078    {
079        Map<String, Object> result = new HashMap<>();
080
081        String isRunning = Boolean.toString(_taskManager.isTaskRunning());
082        result.put("running", isRunning);
083        
084        return result;
085    }
086
087    /**
088     * Get the information of a running task
089     * @param taskName the task name
090     * @return the information
091     */
092    @Callable
093    public Map<String, Object> getInformation(String taskName)
094    {
095        Map<String, Object> result = new HashMap<>();
096
097        // Is task finished ?
098        boolean taskRunning = _taskManager.isTaskRunning();
099        result.put("isRunning", taskRunning);
100        if (taskRunning)
101        {
102            result.put("taskName", StringUtils.defaultString(_taskManager.getRunningTaskType()));
103        }
104        else
105        {
106            result.put("lastTaskName", StringUtils.defaultString(_taskManager.getLastRunningTaskType()));
107        }
108        
109        // progress to json
110        result.put("progress", _taskManager.getProgressInfo());
111
112        // repository state to json
113        result.put("respositoryState", _taskManager.isRepositoryStarted());
114
115        return result;
116    }
117}