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