Search in sources :

Example 11 with App

use of org.structr.core.app.App in project structr by structr.

the class ODFExporter method exportImage.

public static void exportImage(final ODFExporter thisNode, final String uuid) {
    final File output = thisNode.getResultDocument();
    try {
        final App app = StructrApp.getInstance();
        final Image result = app.nodeQuery(Image.class).and(GraphObject.id, uuid).getFirst();
        String imageName = result.getProperty(new StringProperty("name"));
        String contentType = result.getProperty(new StringProperty("contentType"));
        String templateImagePath = null;
        OdfDocument doc = OdfDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
        NodeList nodes = doc.getContentRoot().getElementsByTagName(ODF_IMAGE_PARENT_NAME);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node currentNode = nodes.item(i);
            NamedNodeMap attrs = currentNode.getAttributes();
            Node fieldName = attrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_PARENT_IMAGE_NAME);
            if (fieldName != null && fieldName.getTextContent().equals(imageName)) {
                NamedNodeMap childAttrs = currentNode.getFirstChild().getAttributes();
                Node filePath = childAttrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_FILE_PATH);
                templateImagePath = filePath.getTextContent();
                filePath.setTextContent(ODF_IMAGE_DIRECTORY + imageName);
            }
        }
        OdfPackage pkg = doc.getPackage();
        if (templateImagePath != null && templateImagePath.length() > 0) {
            pkg.remove(templateImagePath);
        }
        pkg.insert(new URI(result.getFileOnDisk().getAbsolutePath()), ODF_IMAGE_DIRECTORY + imageName, contentType);
        pkg.save(output.getFileOnDisk().getAbsolutePath());
        pkg.close();
        doc.close();
    } catch (Exception e) {
        logger.error("Error while exporting image to document", e);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AbstractNode(org.structr.core.entity.AbstractNode) StringProperty(org.structr.core.property.StringProperty) Image(org.structr.web.entity.Image) URI(java.net.URI) FrameworkException(org.structr.common.error.FrameworkException) OdfPackage(org.odftoolkit.odfdom.pkg.OdfPackage) OdfDocument(org.odftoolkit.odfdom.doc.OdfDocument) File(org.structr.web.entity.File)

Example 12 with App

use of org.structr.core.app.App in project structr by structr.

the class ODSExporter method exportAttributes.

public static void exportAttributes(final ODSExporter 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)));
        OdfSpreadsheetDocument spreadsheet = OdfSpreadsheetDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
        OdfTable sheet = spreadsheet.getTableList().get(0);
        Iterator<Entry<String, Object>> it = nodeProperties.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Object> currentEntry = it.next();
            String address = currentEntry.getKey();
            Object val = currentEntry.getValue();
            if (val instanceof Collection) {
                Collection col = (Collection) val;
                writeCollectionToCells(sheet, sheet.getCellByPosition(address), col);
            } else if (val instanceof String[]) {
                String[] arr = (String[]) val;
                List<String> list = new ArrayList<>(Arrays.asList(arr));
                writeCollectionToCells(sheet, sheet.getCellByPosition(address), list);
            } else {
                writeObjectToCell(sheet.getCellByPosition(address), val);
            }
        }
        spreadsheet.save(output.getFileOnDisk().getAbsolutePath());
        spreadsheet.close();
    } catch (Exception e) {
        logger.error("Error while exporting to ODS", e);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) HashMap(java.util.HashMap) OdfSpreadsheetDocument(org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument) VirtualType(org.structr.transform.VirtualType) FrameworkException(org.structr.common.error.FrameworkException) Result(org.structr.core.Result) Entry(java.util.Map.Entry) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) OdfTable(org.odftoolkit.odfdom.doc.table.OdfTable) ArrayList(java.util.ArrayList) List(java.util.List) File(org.structr.web.entity.File)

Example 13 with App

use of org.structr.core.app.App 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)

Example 14 with App

use of org.structr.core.app.App in project structr by structr.

the class StructrODFModuleTest method createTestNode.

protected <T extends AbstractNode> T createTestNode(final Class<T> type, final PropertyMap props, final Principal owner) throws FrameworkException {
    final App backendApp = StructrApp.getInstance(SecurityContext.getInstance(owner, AccessMode.Backend));
    try (final Tx tx = backendApp.tx()) {
        final T result = backendApp.create(type, props);
        tx.success();
        return result;
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx)

Example 15 with App

use of org.structr.core.app.App in project structr by structr.

the class FrameGrabberProcess method processExited.

@Override
public Image processExited(int exitCode) {
    final App app = StructrApp.getInstance(securityContext);
    if (exitCode == 0) {
        try (final Tx tx = app.tx()) {
            // move converted file into place
            final java.io.File diskFile = new java.io.File(outputFileName + fileExtension);
            final java.io.File dstFile = new java.io.File(outputFileName);
            if (diskFile.exists()) {
                Files.move(diskFile.toPath(), dstFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                FileHelper.updateMetadata(newFile);
                // create link between the two videos
                inputFile.setProperty(StructrApp.key(VideoFile.class, "posterImage"), newFile);
            }
            tx.success();
        } catch (FrameworkException | IOException fex) {
            logger.warn("", fex);
        }
    } else {
        // delete file, conversion has failed
        try (final Tx tx = app.tx()) {
            app.delete(newFile);
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("", fex);
        }
    }
    return newFile;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException)

Aggregations

App (org.structr.core.app.App)296 StructrApp (org.structr.core.app.StructrApp)294 Tx (org.structr.core.graph.Tx)201 FrameworkException (org.structr.common.error.FrameworkException)176 LinkedList (java.util.LinkedList)60 SecurityContext (org.structr.common.SecurityContext)56 PropertyMap (org.structr.core.property.PropertyMap)41 Folder (org.structr.web.entity.Folder)38 GraphObject (org.structr.core.GraphObject)35 Principal (org.structr.core.entity.Principal)31 IOException (java.io.IOException)30 AbstractFile (org.structr.web.entity.AbstractFile)27 AbstractNode (org.structr.core.entity.AbstractNode)26 Test (org.junit.Test)24 NodeAttribute (org.structr.core.graph.NodeAttribute)24 File (org.structr.web.entity.File)23 NodeInterface (org.structr.core.graph.NodeInterface)22 SchemaNode (org.structr.core.entity.SchemaNode)19 PropertyKey (org.structr.core.property.PropertyKey)17 Map (java.util.Map)16