Beispiel #1
0
  private static Date convertDate(Attribute attr, Object value) {
    if (value instanceof Date) {
      return (Date) value;
    }

    if (value == null) {
      return null;
    }

    // try to convert value
    Date dateValue;
    if (value instanceof String) {
      String paramStrValue = (String) value;
      try {
        dateValue = getDateFormat().parse(paramStrValue);
      } catch (ParseException e) {
        throw new MolgenisValidationException(
            new ConstraintViolation(
                format(
                    "Attribute [%s] value [%s] does not match date format [%s]",
                    attr.getName(),
                    paramStrValue,
                    MolgenisDateFormat.getDateFormat().toPattern())));
      }
    } else {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Attribute [%s] value is of type [%s] instead of [%s]",
                  attr.getName(), value.getClass().getSimpleName(), String.class.getSimpleName())));
    }
    return dateValue;
  }
Beispiel #2
0
  private static Boolean convertBool(Attribute attr, Object value) {
    if (value instanceof Boolean) {
      return (Boolean) value;
    }

    if (value == null) {
      return null;
    }

    Boolean booleanValue;
    if (value instanceof String) {
      String stringValue = (String) value;
      if (stringValue.equalsIgnoreCase(TRUE.toString())) {
        booleanValue = true;
      } else if (stringValue.equalsIgnoreCase(FALSE.toString())) {
        booleanValue = false;
      } else {
        throw new MolgenisValidationException(
            new ConstraintViolation(
                format(
                    "Attribute [%s] value [%s] cannot be converter to type [%s]",
                    attr.getName(), value, Boolean.class.getSimpleName())));
      }
    } else {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Attribute [%s] value is of type [%s] instead of [%s] or [%s]",
                  attr.getName(),
                  value.getClass().getSimpleName(),
                  String.class.getSimpleName(),
                  Boolean.class.getSimpleName())));
    }
    return booleanValue;
  }
Beispiel #3
0
  private static String convertEnum(Attribute attr, Object value) {
    if (value == null) {
      return null;
    }

    String stringValue;
    if (value instanceof String) {
      stringValue = (String) value;
    } else if (value instanceof Enum) {
      stringValue = value.toString();
    } else {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Attribute [%s] value is of type [%s] instead of [%s] or [%s]",
                  attr.getName(),
                  value.getClass().getSimpleName(),
                  String.class.getSimpleName(),
                  Enum.class.getSimpleName())));
    }

    if (!attr.getEnumOptions().contains(stringValue)) {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Attribute [%s] value [%s] is not a valid enum option",
                  attr.getName(), stringValue)));
    }

    return stringValue;
  }
Beispiel #4
0
  private static Double convertDecimal(Attribute attr, Object value) {
    if (value instanceof Double) {
      return (Double) value;
    }

    if (value == null) {
      return null;
    }

    // try to convert value
    Double doubleValue;
    if (value instanceof String) {
      try {
        doubleValue = Double.valueOf((String) value);
      } catch (NumberFormatException e) {
        throw new MolgenisValidationException(
            new ConstraintViolation(
                format(
                    "Attribute [%s] value [%s] cannot be converter to type [%s]",
                    attr.getName(), value, Double.class.getSimpleName())));
      }
    } else if (value instanceof Number) {
      doubleValue = ((Number) value).doubleValue();
    } else {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Attribute [%s] value is of type [%s] instead of [%s] or [%s]",
                  attr.getName(),
                  value.getClass().getSimpleName(),
                  String.class.getSimpleName(),
                  Number.class.getSimpleName())));
    }
    return doubleValue;
  }
Beispiel #5
0
  private static Integer convertInt(Attribute attr, Object value) {
    if (value instanceof Integer) {
      return (Integer) value;
    }

    if (value == null) {
      return null;
    }

    // try to convert value
    Integer integerValue;
    if (value instanceof String) {
      try {
        integerValue = Integer.valueOf((String) value);
      } catch (NumberFormatException e) {
        throw new MolgenisValidationException(
            new ConstraintViolation(
                format(
                    "Attribute [%s] value [%s] cannot be converter to type [%s]",
                    attr.getName(), value, Integer.class.getSimpleName())));
      }
    } else if (value instanceof Number) {
      integerValue = ((Number) value).intValue();
    } else {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Attribute [%s] value is of type [%s] instead of [%s] or [%s]",
                  attr.getName(),
                  value.getClass().getSimpleName(),
                  String.class.getSimpleName(),
                  Number.class.getSimpleName())));
    }
    return integerValue;
  }
 @Override
 public void setIdValue(Object id) {
   Attribute idAttr = entityType.getIdAttribute();
   if (idAttr == null) {
     throw new IllegalArgumentException(
         format("Entity [%s] doesn't have an id attribute", entityType.getName()));
   }
   set(idAttr.getName(), id);
 }
Beispiel #7
0
 private Object toQueryRuleValue(Object queryRuleValue, Attribute attr) {
   Object value;
   AttributeType attrType = attr.getDataType();
   switch (attrType) {
     case BOOL:
       value = convertBool(attr, queryRuleValue);
       break;
     case EMAIL:
     case HTML:
     case HYPERLINK:
     case SCRIPT:
     case STRING:
     case TEXT:
       value = convertString(attr, queryRuleValue);
       break;
     case ENUM:
       value = convertEnum(attr, queryRuleValue);
       break;
     case CATEGORICAL:
     case XREF:
     case CATEGORICAL_MREF:
     case MREF:
     case ONE_TO_MANY:
       value = convertRef(attr, queryRuleValue);
       break;
     case DATE:
       value = convertDate(attr, queryRuleValue);
       break;
     case DATE_TIME:
       value = convertDateTime(attr, queryRuleValue);
       break;
     case DECIMAL:
       value = convertDecimal(attr, queryRuleValue);
       break;
     case FILE:
       value = convertFile(attr, queryRuleValue);
       break;
     case INT:
       value = convertInt(attr, queryRuleValue);
       break;
     case LONG:
       value = convertLong(attr, queryRuleValue);
       break;
     case COMPOUND:
       throw new MolgenisValidationException(
           new ConstraintViolation(
               format(
                   "Attribute [%s] type [%s] is not allowed",
                   attr.getName(), attrType.toString())));
     default:
       throw new RuntimeException(format("Unknown attribute type [%s]", attrType.toString()));
   }
   return value;
 }
 Entity getDefaultSettings() {
   Entity defaultSettingsEntity = new DynamicEntity(this);
   for (Attribute attr : this.getAtomicAttributes()) {
     // default values are stored/retrieved as strings, so we convert them to the required type
     // here.
     String defaultValue = attr.getDefaultValue();
     if (defaultValue != null) {
       Object typedDefaultValue = getTypedValue(defaultValue, attr, entityManager);
       defaultSettingsEntity.set(attr.getName(), typedDefaultValue);
     }
   }
   return defaultSettingsEntity;
 }
Beispiel #9
0
 private FileMeta convertFile(Attribute attr, Object paramValue) {
   Entity entity = convertRef(attr, paramValue);
   if (entity == null) {
     return null;
   }
   if (!(entity instanceof FileMeta)) {
     throw new MolgenisValidationException(
         new ConstraintViolation(
             format(
                 "Attribute [%s] value is of type [%s] instead of [%s]",
                 attr.getName(),
                 entity.getClass().getSimpleName(),
                 FileMeta.class.getSimpleName())));
   }
   return (FileMeta) entity;
 }
 @Override
 public Object getLabelValue() {
   // abstract entities might not have an label attribute
   Attribute labelAttr = entityType.getLabelAttribute();
   return labelAttr != null ? get(labelAttr.getName()) : null;
 }
 @Override
 public Object getIdValue() {
   // abstract entities might not have an id attribute
   Attribute idAttr = entityType.getIdAttribute();
   return idAttr != null ? get(idAttr.getName()) : null;
 }