This is the 7th day of my participation in Gwen Challenge
Forms in Activiti
- Activiti provides a convenient and flexible way to manually add forms to a business process
- Forms are supported in two ways:
- Render the built-in form through form attributes
- Render external forms through form attributes
The form properties
- All information associated with a business process:
- Contains its own process variables
- By reference to process variables
- Activiti supports storing complex Java objects as process variables:
- Serialized object
- Jpa entity object
- The entire XML document as a string
- Users interact with processes when they start a process and complete user tasks
- Forms need to be rendered by some UI technology before they can interact with the user
- To make it easy to use different UI technologies, the process definition contains a conversion logic for complex Java-type objects in the process variable to a Map
,string>
type of properties
- Use the Activiti API methods to view exposed property information. Any UI technique can then build a form on top of these attributes. This property provides a view specifically for process variables. The attributes that the form needs to display can be obtained from the return value FormData:
StartFormData FormService.getStartFormData(String processDefinitionId)
Copy the code
or
TaskFormdata FormService.getTaskFormData(String taskId)
Copy the code
- By default, the built-in forms engine encounters these variables just like it treats process variables. If task-form attributes and process variables are one-to-one, then task-form attributes do not need to be declared:
<startEvent id="start" />
Copy the code
- All process variables are available when executing to the start event, but
formService.getStartFormData(String processDefinitionId).getFormProperties()
Copy the code
Will be a null value because a specific mapping is not defined
- All submitted attributes in the form will be stored as process variables in the database used by Activiti. This means that adding a simple input field to a form will also be stored as a new variable
- Attributes come from process variables, but do not have to be stored as process variables:
- A process variable may be a JPA entity such as the Address class. StreetName, a form attribute used in a UI technique, may be associated with an expression #{address.street}
- User-submitted form attributes should be stored as process variables
- Store it as a nested property of a process variable using a UEL value expression
- The default behavior of submitted form properties is to store them as process variables, unless a formProperty declares other rules
- Type conversions can also be applied to processing between form data and process variables:
<userTask id="task">
<extensionElements>
<activiti:formProperty id="room" />
<activiti:formProperty id="duration" type="long"/>
<activiti:formProperty id="speaker" variable="SpeakerName" writable="false" />
<activiti:formProperty id="street" expression="#{address.street}" required="true" />
</extensionElements>
</userTask>
Copy the code
- The form property room will be mapped to the String process variable room
- The form attribute duration will be mapped to the java.lang.Long process variable duration
- The form propertiesspeakerWill be mapped to process variablesSpeakerName:
- Writable =”false” can only be used in TaskFormData objects. If the speaker property is submitted, a ActivitiException exception is thrown
- Readable =”false” this attribute will be removed from FormData, but will still be processed after it is committed
- The form propertiesstreetIt’s going to map to thetaJava Bean addressThe properties of thestreetAs a process variable of type String:
- When the submitted form attribute is not provided and required=”true”, an exception is thrown
- FormData can also provide type metadata as part of FormData. The FormData can be obtained from the return value of the following methods:
StartFormData FormService.getStartFormData(String processDefinitionId)
Copy the code
TaskFormdata FormService.getTaskFormData(String taskId)
Copy the code
- Form attribute types:
- string: org.activiti.engine.impl.form.StringFormType
- long: org.activiti.engine.impl.form.LongFormType
- enum: org.activiti.engine.impl.form.EnumFormType
- date: org.activiti.engine.impl.form.DateFormType
- boolean: org.activiti.engine.impl.form.BooleanFormType
- For each form attribute declared,FormProperty information can be obtained as follows:
List<FormProperty> formService.getStartFormData(String processDefinitionId).getFormProperties()
Copy the code
or
List<FormProperty> formService.getTaskFormData(String taskId).getFormProperties()
Copy the code
public interface FormProperty {
/**
the key used to submit the property in {@link FormService#submitStartFormData(String, java.util.Map)}
* or {@link FormService#submitTaskFormData(String, java.util.Map)} */
String getId(a);
/** the display label */
String getName(a);
/** one of the types defined in this interface like e.g. {@link #TYPE_STRING} */
FormType getType(a);
/** optional value that should be used to display in this property */
String getValue(a);
/** is this property read to be displayed in the form and made accessible with the methods
* {@link FormService#getStartFormData(String)} and {@link FormService#getTaskFormData(String)}. */
boolean isReadable(a);
/** is this property expected when a user submits the form? * /
boolean isWritable(a);
/** is this property a required input field */
boolean isRequired(a);
}
Copy the code
- Example:
<startEvent id="start">
<extensionElements>
<activiti:formProperty id="speaker"
name="Speaker"
variable="SpeakerName"
type="string" />
<activiti:formProperty id="start"
type="date"
datePattern="dd-MMM-yyyy" />
<activiti:formProperty id="direction" type="enum">
<activiti:value id="left" name="Go Left" />
<activiti:value id="right" name="Go Right" />
<activiti:value id="up" name="Go Up" />
<activiti:value id="down" name="Go Down" />
</activiti:formProperty>
</extensionElements>
</startEvent>
Copy the code
- All form attribute information is accessible through the API:
- Formproperty.gettype ().getName(): Gets the name of the type
- Formproperty.gettype ().getInformation(“datePattern”): Gets the date matching method
- Formproperty.gettype ().getInformation(“values”): Enumeration values can be obtained
- The Activiti console supports form attributes and can render forms based on form definitions:
<startEvent . >
<extensionElements>
<activiti:formProperty id="numberOfDays" name="Number of days" value="${numberOfDays}" type="long" required="true"/>
<activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" value="${startDate}" datePattern="dd-MM-yyyy hh:mm" type="date" required="true" />
<activiti:formProperty id="vacationMotivation" name="Motivation" value="${vacationMotivation}" type="string" />
</extensionElements>
</userTask>
Copy the code
When using the Activiti console, it is rendered as a startup form for the process
External form rendering
- The API in Activiti allows you to render task forms in ways other than the Activiti process engine, and you can render task forms in a custom way
- All form attributes that need to be rendered are performedassemblyThere are two methods of service:
- StartFormData FormService.getStartFormData(String processDefinitionId)
- TaskFormdata FormService.getTaskFormData(String taskId)
- The form propertiessubmitIn two ways:
- ProcessInstance FormService.submitStartFormData(String processDefinitionId, Map<String,String> properties)
- void FormService.submitStartFormData(String taskId, Map<String,String> properties)
- You can put any form template resource into the business document you want to deploy (if you want to store it according to the version of the process). Will be used as a resource in the deployment
- There are two ways to get a deployment form template:
- String ProcessDefinition.getDeploymentId()
- InputStream RepositoryService.getResourceAsStream(String deploymentId, String resourceName)
- This way you can get the form template definition file, and you can render or display the form in your application
- You can also use this feature to retrieve deployment resources other than the task form for other purposes
- attribute<userTask activiti:formKey=”…”Exposure mode API:
- String FormService.getStartFormData(String processDefinitionId).getFormKey()
- String FormService.getTaskFormData(String taskId).getFormKey()
- You can use the full name in the template for the storage deployment (for exampleorg/activiti/example/form/my-custom-form.xml)But it doesn’t have to be:
- You can store a generic key in a form property and then use an algorithm or substitution to get the template you actually use
- It is more convenient when you need to render different forms using different UI techniques:
- A form that uses a normal screen size Web application
- Mobile phone small screen form
- IM form
- Email form template