All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nosi.core.webapp.bpmn.BPMNTaskController Maven / Gradle / Ivy

Go to download

IGRP Framework is a powerful and highly customizable platform developed by the Operational Nucleus for the Information Society (NOSi) to create web applications, it provides out of box, several modules to make easy to create stand-alone, production-grade web applications: authentication and access-control, business processes automation, reporting, page builder with automatic code generation and incorporation of the Once-Only-Principle, written in Java. IGRP Framework WAR - Contains some keys resources that give UI to IGRP Framework and others supports files.

There is a newer version: 2.0.0.241121-RCM
Show newest version
package nosi.core.webapp.bpmn;

import static nosi.core.i18n.Translator.gt;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Part;
import com.google.gson.Gson;

import nosi.core.gui.components.IGRPMessage;
import nosi.core.webapp.Controller;
import nosi.core.webapp.Core;
import nosi.core.webapp.Igrp;
import nosi.core.webapp.Model;
import nosi.core.webapp.Response;
import nosi.core.webapp.View;
import nosi.core.webapp.activit.rest.business.TaskServiceIGRP;
import nosi.core.webapp.activit.rest.entities.CustomVariableIGRP;
import nosi.core.webapp.activit.rest.entities.Rows;
import nosi.core.webapp.activit.rest.entities.StartProcess;
import nosi.core.webapp.activit.rest.entities.TaskService;
import nosi.core.webapp.activit.rest.entities.TaskServiceQuery;
import nosi.core.webapp.activit.rest.services.TaskServiceRest;
import nosi.core.webapp.helpers.FileHelper;
import nosi.core.xml.XMLWritter;
import nosi.webapps.igrp.dao.Action;
import nosi.webapps.igrp.dao.Application;
import nosi.webapps.igrp.dao.CLob;
import nosi.webapps.igrp.dao.TipoDocumentoEtapa;

/**
 * Emanuel
 * 7 May 2018
 */

public abstract class BPMNTaskController extends Controller implements InterfaceBPMNTask{
	private String page;
	private String myCustomPermission;
	private String usernameNextTask;
	protected RuntimeTask runtimeTask;
	private final BPMNExecution bpmnExecute;
	
	private final List inputDocsErrors;
	private boolean inputDocsAlreadyValidate; 
	
	protected BPMNTaskController() {
		this.runtimeTask = RuntimeTask.getRuntimeTask();
		this.bpmnExecute = new BPMNExecution();
		this.inputDocsErrors = new ArrayList<>();
	}
	@Override
	public Response index(String app,Model model,View view) throws IOException {
		view.setModel(model);
		this.page = this.getClass().getSimpleName().replace("Controller", "");
		return this.renderView(app,model.getClass().getSimpleName(),view,this,this.runtimeTask);
	}

	@Override
	public Response index(String app,Model model,View view,InterfaceBPMNTask bpmnTask) throws IOException {
		view.setModel(model);
		this.page = this.getClass().getSimpleName().replace("Controller", "");
		return this.renderView(app,model.getClass().getSimpleName(),view,bpmnTask,this.runtimeTask);
	}
	
