Search in sources :

Example 16 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class TableCommand method replaceTokens.

private String replaceTokens(String text, Message msg) throws APIException {
    Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}");
    Matcher matcher = pattern.matcher(text);
    StringBuffer buffer = new StringBuffer();
    while (matcher.find()) {
        final String group = matcher.group(1);
        Property p = msg.getProperty(group);
        if (p == null) {
            throw new APIException("Error resolving target. Referenced property: " + group + " not found in message", null, APIErrorCode.InternalError);
        }
        String replacement = p.getValue();
        if (replacement != null) {
            matcher.appendReplacement(buffer, "");
            buffer.append(replacement);
        }
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}
Also used : Pattern(java.util.regex.Pattern) APIException(com.dexels.navajo.article.APIException) Matcher(java.util.regex.Matcher) Property(com.dexels.navajo.document.Property)

Example 17 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class TableCommand method execute.

@Override
public JsonNode execute(ArticleRuntime runtime, ArticleContext context, Map<String, String> parameters, XMLElement element) throws APIException {
    String service = parameters.get("service");
    if (service == null) {
        throw new APIException("No service parameter supplied for table. We need to know which navajo you want to use.", null, APIErrorCode.InternalError);
    }
    Navajo navajo = runtime.getNavajo(service);
    if (navajo == null) {
        throw new APIException("Navajo: " + service + " was not found in the current runtime.", null, APIErrorCode.InternalError);
    }
    String path = parameters.get("path");
    if (path == null) {
        throw new APIException("No path parameter supplied. Which message do you want to listen to.", null, APIErrorCode.InternalError);
    }
    Message message = navajo.getMessage(path);
    if (message == null) {
        throw new APIException("Message: " + path + " not found", null, APIErrorCode.InternalError);
    }
    runtime.setMimeType("application/json; charset=utf-8");
    ArrayNode nodes = runtime.getObjectMapper().createArrayNode();
    for (Message data : message.getElements()) {
        ObjectNode node = runtime.getObjectMapper().createObjectNode();
        for (XMLElement XMLElement : element.getChildren()) {
            final String id = XMLElement.getStringAttribute("id");
            final String type = XMLElement.getStringAttribute("type");
            final String target = XMLElement.getStringAttribute("target");
            if (target != null) {
                // A target is a link, they do not have navajo value.
                node.put(id, resolveTarget(target, runtime, data));
            } else {
                // We default back to the id for the propertyName if not explicit set.
                final String propertyName = XMLElement.getStringAttribute("propertyName", id);
                Property property = data.getProperty(propertyName);
                APIValue.setValueOnNodeForType(node, id, type, property, runtime);
            }
        }
        nodes.add(node);
    }
    return nodes;
}
Also used : APIException(com.dexels.navajo.article.APIException) Message(com.dexels.navajo.document.Message) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Navajo(com.dexels.navajo.document.Navajo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) Property(com.dexels.navajo.document.Property)

Example 18 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class JsonTmlConverterImpl method toMessage.

@Override
public Message toMessage(String messageName, ImmutableMessage message, Navajo rootNavajo) {
    Message cV = NavajoFactory.getInstance().createMessage(rootNavajo, messageName);
    for (String columnName : message.columnNames()) {
        String type = message.columnType(columnName);
        Object value = message.value(columnName).orElse(null);
        Property colProp = NavajoFactory.getInstance().createProperty(rootNavajo, columnName, type, null, 0, "", Property.DIR_OUT);
        switch(type) {
            case Property.CLOCKTIME_PROPERTY:
                if (value != null) {
                    ClockTime ct = new ClockTime((Date) value);
                    colProp.setAnyValue(ct);
                }
                colProp.setType(type);
                break;
            default:
                colProp.setAnyValue(value);
                colProp.setType(type);
                break;
        }
        cV.addProperty(colProp);
    }
    for (Entry<String, List<ImmutableMessage>> e : message.subMessageListMap().entrySet()) {
        Message subArrayMessage = NavajoFactory.getInstance().createMessage(rootNavajo, e.getKey(), Message.MSG_TYPE_ARRAY);
        cV.addMessage(subArrayMessage);
        for (ImmutableMessage elt : e.getValue()) {
            Message msgElt = toMessage(e.getKey(), elt, rootNavajo);
            subArrayMessage.addElement(msgElt);
        }
    }
    for (Entry<String, ImmutableMessage> e : message.subMessageMap().entrySet()) {
        Message msgElt = toMessage(e.getKey(), e.getValue(), rootNavajo);
        cV.addMessage(msgElt);
    }
    return cV;
}
Also used : Message(com.dexels.navajo.document.Message) ReplicationMessage(com.dexels.replication.api.ReplicationMessage) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) List(java.util.List) ClockTime(com.dexels.navajo.document.types.ClockTime) Property(com.dexels.navajo.document.Property)

