use of com.manydesigns.elements.MapKeyValueAccessor in project Portofino by ManyDesigns.
the class TablesAction method saveColumn.
@Path("{db}/{schema}/{table}/{column}")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void saveColumn(@PathParam("db") String db, @PathParam("schema") String schema, @PathParam("table") String tableName, @PathParam("column") String columnName, ColumnAndAnnotations column) throws Exception {
Column existing = DatabaseLogic.findColumnByName(persistence.getModel(), db, schema, tableName, columnName);
if (existing == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
Table table = existing.getTable();
Class<?> type = getColumnType(existing, existing.getJavaType());
Form annotationsForm = new FormBuilder(getApplicableAnnotations(type)).build();
MapKeyValueAccessor annotationsAccessor = new MapKeyValueAccessor(column.getAnnotations());
annotationsForm.readFrom(annotationsAccessor);
if (annotationsForm.validate()) {
BeanUtils.copyProperties(column.getColumn(), existing);
existing.setTable(table);
existing.getAnnotations().removeIf(a -> KNOWN_ANNOTATIONS.contains(a.getType()));
Set<Map.Entry<String, Object>> set = column.getAnnotations().entrySet();
set.forEach(e -> {
if (e.getValue() == null) {
return;
}
Annotation a;
String value = e.getValue().toString();
if (StringUtils.isBlank(value)) {
return;
}
switch(e.getKey()) {
case "dateFormat":
try {
new SimpleDateFormat(value);
} catch (IllegalArgumentException ex) {
logger.error("Invalid date format: " + value, ex);
// TODO I18n
RequestMessages.addErrorMessage("Invalid date format: " + value);
break;
}
a = new Annotation(DATE_FORMAT);
a.getProperties().add(new Property("value", value));
existing.getAnnotations().add(a);
break;
case "decimalFormat":
try {
new java.text.DecimalFormat(value);
} catch (IllegalArgumentException ex) {
logger.error("Invalid decimal format: " + value, ex);
// TODO I18n
RequestMessages.addErrorMessage("Invalid decimal format: " + value);
break;
}
a = new Annotation(DECIMAL_FORMAT);
a.getProperties().add(new Property("value", value));
existing.getAnnotations().add(a);
break;
case "fieldSize":
a = new Annotation(FIELD_SIZE);
a.getProperties().add(new Property("value", value));
existing.getAnnotations().add(a);
break;
case "fileBlob":
if (Boolean.TRUE.equals(e.getValue())) {
a = new Annotation(FILE_BLOB);
existing.getAnnotations().add(a);
}
break;
case "databaseBlobContentTypeProperty":
Annotation databaseBlobAnn1 = existing.ensureAnnotation(DATABASE_BLOB);
databaseBlobAnn1.setProperty("contentTypeProperty", value);
break;
case "databaseBlobFileNameProperty":
Annotation databaseBlobAnn2 = existing.ensureAnnotation(DATABASE_BLOB);
databaseBlobAnn2.setProperty("fileNameProperty", value);
break;
case "databaseBlobTimestampProperty":
Annotation databaseBlobAnn3 = existing.ensureAnnotation(DATABASE_BLOB);
databaseBlobAnn3.setProperty("timestampProperty", value);
break;
case "highlightLinks":
a = new Annotation(HIGHLIGHT_LINKS);
a.getProperties().add(new Property("value", value));
existing.getAnnotations().add(a);
break;
case "minValue":
if (type == Integer.class || type == Long.class || type == BigInteger.class) {
a = new Annotation(MIN_INT_VALUE);
} else {
a = new Annotation(MIN_DECIMAL_VALUE);
}
a.getProperties().add(new Property("value", value));
existing.getAnnotations().add(a);
break;
case "maxValue":
if (type == Integer.class || type == Long.class || type == BigInteger.class) {
a = new Annotation(MAX_INT_VALUE);
} else {
a = new Annotation(MAX_DECIMAL_VALUE);
}
a.getProperties().add(new Property("value", value));
existing.getAnnotations().add(a);
break;
case "regexp":
a = new Annotation(REGEXP);
a.getProperties().add(new Property("value", value));
// Default error message
a.getProperties().add(new Property("errorMessage", "elements.error.field.regexp.format"));
existing.getAnnotations().add(a);
break;
case "stringFormat":
a = new Annotation(((Map) e.getValue()).get("v").toString());
existing.getAnnotations().add(a);
break;
case "typeOfContent":
a = new Annotation(((Map) e.getValue()).get("v").toString());
a.getProperties().add(new Property("value", "true"));
existing.getAnnotations().add(a);
break;
default:
String msg = "Unsupported annotation: " + e.getKey();
logger.error(msg);
// TODO i18n
RequestMessages.addErrorMessage(msg);
}
});
persistence.initModel();
persistence.saveXmlModel();
} else {
throw new WebApplicationException(Response.serverError().entity(annotationsForm).build());
}
}
Aggregations