Search in sources :

Example 1 with CompositeObject

use of com.buschmais.xo.api.CompositeObject in project jqa-core-framework by buschmais.

the class SubGraphFactory method convert.

private <I extends Identifiable> I convert(Object value) throws ReportException {
    if (value == null) {
        return null;
    } else if (value instanceof Map) {
        Map<String, Object> virtualObject = (Map) value;
        Object role = virtualObject.get(ROLE);
        if (role != null) {
            Map<String, Object> properties = (Map<String, Object>) virtualObject.get(PROPERTIES);
            switch(role.toString().toLowerCase()) {
                case NODE:
                    Node node = new Node();
                    node.setId(nodeId--);
                    Collection<String> labels = (Collection<String>) virtualObject.get(LABELS);
                    node.getLabels().addAll(labels);
                    node.getProperties().putAll(properties);
                    node.setLabel((String) virtualObject.get(LABEL));
                    return (I) node;
                case RELATIONSHIP:
                    Relationship relationship = new Relationship();
                    relationship.setId(relationshipId--);
                    Node startNode = convert(virtualObject.get(START_NODE));
                    Node endNode = convert(virtualObject.get(END_NODE));
                    String type = (String) virtualObject.get(TYPE);
                    relationship.setType(type);
                    relationship.setStartNode(startNode);
                    relationship.setEndNode(endNode);
                    relationship.getProperties().putAll(properties);
                    relationship.setLabel((String) virtualObject.get(LABEL));
                    if (startNode == null || endNode == null || type == null) {
                        throw new ReportException("The virtual relationship does not contain either start node, end node or type: " + relationship);
                    }
                    return (I) relationship;
                case GRAPH:
                    SubGraph subgraph = new SubGraph();
                    subgraph.setId(subgraphId--);
                    subgraph.setParent((Node) convert(subgraph, virtualObject.get(PARENT)));
                    addSubGraphChildren(subgraph, virtualObject, NODES, subgraph.getNodes());
                    addSubGraphChildren(subgraph, virtualObject, RELATIONSHIPS, subgraph.getRelationships());
                    return (I) subgraph;
            }
        }
    } else if (value instanceof CompositeObject) {
        CompositeObject compositeObject = (CompositeObject) value;
        I identifiable = convert(compositeObject.getDelegate());
        identifiable.setLabel(ReportHelper.getLabel(value));
        return identifiable;
    } else if (value instanceof Neo4jNode) {
        Neo4jNode<Neo4jLabel, ?, ?, ?> neo4jNode = (Neo4jNode<Neo4jLabel, ?, ?, ?>) value;
        Node node = new Node();
        node.setId(neo4jNode.getId());
        for (Neo4jLabel label : neo4jNode.getLabels()) {
            node.getLabels().add(label.getName());
        }
        node.getProperties().putAll(neo4jNode.getProperties());
        return (I) node;
    } else if (value instanceof Neo4jRelationship) {
        Neo4jRelationship<?, ?, ?, ?, ?> neo4jRelationship = (Neo4jRelationship) value;
        Relationship relationship = new Relationship();
        relationship.setId(neo4jRelationship.getId());
        relationship.setType(neo4jRelationship.getType().getName());
        relationship.setStartNode((Node) convert(neo4jRelationship.getStartNode()));
        relationship.setEndNode((Node) convert(neo4jRelationship.getEndNode()));
        relationship.getProperties().putAll(neo4jRelationship.getProperties());
        return (I) relationship;
    }
    throw new ReportException("Element type not supported: " + value);
}
Also used : Neo4jLabel(com.buschmais.xo.neo4j.api.model.Neo4jLabel) Node(com.buschmais.jqassistant.core.report.api.graph.model.Node) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) Neo4jRelationship(com.buschmais.xo.neo4j.api.model.Neo4jRelationship) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) CompositeObject(com.buschmais.xo.api.CompositeObject) Neo4jRelationship(com.buschmais.xo.neo4j.api.model.Neo4jRelationship) Relationship(com.buschmais.jqassistant.core.report.api.graph.model.Relationship) Collection(java.util.Collection) ReportException(com.buschmais.jqassistant.core.report.api.ReportException) CompositeObject(com.buschmais.xo.api.CompositeObject) Map(java.util.Map) SubGraph(com.buschmais.jqassistant.core.report.api.graph.model.SubGraph)

