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.site;
017
018import java.time.ZonedDateTime;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.cocoon.acting.AbstractAction;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Redirector;
025import org.apache.cocoon.environment.SourceResolver;
026
027import org.ametys.core.user.UserIdentity;
028import org.ametys.runtime.servlet.RuntimeServlet;
029import org.ametys.runtime.servlet.RuntimeServlet.MaintenanceStatus;
030import org.ametys.runtime.servlet.RuntimeServlet.RunMode;
031
032/**
033 * The back is changing the maintenance status
034 */
035public class SetMaintenanceStatus extends AbstractAction
036{
037    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
038    {
039        String statusAsString = ObjectModelHelper.getRequest(objectModel).getParameter("status");
040        String comment = ObjectModelHelper.getRequest(objectModel).getParameter("comment");
041        String initiatorAsString = ObjectModelHelper.getRequest(objectModel).getParameter("initiator");
042        
043        MaintenanceStatus status = RuntimeServlet.MaintenanceStatus.valueOf(statusAsString);
044        
045        if (RuntimeServlet.getRunMode() == RunMode.NORMAL
046            || RuntimeServlet.getRunMode() == RunMode.MAINTENANCE && RuntimeServlet.getMaintenanceStatus() != MaintenanceStatus.AUTOMATIC)
047        {
048            switch (status)
049            {
050                case AUTOMATIC:
051                case FORCED:
052                    RuntimeServlet.setMaintenanceStatus(MaintenanceStatus.FORCED, new RuntimeServlet.ForcedMainteanceInformations(comment, UserIdentity.stringToUserIdentity(initiatorAsString), ZonedDateTime.now()));
053                    RuntimeServlet.setRunMode(RunMode.MAINTENANCE);
054                    break;
055                case NONE:
056                default:
057                    RuntimeServlet.setMaintenanceStatus(MaintenanceStatus.NONE, null);
058                    RuntimeServlet.setRunMode(RunMode.NORMAL);
059            }
060        }
061        
062        return EMPTY_MAP;
063    }
064
065}