001/*
002 *  Copyright 2010 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.web.workflow;
017
018import java.util.Arrays;
019import java.util.Date;
020import java.util.HashMap;
021import java.util.Map;
022
023import javax.jcr.RepositoryException;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.repository.ModifiableWorkflowAwareContent;
028import org.ametys.cms.repository.WorkflowAwareContent;
029import org.ametys.cms.workflow.AbstractContentFunction;
030import org.ametys.core.observation.Event;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032import org.ametys.plugins.repository.version.VersionableAmetysObject;
033import org.ametys.web.WebConstants;
034
035import com.opensymphony.module.propertyset.PropertySet;
036import com.opensymphony.workflow.WorkflowException;
037
038/**
039 * OSWorkflow function for validating a content.
040 */
041public class ValidateContentFunction extends AbstractContentFunction
042{
043    
044    @Override
045    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
046    {
047        _logger.info("Performing content validation");
048        
049        WorkflowAwareContent content = getContent(transientVars);
050        
051        if (!(content instanceof ModifiableWorkflowAwareContent))
052        {
053            throw new IllegalArgumentException("The provided content " + content.getId() + " is not a ModifiableWorkflowAwareContent.");
054        }
055        
056        ModifiableWorkflowAwareContent modifiableContent = (ModifiableWorkflowAwareContent) content;
057        
058        try
059        {
060            // Set the validation metadata.
061            _validateContent(modifiableContent);
062            // Set the current step ID.
063            _setCurrentStepIdAndNotify(modifiableContent, transientVars);
064            // Add the live label on the newly created version.
065            _addLabel(modifiableContent, WebConstants.LIVE_LABEL);
066        }
067        catch (RepositoryException e)
068        {
069            throw new WorkflowException("Unable to link the workflow to the content", e);
070        }
071        catch (AmetysRepositoryException e)
072        {
073            throw new WorkflowException("Unable to validate the content", e);
074        }
075        
076        _notifyObservers(transientVars, modifiableContent);
077    }
078    
079    /**
080     * Notify observers of content validation
081     * @param transientVars The transient variables
082     * @param content The created content
083     * @throws AmetysRepositoryException If an error occurred with the repository
084     * @throws WorkflowException If an error occurred with the workflow
085     */
086    protected void _notifyObservers (Map transientVars, Content content) throws AmetysRepositoryException, WorkflowException
087    {
088        Map<String, Object> eventParams = new HashMap<>();
089        eventParams.put(ObservationConstants.ARGS_CONTENT, content);
090        eventParams.put(ObservationConstants.ARGS_CONTENT_ID, content.getId());
091        _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_VALIDATED, getUser(transientVars), eventParams));
092    }
093
094    /**
095     * Validates the content: set the validation metadata.
096     * @param content the content.
097     * @throws WorkflowException if an error occurs.
098     * @throws RepositoryException if an error occurs.
099     */
100    protected void _validateContent(ModifiableWorkflowAwareContent content) throws WorkflowException, RepositoryException
101    {
102        if (!(content instanceof VersionableAmetysObject))
103        {
104            throw new WorkflowException("Invalid content implementation: " + content);
105        }
106        
107        Date validationDate = new Date();
108        boolean isValid = Arrays.asList(((VersionableAmetysObject) content).getAllLabels()).contains(WebConstants.LIVE_LABEL);
109        if (!isValid)
110        {
111            content.setLastMajorValidationDate(validationDate);
112        }
113        content.setLastValidationDate(validationDate);
114        // Remove the proposal date.
115        content.setProposalDate(null);
116        
117        content.saveChanges();
118    }
119    
120}