001/* 002 * Copyright 2025 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.skill; 017 018import java.util.Arrays; 019import java.util.List; 020import java.util.Map; 021import java.util.Optional; 022 023import org.ametys.cms.ObservationConstants; 024import org.ametys.cms.data.ContentValue; 025import org.ametys.cms.repository.Content; 026import org.ametys.core.observation.Event; 027 028/** 029 * Observer to delete skills that became orphans after the deletion of their parent. 030 * When the deleted content is a Program, delete the linked macro skills that would become orphans 031 * The deletion of macro skills will trigger the deletion of its micro skills 032 * When the deleted content is a Macro skill, delete the linked micro skills that would become orphans 033 */ 034public class DeleteContentSkillObserver extends AbstractSkillsObserver 035{ 036 @Override 037 protected String getSupportedEventId() 038 { 039 return ObservationConstants.EVENT_CONTENT_DELETING; 040 } 041 042 public void observe(Event event, Map<String, Object> transientVars) throws Exception 043 { 044 Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT); 045 046 // When the parent content is deleted, we need to delete the orphan skills 047 ContentValue[] skills = _getSkills(content); 048 if (skills != null && skills.length > 0) 049 { 050 List<Content> skillsToDelete = Arrays.stream(skills) 051 // Only retrieve still existing contents 052 .map(contentValue -> contentValue.getContentIfExists()) 053 // Filter out null values 054 .filter(Optional::isPresent) 055 .map(Optional::get) 056 .map(skillToDeleteContent -> (Content) skillToDeleteContent) 057 .toList(); 058 059 if (!skillsToDelete.isEmpty()) 060 { 061 // Remove the orphan skills 062 _contentDAO.forceDeleteContentsObj(skillsToDelete, null); 063 } 064 } 065 } 066}