001/* 002 * Copyright 2018 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.List; 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.commons.collections.ListUtils; 024 025import org.ametys.cms.content.references.OutgoingReferences; 026import org.ametys.cms.content.references.OutgoingReferencesExtractor; 027import org.ametys.cms.repository.ModifiableContent; 028import org.ametys.cms.repository.WorkflowAwareContent; 029import org.ametys.core.user.UserIdentity; 030import org.ametys.plugins.repository.AmetysRepositoryException; 031import org.ametys.plugins.repository.lock.LockHelper; 032import org.ametys.plugins.repository.lock.LockableAmetysObject; 033import org.ametys.plugins.workflow.EnhancedFunction; 034import org.ametys.runtime.i18n.I18nizableText; 035 036import com.opensymphony.module.propertyset.PropertySet; 037import com.opensymphony.workflow.WorkflowException; 038 039/** 040 * OSWorkflow function to extract outgoing references in a content. 041 * 042 * The required transient variables: 043 * - AbstractContentWorkflowComponent.RESULT_MAP_KEY - Map<String, Object> The map containing the results of the function. 044 * - AbstractContentWorkflowComponent.RESULT_MAP_KEY.result - String "true" when everything goes fine. Missing in other case. 045 * - AbstractContentWorkflowComponent.CONTENT_KEY - WorkflowAwareContent The content that will be edited. Should have the lock token. 046 */ 047public class ExtractOutgoingReferencesFunction extends AbstractContentWorkflowComponent implements EnhancedFunction 048{ 049 /** The outgoing references extractor */ 050 protected OutgoingReferencesExtractor _outgoingReferencesExtractor; 051 052 @Override 053 public void service(ServiceManager smanager) throws ServiceException 054 { 055 super.service(smanager); 056 _outgoingReferencesExtractor = (OutgoingReferencesExtractor) smanager.lookup(OutgoingReferencesExtractor.ROLE); 057 } 058 059 @Override 060 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 061 { 062 _logger.info("Performing extract outgoing references workflow function"); 063 064 065 // Retrieve current content 066 WorkflowAwareContent content = getContent(transientVars); 067 UserIdentity user = getUser(transientVars); 068 069 if (!(content instanceof ModifiableContent)) 070 { 071 throw new IllegalArgumentException("The provided content " + content.getId() + " is not a ModifiableContent."); 072 } 073 074 ModifiableContent modifiableContent = (ModifiableContent) content; 075 076 try 077 { 078 LockableAmetysObject lockableContent = null; 079 if (content instanceof LockableAmetysObject) 080 { 081 lockableContent = (LockableAmetysObject) content; 082 if (lockableContent.isLocked() && !LockHelper.isLockOwner(lockableContent, user)) 083 { 084 throw new WorkflowException("User '" + user + "' try to save content '" + modifiableContent.getName() + "' but it is locked by another user"); 085 } 086 } 087 088 _extractOutgoingReferences(modifiableContent); 089 090 // Commit changes 091 modifiableContent.saveChanges(); 092 093 getResultsMap(transientVars).put("result", "ok"); 094 } 095 catch (AmetysRepositoryException e) 096 { 097 throw new WorkflowException("Unable to edit content " + modifiableContent + " from the repository", e); 098 } 099 } 100 101 /** 102 * Analyze the content to extract outgoing references and store them 103 * @param content The content to analyze 104 */ 105 protected void _extractOutgoingReferences(ModifiableContent content) 106 { 107 Map<String, OutgoingReferences> outgoingReferencesByPath = _outgoingReferencesExtractor.getOutgoingReferences(content); 108 content.setOutgoingReferences(outgoingReferencesByPath); 109 } 110 111 @Override 112 public List<FunctionArgument> getArguments() 113 { 114 return ListUtils.EMPTY_LIST; 115 } 116 117 @Override 118 public I18nizableText getDescription(Map<String, String> args) 119 { 120 return new I18nizableText("plugin.cms", "PLUGINS_CMS_EXTRACT_OUTGOING_REFERENCES_FUNCTION_DESCRIPTION"); 121 } 122}