001/*
002 *  Copyright 2010 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;
022import org.apache.commons.lang3.StringUtils;
023
024import org.ametys.cms.repository.ModifiableContent;
025import org.ametys.core.user.UserIdentity;
026import org.ametys.odf.course.Course;
027import org.ametys.odf.course.CourseFactory;
028import org.ametys.odf.course.ShareableCourseHelper;
029import org.ametys.odf.courselist.CourseList;
030import org.ametys.runtime.i18n.I18nizableText;
031
032import com.opensymphony.workflow.WorkflowException;
033
034/**
035 * OSWorkflow function for creating a {@link Course} content
036 */
037public class CreateCourseFunction extends AbstractCreateODFProgramItemFunction
038{
039    /** Constant for storing the parent course list id to use into the transient variables map. */
040    public static final String COURSE_LIST_ID_KEY = AbstractCreateODFContentFunction.class.getName() + "$courseListContentId";
041    
042    /** Content name prefix for programs */
043    public static final String CONTENT_NAME_PREFIX = "course-";
044
045    /** The shareable course helper */
046    protected ShareableCourseHelper _shareableCourseHelper;
047    
048    @Override
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        super.service(manager);
052        _shareableCourseHelper = (ShareableCourseHelper) manager.lookup(ShareableCourseHelper.ROLE);
053    }
054    
055    @Override
056    protected String _getContentNamePrefix()
057    {
058        return CONTENT_NAME_PREFIX;
059    }
060    
061    @Override
062    protected String _getObjectType(Map transientVars, Map args)
063    {
064        return CourseFactory.COURSE_NODETYPE;
065    }
066    
067    @Override
068    protected void _populateAdditionalData(Map transientVars, ModifiableContent content) throws WorkflowException
069    {
070        super._populateAdditionalData(transientVars, content);
071        
072        String courseListContentId = (String) transientVars.get(COURSE_LIST_ID_KEY);
073        CourseList courseListContent = StringUtils.isNotBlank(courseListContentId) ? _resolver.resolveById(courseListContentId) : null;
074        
075        UserIdentity user = getUser(transientVars);
076        _shareableCourseHelper.initializeShareableFields((Course) content, courseListContent, user, false);
077    }
078    
079    @Override
080    public I18nizableText getLabel()
081    {
082        return new I18nizableText("plugin.odf", "PLUGINS_ODF_CREATE_COURSE_FUNCTION_LABEL");
083    }
084}