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.time.ZonedDateTime; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Optional; 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.core.user.UserIdentity; 032import org.ametys.plugins.repository.AmetysRepositoryException; 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 /** Context parameter for major validation */ 043 public static final String IS_MAJOR = "validation.major"; 044 045 @Override 046 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 047 { 048 _logger.info("Performing content validation"); 049 050 WorkflowAwareContent content = getContent(transientVars); 051 052 if (!(content instanceof ModifiableWorkflowAwareContent)) 053 { 054 throw new IllegalArgumentException("The provided content " + content.getId() + " is not a ModifiableWorkflowAwareContent."); 055 } 056 057 ModifiableWorkflowAwareContent modifiableContent = (ModifiableWorkflowAwareContent) content; 058 059 try 060 { 061 // Set the validation metadata. 062 _validateContent(modifiableContent, transientVars, args); 063 // Set the current step ID. 064 _setCurrentStepIdAndNotify(modifiableContent, transientVars); 065 // Create a new version. 066 _createVersion(modifiableContent); 067 // Add the live label on the newly created version. 068 _addLabel(modifiableContent, CmsConstants.LIVE_LABEL); 069 } 070 catch (RepositoryException e) 071 { 072 throw new WorkflowException("Unable to link the workflow to the content", e); 073 } 074 catch (AmetysRepositoryException e) 075 { 076 throw new WorkflowException("Unable to validate the content", e); 077 } 078 079 _notifyObservers(transientVars, modifiableContent); 080 } 081 082 /** 083 * Notify observers of content validation 084 * @param transientVars The transient variables 085 * @param content The created content 086 * @throws AmetysRepositoryException If an error occurred with the repository 087 * @throws WorkflowException If an error occurred with the workflow 088 */ 089 protected void _notifyObservers (Map transientVars, Content content) throws AmetysRepositoryException, WorkflowException 090 { 091 Map<String, Object> eventParams = new HashMap<>(); 092 eventParams.put(ObservationConstants.ARGS_CONTENT, content); 093 eventParams.put(ObservationConstants.ARGS_CONTENT_ID, content.getId()); 094 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_VALIDATED, getUser(transientVars), eventParams)); 095 } 096 097 /** 098 * Validates the content: set the validation metadata. 099 * @param content the content. 100 * @param transientVars the transient vars 101 * @param args the arguments 102 * @throws WorkflowException if an error occurs. 103 * @throws RepositoryException if an error occurs. 104 */ 105 protected void _validateContent(ModifiableWorkflowAwareContent content, Map transientVars, Map args) throws WorkflowException, RepositoryException 106 { 107 UserIdentity user = getUser(transientVars); 108 109 ZonedDateTime validationDate = ZonedDateTime.now(); 110 111 // Last major validation 112 boolean isMajor = _isMajorValidation(getContextParameters(transientVars), args); 113 if (isMajor) 114 { 115 if (user != null) 116 { 117 content.setLastMajorValidator(user); 118 } 119 content.setLastMajorValidationDate(validationDate); 120 } 121 122 // Last validation 123 if (user != null) 124 { 125 content.setLastValidator(user); 126 } 127 content.setLastValidationDate(validationDate); 128 129 // First validation 130 if (content.getFirstValidationDate() == null) 131 { 132 if (user != null) 133 { 134 content.setFirstValidator(user); 135 } 136 content.setFirstValidationDate(validationDate); 137 } 138 139 // Remove the proposal date. 140 content.setProposalDate(null); 141 142 content.saveChanges(); 143 } 144 145 /** 146 * Search into context parameters and arguments if major information is filled, if not default value is <code>true</code>. 147 * @param contextParameters the context parameters 148 * @param args the arguments 149 * @return <code>true</code> if the current validation is a major validation. 150 */ 151 protected boolean _isMajorValidation(Map<String, Object> contextParameters, Map args) 152 { 153 return Optional.of(contextParameters) 154 .map(params -> contextParameters.get(IS_MAJOR)) 155 .map(Boolean.class::cast) 156 .orElseGet(() -> !"false".equals(args.get("major"))); 157 } 158}