	@Override
	public Response index() throws IOException{	
		if(Core.isNotNull(this.runtimeTask)) {
			Action action = new Action().find().andWhere("application", "=",this.runtimeTask.getAppId())
					.andWhere("page", "=",this.runtimeTask.getTask().getFormKey()).one();
			Response resp = this.call(action.getApplication().getDad(), action.getPage(),"index",this.queryString());
			String content = BPMNButton.removeXMLButton(resp.getContent());
			XMLWritter xml = new XMLWritter("rows", this.getConfig().getResolveUrl("igrp","mapa-processo","get-xsl").replace("&", "&")
					+"&page="+this.runtimeTask.getTask().getFormKey()+"&app="+this.runtimeTask.getAppId());
			xml.addXml(this.getConfig().getHeader(null,action));
			xml.startElement("content");
			xml.writeAttribute("type", "");
			if(Core.isNotNull(this.runtimeTask.getTask().getProcessInstanceId())) {
				xml.addXml(BPMNButton.generateButtonProcess(this.runtimeTask.getTask().getTenantId(),action.getApplication().getId(),
						BPMNConstants.PREFIX_TASK+this.runtimeTask.getTask().getTaskDefinitionKey(),"save",this.runtimeTask.getTask().getProcessInstanceId()).toString());
			}
			if(Core.isNotNull(this.runtimeTask.getTask().getId())) {
				xml.addXml(BPMNButton.generateButtonTask(this.runtimeTask.getTask().getTenantId(),action.getApplication().getId(),
						BPMNConstants.PREFIX_TASK+this.runtimeTask.getTask().getTaskDefinitionKey(),"save", this.runtimeTask.getTask().getTenantId(), this.queryString()).toString());
			}
			xml.addXml(content);
			xml.addXml(BPMNHelper.addFileSeparator(this.runtimeTask.getTask().getTenantId(),this.runtimeTask.getTask().getProcessDefinitionId(),this.runtimeTask.getTask().getTaskDefinitionKey(),null));
			IGRPMessage msg = new IGRPMessage();
			String m = msg.toString();
			if(m!=null){
				xml.addXml(m);
			}
			xml.endElement(); 
			return this.renderView(xml.toString());	
		}
		Core.setAttribute("jakarta.servlet.error.message", gt("Task não tem página associada!"));
		return this.redirect("igrp", "ErrorPage", "exception");
	}

	//Save the task
	@Override
	public Response save() throws IOException, ServletException {
		String processDefinitionId = Core.getParam(BPMNConstants.PRM_DEFINITION_ID);
		String taskId = Core.getParamTaskId();
		if(Core.isNotNullMultiple(this.runtimeTask,taskId)){
			List parts = (List) Igrp.getInstance().getRequest().getParts();
			if(!inputDocsAlreadyValidate && parts !=null && !ValidateInputDocument.validateRequiredDocument(this,parts,this.runtimeTask, this.inputDocsErrors)) { 
				if(!this.inputDocsErrors.isEmpty()) 
					this.inputDocsErrors.forEach(Core::setMessageError); 
				Core.setAttribute(BPMNConstants.PRM_RUNTIME_TASK, this.runtimeTask);
				return this.forward(this.runtimeTask.getTask().getTenantId(), BPMNConstants.PREFIX_TASK+this.runtimeTask.getTask().getTaskDefinitionKey(), "index",this.queryString());
			}
			return this.saveTask(this.runtimeTask.getTask(),taskId,parts);
		}
		if(Core.isNotNull(processDefinitionId)){
			return this.startProcess(processDefinitionId);
		}		
		return this.redirect("igrp", "ErrorPage", "exception");
	} 
	
	protected Response inputDocsHasErrors() throws IOException, ServletException {
		Response response = null; 
		inputDocsAlreadyValidate = true; 
		List parts = (List) Igrp.getInstance().getRequest().getParts();
		if(parts!=null && !ValidateInputDocument.validateRequiredDocument(this, parts, this.runtimeTask, this.inputDocsErrors)) { 
			if(!this.inputDocsErrors.isEmpty()) 
				this.inputDocsErrors.forEach(Core::setMessageError); 
			Core.setAttribute(BPMNConstants.PRM_RUNTIME_TASK, this.runtimeTask); 
			response = this.forward(this.runtimeTask.getTask().getTenantId(), BPMNConstants.PREFIX_TASK+this.runtimeTask.getTask().getTaskDefinitionKey(), "index",this.queryString());
		}
		return response; 
	}

