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