001/*
002 *  Copyright 2023 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.web.content.consistency;
017
018import java.time.ZonedDateTime;
019
020import org.ametys.cms.content.consistency.CheckContentConsistencySchedulable;
021import org.ametys.cms.content.consistency.ContentConsistencyModel;
022import org.ametys.cms.content.consistency.ContentConsistencyResult;
023import org.ametys.cms.repository.Content;
024import org.ametys.cms.repository.WorkflowAwareContent;
025import org.ametys.core.schedule.progression.ContainerProgressionTracker;
026import org.ametys.plugins.repository.AmetysObjectIterable;
027import org.ametys.plugins.repository.query.expression.Expression.Operator;
028import org.ametys.plugins.repository.query.expression.ExpressionContext;
029import org.ametys.plugins.repository.query.expression.MetadataExpression;
030import org.ametys.plugins.repository.query.expression.NotExpression;
031import org.ametys.plugins.repository.query.expression.StringExpression;
032import org.ametys.web.repository.content.WebContent;
033
034/**
035 * ContentConsistencyManager with support for site information
036 */
037public class ContentConsistencyManager extends org.ametys.cms.content.consistency.ContentConsistencyManager
038{
039    @Override
040    protected ContentConsistencyResult storeResult(WorkflowAwareContent content, int successCount, int unknownCount, int unauthorizedCount, int notFoundCount, int serverErrorCount)
041    {
042        ContentConsistencyResult result = super.storeResult(content, successCount, unknownCount, unauthorizedCount, notFoundCount, serverErrorCount);
043        if (result != null && content instanceof WebContent wContent)
044        {
045            result.setValue(ContentConsistencyModel.CONTEXT, wContent.getSiteName());
046            result.saveChanges();
047        }
048        return result;
049    }
050    
051    /**
052     * Check every contents without an associated site for broken links
053     * @param progressionTracker  progression tracker for content checking
054     * @return record describing the results of the checks
055     */
056    public ConsistenciesReport checkContentsWithoutSite(ContainerProgressionTracker progressionTracker)
057    {
058        ZonedDateTime startDate = ZonedDateTime.now();
059        try (AmetysObjectIterable<Content> contentsWithoutSite = _getContents(new NotExpression(new MetadataExpression("site", ExpressionContext.newInstance().withInternal(true)))))
060        {
061            ConsistenciesReport checkReport = _checkContents(contentsWithoutSite, progressionTracker.getStep(CheckContentConsistencySchedulable.CHECK_CONTENTS));
062            removeOutdatedResult(startDate, new NotExpression(new MetadataExpression(ContentConsistencyModel.CONTEXT)), progressionTracker.getStep(CheckContentConsistencySchedulable.REMOVE_OUTDATED_RESULT));
063            return checkReport;
064        }
065    }
066    
067    /**
068     * Check every content from the given site for broken links.
069     * 
070     * Note that content with no associated site won't be check by this methods.
071     * See for {@link #checkContentsWithoutSite(ContainerProgressionTracker)} for this.
072     * @param siteName the site of the contents
073     * @param progressionTracker progression tracker for content checking
074     * @return record describing the results of the checks
075     */
076    public ConsistenciesReport checkContentsFromSite(String siteName, ContainerProgressionTracker progressionTracker)
077    {
078        ZonedDateTime startDate = ZonedDateTime.now();
079        try (AmetysObjectIterable<Content> contentsFromSite = _getContents(new StringExpression("site", Operator.EQ, siteName, ExpressionContext.newInstance().withInternal(true))))
080        {
081            ConsistenciesReport checkReport = _checkContents(contentsFromSite, progressionTracker.getStep(CheckContentConsistencySchedulable.CHECK_CONTENTS));
082            removeOutdatedResult(startDate, new StringExpression(ContentConsistencyModel.CONTEXT, Operator.EQ, siteName), progressionTracker.getStep(CheckContentConsistencySchedulable.REMOVE_OUTDATED_RESULT));
083            return checkReport;
084        }
085    }
086}