Search in sources :

Example 6 with GraphObjectMap

use of org.structr.core.GraphObjectMap 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 7 with GraphObjectMap

use of org.structr.core.GraphObjectMap 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 8 with GraphObjectMap

use of org.structr.core.GraphObjectMap in project structr by structr.

the class FromJsonFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
    if (sources != null && sources.length > 0) {
        if (sources[0] == null) {
            return "";
        }
        try {
            final String source = sources[0].toString();
            final Gson gson = new GsonBuilder().create();
            List<Map<String, Object>> objects = new LinkedList<>();
            if (StringUtils.startsWith(source, "[")) {
                final List<Map<String, Object>> list = gson.fromJson(source, new TypeToken<List<Map<String, Object>>>() {
                }.getType());
                final List<GraphObjectMap> elements = new LinkedList<>();
                if (list != null) {
                    objects.addAll(list);
                }
                for (final Map<String, Object> src : objects) {
                    final GraphObjectMap destination = new GraphObjectMap();
                    elements.add(destination);
                    recursivelyConvertMapToGraphObjectMap(destination, src, 0);
                }
                return elements;
            } else if (StringUtils.startsWith(source, "{")) {
                final Map<String, Object> value = gson.fromJson(source, new TypeToken<Map<String, Object>>() {
                }.getType());
                final GraphObjectMap destination = new GraphObjectMap();
                if (value != null) {
                    recursivelyConvertMapToGraphObjectMap(destination, value, 0);
                }
                return destination;
            }
        } catch (Throwable t) {
            logException(caller, t, sources);
        }
        return "";
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return usage(ctx.isJavaScriptContext());
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList) TypeToken(com.google.gson.reflect.TypeToken) GraphObjectMap(org.structr.core.GraphObjectMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 9 with GraphObjectMap

use of org.structr.core.GraphObjectMap in project structr by structr.

the class UiFunction method headFromUrl.

protected GraphObjectMap headFromUrl(final ActionContext ctx, final String requestUrl, final String username, final String password) throws IOException, FrameworkException {
    final Map<String, String> headers = HttpHelper.head(requestUrl, password, username, ctx.getHeaders());
    final GraphObjectMap response = new GraphObjectMap();
    response.setProperty(new IntProperty("status"), headers.get("status"));
    headers.remove("status");
    final GraphObjectMap map = new GraphObjectMap();
    for (final Entry<String, String> entry : headers.entrySet()) {
        map.put(new StringProperty(entry.getKey()), entry.getValue());
    }
    return map;
}
Also used : IntProperty(org.structr.core.property.IntProperty) GraphObjectMap(org.structr.core.GraphObjectMap) StringProperty(org.structr.core.property.StringProperty)

Example 10 with GraphObjectMap

use of org.structr.core.GraphObjectMap in project structr by structr.

the class HttpDeleteFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 1)) {
        final String uri = sources[0].toString();
        String contentType = "application/json";
        // override default content type
        if (sources.length >= 3 && sources[2] != null) {
            contentType = sources[2].toString();
        }
        final Map<String, String> responseData = HttpHelper.delete(uri, null, null, ctx.getHeaders());
        final int statusCode = Integer.parseInt(responseData.get("status"));
        responseData.remove("status");
        final String responseBody = responseData.get("body");
        responseData.remove("body");
        final GraphObjectMap response = new GraphObjectMap();
        if ("application/json".equals(contentType)) {
            final FromJsonFunction fromJsonFunction = new FromJsonFunction();
            response.setProperty(new StringProperty("body"), fromJsonFunction.apply(ctx, caller, new Object[] { responseBody }));
        } else {
            response.setProperty(new StringProperty("body"), responseBody);
        }
        response.setProperty(new IntProperty("status"), statusCode);
        final GraphObjectMap map = new GraphObjectMap();
        for (final Map.Entry<String, String> entry : responseData.entrySet()) {
            map.put(new StringProperty(entry.getKey()), entry.getValue());
        }
        response.setProperty(new StringProperty("headers"), map);
        return response;
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : IntProperty(org.structr.core.property.IntProperty) GraphObjectMap(org.structr.core.GraphObjectMap) StringProperty(org.structr.core.property.StringProperty) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Aggregations

GraphObjectMap (org.structr.core.GraphObjectMap)60 LinkedList (java.util.LinkedList)27 GraphObject (org.structr.core.GraphObject)24 Map (java.util.Map)18 FrameworkException (org.structr.common.error.FrameworkException)16 StringProperty (org.structr.core.property.StringProperty)15 GenericProperty (org.structr.core.property.GenericProperty)13 PropertyKey (org.structr.core.property.PropertyKey)12 List (java.util.List)11 Result (org.structr.core.Result)10 SecurityContext (org.structr.common.SecurityContext)8 IntProperty (org.structr.core.property.IntProperty)8 RestMethodResult (org.structr.rest.RestMethodResult)8 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)6 Test (org.junit.Test)6 ConfigurationProvider (org.structr.schema.ConfigurationProvider)6 HashMap (java.util.HashMap)5 LinkedHashSet (java.util.LinkedHashSet)5 Tx (org.structr.core.graph.Tx)5