Search in sources :

Example 1 with TextDocument

use of org.odftoolkit.simple.TextDocument in project structr by structr.

the class ODTExporter method exportAttributes.

static void exportAttributes(final ODTExporter thisNode, final String uuid) throws FrameworkException {
    final SecurityContext securityContext = thisNode.getSecurityContext();
    final File output = thisNode.getResultDocument();
    final VirtualType transformation = thisNode.getTransformationProvider();
    try {
        final App app = StructrApp.getInstance();
        final Result result = app.nodeQuery(AbstractNode.class).and(GraphObject.id, uuid).getResult();
        final Result transformedResult = transformation.transformOutput(securityContext, AbstractNode.class, result);
        Map<String, Object> nodeProperties = new HashMap<>();
        GraphObjectMap node = (GraphObjectMap) transformedResult.get(0);
        node.getPropertyKeys(null).forEach(p -> nodeProperties.put(p.dbName(), node.getProperty(p)));
        TextDocument text = TextDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
        NodeList nodes = text.getContentRoot().getElementsByTagName(ODT_FIELD_TAG_NAME);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node currentNode = nodes.item(i);
            NamedNodeMap attrs = currentNode.getAttributes();
            Node fieldName = attrs.getNamedItem(ODT_FIELD_ATTRIBUTE_NAME);
            Object nodeFieldValue = nodeProperties.get(fieldName.getNodeValue());
            Node currentContent = attrs.getNamedItem(ODT_FIELD_ATTRIBUTE_VALUE);
            if (nodeFieldValue != null) {
                if (nodeFieldValue instanceof String[]) {
                    String[] arr = (String[]) nodeFieldValue;
                    List<String> list = new ArrayList<>(Arrays.asList(arr));
                    StringBuilder sb = new StringBuilder();
                    list.forEach(s -> sb.append(s + "\n"));
                    currentContent.setNodeValue(sb.toString());
                } else if (nodeFieldValue instanceof Collection) {
                    Collection col = (Collection) nodeFieldValue;
                    StringBuilder sb = new StringBuilder();
                    col.forEach(s -> sb.append(s + "\n"));
                    currentContent.setNodeValue(sb.toString());
                } else {
                    currentContent.setNodeValue(nodeFieldValue.toString());
                }
            }
        }
        text.save(output.getFileOnDisk().getAbsolutePath());
        text.close();
    } catch (Exception e) {
        logger.error("Error while exporting to ODT", e);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) JsonObjectType(org.structr.schema.json.JsonObjectType) Arrays(java.util.Arrays) NodeList(org.w3c.dom.NodeList) Collection(java.util.Collection) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) HashMap(java.util.HashMap) TextDocument(org.odftoolkit.simple.TextDocument) ArrayList(java.util.ArrayList) File(org.structr.web.entity.File) List(java.util.List) JsonSchema(org.structr.schema.json.JsonSchema) FrameworkException(org.structr.common.error.FrameworkException) App(org.structr.core.app.App) Map(java.util.Map) Node(org.w3c.dom.Node) NamedNodeMap(org.w3c.dom.NamedNodeMap) URI(java.net.URI) GraphObjectMap(org.structr.core.GraphObjectMap) Result(org.structr.core.Result) AbstractNode(org.structr.core.entity.AbstractNode) VirtualType(org.structr.transform.VirtualType) SchemaService(org.structr.schema.SchemaService) TextDocument(org.odftoolkit.simple.TextDocument) NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AbstractNode(org.structr.core.entity.AbstractNode) ArrayList(java.util.ArrayList) VirtualType(org.structr.transform.VirtualType) FrameworkException(org.structr.common.error.FrameworkException) Result(org.structr.core.Result) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) File(org.structr.web.entity.File)

Aggregations

URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TextDocument (org.odftoolkit.simple.TextDocument)1 SecurityContext (org.structr.common.SecurityContext)1 FrameworkException (org.structr.common.error.FrameworkException)1 GraphObject (org.structr.core.GraphObject)1 GraphObjectMap (org.structr.core.GraphObjectMap)1 Result (org.structr.core.Result)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 AbstractNode (org.structr.core.entity.AbstractNode)1 SchemaService (org.structr.schema.SchemaService)1 JsonObjectType (org.structr.schema.json.JsonObjectType)1 JsonSchema (org.structr.schema.json.JsonSchema)1 VirtualType (org.structr.transform.VirtualType)1