@Override public FormRowSet store(Element element, FormRowSet rows, FormData formData) { FormRowSet result = rows; if (rows != null && !rows.isEmpty()) { // store form data to DB result = super.store(element, rows, formData); // handle workflow variables if (!rows.isMultiRow()) { String activityId = formData.getActivityId(); String processId = formData.getProcessId(); if (activityId != null || processId != null) { WorkflowManager workflowManager = (WorkflowManager) WorkflowUtil.getApplicationContext().getBean("workflowManager"); // recursively find element(s) mapped to workflow variable FormRow row = rows.iterator().next(); Map<String, String> variableMap = new HashMap<String, String>(); variableMap = storeWorkflowVariables(element, row, variableMap); if (activityId != null) { workflowManager.activityVariables(activityId, variableMap); } else { workflowManager.processVariables(processId, variableMap); } } } } return result; }
@Override public FormData formatDataForValidation(FormData formData) { String[] paramValues = FormUtil.getRequestParameterValues(this, formData); if ((paramValues == null || paramValues.length == 0) && FormUtil.isFormSubmitted(this, formData)) { String paramName = FormUtil.getElementParameterName(this); formData.addRequestParameterValues(paramName, new String[] {""}); } return formData; }
@Override public FormRowSet load(Element element, String primaryKey, FormData formData) { // load form data from DB FormRowSet rows = super.load(element, primaryKey, formData); if (rows != null) { // handle workflow variables String activityId = formData.getActivityId(); String processId = formData.getProcessId(); WorkflowManager workflowManager = (WorkflowManager) WorkflowUtil.getApplicationContext().getBean("workflowManager"); Collection<WorkflowVariable> variableList = null; if (activityId != null && !activityId.isEmpty()) { variableList = workflowManager.getActivityVariableList(activityId); } else if (processId != null && !processId.isEmpty()) { variableList = workflowManager.getProcessVariableList(processId); } else { variableList = new ArrayList<WorkflowVariable>(); } if (variableList != null && !variableList.isEmpty()) { FormRow row = null; if (rows.isEmpty()) { row = new FormRow(); rows.add(row); } else { row = rows.iterator().next(); } Map<String, String> variableMap = new HashMap<String, String>(); for (WorkflowVariable variable : variableList) { Object val = variable.getVal(); if (val != null) { variableMap.put(variable.getId(), val.toString()); } } loadWorkflowVariables(element, row, variableMap); } } return rows; }
@RequestMapping("/form/embed") public String embedForm( ModelMap model, HttpServletRequest request, HttpServletResponse response, @RequestParam("_submitButtonLabel") String buttonLabel, @RequestParam("_json") String json, @RequestParam("_callback") String callback, @RequestParam("_setting") String callbackSetting, @RequestParam(required = false) String id, @RequestParam(value = "_a", required = false) String action) throws JSONException, UnsupportedEncodingException { FormData formData = new FormData(); if (id != null && !id.isEmpty()) { formData.setPrimaryKeyValue(id); } Form form = formService.loadFormFromJson(json, formData); AppDefinition appDef = AppUtil.getCurrentAppDefinition(); String appId = ""; String appVersion = ""; if (appDef != null) { appId = appDef.getAppId(); appVersion = appDef.getVersion().toString(); } String nonce = request.getParameter("_nonce"); if (form == null || !SecurityUtil.verifyNonce( nonce, new String[] {"EmbedForm", appId, appVersion, form.getPropertyString("id"), nonce})) { response.setStatus(HttpServletResponse.SC_FORBIDDEN); return null; } if (callbackSetting == null || (callbackSetting != null && callbackSetting.isEmpty())) { callbackSetting = "{}"; } form.setProperty( "url", "?_nonce=" + URLEncoder.encode(nonce, "UTF-8") + "&_a=submit&_callback=" + callback + "&_setting=" + StringEscapeUtils.escapeHtml(callbackSetting) + "&_submitButtonLabel=" + StringEscapeUtils.escapeHtml(buttonLabel)); if (form != null) { // if id field not exist, automatically add an id hidden field Element idElement = FormUtil.findElement(FormUtil.PROPERTY_ID, form, formData); if (idElement == null) { Collection<Element> formElements = form.getChildren(); idElement = new HiddenField(); idElement.setProperty(FormUtil.PROPERTY_ID, FormUtil.PROPERTY_ID); idElement.setParent(form); formElements.add(idElement); } // create new section for buttons Section section = new Section(); section.setProperty(FormUtil.PROPERTY_ID, "section-actions"); Collection<Element> sectionChildren = new ArrayList<Element>(); section.setChildren(sectionChildren); Collection<Element> formChildren = form.getChildren(formData); if (formChildren == null) { formChildren = new ArrayList<Element>(); } formChildren.add(section); // add new horizontal column to section Column column = new Column(); column.setProperty("horizontal", "true"); Collection<Element> columnChildren = new ArrayList<Element>(); column.setChildren(columnChildren); sectionChildren.add(column); Element hiddenField = (Element) pluginManager.getPlugin(HiddenField.class.getName()); hiddenField.setProperty(FormUtil.PROPERTY_ID, "_json"); hiddenField.setProperty(FormUtil.PROPERTY_VALUE, json); columnChildren.add((Element) hiddenField); Element submitButton = (Element) pluginManager.getPlugin(SubmitButton.class.getName()); submitButton.setProperty(FormUtil.PROPERTY_ID, "submit"); submitButton.setProperty("label", buttonLabel); columnChildren.add((Element) submitButton); } // generate form HTML String formHtml = null; if ("submit".equals(action)) { formData = formService.retrieveFormDataFromRequest(formData, request); formData = formService.executeFormActions(form, formData); // check for validation errors Map<String, String> errors = formData.getFormErrors(); int errorCount = 0; if (!formData.getStay() && (errors == null || errors.isEmpty())) { // render normal template formHtml = formService.generateElementHtml(form, formData); // convert submitted JSONObject jsonResult = new JSONObject(); // get binder of main form FormStoreBinder mainBinder = form.getStoreBinder(); FormRowSet rows = formData.getStoreBinderData(mainBinder); for (FormRow row : rows) { for (Object o : row.keySet()) { jsonResult.accumulate(o.toString(), row.get(o)); } Map<String, String> tempFilePathMap = row.getTempFilePathMap(); if (tempFilePathMap != null && !tempFilePathMap.isEmpty()) { jsonResult.put(FormUtil.PROPERTY_TEMP_FILE_PATH, tempFilePathMap); } } model.addAttribute("jsonResult", StringEscapeUtils.escapeJavaScript(jsonResult.toString())); } else { // render error template formHtml = formService.generateElementErrorHtml(form, formData); errorCount = errors.size(); } model.addAttribute("setting", callbackSetting); model.addAttribute("callback", callback); model.addAttribute("submitted", Boolean.TRUE); model.addAttribute("errorCount", errorCount); model.addAttribute("stay", formData.getStay()); } else { formHtml = formService.retrieveFormHtml(form, formData); } model.addAttribute("formHtml", formHtml); if (request.getParameter("_mapp") != null) { response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Content-type", "application/xml"); return "mapp/embedForm"; } else { return "fbuilder/embedForm"; } }
@RequestMapping("/client/app/(*:appId)/(~:version)/process/(*:processDefId)") public String clientProcessView( HttpServletRequest request, ModelMap model, @RequestParam("appId") String appId, @RequestParam(required = false) String version, @RequestParam String processDefId, @RequestParam(required = false) String recordId, @RequestParam(required = false) String start) { // clean process def processDefId = WorkflowUtil.getProcessDefIdWithoutVersion(processDefId); AppDefinition appDef = appService.getAppDefinition(appId, version); WorkflowProcess processDef = appService.getWorkflowProcessForApp(appId, appDef.getVersion().toString(), processDefId); // check for permission if (!workflowManager.isUserInWhiteList(processDef.getId())) { return "client/app/processUnauthorized"; } // set app and process details model.addAttribute("appId", appId); model.addAttribute("appVersion", appDef.getVersion()); model.addAttribute("appDefinition", appDef); model.addAttribute("process", processDef); model.addAttribute("queryString", request.getQueryString()); // check for start mapped form FormData formData = new FormData(); formData.setPrimaryKeyValue(recordId); String formUrl = "/web/client/app/" + appId + "/" + appDef.getVersion() + "/process/" + processDefId + "/start"; if (recordId != null) { formUrl += "?recordId=" + recordId; } String formUrlWithContextPath = AppUtil.getRequestContextPath() + formUrl; PackageActivityForm startFormDef = appService.viewStartProcessForm( appId, appDef.getVersion().toString(), processDefId, formData, formUrlWithContextPath); if (startFormDef != null && startFormDef.getForm() != null) { Form startForm = startFormDef.getForm(); // generate form HTML String formHtml = formService.retrieveFormHtml(startForm, formData); String formJson = formService.generateElementJson(startForm); // show form model.addAttribute("form", startForm); model.addAttribute("formJson", formJson); model.addAttribute("formHtml", formHtml); return "client/app/processFormStart"; } else { if (Boolean.valueOf(start).booleanValue()) { // redirect to start URL return "redirect:" + formUrl; } else { // empty start page return "client/app/processStart"; } } }
@RequestMapping("/client/app/(~:appId)/(~:version)/assignment/(*:activityId)/submit") public String clientAssignmentSubmit( HttpServletRequest request, ModelMap model, @RequestParam(required = false) String appId, @RequestParam(required = false) String version, @RequestParam("activityId") String activityId) { // check assignment WorkflowAssignment assignment = workflowManager.getAssignment(activityId); if (assignment == null) { return "client/app/assignmentUnavailable"; } // get app AppDefinition appDef = null; if (appId != null && !appId.isEmpty()) { appDef = appService.getAppDefinition(appId, version); } else { appDef = appService.getAppDefinitionForWorkflowActivity(activityId); } // extract form values from request FormData formData = new FormData(); formData = formService.retrieveFormDataFromRequest(formData, request); // set process instance ID as primary key String processId = assignment.getProcessId(); // load form Long appVersion = (appDef != null) ? appDef.getVersion() : null; String formUrl = AppUtil.getRequestContextPath() + "/web/client/app/" + appId + "/" + appVersion + "/assignment/" + activityId + "/submit"; PackageActivityForm activityForm = appService.viewAssignmentForm(appId, version, activityId, formData, formUrl); Form form = activityForm.getForm(); // submit form FormData formResult = formService.executeFormActions(form, formData); if (formResult.getFormResult(AssignmentWithdrawButton.DEFAULT_ID) != null) { // withdraw assignment workflowManager.assignmentWithdraw(activityId); return "client/app/dialogClose"; } else if (formResult.getFormResult(AssignmentCompleteButton.DEFAULT_ID) != null) { // complete assignment Map<String, String> variableMap = AppUtil.retrieveVariableDataFromRequest(request); formResult = appService.completeAssignmentForm(appId, version, activityId, formData, variableMap); Map<String, String> errors = formResult.getFormErrors(); if (errors.isEmpty() && activityForm.isAutoContinue()) { // redirect to next activity if available WorkflowAssignment nextActivity = workflowManager.getAssignmentByProcess(processId); if (nextActivity != null) { String assignmentUrl = "/web/client/app/" + appId + "/" + appVersion + "/assignment/" + nextActivity.getActivityId(); return "redirect:" + assignmentUrl; } } } String html = null; // check for validation errors Map<String, String> errors = formResult.getFormErrors(); int errorCount = 0; if (errors == null || errors.isEmpty()) { // render normal template html = formService.generateElementHtml(form, formResult); } else { // render error template html = formService.generateElementErrorHtml(form, formResult); errorCount = errors.size(); } String formJson = formService.generateElementJson(form); model.addAttribute("assignment", assignment); model.addAttribute("form", form); model.addAttribute("formHtml", html); model.addAttribute("formJson", formJson); model.addAttribute("formResult", formResult); model.addAttribute("errorCount", errorCount); model.addAttribute("submitted", Boolean.TRUE); model.addAttribute("closeDialog", Boolean.TRUE); return "client/app/assignmentView"; }
@RequestMapping("/client/app/(*:appId)/(~:version)/process/(*:processDefId)/start") public String clientProcessStart( HttpServletRequest request, ModelMap model, @RequestParam("appId") String appId, @RequestParam(required = false) String version, @RequestParam(required = false) String recordId, @RequestParam String processDefId) { // clean process def processDefId = WorkflowUtil.getProcessDefIdWithoutVersion(processDefId); // set app and process details AppDefinition appDef = appService.getAppDefinition(appId, version); WorkflowProcess processDef = appService.getWorkflowProcessForApp(appId, appDef.getVersion().toString(), processDefId); String processDefIdWithVersion = processDef.getId(); model.addAttribute("appId", appId); model.addAttribute("appVersion", appDef.getVersion()); model.addAttribute("appDefinition", appDef); model.addAttribute("process", processDef); // check for permission if (!workflowManager.isUserInWhiteList(processDef.getId())) { return "client/app/processUnauthorized"; } // extract form values from request FormData formData = new FormData(); formData.setPrimaryKeyValue(recordId); formData = formService.retrieveFormDataFromRequest(formData, request); // get workflow variables Map<String, String> variableMap = AppUtil.retrieveVariableDataFromRequest(request); String formUrl = AppUtil.getRequestContextPath() + "/web/client/app/" + appId + "/" + appDef.getVersion() + "/process/" + processDefId + "/start"; if (recordId != null) { formUrl += "?recordId=" + recordId; } PackageActivityForm startFormDef = appService.viewStartProcessForm( appId, appDef.getVersion().toString(), processDefId, formData, formUrl); WorkflowProcessResult result = appService.submitFormToStartProcess( appId, version, processDefId, formData, variableMap, recordId, formUrl); if (startFormDef != null && (startFormDef.getForm() != null || PackageActivityForm.ACTIVITY_FORM_TYPE_EXTERNAL.equals(startFormDef.getType()))) { if (result == null) { // validation error, get form Form startForm = startFormDef.getForm(); // generate form HTML String formHtml = formService.retrieveFormErrorHtml(startForm, formData); String formJson = formService.generateElementJson(startForm); // show form model.addAttribute("form", startForm); model.addAttribute("formJson", formJson); model.addAttribute("formHtml", formHtml); model.addAttribute("activityForm", startFormDef); return "client/app/processFormStart"; } } else { // start process - TODO: handle process linking result = workflowManager.processStart( processDefIdWithVersion, null, variableMap, null, recordId, false); } // set result if (result != null) { WorkflowProcess process = result.getProcess(); model.addAttribute("process", process); // redirect to next activity if available Collection<WorkflowActivity> activities = result.getActivities(); if (activities != null && !activities.isEmpty()) { WorkflowActivity nextActivity = activities.iterator().next(); String assignmentUrl = "/web/client/app/" + appId + "/" + appDef.getVersion() + "/assignment/" + nextActivity.getId() + "?" + request.getQueryString(); return "redirect:" + assignmentUrl; } } return "client/app/processStarted"; }