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.io.IOException; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.ProcessingException; 026import org.apache.cocoon.environment.ObjectModelHelper; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.generation.ServiceableGenerator; 029import org.apache.cocoon.xml.AttributesImpl; 030import org.apache.cocoon.xml.XMLUtils; 031import org.apache.commons.lang3.ArrayUtils; 032import org.xml.sax.SAXException; 033 034import org.ametys.core.user.CurrentUserProvider; 035import org.ametys.core.user.UserIdentity; 036import org.ametys.plugins.bpm.BPMWorkflowManager; 037import org.ametys.plugins.bpm.jcr.JCRWorkflow; 038import org.ametys.plugins.bpm.jcr.JCRWorkflowProcess; 039import org.ametys.plugins.explorer.resources.ModifiableResource; 040import org.ametys.plugins.explorer.resources.ResourceCollection; 041import org.ametys.plugins.repository.AmetysObjectIterable; 042import org.ametys.plugins.repository.AmetysObjectResolver; 043import org.ametys.plugins.workflow.support.WorkflowProvider; 044import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow; 045 046/** 047 * Generator for the CreateProcess Service 048 */ 049public class CreateProcessGenerator extends ServiceableGenerator 050{ 051 052 private BPMWorkflowManager _bpmWorkflowManager; 053 private AmetysObjectResolver _resolver; 054 private WorkflowProvider _workflowProvider; 055 private CurrentUserProvider _currentUserProvider; 056 057 @Override 058 public void service(ServiceManager smanager) throws ServiceException 059 { 060 super.service(smanager); 061 _bpmWorkflowManager = (BPMWorkflowManager) smanager.lookup(BPMWorkflowManager.ROLE); 062 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 063 _workflowProvider = (WorkflowProvider) smanager.lookup(WorkflowProvider.ROLE); 064 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 065 } 066 067 public void generate() throws IOException, SAXException, ProcessingException 068 { 069 contentHandler.startDocument(); 070 071 XMLUtils.startElement(contentHandler, "createProcess"); 072 073 UserIdentity user = _currentUserProvider.getUser(); 074 if (user == null) 075 { 076 XMLUtils.createElement(contentHandler, "error", "anonymous-user"); 077 } 078 else 079 { 080 081 Request request = ObjectModelHelper.getRequest(objectModel); 082 String processId = request.getParameter("processId"); 083 if (processId == null) 084 { 085 _saxWorkflows(); 086 } 087 else 088 { 089 JCRWorkflowProcess process = _resolver.resolveById(processId); 090 AmetysObjectWorkflow aoWorkflow = _workflowProvider.getAmetysObjectWorkflow(process, true); 091 JCRWorkflow workflow = _resolver.resolveById(process.getWorkflow()); 092 Map<String, Object> inputs = new HashMap<>(); 093 inputs.put("workflowId", workflow.getId()); 094 inputs.put("process", process); 095 // Add the workflow used by the process in the request, to be used by any RegisterVariable of the workflow definition 096 request.setAttribute("workflowId", process.getWorkflow()); 097 098 if (ArrayUtils.contains(aoWorkflow.getAvailableActions(process.getWorkflowId(), inputs), BPMWorkflowManager.WORKFLOW_ACTION_EDIT)) 099 { 100 _saxValues(process, workflow); 101 } 102 else 103 { 104 XMLUtils.createElement(contentHandler, "error", "insufficient-rights-edit"); 105 } 106 } 107 } 108 109 XMLUtils.endElement(contentHandler, "createProcess"); 110 111 contentHandler.endDocument(); 112 } 113 114 private void _saxWorkflows() throws SAXException 115 { 116 XMLUtils.startElement(contentHandler, "workflows"); 117 118 List<Map<String, Object>> workflows = _bpmWorkflowManager.getWorkflowsAvailables(); 119 for (Map<String, Object> workflow : workflows) 120 { 121 AttributesImpl attrs = new AttributesImpl(); 122 attrs.addCDATAAttribute("id", (String) workflow.get("id")); 123 attrs.addCDATAAttribute("title", (String) workflow.get("title")); 124 XMLUtils.createElement(contentHandler, "workflow", attrs); 125 } 126 127 XMLUtils.endElement(contentHandler, "workflows"); 128 } 129 130 private void _saxValues(JCRWorkflowProcess process, JCRWorkflow workflow) throws SAXException 131 { 132 if (process != null) 133 { 134 AttributesImpl attrs = new AttributesImpl(); 135 attrs.addCDATAAttribute("id", process.getId()); 136 XMLUtils.startElement(contentHandler, "process", attrs); 137 138 attrs = new AttributesImpl(); 139 attrs.addCDATAAttribute("id", workflow.getId()); 140 attrs.addCDATAAttribute("name", workflow.getName()); 141 XMLUtils.createElement(contentHandler, "workflow", attrs, workflow.getTitle()); 142 143 XMLUtils.createElement(contentHandler, "title", process.getTitle()); 144 XMLUtils.createElement(contentHandler, "description", process.getDescription()); 145 146 ResourceCollection rootAttachments = process.getRootAttachments(false); 147 if (rootAttachments != null) 148 { 149 XMLUtils.startElement(contentHandler, "attachments"); 150 AmetysObjectIterable<ModifiableResource> attachments = rootAttachments.getChildren(); 151 for (ModifiableResource attachment : attachments) 152 { 153 AttributesImpl attachmentAttrs = new AttributesImpl(); 154 attachmentAttrs.addCDATAAttribute("name", attachment.getName()); 155 XMLUtils.createElement(contentHandler, "attachment", attachmentAttrs); 156 } 157 XMLUtils.endElement(contentHandler, "attachments"); 158 } 159 160 XMLUtils.endElement(contentHandler, "process"); 161 } 162 } 163 164}