001/*
002 *  Copyright 2024 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.odf.observation;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.core.observation.AsyncObserver;
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.odf.EducationalPathHelper;
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031
032/**
033 * {@link Observer} for observing content deletion in order to remove repeater'entries with a educational path that reference this content
034 */
035public class RemoveEducationalPathOnContentDeletionObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable
036{
037    /** The educational path helper */
038    protected EducationalPathHelper _educationalPathHelper;
039
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        _educationalPathHelper = (EducationalPathHelper) smanager.lookup(EducationalPathHelper.ROLE);
043    }
044
045    public boolean supports(Event event)
046    {
047        return event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETED);
048    }
049
050    public int getPriority(Event event)
051    {
052        return MIN_PRIORITY;
053    }
054    
055    public void observe(Event event, Map<String, Object> transientVars) throws Exception
056    {
057        String contentId = (String) event.getArguments().get(ObservationConstants.ARGS_CONTENT_ID);
058        if (contentId != null)
059        {
060            _educationalPathHelper.removeEducationalPathReferences(List.of(contentId));
061        }
062    }
063
064}