Example 2 with CompositeObject

use of com.buschmais.xo.api.CompositeObject in project jqa-core-framework by buschmais.

the class XmlReportPlugin method writeColumn.

/**
 * Determines the language and language element of a descriptor from a result
 * column.
 *
 * @param columnName
 *            The name of the column.
 * @param value
 *            The value.
 * @throws XMLStreamException
 *             If a problem occurs.
 */
private void writeColumn(String columnName, Object value) throws XMLStreamException {
    xmlStreamWriter.writeStartElement("column");
    xmlStreamWriter.writeAttribute("name", columnName);
    String label = null;
    if (value instanceof CompositeObject) {
        CompositeObject descriptor = (CompositeObject) value;
        LanguageElement elementValue = LanguageHelper.getLanguageElement(descriptor);
        if (elementValue != null) {
            xmlStreamWriter.writeStartElement("element");
            xmlStreamWriter.writeAttribute("language", elementValue.getLanguage());
            xmlStreamWriter.writeCharacters(elementValue.name());
            // element
            xmlStreamWriter.writeEndElement();
            SourceProvider sourceProvider = elementValue.getSourceProvider();
            label = sourceProvider.getName(descriptor);
            Optional<FileLocation> sourceLocation = sourceProvider.getSourceLocation(descriptor);
            if (sourceLocation.isPresent()) {
                xmlStreamWriter.writeStartElement("source");
                writeSourceLocation(sourceLocation.get());
                // sourceFile
                xmlStreamWriter.writeEndElement();
            }
        }
    } else if (value != null) {
        label = ReportHelper.getLabel(value);
    }
    writeElementWithCharacters("value", label);
    // column
    xmlStreamWriter.writeEndElement();
}
Also used : CompositeObject(com.buschmais.xo.api.CompositeObject) LanguageElement(com.buschmais.jqassistant.core.report.api.model.LanguageElement) FileLocation(com.buschmais.jqassistant.core.report.api.model.source.FileLocation)

Example 3 with CompositeObject

use of com.buschmais.xo.api.CompositeObject in project jqa-core-framework by buschmais.

the class SubGraphFactoryTest method compositeObject.

@Test
public void compositeObject() throws ReportException {
    CompositeObject compositeObject = mock(CompositeObject.class);
    Neo4jNode neo4jNode = getNeo4jNode(1l, "Test");
    doReturn(neo4jNode).when(compositeObject).getDelegate();
    MapBuilder<String, Object> builder = MapBuilder.builder();
    builder.entry("nodes", singletonList(compositeObject));
    Result<ExecutableRule> result = Result.builder().rows(singletonList(builder.build())).build();
    SubGraph graph = factory.createSubGraph(result);
    Map<Long, Node> nodes = graph.getNodes();
    assertThat(nodes).isNotEmpty();
    Node node = nodes.get(1l);
    assertThat(node).isNotNull();
    assertThat(node.getId()).isEqualTo(1l);
    assertThat(node.getLabels()).containsExactly("Test");
}
Also used : CompositeObject(com.buschmais.xo.api.CompositeObject) ExecutableRule(com.buschmais.jqassistant.core.rule.api.model.ExecutableRule) Node(com.buschmais.jqassistant.core.report.api.graph.model.Node) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) CompositeObject(com.buschmais.xo.api.CompositeObject) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) SubGraph(com.buschmais.jqassistant.core.report.api.graph.model.SubGraph) Test(org.junit.jupiter.api.Test)

Example 4 with CompositeObject

use of com.buschmais.xo.api.CompositeObject in project jqa-core-framework by buschmais.

the class XmlReportWriter method writeColumn.

/**
     * Determines the language and language element of a descriptor from a
     * result column.
     *
     * @param columnName The name of the column.
     * @param value      The value.
     * @throws XMLStreamException                                                    If a problem occurs.
     * @throws ReportException If a problem occurs.
     */
