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.web.trash;
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.CmsConstants;
027import org.ametys.cms.ObservationConstants;
028import org.ametys.cms.repository.Content;
029import org.ametys.core.observation.Event;
030import org.ametys.core.observation.ObservationManager;
031import org.ametys.core.observation.Observer;
032import org.ametys.plugins.repository.AmetysObject;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.version.VersionableAmetysObject;
035
036/**
037 * Observer to publish restored content in live if they have a live tag version
038 */
039// Not Async because we notify a new event and we want synchronized observer of this new event to be synchronized
040public class SynchronizeRestoredContentObserver implements Observer, Serviceable
041{
042    /** The observation manager */
043    protected ObservationManager _observationManager;
044    /** The Ametys object resolver */
045    protected AmetysObjectResolver _resolver;
046
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
050        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
051    }
052
053    public int getPriority()
054    {
055        // After org.ametys.cms.trash.observer.ContentAddedObserver
056        return 501;
057    }
058
059    public boolean supports(Event event)
060    {
061        return event.getId().equals(ObservationConstants.EVENT_TRASH_RESTORED);
062    }
063
064    public void observe(Event event, Map<String, Object> transientVars) throws Exception
065    {
066        String ametysObjectId = (String) event.getArguments().get(ObservationConstants.ARGS_AMETYS_OBJECT_ID);
067        AmetysObject ametysObject = _resolver.resolveById(ametysObjectId);
068        
069        if (ametysObject instanceof Content
070            && ametysObject instanceof VersionableAmetysObject content
071            && Arrays.asList(content.getAllLabels()).contains(CmsConstants.LIVE_LABEL))
072        {
073            Map<String, Object> eventParams = new HashMap<>();
074            eventParams.put(ObservationConstants.ARGS_CONTENT, content);
075            eventParams.put(ObservationConstants.ARGS_CONTENT_ID, content.getId());
076            
077            _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_VALIDATED, event.getIssuer(), eventParams));
078            
079        }
080    }
081
082}