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