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;
031
032import com.opensymphony.module.propertyset.PropertySet;
033import com.opensymphony.workflow.FunctionProvider;
034import com.opensymphony.workflow.WorkflowException;
035
036/**
037 * OS Workflow function to notify that the copy has ended.
038 */
039public class NotifyCopyFunction extends AbstractContentWorkflowComponent implements FunctionProvider
040{
041    /** The observation manager */
042    protected ObservationManager _observationManager;
043    
044    /** The user provider */
045    protected CurrentUserProvider _userProvider;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
052        _userProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
053    }
054    
055    @Override
056    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
057    {
058        UserIdentity currentUserIdentity = _userProvider.getUser();
059        WorkflowAwareContent content = getContent(transientVars);
060
061        Map<String, Object> eventParams = new HashMap<>();
062        eventParams.put(ObservationConstants.ARGS_CONTENT, content);
063        eventParams.put(ObservationConstants.ARGS_CONTENT_ID, content.getId());
064        _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_MODIFIED, currentUserIdentity, eventParams));
065    }
066
067}