001/*
002 *  Copyright 2010 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 */
016
017package org.ametys.cms.repository.comment;
018
019import org.ametys.cms.repository.ModifiableContentHelper;
020import org.ametys.plugins.repository.query.expression.Expression;
021
022/**
023 * Expression to search for comments
024 */
025public class CommentExpression implements Expression
026{
027    private boolean _includeValidated;
028    private boolean _includeNonValidated;
029
030    /**
031     * Constructor. Parameters can not be both false.
032     * @param includeValidated will request contents with validated comments 
033     * @param includeNonValidated will request contents with non validated comments 
034     */
035    public CommentExpression(boolean includeValidated, boolean includeNonValidated)
036    {
037        this._includeValidated = includeValidated;
038        this._includeNonValidated = includeNonValidated;
039        
040        if (!includeValidated && !includeNonValidated)
041        {
042            throw new IllegalArgumentException("Can not create a comment expression to search for no comments");
043        }
044    }
045
046    public String build()
047    {
048        if (_includeValidated && _includeNonValidated)
049        {
050            return "ametys-internal:unversioned/ametys:" + ModifiableContentHelper.METADATA_COMMENTS + "/*/@jcr:primaryType = 'ametys:compositeMetadata'";
051        }
052        else if (_includeValidated)
053        {
054            return "ametys-internal:unversioned/ametys:" + ModifiableContentHelper.METADATA_COMMENTS + "/*/@ametys:validated = true()";
055        }
056        else if (_includeNonValidated)
057        {
058            return "ametys-internal:unversioned/ametys:" + ModifiableContentHelper.METADATA_COMMENTS + "/*/@ametys:validated = false()";
059        }
060        else
061        {
062            return "not(ametys-internal:unversioned/ametys:" + ModifiableContentHelper.METADATA_COMMENTS + "/*/@jcr:primaryType = 'ametys:compositeMetadata')";
063        }
064    }
065}