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.classified.ads;
017
018import java.time.Period;
019import java.time.ZonedDateTime;
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.repository.Content;
027import org.ametys.cms.repository.ContentDAO;
028import org.ametys.cms.repository.ContentQueryHelper;
029import org.ametys.cms.repository.ContentTypeExpression;
030import org.ametys.cms.repository.DefaultContent;
031import org.ametys.core.trace.ForensicLogger;
032import org.ametys.core.user.population.UserPopulationDAO;
033import org.ametys.core.user.status.PersonalDataPolicy;
034import org.ametys.core.user.status.UserStatusInfo;
035import org.ametys.plugins.repository.AmetysObjectIterable;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.plugins.repository.query.expression.AndExpression;
038import org.ametys.plugins.repository.query.expression.Expression.Operator;
039import org.ametys.plugins.repository.query.expression.UserExpression;
040import org.ametys.runtime.config.Config;
041import org.ametys.runtime.plugin.component.AbstractLogEnabled;
042
043/**
044 * Data policy removing all the classified adds created by an unknown user after a delay
045 */
046public class ClassifiedAdsDataPolicy extends AbstractLogEnabled implements PersonalDataPolicy, Serviceable
047{
048    /** The content type id */
049    public static final String CLASSIFIED_ADS_CONTENT_TYPE = "org.ametys.plugins.classified.ads.Content.ads";
050
051    /** The content DAO */
052    protected ContentDAO _contentDAO;
053    /** The Ametys object resolver */
054    protected AmetysObjectResolver _resolver;
055    
056    private Period _retentionPeriod;
057    
058    public void service(ServiceManager manager) throws ServiceException
059    {
060        _contentDAO = (ContentDAO) manager.lookup(ContentDAO.ROLE);
061        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
062        
063        Long config = Config.getInstance().<Long>getValue("classified-ads.data-policy.retention.period", false, null);
064        _retentionPeriod = config != null && config >= 0 ? Period.ofMonths(config.intValue()) : null;
065    }
066    
067    public AnonymizationResult process(UserStatusInfo userStatusInfo)
068    {
069        if (_retentionPeriod == null)
070        {
071            return AnonymizationResult.TOO_EARLY;
072        }
073        else if (userStatusInfo.getMissingSinceDate().isBefore(ZonedDateTime.now().minus(_retentionPeriod)))
074        {
075            AndExpression filter = new AndExpression();
076            filter.add(new UserExpression(DefaultContent.METADATA_CREATOR, Operator.EQ, userStatusInfo.getUserIdentity()));
077            filter.add(new ContentTypeExpression(Operator.EQ, CLASSIFIED_ADS_CONTENT_TYPE));
078            String query = ContentQueryHelper.getContentXPathQuery(filter);
079            
080            try (AmetysObjectIterable<Content> contents = _resolver.query(query))
081            {
082                if (contents.getSize() > 0)
083                {
084                    _contentDAO.forceDeleteContentsObj(contents.stream().toList(), null);
085                    ForensicLogger.info("data.policy.gdpr.remove.classified-ads", Map.of("handled", Long.toString(contents.getSize()), "identity", userStatusInfo.getUserIdentity()), UserPopulationDAO.SYSTEM_USER_IDENTITY);
086                    return AnonymizationResult.PROCESSED;
087                }
088                else
089                {
090                    return AnonymizationResult.NO_DATA;
091                }
092            }
093            
094        }
095        else
096        {
097            return AnonymizationResult.TOO_EARLY;
098        }
099    }
100
101}