001/* 002 * Copyright 2017 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.plugins.bpm.process; 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.cocoon.servlet.multipart.Part; 024import org.apache.cocoon.servlet.multipart.PartOnDisk; 025 026import org.ametys.plugins.bpm.jcr.JCRWorkflowProcess; 027import org.ametys.plugins.explorer.resources.ModifiableResource; 028import org.ametys.plugins.explorer.resources.ModifiableResourceCollection; 029import org.ametys.plugins.explorer.resources.actions.AddOrUpdateResourceHelper; 030import org.ametys.plugins.explorer.resources.actions.AddOrUpdateResourceHelper.ResourceOperationMode; 031import org.ametys.plugins.workflow.AbstractWorkflowComponent; 032 033import com.opensymphony.module.propertyset.PropertySet; 034import com.opensymphony.workflow.FunctionProvider; 035import com.opensymphony.workflow.WorkflowException; 036 037/** 038 * Workflow action to edit a process 039 */ 040public class EditProcessFunction extends AbstractWorkflowComponent implements FunctionProvider 041{ 042 private AddOrUpdateResourceHelper _addOrUpdateResourceHelper; 043 044 @Override 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 super.service(manager); 048 _addOrUpdateResourceHelper = (AddOrUpdateResourceHelper) manager.lookup(AddOrUpdateResourceHelper.ROLE); 049 } 050 051 @SuppressWarnings("unchecked") 052 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException 053 { 054 JCRWorkflowProcess process = (JCRWorkflowProcess) transientVars.get("process"); 055 String title = (String) transientVars.get("title"); 056 String description = (String) transientVars.getOrDefault("description", null); 057 List<PartOnDisk> uploadedAttachments = (List<PartOnDisk>) transientVars.getOrDefault("uploadedAttachments", null); 058 List<String> attachmentsUntouched = (List<String>) transientVars.getOrDefault("attachmentsUntouched", null); 059 060 process.setTitle(title); 061 process.setDescription(description); 062 063 ModifiableResourceCollection rootAttachments = (ModifiableResourceCollection) process.getRootAttachments(true); 064 _addOrUpdateResourceHelper.checkAddResourceRight(rootAttachments); 065 066 // Remove attachments that are not in the "untouched" list 067 rootAttachments.getChildren().stream() 068 .map(attachment -> attachment.getName()) 069 .filter(attachment -> attachmentsUntouched == null || !attachmentsUntouched.contains(attachment)) 070 .forEach(attachment -> 071 { 072 ModifiableResource child = rootAttachments.getChild(attachment); 073 child.remove(); 074 }); 075 076 077 if (uploadedAttachments != null) 078 { 079 for (Part attachment : uploadedAttachments) 080 { 081 _addOrUpdateResourceHelper.performResourceOperation(attachment, rootAttachments, ResourceOperationMode.ADD); 082 } 083 } 084 085 if (process.needsSave()) 086 { 087 process.saveChanges(); 088 } 089 } 090 091}