private void writeColumn(String columnName, Object value) throws XMLStreamException, ReportException {
    xmlStreamWriter.writeStartElement("column");
    xmlStreamWriter.writeAttribute("name", columnName);
    String stringValue = null;
    if (value instanceof CompositeObject) {
        CompositeObject descriptor = (CompositeObject) value;
        LanguageElement elementValue = LanguageHelper.getLanguageElement(descriptor);
        if (elementValue != null) {
            xmlStreamWriter.writeStartElement("element");
            xmlStreamWriter.writeAttribute("language", elementValue.getLanguage());
            xmlStreamWriter.writeCharacters(elementValue.name());
            // element
            xmlStreamWriter.writeEndElement();
            SourceProvider sourceProvider = elementValue.getSourceProvider();
            stringValue = sourceProvider.getName(descriptor);
            String sourceFile = sourceProvider.getSourceFile(descriptor);
            Integer lineNumber = sourceProvider.getLineNumber(descriptor);
            if (sourceFile != null) {
                xmlStreamWriter.writeStartElement("source");
                xmlStreamWriter.writeAttribute("name", sourceFile);
                if (lineNumber != null) {
                    xmlStreamWriter.writeAttribute("line", lineNumber.toString());
                }
                // sourceFile
                xmlStreamWriter.writeEndElement();
            }
        }
    } else if (value != null) {
        stringValue = ReportHelper.getLabel(value);
    }
    xmlStreamWriter.writeStartElement("value");
    xmlStreamWriter.writeCharacters(stringValue);
    // value
    xmlStreamWriter.writeEndElement();
    // column
    xmlStreamWriter.writeEndElement();
}
Also used : CompositeObject(com.buschmais.xo.api.CompositeObject)

Example 5 with CompositeObject

use of com.buschmais.xo.api.CompositeObject in project jqa-core-framework by buschmais.

the class ReportHelper method getLabel.

/**
     * Converts a value to its string representation.
     *
     * @param value
     *            The value.
     * @return The string representation
     */
public static String getLabel(Object value) {
    if (value != null) {
        if (value instanceof CompositeObject) {
            CompositeObject descriptor = (CompositeObject) value;
            String label = getLanguageLabel(descriptor);
            return label != null ? label : descriptor.toString();
        } else if (value.getClass().isArray()) {
            Object[] objects = (Object[]) value;
            return getLabel(Arrays.asList(objects));
        } else if (value instanceof Iterable) {
            StringBuilder sb = new StringBuilder();
            for (Object o : ((Iterable) value)) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(getLabel(o));
            }
            return "[" + sb.toString() + "]";
        } else if (value instanceof Map) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Object> entry : ((Map<String, Object>) value).entrySet()) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(entry.getKey());
                sb.append(":");
                sb.append(getLabel(entry.getValue()));
            }
            return "{" + sb.toString() + "}";
        }
        return value.toString();
    }
    return null;
}
Also used : CompositeObject(com.buschmais.xo.api.CompositeObject) CompositeObject(com.buschmais.xo.api.CompositeObject)

Aggregations

CompositeObject (com.buschmais.xo.api.CompositeObject)7 Neo4jNode (com.buschmais.xo.neo4j.api.model.Neo4jNode)4 Node (com.buschmais.jqassistant.core.report.api.graph.model.Node)3 SubGraph (com.buschmais.jqassistant.core.report.api.graph.model.SubGraph)3 ReportException (com.buschmais.jqassistant.core.report.api.ReportException)2 Relationship (com.buschmais.jqassistant.core.report.api.graph.model.Relationship)2 Neo4jLabel (com.buschmais.xo.neo4j.api.model.Neo4jLabel)2 Neo4jRelationship (com.buschmais.xo.neo4j.api.model.Neo4jRelationship)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Test (org.junit.jupiter.api.Test)2 Identifiable (com.buschmais.jqassistant.core.report.api.graph.model.Identifiable)1 LanguageElement (com.buschmais.jqassistant.core.report.api.model.LanguageElement)1 FileLocation (com.buschmais.jqassistant.core.report.api.model.source.FileLocation)1 TestDescriptorWithLanguageElement (com.buschmais.jqassistant.core.report.model.TestDescriptorWithLanguageElement)1 ExecutableRule (com.buschmais.jqassistant.core.rule.api.model.ExecutableRule)1 StringContains.containsString (org.hamcrest.core.StringContains.containsString)1