All files / src/web/flowable/roundTrip elementStateExtraction.ts

100% Statements 75/75
95.94% Branches 71/74
100% Functions 6/6
100% Lines 75/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188                                                  4x     6338x                                                         6338x 14902x 6509x     8393x 8393x   14902x     14902x 719x 719x     7674x 72x           363x 207x 207x     156x 6x 6x     150x 35x 35x     115x 6x 6x     109x 6x 6x     103x 3x 3x     100x 10x 10x 10x     90x 14x 14x 14x 14x             14x     76x       6338x 6338x 6085x     253x 601x 238x     363x 287x     76x         6338x 6338x 6338x   6338x 6338x 10x     6338x 6338x 48x     6338x 6338x 6338x 6338x 6338x 6338x 6338x       12014x 12014x 5676x     6338x 6338x 6338x 6338x 6338x  
import type { Element as XmlElement } from '@xmldom/xmldom';
import {
	FLOWABLE_ATTRIBUTE_KEYS,
	type FlowableAttributeKey,
	type FlowableDocumentState,
	type FlowableElementState,
} from '../types';
import {
	buildXmlIdentity,
	findDirectChild,
	getActivitiAttribute,
	getElementChildren,
	isActivitiElement,
	serializer,
} from './xmlUtils';
import {
	parseFieldExtension,
	parseFormProperty,
	parseIOParameter,
	parseListener,
	parseMultiInstance,
	parseTimerDefinition,
} from './xmlParsers';
import { ACTIVITI_NAMESPACE } from './constants';
 
const editableActivitiAttributeKeys = new Set<FlowableAttributeKey>(FLOWABLE_ATTRIBUTE_KEYS);
 
function createElementState(id: string, type: string): FlowableElementState {
	return {
		id,
		type,
		activitiAttributes: {},
		fieldExtensions: [],
		taskListeners: [],
		executionListeners: [],
		formProperties: [],
		inputParameters: [],
		outputParameters: [],
		multiInstance: null,
		conditionExpression: '',
		script: '',
		timerDefinition: null,
		errorRef: '',
		signalRef: '',
		messageRef: '',
		terminateAll: '',
		compensateActivityRef: '',
		isForCompensation: '',
		failedJobRetryTimeCycle: '',
		exceptionMaps: [],
		documentation: '',
		preservedAttributes: {},
		preservedExtensionElements: [],
	};
}
 
function extractEditableAttributes(node: XmlElement, elementState: FlowableElementState): void {
	for (const attribute of Array.from(node.attributes)) {
		if (attribute.name.startsWith('xmlns:') || attribute.name === 'id') {
			continue;
		}
 
		const attributeKey = attribute.localName as FlowableAttributeKey;
		const isManagedNamespacedAttribute = (attribute.namespaceURI || '') === ACTIVITI_NAMESPACE
			&& editableActivitiAttributeKeys.has(attributeKey);
		const isManagedLegacyAttribute = !attribute.name.includes(':')
			&& editableActivitiAttributeKeys.has(attribute.name as FlowableAttributeKey);
 
		if (isManagedNamespacedAttribute || isManagedLegacyAttribute) {
			elementState.activitiAttributes[isManagedLegacyAttribute ? attribute.name as FlowableAttributeKey : attributeKey] = attribute.value;
			continue;
		}
 
		if (attribute.name.includes(':')) {
			elementState.preservedAttributes[attribute.name] = attribute.value;
		}
	}
}
 
