001/*
002 *  Copyright 2026 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.observation.solr;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028
029import org.ametys.cms.ObservationConstants;
030import org.ametys.cms.content.indexing.solr.observation.ObserverHelper;
031import org.ametys.cms.repository.Content;
032import org.ametys.core.observation.Event;
033import org.ametys.core.observation.Observer;
034import org.ametys.odf.ODFHelper;
035import org.ametys.odf.ProgramItem;
036import org.ametys.odf.program.Program;
037import org.ametys.odf.skill.ODFSkillsHelper;
038import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
039import org.ametys.web.WebConstants;
040import org.ametys.web.indexing.observation.AbstractSolrObserver;
041
042
043/**
044 * {@link Observer} to update the parent programs skills property as soon as a course was added, removed or modified in a program tree.
045 */
046public class IndexProgramMicroSkillsPropertyOnProgramItemValidatedOrUnpublishedObserver extends AbstractSolrObserver implements Contextualizable
047{
048    private ODFHelper _odfHelper;
049    
050    private Context _context;
051
052    @Override
053    public void service(ServiceManager smanager) throws ServiceException
054    {
055        super.service(smanager);
056        _odfHelper = (ODFHelper) smanager.lookup(ODFHelper.ROLE);
057    }
058    
059    public void contextualize(Context context) throws ContextException
060    {
061        _context = context;
062    }
063
064    public boolean supports(Event event)
065    {
066        if (!ODFSkillsHelper.isSkillsEnabled())
067        {
068            return false;
069        }
070        
071        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
072        return (event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED)
073                || event.getId().equals(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE))
074                && content instanceof ProgramItem;
075    }
076
077    public void observe(Event event, Map<String, Object> transientVars) throws Exception
078    {
079        if (ObserverHelper.isNotSuspendedObservationForIndexation())
080        {
081            Request request = ContextHelper.getRequest(_context);
082            String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
083            
084            try
085            {
086                // Force the workspace.
087                RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE);
088                
089                ProgramItem programItem = (ProgramItem) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
090                
091                List<Program> parentPrograms = _odfHelper.getParentPrograms(programItem)
092                        .stream()
093                        .filter(program -> _resolver.hasAmetysObjectForId(program.getId())) // check if the parent program exists in Live workspace
094                        .map(program -> (Program) _resolver.resolveById(program.getId())) // resolve parent program in Live workspace
095                        .toList();
096
097                
098                for (Program program : parentPrograms)
099                {
100                    // Do a partial update of the parent program skills property
101                    _solrIndexer.updateProperty(program, Program.COMPUTED_MICRO_SKILLS, WebConstants.LIVE_WORKSPACE);
102                }
103            }
104            finally
105            {
106                // Restore the workspace.
107                RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
108            }
109        }
110    }
111}