001/*
002 *  Copyright 2018 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.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.repository.ContentQueryHelper;
024import org.ametys.cms.repository.ContentTypeExpression;
025import org.ametys.cms.repository.WorkflowAwareContent;
026import org.ametys.cms.workflow.AbstractContentFunction;
027import org.ametys.cms.workflow.ContentWorkflowHelper;
028import org.ametys.odf.course.Course;
029import org.ametys.odf.coursepart.CoursePart;
030import org.ametys.odf.coursepart.CoursePartFactory;
031import org.ametys.plugins.repository.AmetysObjectIterable;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.plugins.repository.query.expression.AndExpression;
034import org.ametys.plugins.repository.query.expression.Expression;
035import org.ametys.plugins.repository.query.expression.Expression.Operator;
036import org.ametys.plugins.repository.query.expression.StringExpression;
037
038import com.opensymphony.module.propertyset.PropertySet;
039import com.opensymphony.workflow.WorkflowException;
040
041/**
042 * Do action on CoursePart of given course.
043 */
044public abstract class AbstractCoursePartFunction extends AbstractContentFunction
045{
046    /** Ametys object resolver */
047    protected AmetysObjectResolver _resolver;
048    
049    /** Content workflow helper */
050    protected ContentWorkflowHelper _contentWorkflowHelper;
051
052    @Override
053    public void service(ServiceManager smanager) throws ServiceException
054    {
055        super.service(smanager);
056        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
057        _contentWorkflowHelper = (ContentWorkflowHelper) smanager.lookup(ContentWorkflowHelper.ROLE);
058    }
059    
060    @Override
061    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
062    {
063        // Retrieve current content
064        WorkflowAwareContent content = getContent(transientVars);
065        
066        if (content instanceof Course)
067        {
068            // Build the expression to retrieve course parts which the course is holding
069            Expression expression = new AndExpression(
070                    new ContentTypeExpression(Operator.EQ, CoursePartFactory.COURSE_PART_CONTENT_TYPE),
071                    new StringExpression(CoursePart.COURSE_HOLDER, Operator.EQ, content.getId())
072            );
073            String xpathQuery = ContentQueryHelper.getContentXPathQuery(expression);
074            
075            int actionId = _getCoursePartActionId();
076            AmetysObjectIterable<CoursePart> courseParts = _resolver.query(xpathQuery);
077            for (CoursePart coursePart : courseParts)
078            {
079                if (_contentWorkflowHelper.isAvailableAction(coursePart, actionId))
080                {
081                    _contentWorkflowHelper.doAction(coursePart, actionId);
082                }
083            }
084        }
085    }
086    
087    @Override
088    public FunctionType getFunctionExecType()
089    {
090        return FunctionType.POST;
091    }
092    
093    /**
094     * Get the id of the action to do for course part
095     * @return the id of the action to do for course part
096     */
097    protected abstract Integer _getCoursePartActionId();
098}