org.activiti.validation.validator
- 目录
- 概述
- 需求:
- 设计思路
- 实现思路分析
- 1.ActivitiEventListenerValidator
- 3.AssociationValidator
- 4.validateAtLeastOneExecutable
- 5.DataObjectValidator
- 拓展实现
- 参考资料和推荐阅读
Survive by day and develop by night.
talk for import biz , show your perfect code,full busy,skip hardness,make a better result,wait for change,challenge Survive.
happy for hardess to solve denpendies.
目录
概述
验证的是一个非常常见的需求。
需求:
设计思路
实现思路分析
1.ActivitiEventListenerValidator
ublic class ActivitiEventListenerValidator extends ProcessLevelValidator {
@Override
protected void executeValidation(BpmnModel bpmnModel, Process process, List<ValidationError> errors) {
List<EventListener> eventListeners = process.getEventListeners();
if (eventListeners != null) {
for (EventListener eventListener : eventListeners) {
if (eventListener.getImplementationType() != null && eventListener.getImplementationType().equals(ImplementationType.IMPLEMENTATION_TYPE_INVALID_THROW_EVENT)) {
addError(errors, Problems.EVENT_LISTENER_INVALID_THROW_EVENT_TYPE, process, eventListener, "Invalid or unsupported throw event type on event listener");
} else if (eventListener.getImplementationType() == null || eventListener.getImplementationType().length() == 0) {
addError(errors, Problems.EVENT_LISTENER_IMPLEMENTATION_MISSING, process, eventListener, "Element 'class', 'delegateExpression' or 'throwEvent' is mandatory on eventListener");
} else if (eventListener.getImplementationType() != null) {
if (!ImplementationType.IMPLEMENTATION_TYPE_CLASS.equals(eventListener.getImplementationType())
&& !ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION.equals(eventListener.getImplementationType())
&& !ImplementationType.IMPLEMENTATION_TYPE_THROW_SIGNAL_EVENT.equals(eventListener.getImplementationType())
&& !ImplementationType.IMPLEMENTATION_TYPE_THROW_GLOBAL_SIGNAL_EVENT.equals(eventListener.getImplementationType())
&& !ImplementationType.IMPLEMENTATION_TYPE_THROW_MESSAGE_EVENT.equals(eventListener.getImplementationType())
&& !ImplementationType.IMPLEMENTATION_TYPE_THROW_ERROR_EVENT.equals(eventListener.getImplementationType())) {
addError(errors, Problems.EVENT_LISTENER_INVALID_IMPLEMENTATION, process, eventListener, "Unsupported implementation type for event listener");
}
}
}
}
}
3.AssociationValidator
public class AssociationValidator extends ValidatorImpl {
@Override
public void validate(BpmnModel bpmnModel, List<ValidationError> errors) {
// Global associations
Collection<Artifact> artifacts = bpmnModel.getGlobalArtifacts();
if (artifacts != null) {
for (Artifact artifact : artifacts) {
if (artifact instanceof Association) {
validate(null, (Association) artifact, errors);
}
}
}
// Process associations
for (Process process : bpmnModel.getProcesses()) {
artifacts = process.getArtifacts();
for (Artifact artifact : artifacts) {
if (artifact instanceof Association) {
validate(process, (Association) artifact, errors);
}
}
}
}
protected void validate(Process process, Association association, List<ValidationError> errors) {
if (StringUtils.isEmpty(association.getSourceRef())) {
addError(errors, Problems.ASSOCIATION_INVALID_SOURCE_REFERENCE, process, association, "association element missing attribute 'sourceRef'");
}
if (StringUtils.isEmpty(association.getTargetRef())) {
addError(errors, Problems.ASSOCIATION_INVALID_TARGET_REFERENCE, process, association, "association element missing attribute 'targetRef'");
}
}
4.validateAtLeastOneExecutable
* Returns 'true' if at least one process definition in the {@link BpmnModel} is executable.
*/
protected boolean validateAtLeastOneExecutable(BpmnModel bpmnModel, List<ValidationError> errors) {
int nrOfExecutableDefinitions = 0;
for (Process process : bpmnModel.getProcesses()) {
if (process.isExecutable()) {
nrOfExecutableDefinitions++;
}
}
if (nrOfExecutableDefinitions == 0) {
addError(errors, Problems.ALL_PROCESS_DEFINITIONS_NOT_EXECUTABLE,
"All process definition are set to be non-executable (property 'isExecutable' on process). This is not allowed.");
}
return nrOfExecutableDefinitions > 0;
}
protected List<Process> getProcessesWithSameId(final List<Process> processes) {
List<Process> filteredProcesses = processes.stream()
.filter(process -> process.getName() != null).collect(Collectors.toList());
return getDuplicatesMap(filteredProcesses).values().stream()
.filter(duplicates -> duplicates.size() > 1)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}
private static Map<String, List<Process>> getDuplicatesMap(List<Process> processes) {
return processes.stream().collect(Collectors.groupingBy(Process::getId));
}
5.DataObjectValidator
*/
public class DataObjectValidator extends ProcessLevelValidator {
@Override
protected void executeValidation(BpmnModel bpmnModel, Process process, List<ValidationError> errors) {
// Gather data objects
List<ValuedDataObject> allDataObjects = new ArrayList<ValuedDataObject>();
allDataObjects.addAll(process.getDataObjects());
List<SubProcess> subProcesses = process.findFlowElementsOfType(SubProcess.class, true);
for (SubProcess subProcess : subProcesses) {
allDataObjects.addAll(subProcess.getDataObjects());
}
// Validate
for (ValuedDataObject dataObject : allDataObjects) {
if (StringUtils.isEmpty(dataObject.getName())) {
addError(errors, Problems.DATA_OBJECT_MISSING_NAME, process, dataObject, "Name is mandatory for a data object");
}
}
}
}
拓展实现
public class EventValidator extends ProcessLevelValidator {
@Override
protected void executeValidation(BpmnModel bpmnModel, Process process, List<ValidationError> errors) {
List<Event> events = process.findFlowElementsOfType(Event.class);
for (Event event : events) {
if (event.getEventDefinitions() != null) {
for (EventDefinition eventDefinition : event.getEventDefinitions()) {
if (eventDefinition instanceof MessageEventDefinition) {
handleMessageEventDefinition(bpmnModel, process, event, eventDefinition, errors);
} else if (eventDefinition instanceof SignalEventDefinition) {
handleSignalEventDefinition(bpmnModel, process, event, eventDefinition, errors);
} else if (eventDefinition instanceof TimerEventDefinition) {
handleTimerEventDefinition(process, event, eventDefinition, errors);
} else if (eventDefinition instanceof CompensateEventDefinition) {
handleCompensationEventDefinition(bpmnModel, process, event, eventDefinition, errors);
}
}
}
}
}
protected void handleMessageEventDefinition(BpmnModel bpmnModel, Process process, Event event, EventDefinition eventDefinition, List<ValidationError> errors) {
MessageEventDefinition messageEventDefinition = (MessageEventDefinition) eventDefinition;
if (StringUtils.isEmpty(messageEventDefinition.getMessageRef())) {
if (StringUtils.isEmpty(messageEventDefinition.getMessageExpression())) {
// message ref should be filled in
addError(errors, Problems.MESSAGE_EVENT_MISSING_MESSAGE_REF, process, event, "attribute 'messageRef' is required");
}
} else if (!bpmnModel.containsMessageId(messageEventDefinition.getMessageRef())) {
// message ref should exist
addError(errors, Problems.MESSAGE_EVENT_INVALID_MESSAGE_REF, process, event, "Invalid 'messageRef': no message with that id can be found in the model");
}
}
参考资料和推荐阅读
[1]. https://www.activiti.org/
欢迎阅读,各位老铁,如果对你有帮助,点个赞加个关注呗!~