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;
036import org.ametys.plugins.repository.RepositoryConstants;
037import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
038
039/**
040 * Action for adding or updating a resource file.
041 */
042public class AddOrUpdateResourceAction extends AbstractNotifierAction
043{
044    /** resource operation helper */
045    protected AddOrUpdateResourceHelper _addOrUpdateResourceHelper;
046    
047    @Override
048    public void service(ServiceManager serviceManager) throws ServiceException
049    {
050        super.service(serviceManager);
051        _addOrUpdateResourceHelper = (AddOrUpdateResourceHelper) serviceManager.lookup(AddOrUpdateResourceHelper.ROLE);
052    }
053    
054    @Override
055    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
056    {
057        Request request = ObjectModelHelper.getRequest(objectModel);
058        
059        // Retrieve the current workspace.
060        String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
061        
062        try
063        {
064            // Force the default workspace.
065            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE);
066            
067            _parameters2Attributes(request);
068            
069            Part part = (Part) request.get("filename");
070            String parentId = request.getParameter("collection");
071            
072            boolean unzip = Boolean.valueOf(request.getParameter("unzip"));
073            String rawMode = parameters.getParameter("mode", "add");
074            
075            if (unzip)
076            {
077                rawMode = "add-unzip";
078            }
079            
080            ResourceOperationMode mode = ResourceOperationMode.createsFromRawMode(rawMode);
081            
082            // perform the add/update operation
083            _addOrUpdateResourceHelper.checkAddResourceRight(parentId);
084            ResourceOperationResult operationResult = _addOrUpdateResourceHelper.performResourceOperation(part, parentId, mode);
085            
086            Map<String, Object> result = new HashMap<>();
087            
088            if (!operationResult.isSuccess())
089            {
090                result.put("success", false);
091                result.put("message", operationResult.getErrorMessage());
092            }
093            else
094            {
095                result.put("parentID", parentId);
096                result.put("success", true);
097                
098                if (operationResult.isUnzip())
099                {
100                    result.put("unzip", true);
101                    result.put("reload", true);
102                }
103                else
104                {
105                    Resource resource = operationResult.getResource();
106                    result.put("id", resource.getId());
107                    result.put("name", resource.getName());
108                }
109            }
110            
111            request.setAttribute(JSonReader.OBJECT_TO_READ, result);
112            return EMPTY_MAP;
113        }
114        finally
115        {
116            // Restore context
117            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
118        }
119    }
120
121    private void _parameters2Attributes(Request request)
122    {
123        Enumeration paramNames = request.getParameterNames();
124        while (paramNames.hasMoreElements())
125        {
126            String paramName = (String) paramNames.nextElement();
127            if (paramName.startsWith("context_"))
128            {
129                request.setAttribute(paramName.substring("context_".length()), request.getParameter(paramName));
130            }
131        }
132    }
133}