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

io.camunda.exporter.schema.IndexMapping 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.schema;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.camunda.webapps.schema.descriptors.IndexDescriptor;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public record IndexMapping(
    String indexName,
    String dynamic,
    Set properties,
    Map metaProperties) {

  public Map toMap() {
    return properties.stream()
        .collect(
            Collectors.toMap(IndexMappingProperty::name, IndexMappingProperty::typeDefinition));
  }

  public boolean isDynamic() {
    return Boolean.parseBoolean(dynamic);
  }

  @SuppressWarnings("unchecked")
  public static IndexMapping from(
      final IndexDescriptor indexDescriptor, final ObjectMapper mapper) {
    try (final var mappingsStream =
        SchemaManager.class.getResourceAsStream(indexDescriptor.getMappingsClasspathFilename())) {
      final var nestedType = new TypeReference>>() {};
      final Map mappings =
          mapper.readValue(mappingsStream, nestedType).get("mappings");
      final Map properties = (Map) mappings.get("properties");
      final var dynamic = mappings.get("dynamic");

      return new IndexMapping.Builder()
          .dynamic(dynamic == null ? "strict" : dynamic.toString())
          .properties(
              properties.entrySet().stream()
                  .map(IndexMappingProperty::createIndexMappingProperty)
                  .collect(Collectors.toSet()))
          .build();
    } catch (final IOException e) {
      throw new IllegalArgumentException(
          String.format(
              "Failed to parse index json [%s]", indexDescriptor.getMappingsClasspathFilename()),
          e);
    }
  }

  public static class Builder {
    private Set properties;
    private Map metaProperties;
    private String indexName;
    private String dynamic;

    public Builder indexName(final String indexName) {
      this.indexName = indexName;
      return this;
    }

    public Builder dynamic(final String dynamic) {
      this.dynamic = dynamic;
      return this;
    }

    public Builder properties(final Set properties) {
      this.properties = properties;
      return this;
    }

    public Builder metaProperties(final Map metaProperties) {
      this.metaProperties = metaProperties;
      return this;
    }

    public IndexMapping build() {
      return new IndexMapping(indexName, dynamic, properties, metaProperties);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy