001/*
002 *  Copyright 2016 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.plugins.repository.activities;
017
018import org.apache.commons.lang.StringUtils;
019
020import org.ametys.plugins.repository.query.SortCriteria;
021import org.ametys.plugins.repository.query.expression.Expression;
022
023/**
024 * Helper for implementing {@link ActivityHolder} in JCR.
025 */
026public final class ActivityHelper
027{
028    private ActivityHelper()
029    {
030        // hide constructor
031    }
032    /**
033     * Creates the XPath query corresponding to specified {@link Expression}.
034     * The query will include to sort the result by activity date.
035     * If no expression is provided, all the activity nodes will be returned
036     * @param activityExpression the query predicates. can be null.
037     * @return the created XPath query.
038     */
039    public static String getActivityXPathQuery(Expression activityExpression)
040    {
041        String predicats = null;
042        
043        if (activityExpression != null)
044        {
045            predicats = StringUtils.trimToNull(activityExpression.build());
046        }
047        
048        StringBuilder sb = new StringBuilder();
049        sb.append(" //element(*, ametys:activity)");
050        if (predicats != null)
051        {
052            sb.append("[").append(predicats).append("]");
053        }
054        SortCriteria sort = new SortCriteria();
055        sort.addCriterion(ActivityFactory.DATE, false, false);
056        
057        sb.append(" ").append(sort.build());
058        return sb.toString();
059    }
060}