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