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

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

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

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

import java.util.Map;

import static com.almworks.jira.structure.api.util.JsonMapUtil.unwrapJsonCollection;

/**
 * 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);
    value = unwrapJsonCollection(value);
    if (!(value instanceof Map)) {
      return null;
    }
    //noinspection unchecked
    return new SpecParams((Map) value);
  }

  @Nullable
  public  AttributeSpec getAttributeParameter(ValueFormat expectedFormat) {
    return getAttributeParameter(SharedAttributeSpecs.Param.ATTRIBUTE, expectedFormat);
  }

  @Nullable
  public  AttributeSpec getAttributeParameter(String name, ValueFormat expectedFormat) {
    SpecParams attributeObject = getObject(name);
    if (attributeObject == null) {
      return null;
    }
    String format = attributeObject.getString(SharedAttributeSpecs.Param.FORMAT);
    if (format != null && expectedFormat != null){
      if (!expectedFormat.getFormatId().equals(format) && !ValueFormat.ANY.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(SharedAttributeSpecs.Param.ID);
    Object params = attributeObject.get(SharedAttributeSpecs.Param.PARAMS);
    params = unwrapJsonCollection(params);
    //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 - 2024 Weber Informatics LLC | Privacy Policy