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