Example 19 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class JsonTmlConverterImpl method createColumnsMessage.

private Message createColumnsMessage(ReplicationMessage message, Navajo n) {
    Message cV = NavajoFactory.getInstance().createMessage(n, "Columns", Message.MSG_TYPE_ARRAY);
    for (String columnName : message.columnNames()) {
        Message vM = NavajoFactory.getInstance().createMessage(n, "Columns");
        cV.addElement(vM);
        Object value = message.columnValue(columnName);
        Property colPropName = NavajoFactory.getInstance().createProperty(n, "Name", Property.STRING_PROPERTY, columnName, 0, "", Property.DIR_OUT);
        Property colPropValue = NavajoFactory.getInstance().createProperty(n, "Value", Property.STRING_PROPERTY, "", 0, "", Property.DIR_OUT);
        colPropValue.setAnyValue(value);
        vM.addProperty(colPropName);
        vM.addProperty(colPropValue);
    }
    return cV;
}
Also used : Message(com.dexels.navajo.document.Message) ReplicationMessage(com.dexels.replication.api.ReplicationMessage) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Property(com.dexels.navajo.document.Property)

Example 20 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class JsonTmlConverterImpl method createTransactionMessage.

private Message createTransactionMessage(ReplicationMessage message, String tenant, String table, Optional<String> datasource, Navajo n) {
    String transactionId = message.transactionId();
    Message msg = NavajoFactory.getInstance().createMessage(n, "Transaction");
    Property transactionIdProp = NavajoFactory.getInstance().createProperty(n, "TransactionId", Property.STRING_PROPERTY, transactionId, 0, "", Property.DIR_OUT);
    Property timestamp = NavajoFactory.getInstance().createProperty(n, "Timestamp", Property.LONG_PROPERTY, message.timestamp() + "", 0, "", Property.DIR_OUT);
    Property dbOperation = NavajoFactory.getInstance().createProperty(n, "Operation", Property.STRING_PROPERTY, message.operation().toString(), 0, "", Property.DIR_OUT);
    Property tenantProp = NavajoFactory.getInstance().createProperty(n, "Tenant", Property.STRING_PROPERTY, tenant, 0, "", Property.DIR_OUT);
    Property tableProp = NavajoFactory.getInstance().createProperty(n, "SourceTable", Property.STRING_PROPERTY, table, 0, "", Property.DIR_OUT);
    Property status = NavajoFactory.getInstance().createProperty(n, "Status", Property.STRING_PROPERTY, "PENDING", 0, "", Property.DIR_OUT);
    msg.addProperty(transactionIdProp);
    msg.addProperty(tenantProp);
    msg.addProperty(tableProp);
    if (datasource.isPresent()) {
        Property source = NavajoFactory.getInstance().createProperty(n, "DataSource", Property.STRING_PROPERTY, datasource.get(), 0, "", Property.DIR_OUT);
        msg.addProperty(source);
    }
    msg.addProperty(timestamp);
    msg.addProperty(dbOperation);
    msg.addProperty(status);
    return msg;
}
Also used : Message(com.dexels.navajo.document.Message) ReplicationMessage(com.dexels.replication.api.ReplicationMessage) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Property(com.dexels.navajo.document.Property)

Aggregations

Property (com.dexels.navajo.document.Property)253 Message (com.dexels.navajo.document.Message)148 Test (org.junit.Test)88 Navajo (com.dexels.navajo.document.Navajo)84 Selection (com.dexels.navajo.document.Selection)36 NavajoException (com.dexels.navajo.document.NavajoException)30 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)17 ArrayList (java.util.ArrayList)17 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)16 Binary (com.dexels.navajo.document.types.Binary)16 UserException (com.dexels.navajo.script.api.UserException)16 Access (com.dexels.navajo.script.api.Access)15 StringWriter (java.io.StringWriter)13 List (java.util.List)11 Operand (com.dexels.navajo.document.Operand)10 MappableException (com.dexels.navajo.script.api.MappableException)9 IOException (java.io.IOException)9 Writer (java.io.Writer)9 StringTokenizer (java.util.StringTokenizer)9 JSONTML (com.dexels.navajo.document.json.JSONTML)8