001/*
002 *  Copyright 2024 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.pagesubscription.observation;
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.avalon.framework.service.Serviceable;
027import org.apache.cocoon.components.ContextHelper;
028import org.apache.cocoon.environment.Request;
029
030import org.ametys.core.observation.Event;
031import org.ametys.core.observation.Observer;
032import org.ametys.plugins.pagesubscription.Subscription;
033import org.ametys.plugins.pagesubscription.context.PageSubscriptionContext;
034import org.ametys.plugins.pagesubscription.type.PageSubscriptionType;
035import org.ametys.plugins.pagesubscription.type.SubscriptionTypeExtensionPoint;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.plugins.repository.RepositoryConstants;
038import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
039import org.ametys.runtime.plugin.component.AbstractLogEnabled;
040import org.ametys.web.ObservationConstants;
041import org.ametys.web.repository.page.Page;
042
043/**
044 * Observer to unsubscribe to a page when the page is deleted
045 */
046public class RemoveSubscriptionOnPageDeletedObserver extends AbstractLogEnabled implements Observer, Serviceable, Contextualizable
047{
048    /** The ametys object resolver */
049    protected AmetysObjectResolver _resolver;
050    
051    /** The subscription type extension point */
052    protected SubscriptionTypeExtensionPoint _subscriptionTypeEP;
053    
054    /** The context */
055    protected Context _context;
056    
057    @Override
058    public void service(ServiceManager manager) throws ServiceException
059    {
060        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
061        _subscriptionTypeEP = (SubscriptionTypeExtensionPoint) manager.lookup(SubscriptionTypeExtensionPoint.ROLE);
062    }
063    
064    public void contextualize(Context context) throws ContextException
065    {
066        _context = context;
067    }
068    
069    @Override
070    public boolean supports(Event event)
071    {
072        return event.getId().equals(ObservationConstants.EVENT_PAGE_DELETING);
073    }
074
075    public int getPriority()
076    {
077        return MAX_PRIORITY;
078    }
079
080    @Override
081    public void observe(Event event, Map<String, Object> transientVars) throws Exception
082    {
083        Request request = ContextHelper.getRequest(_context);
084        // Retrieve the current workspace.
085        String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
086        
087        try
088        {
089            // Force default the workspace.
090            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE);
091            String pageId = (String) event.getArguments().get(ObservationConstants.ARGS_PAGE_ID);
092            
093            PageSubscriptionType pageSubscriptionType = (PageSubscriptionType) _subscriptionTypeEP.getExtension(PageSubscriptionType.ID);
094            Page page = _resolver.resolveById(pageId);
095            
096            PageSubscriptionContext context = PageSubscriptionContext.newInstance().withPage(page);
097            List<Subscription> subscriptions = pageSubscriptionType.getSubscriptions(page.getSite(), null, null, context);
098            for (Subscription subscription : subscriptions)
099            {
100                // unsubscribe
101                pageSubscriptionType.unsubscribe(subscription);
102            }
103        }
104        finally
105        {
106            // Restore current workspace
107            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
108        }
109    }
110}