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.HashMap; 019import java.util.HashSet; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023import java.util.stream.Collectors; 024 025import org.apache.avalon.framework.parameters.Parameters; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029import org.apache.cocoon.acting.AbstractAction; 030import org.apache.cocoon.environment.ObjectModelHelper; 031import org.apache.cocoon.environment.Redirector; 032import org.apache.cocoon.environment.Request; 033import org.apache.cocoon.environment.SourceResolver; 034import org.apache.solr.common.StringUtils; 035 036import org.ametys.cms.repository.Content; 037import org.ametys.cms.repository.ContentQueryHelper; 038import org.ametys.cms.repository.DefaultContent; 039import org.ametys.odf.ProgramItem; 040import org.ametys.odf.helper.DeleteODFContentHelper; 041import org.ametys.odf.helper.DeleteODFContentHelper.DeleteMode; 042import org.ametys.odf.program.ProgramFactory; 043import org.ametys.plugins.odfsync.cdmfr.components.ImportCDMFrComponent; 044import org.ametys.plugins.repository.AmetysObjectIterable; 045import org.ametys.plugins.repository.AmetysObjectResolver; 046import org.ametys.plugins.repository.query.expression.AndExpression; 047import org.ametys.plugins.repository.query.expression.Expression; 048 049/** 050 * Delete a program when program was removed on remote server 051 */ 052public class DeleteRemoteProgramAction extends AbstractAction implements Serviceable 053{ 054 /** The Ametys Object resolver */ 055 protected AmetysObjectResolver _resolver; 056 057 /** The delete ODF content helper */ 058 protected DeleteODFContentHelper _deleteODFContentHelper; 059 060 /** The import CDM-fr component */ 061 protected ImportCDMFrComponent _importCDMFrComponent; 062 063 public void service(ServiceManager manager) throws ServiceException 064 { 065 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 066 _deleteODFContentHelper = (DeleteODFContentHelper) manager.lookup(DeleteODFContentHelper.ROLE); 067 _importCDMFrComponent = (ImportCDMFrComponent) manager.lookup(ImportCDMFrComponent.ROLE); 068 } 069 070 @Override 071 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 072 { 073 Map<String, Object> results = new HashMap<>(); 074 075 Request request = ObjectModelHelper.getRequest(objectModel); 076 077 Set<String> errors = new HashSet<>(); 078 String code = _getParameterValue(request, ProgramItem.METADATA_CODE, errors); 079 String catalog = _getParameterValue(request, ProgramItem.METADATA_CATALOG, errors); 080 String lang = _getParameterValue(request, DefaultContent.METADATA_LANGUAGE, errors); 081 082 if (errors.isEmpty()) 083 { 084 List<Expression> expList = _importCDMFrComponent.getExpressionsList(lang, code, ProgramFactory.PROGRAM_CONTENT_TYPE, catalog); 085 AndExpression andExp = new AndExpression(expList.toArray(new Expression[expList.size()])); 086 String xPathQuery = ContentQueryHelper.getContentXPathQuery(andExp); 087 088 AmetysObjectIterable<Content> contents = _resolver.query(xPathQuery); 089 List<String> contentsId = contents 090 .stream() 091 .map(Content::getId) 092 .collect(Collectors.toList()); 093 094 return _deleteODFContentHelper.deleteContents(contentsId, DeleteMode.FULL.name()); 095 } 096 097 results.put("errors", errors); 098 return results; 099 } 100 101 private String _getParameterValue(Request request, String parameterName, Set<String> errors) 102 { 103 String result = request.getParameter(parameterName); 104 if (StringUtils.isEmpty(result)) 105 { 106 errors.add("Metadata '" + parameterName + "' is mandatory."); 107 } 108 return result; 109 110 } 111}