class OperationsHelper {

    public Operation addState(Operation operation) {
        if (operation.getState() != null) {
            return operation;
        }
        Operation.State state = computeState(operation);
        // Fixes bug XSBUG-2035: Inconsistency in 'operation', 'act_hi_procinst' and 'act_ru_execution' tables
        if (operation.hasAcquiredLock() && (state.equals(Operation.State.ABORTED) || state.equals(Operation.State.FINISHED))) {
            operation = ImmutableOperation.builder()
                                          .from(operation)
                                          .hasAcquiredLock(false)
                                          .state(state)
                                          .build();
            operationService.update(operation, operation);
        }
        return ImmutableOperation.copyOf(operation)
                                 .withState(state);
    }

    @Inject public  OperationsHelper(OperationService operationService, ProcessTypeToOperationMetadataMapper metadataMapper,
                            ProcessHelper processHelper);

}