All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.camunda.exporter.handlers.UserTaskVariableHandler Maven / Gradle / Ivy

There is a newer version: 8.7.0-alpha2
Show newest version
/*
 * 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.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
package io.camunda.exporter.handlers;

import io.camunda.exporter.store.BatchRequest;
import io.camunda.webapps.schema.descriptors.tasklist.template.TaskTemplate;
import io.camunda.webapps.schema.entities.tasklist.TaskJoinRelationship;
import io.camunda.webapps.schema.entities.tasklist.TaskJoinRelationship.TaskJoinRelationshipType;
import io.camunda.webapps.schema.entities.tasklist.TaskVariableEntity;
import io.camunda.zeebe.protocol.record.Record;
import io.camunda.zeebe.protocol.record.ValueType;
import io.camunda.zeebe.protocol.record.intent.VariableIntent;
import io.camunda.zeebe.protocol.record.value.VariableRecordValue;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserTaskVariableHandler
    implements ExportHandler {

  private static final Logger LOG = LoggerFactory.getLogger(UserTaskVariableHandler.class);

  private static final String ID_PATTERN = "%s-%s";
  protected final int variableSizeThreshold;
  private final String indexName;

  public UserTaskVariableHandler(final String indexName, final int variableSizeThreshold) {
    this.indexName = indexName;
    this.variableSizeThreshold = variableSizeThreshold;
  }

  @Override
  public ValueType getHandledValueType() {
    return ValueType.VARIABLE;
  }

  @Override
  public Class getEntityType() {
    return TaskVariableEntity.class;
  }

  @Override
  public boolean handlesRecord(final Record record) {
    return !VariableIntent.MIGRATED.equals(record.getIntent());
  }

  @Override
  public List generateIds(final Record record) {
    return List.of(
        ID_PATTERN.formatted(record.getValue().getScopeKey(), record.getValue().getName()));
  }

  @Override
  public TaskVariableEntity createNewEntity(final String id) {
    return new TaskVariableEntity().setId(id);
  }

  @Override
  public void updateEntity(
      final Record record, final TaskVariableEntity entity) {
    entity
        .setPartitionId(record.getPartitionId())
        .setPosition(record.getPosition())
        .setTenantId(record.getValue().getTenantId())
        .setKey(record.getKey())
        .setProcessInstanceId(record.getValue().getProcessInstanceKey())
        .setScopeKey(record.getValue().getScopeKey())
        .setName(record.getValue().getName());

    if (record.getValue().getValue().length() > variableSizeThreshold) {
      entity.setValue(record.getValue().getValue().substring(0, variableSizeThreshold));
      entity.setFullValue(record.getValue().getValue());
      entity.setIsTruncated(true);
    } else {
      entity.setValue(record.getValue().getValue());
      entity.setFullValue(null);
      entity.setIsTruncated(false);
    }

    final TaskJoinRelationship joinRelationship = new TaskJoinRelationship();
    joinRelationship.setParent(entity.getScopeKey());
    joinRelationship.setName(
        // Whether it's a process or a task variable
        Objects.equals(entity.getProcessInstanceId(), entity.getScopeKey())
            ? TaskJoinRelationshipType.PROCESS_VARIABLE.getType()
            : TaskJoinRelationshipType.TASK_VARIABLE.getType());
    entity.setJoin(joinRelationship);
  }

  @Override
  public void flush(final TaskVariableEntity entity, final BatchRequest batchRequest) {
    final Map updateFields = new HashMap<>();

    updateFields.put(TaskTemplate.VARIABLE_VALUE, entity.getValue());
    updateFields.put(TaskTemplate.VARIABLE_FULL_VALUE, entity.getFullValue());
    updateFields.put(TaskTemplate.IS_TRUNCATED, entity.getIsTruncated());

    batchRequest.upsertWithRouting(
        indexName, entity.getId(), entity, updateFields, String.valueOf(entity.getScopeKey()));
  }

  @Override
  public String getIndexName() {
    return indexName;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy