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