001/*
002 *  Copyright 2017 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.linkdirectory.link;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.Parameters;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.acting.ServiceableAction;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031
032import org.ametys.cms.repository.RequestAttributeWorkspaceSelector;
033import org.ametys.core.cocoon.JSonReader;
034import org.ametys.core.user.CurrentUserProvider;
035import org.ametys.core.user.UserIdentity;
036import org.ametys.core.util.JSONUtils;
037import org.ametys.plugins.linkdirectory.Link;
038import org.ametys.plugins.repository.RepositoryConstants;
039
040/**
041 * Removes a user link in Ametys.
042 */
043public class RemoveUserLinkAction extends ServiceableAction
044{
045    /** The DAO for {@link Link}s */
046    protected LinkDAO _linkDAO;
047    /** The current user provider */
048    protected CurrentUserProvider _currentUserProvider;
049    /** The JSON utils */
050    protected JSONUtils _jsonUtils;
051
052    @Override
053    public void service(ServiceManager smanager) throws ServiceException
054    {
055        super.service(smanager);
056        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
057        _jsonUtils = (JSONUtils) smanager.lookup(JSONUtils.ROLE);
058        _linkDAO = (LinkDAO) smanager.lookup(LinkDAO.ROLE);
059    }
060    
061    @Override
062    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
063    {
064        Request request = ObjectModelHelper.getRequest(objectModel);
065        Map<String, Object> result = new HashMap<>();
066        
067        UserIdentity user = _currentUserProvider.getUser();
068        
069        // Check user authenticated
070        if (user != null)
071        {
072            String idsAsString = request.getParameter("ids");
073            Object[] array = _jsonUtils.convertJsonToArray(idsAsString);
074            
075            List<String> linksToRemove = new ArrayList<>();
076            for (Object id : array)
077            {
078                if (id instanceof String)
079                {
080                    linksToRemove.add((String) id);
081                }
082            }
083            
084            _deleteLinks(request, linksToRemove);
085            result.put("error", "none");
086        }
087        else
088        {
089            result.put("error", "unauthenticated-user");
090        }
091        
092        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
093        return EMPTY_MAP;
094    }
095    
096    private List<String> _deleteLinks(Request request, List<String> ids)
097    {
098        // Retrieve the current workspace.
099        String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
100        
101        try
102        {
103            // Force the workspace.
104            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE);
105            
106            return _linkDAO.deleteLinks(ids);
107        }
108        finally
109        {
110            // Restore context
111            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
112        }
113    }
114}