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