001/*
002 *  Copyright 2014 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.odf.workflow;
017
018import java.util.Set;
019
020import org.ametys.cms.repository.Content;
021import org.ametys.cms.repository.DefaultContent;
022import org.ametys.cms.repository.WorkflowStepExpression;
023import org.ametys.cms.workflow.WorkflowTasksComponent;
024import org.ametys.core.user.User;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.query.SortCriteria;
027import org.ametys.plugins.repository.query.expression.AndExpression;
028import org.ametys.plugins.repository.query.expression.Expression;
029import org.ametys.plugins.repository.query.expression.Expression.Operator;
030import org.ametys.plugins.repository.query.expression.OrExpression;
031import org.ametys.plugins.repository.query.expression.StringExpression;
032
033/**
034 * Override WorkflowTasksComponent to target ODF contents
035 */
036public abstract class AbstractOdfWorkflowTasksComponent extends WorkflowTasksComponent
037{
038    @Override
039    protected AmetysObjectIterable<Content> _getContents(TaskStep step, User user)
040    {
041        Expression expr = null;
042        Set<Integer> stepIds = step.getStepIds();
043        for (Integer stepId : stepIds)
044        {
045            Expression stepExpr = new WorkflowStepExpression(Operator.EQ, stepId);
046            expr = expr == null ? stepExpr : new OrExpression(expr, stepExpr);
047        }
048        
049        Expression contentExpr = null;
050        if (step.getUserContentsOnly())
051        {
052            contentExpr = new StringExpression(DefaultContent.METADATA_CONTRIBUTOR, Operator.EQ, user.getName());
053            expr = new AndExpression(expr, contentExpr);
054        }
055
056        Expression contentTypeExpr = getContentTypeExpression();
057        expr = new AndExpression(expr, contentTypeExpr);
058        
059        SortCriteria sort = new SortCriteria();
060        sort.addCriterion(DefaultContent.METADATA_MODIFIED, false, false);
061        String xpathQuery = org.ametys.plugins.repository.query.QueryHelper.getXPathQuery(null, "ametys:content", expr, sort);
062
063        return _objectResolver.query(xpathQuery);
064    }
065    
066    /**
067     * Get the content type expression concerned by this component
068     * @return the content type expression
069     */
070    protected abstract Expression getContentTypeExpression ();
071
072}