use of com.revolsys.ui.html.serializer.key.KeySerializer in project com.revolsys.open by revolsys.
the class HtmlUiBuilder method serialize.
/**
* Serialize the value represented by the key from the object.
*
* @param out The XML writer to serialize to.
* @param object The object to get the value from.
* @param key The key to serialize.
* @throws IOException If there was an I/O error serializing the value.
*/
public void serialize(final XmlWriter out, final Object object, final String key) {
if (object == null) {
serializeNullLabel(out, key);
} else {
final Object serializer = this.keySerializers.get(key);
if (serializer == null) {
String path = null;
String valueKey = key;
final Matcher linkMatcher = LINK_KEY_PATTERN.matcher(key);
if (linkMatcher.matches()) {
path = linkMatcher.group(1);
valueKey = linkMatcher.group(2);
}
HtmlUiBuilder<? extends Object> uiBuilder = this;
final String[] parts = valueKey.split("\\.");
Object currentObject = object;
for (int i = 0; i < parts.length - 1; i++) {
final String keyName = parts[i];
try {
currentObject = getProperty(currentObject, keyName);
if (currentObject == null) {
serializeNullLabel(out, keyName);
return;
}
uiBuilder = getBuilder(currentObject);
} catch (final IllegalArgumentException e) {
final String message = currentObject.getClass().getName() + " does not have a property " + keyName;
this.log.error(e.getMessage(), e);
out.element(HtmlElem.B, message);
return;
}
}
final String lastKey = parts[parts.length - 1];
if (path == null) {
if (uiBuilder == this) {
try {
final Object value = getProperty(currentObject, lastKey);
if (value == null) {
serializeNullLabel(out, lastKey);
return;
} else {
final TypeSerializer typeSerializer = this.classSerializers.get(value.getClass());
if (typeSerializer == null) {
final String stringValue;
if (value instanceof Collection) {
final Collection<?> collection = (Collection<?>) value;
stringValue = Strings.toString(", ", collection);
} else {
stringValue = value.toString();
}
if (stringValue.length() > 0) {
out.text(stringValue);
return;
}
} else {
typeSerializer.serialize(out, value);
return;
}
}
} catch (final IllegalArgumentException e) {
final String message = currentObject.getClass().getName() + " does not have a property " + key;
this.log.error(e.getMessage(), e);
out.element(HtmlElem.B, message);
return;
}
} else {
uiBuilder.serialize(out, currentObject, lastKey);
}
} else {
uiBuilder.serializeLink(out, currentObject, lastKey, path);
}
} else {
if (serializer instanceof TypeSerializer) {
final TypeSerializer typeSerializer = (TypeSerializer) serializer;
typeSerializer.serialize(out, object);
return;
} else if (serializer instanceof KeySerializer) {
final KeySerializer keySerializer = (KeySerializer) serializer;
keySerializer.serialize(out, object);
return;
}
}
}
}
use of com.revolsys.ui.html.serializer.key.KeySerializer in project com.revolsys.open by revolsys.
the class HtmlUiBuilder method newDataTableMap.
public Map<String, Object> newDataTableMap(final HttpServletRequest request, final RecordStore recordStore, Query query, final String pageName) {
final Map<String, Object> response = new LinkedHashMap<>();
try {
final int recordCount = recordStore.getRecordCount(query);
final List<List<String>> rows = new ArrayList<>();
if (recordCount > 0) {
int recordCountMax = 50;
final String lengthString = request.getParameter("length");
if (Property.hasValue(lengthString)) {
if (!"NAN".equalsIgnoreCase(lengthString)) {
try {
recordCountMax = Integer.valueOf(lengthString);
} catch (final Throwable e) {
}
}
}
final int offset = HttpServletUtils.getIntegerParameter(request, "start");
query = query.clone();
query.setOffset(offset);
query.setLimit(recordCountMax);
final List<KeySerializer> serializers = getSerializers(pageName, "list");
try (Reader<Record> reader = recordStore.getRecords(query)) {
for (Record record : reader) {
record = convertRecord(record);
final List<String> row = new ArrayList<>();
for (final KeySerializer serializer : serializers) {
final String html = serializer.toString(record);
row.add(html);
}
rows.add(row);
}
}
}
response.put("draw", HttpServletUtils.getIntegerParameter(request, "draw"));
response.put("recordsTotal", recordCount);
response.put("recordsFiltered", recordCount);
response.put("data", rows);
} catch (final Throwable e) {
Logs.error(this, "Error executing query: " + query, e);
response.put("error", "Error executing query");
}
return response;
}
use of com.revolsys.ui.html.serializer.key.KeySerializer in project com.revolsys.open by revolsys.
the class HtmlUiBuilder method newView.
protected void newView(final String name, final List<?> elements) {
final List<KeySerializer> serializers = new ArrayList<>();
this.viewSerializers.put(name, serializers);
for (final Object element : elements) {
if (element != null) {
KeySerializer serializer = null;
if (element instanceof KeySerializer) {
serializer = (KeySerializer) element;
} else {
final String key = element.toString();
serializer = this.keySerializers.get(key);
if (serializer == null) {
final String title = getLabel(key);
serializer = new BuilderSerializer(key, title, this);
}
}
if (serializer instanceof HtmlUiBuilderAware) {
@SuppressWarnings("unchecked") final HtmlUiBuilderAware<HtmlUiBuilder<?>> builderAware = (HtmlUiBuilderAware<HtmlUiBuilder<?>>) serializer;
builderAware.setHtmlUiBuilder(this);
}
serializers.add(serializer);
}
}
}
Aggregations