001/*
002 *  Copyright 2016 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.odfweb.userpref;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.cms.ObservationConstants;
025import org.ametys.cms.repository.Content;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.odf.course.Course;
029import org.ametys.odf.program.Program;
030
031/**
032 * This observer deletes the references of a {@link Program} or a {@link Course} on content deletion for all users' preferences. 
033 */
034public class ODFCartUserPreferencesObserver implements Observer, Serviceable
035{
036    private ODFCartUserPreferencesStorage _odfUserPrefStorage;
037    
038    @Override
039    public void service(ServiceManager smanager) throws ServiceException
040    {
041        _odfUserPrefStorage = (ODFCartUserPreferencesStorage) smanager.lookup(ODFCartUserPreferencesStorage.ROLE);
042    }
043    
044    @Override
045    public boolean supports(Event event)
046    {
047        return event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETING);
048    }
049
050    @Override
051    public int getPriority(Event event)
052    {
053        return MAX_PRIORITY;
054    }
055
056    @Override
057    public void observe(Event event, Map<String, Object> transientVars) throws Exception
058    {
059        Map<String, Object> arguments = event.getArguments();
060        if (arguments.containsKey(ObservationConstants.ARGS_CONTENT))
061        {
062            Content content = (Content) arguments.get(ObservationConstants.ARGS_CONTENT);
063            if (content instanceof Program)
064            {
065                _odfUserPrefStorage.removeProgramFromUserPreferences(content.getId());
066            }
067            else if (content instanceof Course)
068            {
069                _odfUserPrefStorage.removeCourseFromUserPreferences(content.getId());
070            }
071        }
072    }
073}