001/*
002 *  Copyright 2022 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.plugins.workspaces.project.notification;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import javax.jcr.RepositoryException;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.commons.collections.ListUtils;
027
028import org.ametys.cms.CmsConstants;
029import org.ametys.cms.contenttype.ContentTypesHelper;
030import org.ametys.cms.repository.WorkflowAwareContent;
031import org.ametys.cms.workflow.AbstractContentFunction;
032import org.ametys.core.observation.Event;
033import org.ametys.plugins.workspaces.ObservationConstants;
034import org.ametys.plugins.workspaces.WorkspacesConstants;
035import org.ametys.runtime.i18n.I18nizableText;
036
037import com.opensymphony.module.propertyset.PropertySet;
038import com.opensymphony.workflow.WorkflowException;
039
040/**
041 * Function to mark that a news is notified  
042 *
043 */
044public class SetNotifiedFunction extends AbstractContentFunction
045{
046    /** Property to mark as notified */
047    public static final String NOTIFIED_PROPERTY_NAME = "notified";
048    
049    /** The user manager */
050    protected ContentTypesHelper _cTypeHelper;
051
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _cTypeHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
057    }
058    
059    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
060    {
061        WorkflowAwareContent content = getContent(transientVars);
062        
063        if (_cTypeHelper.isInstanceOf(content, WorkspacesConstants.PROJECT_NEWS_CONTENT_TYPE_ID))
064        {
065            try
066            {
067                _markAsNotified(content, transientVars);
068                
069                _createVersion(content);
070                
071                _addLabel(content, CmsConstants.LIVE_LABEL);
072                
073                _notifyObservers(content, transientVars);
074            }
075            catch (RepositoryException e)
076            {
077                throw new WorkflowException("Unable to set notified markup", e);
078            }
079        }
080    }
081    
082    /**
083     * Mark the content as notified
084     * @param content the content
085     * @param transientVars the transient variables
086     */
087    protected void _markAsNotified(WorkflowAwareContent content, Map transientVars)
088    {
089        if (content != null)
090        {
091            content.getInternalDataHolder().setValue(NOTIFIED_PROPERTY_NAME, true);
092            content.saveChanges();
093        }
094    }
095    
096    /**
097     * Notify observers of news publication
098     * @param content the content 
099     * @param transientVars the transient variables
100     * @throws WorkflowException if an error occurred
101     */
102    protected void _notifyObservers(WorkflowAwareContent content, Map transientVars) throws WorkflowException
103    {
104        if (content != null)
105        {
106            Map<String, Object> eventParams = new HashMap<>();
107            eventParams.put(org.ametys.cms.ObservationConstants.ARGS_CONTENT, content);
108            eventParams.put(org.ametys.cms.ObservationConstants.ARGS_CONTENT_ID, content.getId());
109            _observationManager.notify(new Event(ObservationConstants.EVENT_PROJECT_NEWS_PUBLISHED, getUser(transientVars), eventParams));
110            
111            _observationManager.notify(new Event(org.ametys.cms.ObservationConstants.EVENT_CONTENT_VALIDATED, getUser(transientVars), eventParams));
112        }
113    }
114
115    @Override
116    public List<FunctionArgument> getArguments()
117    {
118        return ListUtils.EMPTY_LIST;
119    }
120
121    @Override
122    public I18nizableText getDescription(Map<String, String> args)
123    {
124        return new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_SET_NOTIFIED_FUNCTION_DESCRIPTION");
125    }
126    
127}