package ru.micord.ervu.component.field;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import ru.micord.ervu.component.formatter.FieldFormatter;
import model.FieldData;

import ru.cg.webbpm.modules.standard_annotations.validation.NotNull;

/**
 * @author gulnaz
 */
public class SubpoenaFieldLoadComponent extends BaseFieldLoadComponent<Map<String, Object>> {

  @NotNull
  public String fieldId;

  public FieldFormatter<Object, Object>[] formatters;

  @Override
  public FieldData loadFrom(Map<String, Object> dto) {
    AtomicReference<Object> valueRef = new AtomicReference<>(dto.get(fieldId));

    if (formatters != null) {
      Arrays.stream(formatters).forEach(formatter -> {
        valueRef.set(formatter.format(valueRef.get()));
      });
    }
    return new FieldData(getObjectId(), valueRef.get());
  }
}
