org.camunda.bpm.engine.impl.form.engine.HtmlFormEngine Maven / Gradle / Ivy
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.engine.impl.form.engine;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.camunda.bpm.engine.form.FormData;
import org.camunda.bpm.engine.form.FormField;
import org.camunda.bpm.engine.form.FormFieldValidationConstraint;
import org.camunda.bpm.engine.form.FormProperty;
import org.camunda.bpm.engine.form.StartFormData;
import org.camunda.bpm.engine.form.TaskFormData;
import org.camunda.bpm.engine.impl.form.type.BooleanFormType;
import org.camunda.bpm.engine.impl.form.type.DateFormType;
import org.camunda.bpm.engine.impl.form.type.EnumFormType;
import org.camunda.bpm.engine.impl.form.type.StringFormType;
/**
* A simple {@link FormEngine} implementaiton which renders
* forms as HTML such that they can be used as embedded forms
* inside camunda Tasklist.
*
* @author Daniel Meyer
*
*/
public class HtmlFormEngine implements FormEngine {
/* elements */
protected static final String FORM_ELEMENT = "form";
protected static final String DIV_ELEMENT = "div";
protected static final String SPAN_ELEMENT = "span";
protected static final String LABEL_ELEMENT = "label";
protected static final String INPUT_ELEMENT = "input";
protected static final String BUTTON_ELEMENT = "button";
protected static final String SELECT_ELEMENT = "select";
protected static final String OPTION_ELEMENT = "option";
protected static final String I_ELEMENT = "i";
protected static final String SCRIPT_ELEMENT = "script";
/* attributes */
protected static final String NAME_ATTRIBUTE = "name";
protected static final String CLASS_ATTRIBUTE = "class";
protected static final String ROLE_ATTRIBUTE = "role";
protected static final String FOR_ATTRIBUTE = "for";
protected static final String VALUE_ATTRIBUTE = "value";
protected static final String TYPE_ATTRIBUTE = "type";
protected static final String SELECTED_ATTRIBUTE = "selected";
/* datepicker attributes*/
protected static final String IS_OPEN_ATTRIBUTE = "is-open";
protected static final String DATEPICKER_POPUP_ATTRIBUTE = "uib-datepicker-popup";
/* camunda attributes */
protected static final String CAM_VARIABLE_TYPE_ATTRIBUTE = "cam-variable-type";
protected static final String CAM_VARIABLE_NAME_ATTRIBUTE = "cam-variable-name";
protected static final String CAM_SCRIPT_ATTRIBUTE = "cam-script";
protected static final String CAM_BUSINESS_KEY_ATTRIBUTE = "cam-business-key";
/* angular attributes*/
protected static final String NG_CLICK_ATTRIBUTE = "ng-click";
protected static final String NG_IF_ATTRIBUTE = "ng-if";
protected static final String NG_SHOW_ATTRIBUTE = "ng-show";
/* classes */
protected static final String FORM_GROUP_CLASS = "form-group";
protected static final String FORM_CONTROL_CLASS = "form-control";
protected static final String INPUT_GROUP_CLASS = "input-group";
protected static final String INPUT_GROUP_BTN_CLASS = "input-group-btn";
protected static final String BUTTON_DEFAULT_CLASS = "btn btn-default";
protected static final String HAS_ERROR_CLASS = "has-error";
protected static final String HELP_BLOCK_CLASS = "help-block";
/* input[type] */
protected static final String TEXT_INPUT_TYPE = "text";
protected static final String CHECKBOX_INPUT_TYPE = "checkbox";
/* button[type] */
protected static final String BUTTON_BUTTON_TYPE = "button";
/* script[type] */
protected static final String TEXT_FORM_SCRIPT_TYPE = "text/form-script";
/* glyphicons */
protected static final String CALENDAR_GLYPHICON = "glyphicon glyphicon-calendar";
/* generated form name */
protected static final String GENERATED_FORM_NAME = "generatedForm";
protected static final String FORM_ROLE = "form";
/* error types */
protected static final String REQUIRED_ERROR_TYPE = "required";
protected static final String DATE_ERROR_TYPE = "date";
/* form element selector */
protected static final String FORM_ELEMENT_SELECTOR = "this." + GENERATED_FORM_NAME + ".%s";
/* expressions */
protected static final String INVALID_EXPRESSION = FORM_ELEMENT_SELECTOR + ".$invalid";
protected static final String DIRTY_EXPRESSION = FORM_ELEMENT_SELECTOR + ".$dirty";
protected static final String ERROR_EXPRESSION = FORM_ELEMENT_SELECTOR + ".$error";
protected static final String DATE_ERROR_EXPRESSION = ERROR_EXPRESSION + ".date";
protected static final String REQUIRED_ERROR_EXPRESSION = ERROR_EXPRESSION + ".required";
protected static final String TYPE_ERROR_EXPRESSION = ERROR_EXPRESSION + ".camVariableType";
/* JavaScript snippets */
protected static final String DATE_FIELD_OPENED_ATTRIBUTE = "dateFieldOpened%s";
protected static final String OPEN_DATEPICKER_SNIPPET = "$scope.open%s = function ($event) { $event.preventDefault(); $event.stopPropagation(); $scope.dateFieldOpened%s = true; };";
protected static final String OPEN_DATEPICKER_FUNCTION_SNIPPET = "open%s($event)";
/* messages */
protected static final String REQUIRED_FIELD_MESSAGE = "Required field";
protected static final String TYPE_FIELD_MESSAGE = "Only a %s value is allowed";
protected static final String INVALID_DATE_FIELD_MESSAGE = "Invalid date format: the date should have the pattern ";
protected static final String DATE_PATTERN_ATTRIBUTE = "datePattern";
/* constraints */
public static final String CONSTRAINT_READONLY = "readonly";
public static final String CONSTRAINT_REQUIRED = "required";
public static final String CONSTRAINT_DISABLED = "disabled";
public String getName() {
return "html";
}
public Object renderStartForm(StartFormData startForm) {
return renderFormData(startForm);
}
public Object renderTaskForm(TaskFormData taskForm) {
return renderFormData(taskForm);
}
protected String renderFormData(FormData formData) {
if(formData == null
|| (formData.getFormFields() == null || formData.getFormFields().isEmpty())
&& (formData.getFormProperties() == null || formData.getFormProperties().isEmpty())) {
return null;
} else {
HtmlElementWriter formElement = new HtmlElementWriter(FORM_ELEMENT)
.attribute(NAME_ATTRIBUTE, GENERATED_FORM_NAME)
.attribute(ROLE_ATTRIBUTE, FORM_ROLE);
HtmlDocumentBuilder documentBuilder = new HtmlDocumentBuilder(formElement);
// render fields
for (FormField formField : formData.getFormFields()) {
renderFormField(formField, documentBuilder);
}
// render deprecated form properties
for (FormProperty formProperty : formData.getFormProperties()) {
renderFormField(new FormPropertyAdapter(formProperty), documentBuilder);
}
// end document element
documentBuilder.endElement();
return documentBuilder.getHtmlString();
}
}
protected void renderFormField(FormField formField, HtmlDocumentBuilder documentBuilder) {
// start group
HtmlElementWriter divElement = new HtmlElementWriter(DIV_ELEMENT)
.attribute(CLASS_ATTRIBUTE, FORM_GROUP_CLASS);
documentBuilder.startElement(divElement);
String formFieldId = formField.getId();
String formFieldLabel = formField.getLabel();
// write label
if (formFieldLabel != null && !formFieldLabel.isEmpty()) {
HtmlElementWriter labelElement = new HtmlElementWriter(LABEL_ELEMENT)
.attribute(FOR_ATTRIBUTE, formFieldId)
.textContent(formFieldLabel);
//
documentBuilder.startElement(labelElement).endElement();
}
// render form control
if(isEnum(formField)) {
//