function extractManagedExtensionChild(child: XmlElement, elementState: FlowableElementState): boolean {
	if (isActivitiElement(child, 'field')) {
		elementState.fieldExtensions.push(parseFieldExtension(child));
		return true;
	}
 
	if (isActivitiElement(child, 'taskListener')) {
		elementState.taskListeners.push(parseListener(child));
		return true;
	}
 
	if (isActivitiElement(child, 'executionListener')) {
		elementState.executionListeners.push(parseListener(child));
		return true;
	}
 
	if (isActivitiElement(child, 'formProperty')) {
		elementState.formProperties.push(parseFormProperty(child));
		return true;
	}
 
	if (isActivitiElement(child, 'in')) {
		elementState.inputParameters.push(parseIOParameter(child));
		return true;
	}
 
	if (isActivitiElement(child, 'out')) {
		elementState.outputParameters.push(parseIOParameter(child));
		return true;
	}
 
	if (isActivitiElement(child, 'failedJobRetryTimeCycle')) {
		const retryElement = child as XmlElement;
		elementState.failedJobRetryTimeCycle = retryElement.textContent || '';
		return true;
	}
 
	if (isActivitiElement(child, 'mapException')) {
		const mapExceptionElement = child as XmlElement;
		const errorCode = mapExceptionElement.getAttribute('errorCode') || '';
		const className = mapExceptionElement.textContent || '';
		elementState.exceptionMaps.push({
			id: `exception-${errorCode}-${className}`,
			errorCode,
			className,
			includeChildExceptions: mapExceptionElement.getAttribute('includeChildExceptions') === 'true',
			xmlIdentity: buildXmlIdentity('exceptionMap', mapExceptionElement),
		});
		return true;
	}
 
	return false;
}
 
function extractExtensionElements(node: XmlElement, elementState: FlowableElementState): void {
	const extensionElements = findDirectChild(node, 'extensionElements');
	if (!extensionElements) {
		return;
	}
 
	for (const child of getElementChildren(extensionElements)) {
		if (isActivitiElement(child, 'eventListener') || isActivitiElement(child, 'localization')) {
			continue;
		}
 
		if (extractManagedExtensionChild(child, elementState)) {
			continue;
		}
 
		elementState.preservedExtensionElements.push(serializer.serializeToString(child));
	}
}
 
function extractDirectChildState(node: XmlElement, elementState: FlowableElementState): void {
	elementState.documentation = findDirectChild(node, 'documentation')?.textContent || '';
	elementState.conditionExpression = findDirectChild(node, 'conditionExpression')?.textContent || '';
	elementState.script = findDirectChild(node, 'script')?.textContent || '';
 
	const multiInstanceElement = findDirectChild(node, 'multiInstanceLoopCharacteristics');
	if (multiInstanceElement) {
		elementState.multiInstance = parseMultiInstance(multiInstanceElement);
	}
 
	const timerEventDefinitionElement = findDirectChild(node, 'timerEventDefinition');
	if (timerEventDefinitionElement) {
		elementState.timerDefinition = parseTimerDefinition(timerEventDefinitionElement);
	}
 
	elementState.errorRef = findDirectChild(node, 'errorEventDefinition')?.getAttribute('errorRef') || '';
	elementState.signalRef = findDirectChild(node, 'signalEventDefinition')?.getAttribute('signalRef') || '';
	elementState.messageRef = findDirectChild(node, 'messageEventDefinition')?.getAttribute('messageRef') || '';
	const terminateEventDefinitionElement = findDirectChild(node, 'terminateEventDefinition');
	elementState.terminateAll = terminateEventDefinitionElement ? getActivitiAttribute(terminateEventDefinitionElement, 'terminateAll') || '' : '';
	elementState.compensateActivityRef = findDirectChild(node, 'compensateEventDefinition')?.getAttribute('activityRef') || '';
	elementState.isForCompensation = node.getAttribute('isForCompensation') || '';
}
 
export function extractElementState(node: XmlElement, documentState: FlowableDocumentState): void {
	const id = node.getAttribute('id');
	if (!id) {
		return;
	}
 
	const elementState = createElementState(id, node.nodeName);
	extractEditableAttributes(node, elementState);
	extractExtensionElements(node, elementState);
	extractDirectChildState(node, elementState);
	documentState.elements[id] = elementState;
}