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 */
016package org.ametys.cms.repository;
017
018import org.ametys.plugins.repository.AmetysObjectResolver;
019import org.ametys.plugins.repository.query.QueryHelper;
020import org.ametys.plugins.repository.query.SortCriteria;
021import org.ametys.plugins.repository.query.expression.Expression;
022
023/**
024 * Helper for creating JCR XPath queries involving content predicates.<br>
025 * Created XPath queries are like : <code>//element(*, ametys:content)[<i>&lt;content predicate&gt;</i>]</code>
026 */
027public final class ContentQueryHelper
028{
029    private ContentQueryHelper()
030    {
031        // empty constructor
032    }
033    
034    /**
035     * Creates the XPath query corresponding to specified {@link Expression}.
036     * @param contentExpression the query predicates
037     * @return the created XPath query
038     */
039    public static String getContentXPathQuery(Expression contentExpression)
040    {
041        return getContentXPathQuery(contentExpression, null);
042    }
043    
044    /**
045     * Creates the XPath query corresponding to specified {@link Expression}.
046     * @param contentExpression the query predicates
047     * @param sortCriteria criteria for sorting results
048     * @return the created XPath query
049     */
050    public static String getContentXPathQuery(Expression contentExpression, SortCriteria sortCriteria)
051    {
052        return QueryHelper.getXPathQuery(null, "ametys:content", contentExpression, sortCriteria);
053    }
054    
055    /**
056     * Creates the XPath query joining criteria on workflow and criteria on content's metadata.
057     * @param workflowExpression the workflow query predicates
058     * @param contentExpression the content query predicates
059     * @return the created XPath query
060     */
061    public static String getWorkflowXPathQuery(Expression workflowExpression, Expression contentExpression)
062    {
063        if (workflowExpression == null)
064        {
065            return getContentXPathQuery(contentExpression);
066        }
067        
068        String contentPredicate = contentExpression == null ? "" : "[" + contentExpression.build() + "]"; 
069        
070        return "/jcr:root/" + AmetysObjectResolver.ROOT_REPO + "//element(*, oswf:entry)[" + workflowExpression.build() + "]/../.." + contentPredicate;
071    }
072}