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

com.almworks.jira.structure.util.SpecParams Maven / Gradle / Ivy

There is a newer version: 17.25.3
Show newest version
package com.almworks.jira.structure.util;

import com.almworks.jira.structure.api2g.attribute.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Usable by AttributeSpec and other Specs that have Map parameters.
 */
public class SpecParams extends MapObject {
  private static final Logger logger = LoggerFactory.getLogger(SpecParams.class);

  public SpecParams(Map map) {
    super(map);
  }

  @Nullable
  @Override
  public SpecParams getObject(String name) {
    Object value = myObject.opt(name);
    if (!(value instanceof Map)) {
      return null;
    }
    //noinspection unchecked
    return new SpecParams((Map) value);
  }

  @Nullable
  public  AttributeSpec getAttributeParameter(ValueFormat expectedFormat) {
    return getAttributeParameter(AttributeSpecUtil.DEFAULT_ATTRIBUTESPEC_PARAMETER_NAME, expectedFormat);
  }

  @Nullable
  public  AttributeSpec getAttributeParameter(String name, ValueFormat expectedFormat) {
    SpecParams attributeObject = getObject(name);
    if (attributeObject == null) {
      return null;
    }
    String format = attributeObject.getString(AttributeSpecUtil.DEFAULT_FORMAT_PARAMETER_NAME);
    if (format != null && expectedFormat != null && !expectedFormat.getFormatId().equals(format)) {
      return null;
    }
    if (expectedFormat == null) {
      ValueFormat standardFormat = ValueFormat.getStandardFormat(format);
      if (standardFormat != null) {
        expectedFormat = (ValueFormat) standardFormat;
      }
    }
    return buildAttributeFromParamsMap(attributeObject, expectedFormat);
  }

  @Nullable
  private  AttributeSpec buildAttributeFromParamsMap(@NotNull SpecParams attributeObject, ValueFormat expectedFormat) {
    if (expectedFormat == null) return null;
    String id = attributeObject.getString(AttributeSpecUtil.DEFAULT_ID_PARAMETER_NAME);
    Object params = attributeObject.get(AttributeSpecUtil.DEFAULT_PARAMS_PARAMETER_NAME);
    //noinspection unchecked
    Map paramsMap = params instanceof Map ? (Map) params : null;
    try {
      return new AttributeSpec(id, expectedFormat, paramsMap);
    } catch (IllegalArgumentException e) {
      logger.debug("invalid params - cannot extract AttributeSpec: " + attributeObject);
      return null;
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy