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.odf.observation; 017 018import java.util.Arrays; 019import java.util.HashMap; 020import java.util.LinkedHashSet; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027import org.apache.commons.lang3.ArrayUtils; 028 029import org.ametys.cms.ObservationConstants; 030import org.ametys.cms.repository.Content; 031import org.ametys.cms.workflow.ContentWorkflowHelper; 032import org.ametys.core.observation.Event; 033import org.ametys.core.observation.ObservationManager; 034import org.ametys.core.observation.Observer; 035import org.ametys.core.user.CurrentUserProvider; 036import org.ametys.odf.course.Course; 037import org.ametys.odf.coursepart.CoursePart; 038import org.ametys.runtime.plugin.component.AbstractLogEnabled; 039 040/** 041 * Observer to unlink a {@link CoursePart} on {@link Course} deletion. 042 */ 043public class CourseDeletedObserver extends AbstractLogEnabled implements Observer, Serviceable 044{ 045 private ObservationManager _observationManager; 046 private CurrentUserProvider _userProvider; 047 private ContentWorkflowHelper _contentWorkflowHelper; 048 049 @Override 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE); 053 _userProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 054 _contentWorkflowHelper = (ContentWorkflowHelper) manager.lookup(ContentWorkflowHelper.ROLE); 055 } 056 057 @Override 058 public boolean supports(Event event) 059 { 060 if (event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETING)) 061 { 062 Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT); 063 return content instanceof Course; 064 } 065 066 return false; 067 } 068 069 @Override 070 public int getPriority(Event event) 071 { 072 return 0; 073 } 074 075 @Override 076 public void observe(Event event, Map<String, Object> transientVars) throws Exception 077 { 078 Course course = (Course) event.getArguments().get(ObservationConstants.ARGS_CONTENT); 079 for (CoursePart coursePart : course.getCourseParts()) 080 { 081 String[] courses = coursePart.getMetadataHolder().getStringArray(CoursePart.METADATA_PARENT_COURSES, ArrayUtils.EMPTY_STRING_ARRAY); 082 Set<String> newCourses = new LinkedHashSet<>(); 083 newCourses.addAll(Arrays.asList(courses)); 084 newCourses.remove(course.getId()); 085 coursePart.getMetadataHolder().setMetadata(CoursePart.METADATA_PARENT_COURSES, newCourses.toArray(new String[0])); 086 coursePart.saveChanges(); 087 088 // Notify listeners 089 Map<String, Object> eventParams = new HashMap<>(); 090 eventParams.put(ObservationConstants.ARGS_CONTENT, coursePart); 091 eventParams.put(ObservationConstants.ARGS_CONTENT_ID, coursePart.getId()); 092 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_MODIFIED, _userProvider.getUser(), eventParams)); 093 094 _contentWorkflowHelper.doAction(coursePart, 22); 095 } 096 } 097 098}