	private Response startProcess(String processDefinitionId) throws IOException  {
		StartProcess st = bpmnExecute.executeStartProcess(processDefinitionId,this.myCustomPermission);
		if (Core.isNull(st)) {
			return this.redirect("igrp", "Dash_board_processo", "index");
		}
		Core.setMessageSuccess();
		TaskServiceIGRP task = new TaskServiceIGRP();
		task.clearFilterBody();
		task.addFilterBody("processDefinitionId", processDefinitionId);
		task.addFilterBody("processInstanceId", st.getId());
		List tasks = task.getAvailableTasks();
		if (tasks != null && !tasks.isEmpty()) {
			return this.renderNextTask(tasks);
		} else {
			return this.redirect("igrp", "Dash_board_processo", "index");
		}
	}
	

	
	private Response saveTask(TaskService task,String taskId,List parts) throws IOException  {
		TaskServiceIGRP taskServiceRest = new TaskServiceIGRP();
		StartProcess st = this.bpmnExecute.executeTask(task, parts,this.myCustomPermission);
		if(Core.isNull(st)) {
			return this.redirect("igrp", "ErrorPage", "exception");
		}else {
			this.saveFiles(parts,taskId);
			Core.removeAttribute("taskId");
			Core.setMessageSuccess();
			taskServiceRest.clearFilterBody();
			taskServiceRest.addFilterBody("processDefinitionId",task.getProcessDefinitionId());
			taskServiceRest.addFilterBody("processInstanceId", task.getProcessInstanceId());
			List tasks = taskServiceRest.getAvailableTasks();
			if(tasks!=null  && !tasks.isEmpty()) {
				return this.renderNextTask(task,tasks);
			}else {
				return this.redirect("igrp","ExecucaoTarefas","index");
			}
		}
	}
	

	


	private void saveFiles(List parts_,String taskId) {
		Object[] id_tp_doc = Core.getParamArray("p_formlist_documento_id_tp_doc_fk");	
		if(id_tp_doc!=null && parts_!=null) {
			try {
				id_tp_doc = Arrays.stream(id_tp_doc).filter(Core::isNotNull).toArray();
				
				Object[] doc_id = Core.getParamArray("p_formlist_documento_doc_id_fk");	
				doc_id = Arrays.stream(doc_id).filter(Core::isNotNull).toArray();
	
				Object[] input_type = Core.getParamArray("p_formlist_documento_task_documento_fk_desc");	
				input_type = Arrays.stream(input_type).filter(Core::isNotNull).toArray();
	
				List parts = parts_.stream().filter(p->p.getName().equalsIgnoreCase("p_formlist_documento_task_documento_fk")).collect(Collectors.toList());

				this.saveFiles(parts,id_tp_doc,doc_id,input_type,taskId);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			}
		}
	}

