Search in sources :

Example 1 with ArticleCommand

use of com.dexels.navajo.article.command.ArticleCommand in project navajo by Dexels.

the class BaseRuntimeImpl method execute.

@Override
public void execute(ArticleContext context) throws APIException, NoJSONOutputException {
    verifyScopes();
    List<XMLElement> children = article.getChildren();
    try {
        List<JsonNode> elements = new ArrayList<JsonNode>();
        for (XMLElement e : children) {
            String name = e.getName();
            if (name.startsWith("_")) {
                // at runtime
                continue;
            }
            ArticleCommand ac = context.getCommand(name);
            if (ac == null) {
                throw new APIException("Unknown command: " + name, null, APIErrorCode.InternalError);
            }
            Map<String, String> parameters = new HashMap<String, String>();
            for (Iterator<String> iterator = e.enumerateAttributeNames(); iterator.hasNext(); ) {
                String attributeName = iterator.next();
                parameters.put(attributeName, e.getStringAttribute(attributeName));
            }
            JsonNode node = ac.execute(this, context, parameters, e);
            if (node != null) {
                elements.add(node);
            }
        }
        if (elements.size() == 0) {
            writeNode(rootNode);
        } else if (elements.size() == 1) {
            // HUH?
            writeNode(elements.iterator().next());
        } else {
            ArrayNode an = getObjectMapper().createArrayNode();
            for (JsonNode jsonNode : elements) {
                an.add(jsonNode);
            }
            writeNode(an);
        }
    // mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    // mapper.writeValue(getOutputWriter(), rootNode);
    // commit();
    } catch (IOException e1) {
        logger.error("Error: ", e1);
    }
}
Also used : ArticleCommand(com.dexels.navajo.article.command.ArticleCommand) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) APIException(com.dexels.navajo.article.APIException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with ArticleCommand

use of com.dexels.navajo.article.command.ArticleCommand in project navajo by Dexels.

the class BaseContextImpl method interpretMeta.

public void interpretMeta(XMLElement article, ObjectMapper mapper, ObjectNode articleNode, boolean extended) throws APIException {
    String outputType = article.getStringAttribute("output");
    if (outputType != null) {
        articleNode.put("output", outputType);
    }
    String scopes = article.getStringAttribute("scopes");
    if (scopes != null) {
        String[] scopeArray = scopes.split(",");
        ArrayNode scopeArgs = mapper.createArrayNode();
        for (String scope : scopeArray) {
            scopeArgs.add(scope);
        }
        articleNode.set("scopes", scopeArgs);
    }
    String description = article.getStringAttribute("description");
    if (extended && description != null && description.length() != 0) {
        articleNode.put("description", description);
    }
    XMLElement argTag = article.getChildByTagName("_arguments");
    ArrayNode inputArgs = mapper.createArrayNode();
    articleNode.set("input", inputArgs);
    if (argTag != null) {
        List<XMLElement> args = argTag.getChildren();
        for (XMLElement xmlElement : args) {
            // name="aantalregels" description="Maximum aantal regels"
            // type="integer" optional="true" default="5"
            ObjectNode input = mapper.createObjectNode();
            input.put("name", xmlElement.getStringAttribute("name"));
            input.put("description", xmlElement.getStringAttribute("description"));
            input.put("type", xmlElement.getStringAttribute("type"));
            final boolean optional = xmlElement.getBooleanAttribute("optional", "true", "false", false);
            input.put("optional", optional);
            final String defaultValue = xmlElement.getStringAttribute("default");
            if (defaultValue != null) {
                input.put("default", defaultValue);
            }
            final String sourcearticle = xmlElement.getStringAttribute("sourcearticle");
            if (sourcearticle != null) {
                input.put("sourcearticle", sourcearticle);
            }
            final String sourcekey = xmlElement.getStringAttribute("sourcekey");
            if (sourcekey != null) {
                input.put("sourcekey", sourcekey);
            }
            inputArgs.add(input);
        }
    }
    ArrayNode outputArgs = mapper.createArrayNode();
    articleNode.set("output", outputArgs);
    List<XMLElement> children = article.getChildren();
    for (XMLElement e : children) {
        String name = e.getName();
        if (name.startsWith("_")) {
            continue;
        }
        ArticleCommand ac = getCommand(name);
        if (ac == null) {
            throw new APIException("Unknown command: " + name, null, APIErrorCode.InternalError);
        }
        Map<String, String> parameters = new HashMap<String, String>();
        for (Iterator<String> iterator = e.enumerateAttributeNames(); iterator.hasNext(); ) {
            String attributeName = iterator.next();
            parameters.put(attributeName, e.getStringAttribute(attributeName));
        }
        ac.writeMetadata(e, outputArgs, mapper);
    }
}
Also used : APIException(com.dexels.navajo.article.APIException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArticleCommand(com.dexels.navajo.article.command.ArticleCommand) HashMap(java.util.HashMap) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement)

Aggregations

APIException (com.dexels.navajo.article.APIException)2 ArticleCommand (com.dexels.navajo.article.command.ArticleCommand)2 CaseSensitiveXMLElement (com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement)2 XMLElement (com.dexels.navajo.document.nanoimpl.XMLElement)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 HashMap (java.util.HashMap)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1