001/*
002 *  Copyright 2018 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 */
016
017package org.ametys.cms.repository.comment.actions;
018
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Redirector;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.environment.SourceResolver;
028import org.apache.commons.lang.StringUtils;
029
030import org.ametys.cms.content.ContentHelper;
031import org.ametys.core.cocoon.ActionResultGenerator;
032import org.ametys.plugins.core.user.UserHelper;
033
034/**
035 * Delete a comment
036 */
037public class DeleteCommentAction extends AbstractCommentAction
038{
039    /** The content helper */
040    protected ContentHelper _contentHelper;
041   
042    /** The user helper */
043    protected UserHelper _userHelper;
044   
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE);
050        _userHelper = (UserHelper) smanager.lookup(UserHelper.ROLE);
051    }
052    
053    @Override
054    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
055    {
056        Request request = ObjectModelHelper.getRequest(objectModel);
057
058        String contentId = request.getParameter(PARAMETER_CONTENTID);
059        if (StringUtils.isBlank(contentId))
060        {
061            throw new IllegalArgumentException("Missing or empty 'content-id' parameter to like a comment");
062        }
063        
064        String commentId = request.getParameter(PARAMETER_COMMENTID);
065        if (StringUtils.isBlank(commentId))
066        {
067            throw new IllegalArgumentException("Missing or empty 'comment-id' parameter to like a comment");
068        }
069        
070        Map<String, Object> results = _commentsDAO.deleteComment(contentId, commentId);
071        
072        request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, results);
073        return EMPTY_MAP;
074    }
075}