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    @Override
076    public int getPriority(Event event)
077    {
078        return MAX_PRIORITY;
079    }
080
081    @Override
082    public void observe(Event event, Map<String, Object> transientVars) throws Exception
083    {
084        Request request = ContextHelper.getRequest(_context);
085        // Retrieve the current workspace.
086        String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
087        
088        try
089        {
090            // Force default the workspace.
091            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE);
092            String pageId = (String) event.getArguments().get(ObservationConstants.ARGS_PAGE_ID);
093            
094            PageSubscriptionType pageSubscriptionType = (PageSubscriptionType) _subscriptionTypeEP.getExtension(PageSubscriptionType.ID);
095            Page page = _resolver.resolveById(pageId);
096            
097            PageSubscriptionContext context = PageSubscriptionContext.newInstance().withPage(page);
098            List<Subscription> subscriptions = pageSubscriptionType.getSubscriptions(page.getSite(), null, null, context);
099            for (Subscription subscription : subscriptions)
100            {
101                // unsubscribe
102                pageSubscriptionType.unsubscribe(subscription);
103            }
104        }
105        finally 
106        {
107            // Restore current workspace
108            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp);
109        }
110    }
111}