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.HashMap; 019import java.util.Map; 020 021import javax.jcr.RepositoryException; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.ObservationConstants; 027import org.ametys.cms.repository.ContentQueryHelper; 028import org.ametys.cms.repository.ContentTypeExpression; 029import org.ametys.cms.repository.WorkflowAwareContent; 030import org.ametys.cms.workflow.AbstractContentFunction; 031import org.ametys.core.observation.Event; 032import org.ametys.odf.course.Course; 033import org.ametys.odf.coursepart.CoursePart; 034import org.ametys.odf.coursepart.CoursePartFactory; 035import org.ametys.plugins.repository.AmetysObjectIterable; 036import org.ametys.plugins.repository.AmetysObjectResolver; 037import org.ametys.plugins.repository.query.expression.AndExpression; 038import org.ametys.plugins.repository.query.expression.Expression; 039import org.ametys.plugins.repository.query.expression.Expression.Operator; 040import org.ametys.plugins.repository.query.expression.StringExpression; 041 042import com.opensymphony.module.propertyset.PropertySet; 043import com.opensymphony.workflow.WorkflowException; 044 045/** 046 * Move Live tag on CoursePart when the course is validated. 047 */ 048public class MoveLiveTagOnCoursePartFunction extends AbstractContentFunction 049{ 050 /** Ametys object resolver */ 051 protected AmetysObjectResolver _resolver; 052 053 @Override 054 public void service(ServiceManager smanager) throws ServiceException 055 { 056 super.service(smanager); 057 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.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 AmetysObjectIterable<CoursePart> courseParts = _resolver.query(xpathQuery); 076 for (CoursePart coursePart : courseParts) 077 { 078 // Create a new version. 079 _createVersion(coursePart); 080 081 // Add the valid label on the newly created version. 082 try 083 { 084 _addLabel(coursePart, ValidateODFContentFunction.VALID_LABEL); 085 086 // Notify observers 087 Map<String, Object> eventParams = new HashMap<>(); 088 eventParams.put(ObservationConstants.ARGS_CONTENT, coursePart); 089 eventParams.put(ObservationConstants.ARGS_CONTENT_ID, coursePart.getId()); 090 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_VALIDATED, getUser(transientVars), eventParams)); 091 } 092 catch (RepositoryException e) 093 { 094 throw new WorkflowException("Unable to create the Live label on the content " + coursePart.getTitle() + " (" + coursePart.getId() + ")", e); 095 } 096 } 097 } 098 } 099}