001/* 002 * Copyright 2011 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.cms.workflow; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.commons.collections.ListUtils; 025import org.apache.commons.lang.ArrayUtils; 026 027import org.ametys.cms.CmsConstants; 028import org.ametys.cms.ObservationConstants; 029import org.ametys.cms.repository.WorkflowAwareContent; 030import org.ametys.core.observation.Event; 031import org.ametys.core.observation.ObservationManager; 032import org.ametys.core.user.CurrentUserProvider; 033import org.ametys.plugins.repository.AmetysRepositoryException; 034import org.ametys.plugins.repository.version.VersionableAmetysObject; 035import org.ametys.runtime.i18n.I18nizableText; 036 037import com.opensymphony.module.propertyset.PropertySet; 038import com.opensymphony.workflow.WorkflowException; 039 040/** 041 * OSWorkflow function for validating a content. 042 */ 043public class RemoveLiveLabelFunction extends AbstractContentFunction 044{ 045 /** Observer manager. */ 046 protected ObservationManager _observerManager; 047 /** Current user provider */ 048 protected CurrentUserProvider _currentUserProvider; 049 050 @Override 051 public void service(ServiceManager manager) throws ServiceException 052 { 053 super.service(manager); 054 _observerManager = (ObservationManager) manager.lookup(ObservationManager.ROLE); 055 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 056 } 057 058 @Override 059 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 060 { 061 _logger.info("Performing content unpublish."); 062 WorkflowAwareContent content = getContent(transientVars); 063 064 try 065 { 066 unpublishContent(content); 067 } 068 catch (AmetysRepositoryException e) 069 { 070 throw new WorkflowException("Error unpublishing the content", e); 071 } 072 073 notifyObservers(content); 074 } 075 076 /** 077 * Notify observers of content validation 078 * @param content The content to unpublish 079 */ 080 protected void notifyObservers(WorkflowAwareContent content) 081 { 082 _observerManager.notify(new Event(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE, _currentUserProvider.getUser(), _getEventParams(content))); 083 } 084 085 /** 086 * Get the event params for notifications. 087 * @param content The content to unpublish 088 * @return a {@link Map} of params 089 */ 090 protected Map<String, Object> _getEventParams(WorkflowAwareContent content) 091 { 092 Map<String, Object> eventParams = new HashMap<>(); 093 eventParams.put(ObservationConstants.ARGS_CONTENT, content); 094 return eventParams; 095 } 096 097 /** 098 * Unpublish the content. 099 * @param content the content to unpublish. 100 * @throws WorkflowException if an error occurs. 101 */ 102 protected void unpublishContent(WorkflowAwareContent content) throws WorkflowException 103 { 104 if (!(content instanceof VersionableAmetysObject)) 105 { 106 throw new WorkflowException("Invalid content implementation: " + content); 107 } 108 109 VersionableAmetysObject versionableContent = (VersionableAmetysObject) content; 110 111 if (ArrayUtils.contains(versionableContent.getAllLabels(), CmsConstants.LIVE_LABEL)) 112 { 113 versionableContent.removeLabel(CmsConstants.LIVE_LABEL); 114 } 115 116 content.saveChanges(); 117 } 118 119 @Override 120 public List<FunctionArgument> getArguments() 121 { 122 return ListUtils.EMPTY_LIST; 123 } 124 125 @Override 126 public I18nizableText getDescription(Map<String, String> args) 127 { 128 return new I18nizableText("plugin.cms", "PLUGINS_CMS_REMOVE_LIVE_LABEL_FUNCTION_DESCRIPTION"); 129 } 130}