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 being unlinked from their parent.
030 * We compute the skills to delete by comparing the previous skills with the current ones.
031 * When the modified content is a Program, delete the linked macro skills that became orphans
032 *    The deletion of macro skills will trigger the deletion of its micro skills
033 * When the modified content is a Macro skill, delete the linked micro skills that became orphans
034 */
035public class UpdateContentSkillStep2Observer extends AbstractSkillsStepObserver
036{
037    @Override
038    protected String getSupportedEventId()
039    {
040        return ObservationConstants.EVENT_CONTENT_MODIFIED;
041    }
042    
043    public void observe(Event event, Map<String, Object> transientVars) throws Exception
044    {
045        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
046        // When the content is modified, we need to check if the skills are still linked to the content
047        ContentValue[] previousSkills = _getRequestAttribute("previousSkills", content);
048        if (previousSkills != null)
049        {
050            ContentValue[] currentSkills = _getSkills(content);
051
052            // Compare the previous skills with the current ones to retrieve the skills which became orphans to delete them
053            ContentValue[] skillsToDeleteContentValue = getSkillsToDelete(previousSkills, currentSkills);
054            if (skillsToDeleteContentValue != null)
055            {
056                List<Content> skillsToDelete = Arrays.stream(skillsToDeleteContentValue)
057                        .map(contentValue -> contentValue.getContentIfExists())
058                        .filter(Optional::isPresent)
059                        .map(Optional::get)
060                        .map(skillToDelete -> (Content) skillToDelete)
061                        .toList();
062                
063                if (!skillsToDelete.isEmpty())
064                {
065                    // Remove the orphan skills
066                    _contentDAO.forceDeleteContentsObj(skillsToDelete, null);
067                }
068            }
069        }
070    }
071}