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.util.List;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025
026import org.ametys.cms.content.consistency.ContentConsistencyResult;
027import org.ametys.plugins.repository.query.expression.Expression;
028import org.ametys.plugins.repository.query.expression.Expression.Operator;
029import org.ametys.plugins.repository.query.expression.MetadataExpression;
030import org.ametys.plugins.repository.query.expression.NotExpression;
031import org.ametys.plugins.repository.query.expression.OrExpression;
032import org.ametys.plugins.repository.query.expression.StringExpression;
033import org.ametys.web.WebHelper;
034
035/**
036 * Override to restrict the search to the current site or outside
037 */
038public class ContentConsistencySearcher extends org.ametys.cms.content.consistency.ContentConsistencySearcher implements Contextualizable
039{
040    private Context _context;
041
042    public void contextualize(Context context) throws ContextException
043    {
044        _context = context;
045    }
046    
047    @Override
048    protected Expression getExpression(List<Expression> criteriaExpressions)
049    {
050        Request request = ContextHelper.getRequest(_context);
051        String siteName = WebHelper.getSiteName(request);
052        if (siteName != null)
053        {
054            criteriaExpressions.add(
055                    new OrExpression(
056                            new StringExpression(ContentConsistencyResult.CONTEXT, Operator.EQ, siteName),
057                            new NotExpression(new MetadataExpression(ContentConsistencyResult.CONTEXT))
058                    )
059            );
060        }
061        return super.getExpression(criteriaExpressions);
062    }
063}