001/*
002 *  Copyright 2010 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.plugins.explorer.resources.actions;
017
018import java.util.Enumeration;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.cocoon.servlet.multipart.Part;
030
031import org.ametys.core.cocoon.JSonReader;
032import org.ametys.core.observation.AbstractNotifierAction;
033import org.ametys.plugins.explorer.resources.Resource;
034import org.ametys.plugins.explorer.resources.actions.AddOrUpdateResourceHelper.ResourceOperationMode;
035import org.ametys.plugins.explorer.resources.actions.AddOrUpdateResourceHelper.ResourceOperationResult;
036
037/**
038 * Action for adding or updating a resource file.
039 */
040public class AddOrUpdateResourceAction extends AbstractNotifierAction
041{
042    /** resource operation helper */
043    protected AddOrUpdateResourceHelper _addOrUpdateResourceHelper;
044    
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        super.service(serviceManager);
049        _addOrUpdateResourceHelper = (AddOrUpdateResourceHelper) serviceManager.lookup(AddOrUpdateResourceHelper.ROLE);
050    }
051    
052    @Override
053    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
054    {
055        Request request = ObjectModelHelper.getRequest(objectModel);
056        _parameters2Attributes(request);
057        
058        Part part = (Part) request.get("filename");
059        String parentId = request.getParameter("collection");
060        
061        boolean unzip = Boolean.valueOf(request.getParameter("unzip"));
062        String rawMode = parameters.getParameter("mode", "add");
063        
064        if (unzip)
065        {
066            rawMode = "add-unzip";
067        }
068        
069        ResourceOperationMode mode = ResourceOperationMode.createsFromRawMode(rawMode);
070        
071        // perform the add/update operation
072        ResourceOperationResult operationResult = _addOrUpdateResourceHelper.performResourceOperation(part, parentId, mode);
073        
074        Map<String, Object> result = new HashMap<>();
075        
076        if (!operationResult.isSuccess())
077        {
078            result.put("success", false);
079            result.put("message", operationResult.getErrorMessage());
080        }
081        else
082        {
083            result.put("parentID", parentId);
084            result.put("success", true);
085            
086            if (operationResult.isUnzip())
087            {
088                result.put("unzip", true);
089                result.put("reload", true);
090            }
091            else
092            {
093                Resource resource = operationResult.getResource();
094                result.put("id", resource.getId());
095                result.put("name", resource.getName());
096            }
097        }
098        
099        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
100        return EMPTY_MAP;
101    }
102
103    private void _parameters2Attributes(Request request)
104    {
105        Enumeration paramNames = request.getParameterNames();
106        while (paramNames.hasMoreElements())
107        {
108            String paramName = (String) paramNames.nextElement();
109            if (paramName.startsWith("context_"))
110            {
111                request.setAttribute(paramName.substring("context_".length()), request.getParameter(paramName));
112            }
113        }
114    }
115}