	private void saveFiles(List parts,Object[] id_tp_doc,Object[] doc_id,Object[] input_type,String taskId) {
		Application app = Core.findApplicationByDad(Core.getCurrentDadParam());
		for(int i=0;i tasks) throws IOException {
		return renderNextTask(null,tasks);
	}

	private Response renderNextTask(TaskService task,List tasks) throws IOException {
		TaskService nextTask = tasks.get(tasks.size()-1);
		if(Core.isNull(this.usernameNextTask))
			this.usernameNextTask=Core.getCurrentUser().getUser_name();
		new TaskServiceRest().claimTask(nextTask.getId(), this.usernameNextTask);
		if(!Core.getCurrentUser().getUser_name().equalsIgnoreCase(this.usernameNextTask))		
			return this.redirect("igrp","ExecucaoTarefas","index");
		
		//If is the same user let it in the same page to be continued
		Application app = new Application().findByDad(nextTask.getTenantId());
		this.runtimeTask = new RuntimeTask();
		this.runtimeTask.setTask(nextTask);
		this.runtimeTask.setAppId(app.getId());
		if(task!=null) {
			this.runtimeTask.setPreiviewApp(task.getTenantId());
			this.runtimeTask.setPreiviewProcessDefinition(task.getProcessDefinitionId());
			this.runtimeTask.setPreviewTask(task.getTaskDefinitionKey());
			this.runtimeTask.setPreviewTaskId(task.getId());
		}
		this.runtimeTask.setShowTimeLine(true);
		this.runtimeTask.setSaveButton(true);
		this.addQueryString(BPMNConstants.PRM_TASK_ID, nextTask.getId());
		return this.redirect("igrp","ExecucaoTarefas","executar_button_minha_tarefas",this.queryString());
	}

	@Override
	public Response update() throws IOException{
 	
		return this.redirect("igrp", "ErrorPage", "exception");
	}

	@Override
	public List getOutputDocumentType() {
		String currentTaskDefinition = this.runtimeTask.getTask().getTaskDefinitionKey();
		String currentProcessDefinition = this.runtimeTask.getTask().getProcessDefinitionKey();
		String currentTaskApp = this.runtimeTask.getTask().getTenantId();
		currentTaskApp = Core.isNotNull(currentTaskApp)?currentTaskApp:this.runtimeTask.getPreiviewApp();
		
		return BPMNHelper.getOutputDocumentType(currentTaskApp,currentProcessDefinition,currentTaskDefinition);
	}

	@Override
	public List getInputDocumentType() {
		String appDad = this.runtimeTask.getTask().getTenantId();
		String taskDefinition = this.runtimeTask.getTask().getTaskDefinitionKey();
		String processDefinition = this.runtimeTask.getTask().getProcessDefinitionKey();
       return BPMNHelper.getInputDocumentTypeHistory(appDad,processDefinition, taskDefinition);
	}


	@Override
	public String details(TaskServiceQuery task) throws IOException {
		this.page = BPMNConstants.PREFIX_TASK+task.getTaskDefinitionKey();
		Gson gson = new Gson();		
		Action action = new Action().find()
									.andWhere("page", "=",this.page)
									.andWhere("application.dad", "=",task.getTenantId())
									 .andWhere("processKey", "=", task.getProcessDefinitionKey())
									.one();
		String json = "";
		final Object taskVariable = Core.getTaskVariable(BPMNConstants.CUSTOM_VARIABLE_IGRP_ACTIVITI + "_" + task.getId());
		if(Core.isNotNull(taskVariable)){
			json=taskVariable.toString();
		}
		if(Core.isNotNull(json)) {
			CustomVariableIGRP custom = gson.fromJson(json, CustomVariableIGRP.class);
			if(custom!=null){
				for(Rows rows:custom.getRows()) {
					if(!rows.getName().equalsIgnoreCase("page_igrp_ativiti") && !rows.getName().equalsIgnoreCase("app_igrp_ativiti")
							   && !rows.getName().equalsIgnoreCase("processDefinition") &&!rows.getName().equalsIgnoreCase("taskDefinition")
							   &&!rows.getName().equalsIgnoreCase("taskId") &&!rows.getName().equalsIgnoreCase("appId")) {
						for(Object obj:rows.getValue()) {
							this.addQueryString(rows.getName(), obj.toString());
						}
					}
				}
			}
		}	
		this.runtimeTask = new RuntimeTask(task, action.getApplication().getId(), task.getTaskDefinitionKey(), task.getTenantId(),
				task.getProcessDefinitionKey(), false, task.getId());
		this.runtimeTask.setSaveButton(false);
		this.runtimeTask.setDetails(true);
		
		this.addQueryString("report_p_prm_definitionid", task.getProcessInstanceId())
	        .addQueryString("current_app_conn", task.getTenantId());
	        Core.setAttribute(BPMNConstants.PRM_RUNTIME_TASK, this.runtimeTask);
	        Core.setAttribute(BPMNConstants.PRM_TASK_OBJ, task);
		 Response resp = this.call(task.getTenantId(),this.page, "index",this.queryString());
       return resp.getContent();
	}
	
	protected void setCustomPermission(String customPermission) {
		this.myCustomPermission = customPermission;
	}
	
	protected void setNextTaskToUser(String usernameNextTask) {
		this.usernameNextTask = usernameNextTask;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy