public static JSONObject convert(final PropertyDefinition<?> propertyDefinition, final DateTimeFormat dateTimeFormat) {
if (propertyDefinition == null) {
return null;
}
JSONObject result = new JSONObject();
// type specific
if (propertyDefinition instanceof PropertyStringDefinition) {
setIfNotNull(JSON_PROPERTY_TYPE_MAX_LENGTH, ((PropertyStringDefinition) propertyDefinition).getMaxLength(),
result);
} else if (propertyDefinition instanceof PropertyIdDefinition) {
// nothing to do
} else if (propertyDefinition instanceof PropertyIntegerDefinition) {
setIfNotNull(JSON_PROPERTY_TYPE_MIN_VALUE, ((PropertyIntegerDefinition) propertyDefinition).getMinValue(),
result);
setIfNotNull(JSON_PROPERTY_TYPE_MAX_VALUE, ((PropertyIntegerDefinition) propertyDefinition).getMaxValue(),
result);
} else if (propertyDefinition instanceof PropertyDecimalDefinition) {
setIfNotNull(JSON_PROPERTY_TYPE_MIN_VALUE, ((PropertyDecimalDefinition) propertyDefinition).getMinValue(),
result);
setIfNotNull(JSON_PROPERTY_TYPE_MAX_VALUE, ((PropertyDecimalDefinition) propertyDefinition).getMaxValue(),
result);
DecimalPrecision precision = ((PropertyDecimalDefinition) propertyDefinition).getPrecision();
if (precision != null) {
result.put(JSON_PROPERTY_TYPE_PRECISION, precision.value());
}
} else if (propertyDefinition instanceof PropertyBooleanDefinition) {
// nothing to do
} else if (propertyDefinition instanceof PropertyDateTimeDefinition) {
DateTimeResolution resolution = ((PropertyDateTimeDefinition) propertyDefinition).getDateTimeResolution();
if (resolution != null) {
result.put(JSON_PROPERTY_TYPE_RESOLUTION, resolution.value());
}
} else if (propertyDefinition instanceof PropertyHtmlDefinition) {
// nothing to do
} else if (propertyDefinition instanceof PropertyUriDefinition) {
// nothing to do
} else {
assert false;
}
// default value
if (propertyDefinition.getDefaultValue() != null) {
if (propertyDefinition.getCardinality() == Cardinality.SINGLE) {
if (!propertyDefinition.getDefaultValue().isEmpty()) {
result.put(JSON_PROPERTY_TYPE_DEAULT_VALUE,
getJSONValue(propertyDefinition.getDefaultValue().get(0), dateTimeFormat));
}
} else {
JSONArray values = new JSONArray();
for (Object value : propertyDefinition.getDefaultValue()) {
values.add(getJSONValue(value, dateTimeFormat));
}
result.put(JSON_PROPERTY_TYPE_DEAULT_VALUE, values);
}
}
// choices
if (isNotEmpty(propertyDefinition.getChoices())) {
result.put(
JSON_PROPERTY_TYPE_CHOICE,
convertChoices(propertyDefinition.getChoices(), propertyDefinition.getCardinality(), dateTimeFormat));
}
// generic
result.put(JSON_PROPERTY_TYPE_ID, propertyDefinition.getId());
result.put(JSON_PROPERTY_TYPE_LOCALNAME, propertyDefinition.getLocalName());
setIfNotNull(JSON_PROPERTY_TYPE_LOCALNAMESPACE, propertyDefinition.getLocalNamespace(), result);
setIfNotNull(JSON_PROPERTY_TYPE_DISPLAYNAME, propertyDefinition.getDisplayName(), result);
setIfNotNull(JSON_PROPERTY_TYPE_QUERYNAME, propertyDefinition.getQueryName(), result);
setIfNotNull(JSON_PROPERTY_TYPE_DESCRIPTION, propertyDefinition.getDescription(), result);
result.put(JSON_PROPERTY_TYPE_PROPERTY_TYPE, getJSONEnumValue(propertyDefinition.getPropertyType()));
result.put(JSON_PROPERTY_TYPE_CARDINALITY, getJSONEnumValue(propertyDefinition.getCardinality()));
result.put(JSON_PROPERTY_TYPE_UPDATABILITY, getJSONEnumValue(propertyDefinition.getUpdatability()));
setIfNotNull(JSON_PROPERTY_TYPE_INHERITED, propertyDefinition.isInherited(), result);
result.put(JSON_PROPERTY_TYPE_REQUIRED, propertyDefinition.isRequired());
result.put(JSON_PROPERTY_TYPE_QUERYABLE, propertyDefinition.isQueryable());
result.put(JSON_PROPERTY_TYPE_ORDERABLE, propertyDefinition.isOrderable());
setIfNotNull(JSON_PROPERTY_TYPE_OPENCHOICE, propertyDefinition.isOpenChoice(), result);
convertExtension(propertyDefinition, result);
return result;