001/*
002 *  Copyright 2012 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.configuration;
017
018import java.io.File;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.thread.ThreadSafe;
024import org.apache.cocoon.acting.AbstractAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029
030import org.ametys.core.cocoon.ActionResultGenerator;
031import org.ametys.runtime.config.ConfigManager;
032import org.ametys.runtime.parameter.Errors;
033import org.ametys.runtime.servlet.RuntimeServlet;
034import org.ametys.runtime.util.AmetysHomeHelper;
035
036
037/**
038 * This action is in charge to get and save the config values entered by the user.<br>
039 * The backup is delegated to <code>Config</code>
040 */
041public class SaveConfigAction extends AbstractAction implements ThreadSafe
042{   
043    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String src, Parameters parameters) throws Exception
044    {
045        if (getLogger().isDebugEnabled())
046        {
047            getLogger().debug("Starting SaveConfigAction");
048        }
049
050        Request request = ObjectModelHelper.getRequest(objectModel);
051        Map<String, Object> result = new HashMap<> ();
052        
053        try
054        {
055            Map<String, String> untypedValues = new HashMap<>();
056            
057            ConfigManager configManager = ConfigManager.getInstance();
058            String[] ids = configManager.getParametersIds();
059            for (int i = 0; i < ids.length; i++)
060            {
061                String untypedValue = request.getParameter(ids[i]);
062                untypedValues.put(ids[i], untypedValue);
063            }
064
065            // Save configuration
066            Map<String, Errors> errorFields = configManager.save(untypedValues, new File(AmetysHomeHelper.getAmetysHomeConfig(), RuntimeServlet.CONFIG_FILE_NAME).getCanonicalPath());
067            if (errorFields.size() > 0)
068            {
069                for (String paramId : errorFields.keySet())
070                {
071                    Errors errors = errorFields.get(paramId);
072                    result.put(paramId, errors);
073                }
074                
075                request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
076                return EMPTY_MAP;
077            }
078        }
079        catch (Exception e)
080        {
081            getLogger().error("An error occured while saving config modifications", e);
082            
083            result.put("error", e.getMessage());
084            request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result);
085            return EMPTY_MAP;
086        }
087
088        // Set the request attribute for Cocoon reloading
089        if (getLogger().isDebugEnabled())
090        {
091            getLogger().debug("Positionning org.ametys.runtime.reload=true for Cocoon reloading");
092        }
093        request.setAttribute("org.ametys.runtime.reload", true);
094
095        return EMPTY_MAP;
096    }
097}