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