001/*
002 *  Copyright 2023 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.runtime.plugins.admin.system;
017
018import java.time.ZonedDateTime;
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022
023import org.ametys.core.ui.Callable;
024import org.ametys.core.ui.StaticClientSideElement;
025import org.ametys.runtime.servlet.RuntimeServlet;
026import org.ametys.runtime.servlet.RuntimeServlet.ForcedMainteanceInformations;
027import org.ametys.runtime.servlet.RuntimeServlet.MaintenanceStatus;
028import org.ametys.runtime.servlet.RuntimeServlet.RunMode;
029
030/**
031 * The maintenance button
032 */
033public class MaintenanceClientSideElement extends StaticClientSideElement
034{
035    @Override
036    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
037    {
038        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
039        List<Script> modifiedScripts = new ArrayList<>();
040        
041        for (Script script : scripts)
042        {
043            Script modifiedScript = new Script(script);
044            modifiedScripts.add(modifiedScript);
045            
046            switch (RuntimeServlet.getMaintenanceStatus())
047            {
048                case AUTOMATIC:
049                    modifiedScript.getParameters().put("toggle-state", "true");
050                    modifiedScript.getParameters().put("disabled", "true");
051                    modifiedScript.getParameters().put("description", modifiedScript.getParameters().get("automatic-description"));
052                    modifiedScript.getParameters().put("icon-decorator", modifiedScript.getParameters().get("on-icon-decorator"));
053                    modifiedScript.getParameters().put("icon-decorator-type", modifiedScript.getParameters().get("on-icon-decorator-type"));
054                    break;
055                case FORCED: 
056                    modifiedScript.getParameters().put("toggle-state", "true");
057                    modifiedScript.getParameters().put("disabled", "false");
058                    modifiedScript.getParameters().put("description", modifiedScript.getParameters().get("forced-on-description"));
059                    modifiedScript.getParameters().put("icon-decorator", modifiedScript.getParameters().get("on-icon-decorator"));
060                    modifiedScript.getParameters().put("icon-decorator-type", modifiedScript.getParameters().get("on-icon-decorator-type"));
061                    break;
062                case NONE:
063                default:
064                    modifiedScript.getParameters().put("toggle-state", "false");
065                    modifiedScript.getParameters().put("disabled", "false");
066                    modifiedScript.getParameters().put("description", modifiedScript.getParameters().get("forced-off-description"));
067                    modifiedScript.getParameters().put("icon-decorator", modifiedScript.getParameters().get("off-icon-decorator"));
068                    modifiedScript.getParameters().put("icon-decorator-type", modifiedScript.getParameters().get("off-icon-decorator-type"));
069                    break;
070            }
071        }
072        
073        return modifiedScripts;
074    }
075    
076    /**
077     * Set maintenance status to normal, so maintenance is not activated anymore
078     */
079    @Callable(context = "/admin", right = "Runtime_Rights_Admin_Maintenance_Activate")
080    public void goToNormalMode()
081    {
082        if (RuntimeServlet.getRunMode() == RunMode.MAINTENANCE
083            && RuntimeServlet.getMaintenanceStatus() == MaintenanceStatus.FORCED)
084        {
085            RuntimeServlet.setMaintenanceStatus(MaintenanceStatus.NONE, null);
086            RuntimeServlet.setRunMode(RunMode.NORMAL);
087        }
088    }
089    
090    /**
091     * Set maintenance status to maintenance, so maintenance is active
092     * @param comment The maintenance comment
093     */
094    @Callable(context = "/admin", right = "Runtime_Rights_Admin_Maintenance_Activate")
095    public void goToMaintenanceMode(String comment)
096    {
097        if (RuntimeServlet.getRunMode() == RunMode.NORMAL)
098        {
099            RuntimeServlet.setMaintenanceStatus(MaintenanceStatus.FORCED, new ForcedMainteanceInformations(comment, _currentUserProvider.getUser(), ZonedDateTime.now()));
100            RuntimeServlet.setRunMode(RunMode.MAINTENANCE);
101        }
102    }
103}