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