Search in sources :

Example 11 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class ParseJavaFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!(arrayHasLengthAndAllElementsNotNull(sources, 1) && sources[0] instanceof String)) {
            return null;
        }
        try {
            final SecurityContext securityContext = ctx.getSecurityContext();
            final App app = StructrApp.getInstance(securityContext);
            // Parse string as Java code
            final String resultJson = new GsonBuilder().setPrettyPrinting().create().toJson(new JavaParserModule(app).parse((String) sources[0]).get());
            return resultJson;
        } catch (final ParseProblemException ex) {
            logException(caller, ex, sources);
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return "";
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) GsonBuilder(com.google.gson.GsonBuilder) SecurityContext(org.structr.common.SecurityContext) ParseProblemException(com.github.javaparser.ParseProblemException)

Example 12 with SecurityContext

use of org.structr.common.SecurityContext 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 SecurityContext

use of org.structr.common.SecurityContext 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 SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class VideoFile method updateVideoInfo.

static void updateVideoInfo(final VideoFile thisVideo) {
    final SecurityContext securityContext = thisVideo.getSecurityContext();
    try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
        final Map<String, Object> info = AVConv.newInstance(securityContext, thisVideo).getVideoInfo();
        if (info != null && info.containsKey("streams")) {
            final List<Map<String, Object>> streams = (List<Map<String, Object>>) info.get("streams");
            for (final Map<String, Object> stream : streams) {
                final String codecType = (String) stream.get("codec_type");
                if (codecType != null) {
                    if ("video".equals(codecType)) {
                        VideoFile.setIfNotNull(thisVideo, StructrApp.key(VideoFile.class, "videoCodecName"), stream.get("codec_long_name"));
                        VideoFile.setIfNotNull(thisVideo, StructrApp.key(VideoFile.class, "videoCodec"), stream.get("codec_name"));
                        VideoFile.setIfNotNull(thisVideo, StructrApp.key(VideoFile.class, "pixelFormat"), stream.get("pix_fmt"));
                        VideoFile.setIfNotNull(thisVideo, StructrApp.key(VideoFile.class, "width"), VideoFile.toInt(stream.get("width")));
                        VideoFile.setIfNotNull(thisVideo, StructrApp.key(VideoFile.class, "height"), VideoFile.toInt(stream.get("height")));
                        VideoFile.setIfNotNull(thisVideo, StructrApp.key(VideoFile.class, "duration"), VideoFile.toDouble(stream.get("duration")));
                    } else if ("audio".equals(codecType)) {
                        VideoFile.setIfNotNull(thisVideo, StructrApp.key(VideoFile.class, "audioCodecName"), stream.get("codec_long_name"));
                        VideoFile.setIfNotNull(thisVideo, StructrApp.key(VideoFile.class, "audioCodec"), stream.get("codec_name"));
                        VideoFile.setIfNotNull(thisVideo, StructrApp.key(VideoFile.class, "sampleRate"), VideoFile.toInt(stream.get("sampleRate")));
                    }
                }
            }
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 15 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class DeployCommand method importListData.

private <T extends NodeInterface> void importListData(final Class<T> type, final List<Map<String, Object>> data, final PropertyMap... additionalData) throws FrameworkException {
    final SecurityContext context = SecurityContext.getSuperUserInstance();
    context.setDoTransactionNotifications(false);
    final App app = StructrApp.getInstance(context);
    try (final Tx tx = app.tx()) {
        for (final T toDelete : app.nodeQuery(type).getAsList()) {
            app.delete(toDelete);
        }
        for (final Map<String, Object> entry : data) {
            final PropertyMap map = PropertyMap.inputTypeToJavaType(context, type, entry);
            // allow caller to insert additional data for better creation performance
            for (final PropertyMap add : additionalData) {
                map.putAll(add);
            }
            app.create(type, map);
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("Unable to import {}, aborting with {}", type.getSimpleName(), fex.getMessage());
        fex.printStackTrace();
        throw fex;
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext)

Aggregations

SecurityContext (org.structr.common.SecurityContext)131 FrameworkException (org.structr.common.error.FrameworkException)76 App (org.structr.core.app.App)56 StructrApp (org.structr.core.app.StructrApp)56 Tx (org.structr.core.graph.Tx)36 GraphObject (org.structr.core.GraphObject)35 PropertyKey (org.structr.core.property.PropertyKey)26 PropertyMap (org.structr.core.property.PropertyMap)26 AbstractNode (org.structr.core.entity.AbstractNode)19 IOException (java.io.IOException)18 Map (java.util.Map)17 File (org.structr.web.entity.File)14 LinkedList (java.util.LinkedList)13 DatabaseService (org.structr.api.DatabaseService)12 DOMNode (org.structr.web.entity.dom.DOMNode)12 Result (org.structr.core.Result)11 PropertyConverter (org.structr.core.converter.PropertyConverter)11 GraphObjectMap (org.structr.core.GraphObjectMap)10 Query (org.structr.core.app.Query)10 Principal (org.structr.core.entity.Principal)10