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.plugins.pagesubscription.tracking;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Map.Entry;
024import java.util.Objects;
025import java.util.Set;
026
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.avalon.framework.service.Serviceable;
030
031import org.ametys.core.group.Group;
032import org.ametys.core.group.GroupManager;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.plugins.pagesubscription.Subscription;
035import org.ametys.plugins.pagesubscription.type.PageSubscriptionType;
036import org.ametys.plugins.pagesubscription.type.SubscriptionTypeExtensionPoint;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.plugins.webanalytics.matomo.tracking.AbstractMatomoEventTrackingProvider;
039import org.ametys.web.repository.page.Page;
040import org.ametys.web.repository.site.Site;
041
042/**
043 * The matomo tracking provider for the page subscription
044 */
045public class PageSubscriptionMatomoTrackingProvider extends AbstractMatomoEventTrackingProvider implements Serviceable
046{
047    /** The subscription type extension point */
048    protected SubscriptionTypeExtensionPoint _subscriptionTypeEP;
049    
050    /** The group manager */
051    protected GroupManager _groupManager;
052    
053    /** The ametys object resolver */
054    protected AmetysObjectResolver _resolver;
055    
056    public void service(ServiceManager manager) throws ServiceException
057    {
058        _subscriptionTypeEP = (SubscriptionTypeExtensionPoint) manager.lookup(SubscriptionTypeExtensionPoint.ROLE);
059        _groupManager = (GroupManager) manager.lookup(GroupManager.ROLE);
060        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
061    }
062    
063    @Override
064    protected List<MatomoEvent> getEvents(Site site) 
065    {
066        List<MatomoEvent> requests = new ArrayList<>();
067        for (Entry<String, Set<UserIdentity>> entry: _getSubscribers(site).entrySet())
068        {
069            String pageId = entry.getKey();
070            Page page = _silentlyResolve(pageId);
071            if (page != null)
072            {
073                requests.add(new MatomoEvent(
074                        page.getSitemapName() + "/" + page.getPathInSitemap(),
075                        entry.getValue().size()
076                        ));
077            }
078        }
079        
080        return requests;
081    }
082    
083    private Page _silentlyResolve(String pageId)
084    {
085        try
086        {
087            return _resolver.resolveById(pageId);
088        }
089        catch (Exception e) 
090        {
091            getLogger().warn("Cannot track subscription for the page with id '{}'. It does not seem to exist.", pageId, e);
092            return null;
093        }
094    }
095    
096    private Map<String, Set<UserIdentity>> _getSubscribers(Site site)
097    {
098        PageSubscriptionType subscriptionType = (PageSubscriptionType) _subscriptionTypeEP.getExtension(PageSubscriptionType.ID);
099
100        Map<String, Set<UserIdentity>> subscribers = new HashMap<>();
101        for (Subscription subscription : subscriptionType.getSubscriptions(site, null, null, null))
102        {
103            String pageId = subscriptionType.getTarget(subscription);
104            
105            Set<UserIdentity> users = subscribers.computeIfAbsent(pageId, __ -> new HashSet<>());
106            subscription.getSubscriber().ifPresent(users::add);
107            
108            subscription.getSubscribersGroup()
109                .map(_groupManager::getGroup)
110                .filter(Objects::nonNull)
111                .map(Group::getUsers)
112                .ifPresent(users::addAll);
113        }
114        
115        return subscribers;
116    }
117    
118}