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.userdirectory.cachepolicy;
017
018import java.util.Arrays;
019import java.util.Collections;
020import java.util.List;
021import java.util.Set;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.cms.ObservationConstants;
028import org.ametys.cms.contenttype.ContentTypesHelper;
029import org.ametys.cms.repository.Content;
030import org.ametys.core.observation.Event;
031import org.ametys.plugins.userdirectory.UserDirectoryHelper;
032import org.ametys.web.cache.pageelement.AbstractSimplePageElementCachePolicy;
033import org.ametys.web.cache.pageelement.PageElementCachePolicy;
034
035/**
036 * {@link PageElementCachePolicy} for the insert user service.
037 */
038public class InsertUserServiceCachePolicy extends AbstractSimplePageElementCachePolicy implements Serviceable
039{
040
041    /** The insert user service ID. */
042    public static final String SERVICE_INSERT_USER_ID = "org.ametys.plugins.userdirectory.service.InsertUser";
043    
044    private ContentTypesHelper _contentTypesHelper;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
050    }
051    
052    @Override
053    public Set<String> getPageElementTypes()
054    {
055        return Collections.singleton("SERVICE:" + SERVICE_INSERT_USER_ID);
056    }
057    
058    @Override
059    protected boolean _supports (Event event, String workspace)
060    {
061        boolean support = super._supports(event, workspace);
062        
063        if (support)
064        {
065            Object object = event.getArguments().get(ObservationConstants.ARGS_CONTENT);
066            if (object != null && object instanceof Content content)
067            {
068                return _contentTypesHelper.isInstanceOf(content, UserDirectoryHelper.ABSTRACT_USER_CONTENT_TYPE);
069            }
070        }
071        
072        return support;
073    }
074    
075    @Override
076    protected List<String> _getRemovingCacheEventIds(String workspace)
077    {
078        if ("default".equals(workspace))
079        {
080            return Arrays.asList(ObservationConstants.EVENT_CONTENT_MODIFIED, ObservationConstants.EVENT_CONTENT_DELETING);
081        }
082        else if ("live".equals(workspace))
083        {
084            return Arrays.asList(ObservationConstants.EVENT_CONTENT_VALIDATED,
085                                 ObservationConstants.EVENT_CONTENT_DELETING,
086                                 ObservationConstants.EVENT_CONTENT_UNTAG_LIVE);
087        }
088        
089        return Collections.emptyList();
090    }
091}