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.CmsConstants; 027import org.ametys.cms.ObservationConstants; 028import org.ametys.cms.repository.ContentQueryHelper; 029import org.ametys.cms.repository.ContentTypeExpression; 030import org.ametys.cms.repository.WorkflowAwareContent; 031import org.ametys.cms.workflow.AbstractContentFunction; 032import org.ametys.core.observation.Event; 033import org.ametys.odf.course.Course; 034import org.ametys.odf.coursepart.CoursePart; 035import org.ametys.odf.coursepart.CoursePartFactory; 036import org.ametys.plugins.repository.AmetysObjectIterable; 037import org.ametys.plugins.repository.AmetysObjectResolver; 038import org.ametys.plugins.repository.query.expression.AndExpression; 039import org.ametys.plugins.repository.query.expression.Expression; 040import org.ametys.plugins.repository.query.expression.Expression.Operator; 041import org.ametys.plugins.repository.query.expression.StringExpression; 042 043import com.opensymphony.module.propertyset.PropertySet; 044import com.opensymphony.workflow.WorkflowException; 045 046/** 047 * Move Live tag on CoursePart when the course is validated. 048 */ 049public class MoveLiveTagOnCoursePartFunction extends AbstractContentFunction 050{ 051 /** Ametys object resolver */ 052 protected AmetysObjectResolver _resolver; 053 054 @Override 055 public void service(ServiceManager smanager) throws ServiceException 056 { 057 super.service(smanager); 058 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 059 } 060 061 @Override 062 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 063 { 064 // Retrieve current content 065 WorkflowAwareContent content = getContent(transientVars); 066 067 if (content instanceof Course) 068 { 069 // Build the expression to retrieve course parts which the course is holding 070 Expression expression = new AndExpression( 071 new ContentTypeExpression(Operator.EQ, CoursePartFactory.COURSE_PART_CONTENT_TYPE), 072 new StringExpression(CoursePart.COURSE_HOLDER, Operator.EQ, content.getId()) 073 ); 074 String xpathQuery = ContentQueryHelper.getContentXPathQuery(expression); 075 076 AmetysObjectIterable<CoursePart> courseParts = _resolver.query(xpathQuery); 077 for (CoursePart coursePart : courseParts) 078 { 079 // Create a new version. 080 _createVersion(coursePart); 081 082 // Add the valid label on the newly created version. 083 try 084 { 085 _addLabel(coursePart, CmsConstants.LIVE_LABEL); 086 087 // Notify observers 088 Map<String, Object> eventParams = new HashMap<>(); 089 eventParams.put(ObservationConstants.ARGS_CONTENT, coursePart); 090 eventParams.put(ObservationConstants.ARGS_CONTENT_ID, coursePart.getId()); 091 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_VALIDATED, getUser(transientVars), eventParams)); 092 } 093 catch (RepositoryException e) 094 { 095 throw new WorkflowException("Unable to create the Live label on the content " + coursePart.getTitle() + " (" + coursePart.getId() + ")", e); 096 } 097 } 098 } 099 } 100}