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 */
016package org.ametys.plugins.odfsync.cdmfr.actions;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.cocoon.acting.AbstractAction;
029import org.apache.cocoon.environment.ObjectModelHelper;
030import org.apache.cocoon.environment.Redirector;
031import org.apache.cocoon.environment.Request;
032import org.apache.cocoon.environment.SourceResolver;
033import org.apache.commons.lang3.StringUtils;
034
035import org.ametys.cms.repository.Content;
036import org.ametys.cms.repository.DefaultContent;
037import org.ametys.odf.ODFHelper;
038import org.ametys.odf.ProgramItem;
039import org.ametys.odf.helper.DeleteODFContentHelper;
040import org.ametys.odf.helper.DeleteODFContentHelper.DeleteMode;
041import org.ametys.odf.program.ProgramFactory;
042import org.ametys.plugins.repository.AmetysObjectResolver;
043
044/**
045 * Delete a program when program was removed on remote server
046 */
047public class DeleteRemoteProgramAction extends AbstractAction implements Serviceable
048{
049    /** The Ametys Object resolver */
050    protected AmetysObjectResolver _resolver;
051    
052    /** The delete ODF content helper */
053    protected DeleteODFContentHelper _deleteODFContentHelper;
054    
055    /** The odf helper */
056    protected ODFHelper _odfHelper;
057    
058    public void service(ServiceManager manager) throws ServiceException
059    {
060        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
061        _deleteODFContentHelper = (DeleteODFContentHelper) manager.lookup(DeleteODFContentHelper.ROLE);
062        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
063    }
064    
065    @Override
066    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
067    {
068        Map<String, Object> results = new HashMap<>();
069        
070        Request request = ObjectModelHelper.getRequest(objectModel);
071
072        Set<String> errors = new HashSet<>();
073        String code = _getParameterValue(request, ProgramItem.CODE, errors);
074        String catalog = _getParameterValue(request, ProgramItem.CATALOG, errors);
075        String lang = _getParameterValue(request, DefaultContent.METADATA_LANGUAGE, errors);
076
077        if (errors.isEmpty())
078        {
079            Content content = _odfHelper.getODFContent(ProgramFactory.PROGRAM_CONTENT_TYPE, code, catalog, lang);
080            
081            // Delete contents bypassing the rights check
082            _deleteODFContentHelper.deleteContents(Collections.singletonList(content.getId()), DeleteMode.FULL.name(), true);
083        }
084        else
085        {
086            results.put("errors", errors);
087        }
088        
089        return results;
090    }
091    
092    private String _getParameterValue(Request request, String parameterName, Set<String> errors)
093    {
094        String result = request.getParameter(parameterName);
095        if (StringUtils.isEmpty(result))
096        {
097            errors.add("Metadata '" + parameterName + "' is mandatory.");
098        }
099        
100        return result;
101    }
102}