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