Search in sources :

Example 11 with KeySerializer

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;
            }
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) TypeSerializer(com.revolsys.ui.html.serializer.type.TypeSerializer) Collection(java.util.Collection) KeySerializer(com.revolsys.ui.html.serializer.key.KeySerializer)

Example 12 with KeySerializer

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;
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) KeySerializer(com.revolsys.ui.html.serializer.key.KeySerializer) List(java.util.List) ArrayList(java.util.ArrayList) Record(com.revolsys.record.Record)

Example 13 with KeySerializer

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);
        }
    }
}
Also used : BuilderSerializer(com.revolsys.ui.html.serializer.BuilderSerializer) ArrayList(java.util.ArrayList) KeySerializer(com.revolsys.ui.html.serializer.key.KeySerializer)

Aggregations

KeySerializer (com.revolsys.ui.html.serializer.key.KeySerializer)13 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)3 ElementContainer (com.revolsys.ui.html.view.ElementContainer)2 TabElementContainer (com.revolsys.ui.html.view.TabElementContainer)2 Menu (com.revolsys.ui.model.Menu)2 List (java.util.List)2 Record (com.revolsys.record.Record)1 Or (com.revolsys.record.query.Or)1 RecordStore (com.revolsys.record.schema.RecordStore)1 Field (com.revolsys.ui.html.fields.Field)1 TextAreaField (com.revolsys.ui.html.fields.TextAreaField)1 TextField (com.revolsys.ui.html.fields.TextField)1 TableBodyLayout (com.revolsys.ui.html.layout.TableBodyLayout)1 BuilderSerializer (com.revolsys.ui.html.serializer.BuilderSerializer)1 KeySerializerTableSerializer (com.revolsys.ui.html.serializer.KeySerializerTableSerializer)1 RowsTableSerializer (com.revolsys.ui.html.serializer.RowsTableSerializer)1 TypeSerializer (com.revolsys.ui.html.serializer.type.TypeSerializer)1 ButtonsToolbarElement (com.revolsys.ui.html.view.ButtonsToolbarElement)1 Element (com.revolsys.ui.html.view.Element)1