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.copy;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.ObservationConstants;
025import org.ametys.cms.repository.WorkflowAwareContent;
026import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.ObservationManager;
029import org.ametys.core.user.CurrentUserProvider;
030import org.ametys.core.user.UserIdentity;
031import org.ametys.plugins.workflow.EnhancedFunction;
032import org.ametys.runtime.i18n.I18nizableText;
033
034import com.opensymphony.module.propertyset.PropertySet;
035import com.opensymphony.workflow.WorkflowException;
036
037/**
038 * OS Workflow function to notify that the copy has ended.
039 */
040public class NotifyCopyFunction extends AbstractContentWorkflowComponent implements EnhancedFunction
041{
042    /** The observation manager */
043    protected ObservationManager _observationManager;
044    
045    /** The user provider */
046    protected CurrentUserProvider _userProvider;
047    
048    @Override
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        super.service(manager);
052        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
053        _userProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
054    }
055    
056    @Override
057    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
058    {
059        UserIdentity currentUserIdentity = _userProvider.getUser();
060        WorkflowAwareContent content = getContent(transientVars);
061
062        Map<String, Object> eventParams = new HashMap<>();
063        eventParams.put(ObservationConstants.ARGS_CONTENT, content);
064        eventParams.put(ObservationConstants.ARGS_CONTENT_ID, content.getId());
065        _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_MODIFIED, currentUserIdentity, eventParams));
066    }
067    
068    @Override
069    public FunctionType getFunctionExecType()
070    {
071        return FunctionType.POST;
072    }
073    
074    @Override
075    public I18nizableText getLabel()
076    {
077        return new I18nizableText("plugin.odf", "PLUGINS_ODF_NOTIFY_COPY_FUNCTION_LABEL");
078    }
079}