Search in sources :

Example 31 with GraphObject

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

the class StructrPropertyValueChannel method setConvertedPropertyValue.

public static void setConvertedPropertyValue(final SecurityContext securityContext, final GraphObject graphObject, final PropertyKey key, final String value) throws IOException, FrameworkException {
    final PropertyConverter converter = key.inputConverter(securityContext);
    final String previousUuid = graphObject.getUuid();
    Object actualValue = value;
    if (converter != null) {
        actualValue = converter.convert(actualValue);
    }
    if (key.isReadOnly()) {
        graphObject.unlockReadOnlyPropertiesOnce();
    }
    if (key.isSystemInternal()) {
        graphObject.unlockSystemPropertiesOnce();
    }
    // set value
    graphObject.setProperty(key, actualValue);
    // move "hidden" entries to new UUID
    if (GraphObject.id.equals(key)) {
        final HiddenFileEntry entry = StructrPath.HIDDEN_PROPERTY_FILES.get(previousUuid);
        if (entry != null) {
            StructrPath.HIDDEN_PROPERTY_FILES.remove(previousUuid);
            StructrPath.HIDDEN_PROPERTY_FILES.put(value, entry);
        }
    }
}
Also used : PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) HiddenFileEntry(org.structr.files.ssh.filesystem.StructrPath.HiddenFileEntry)

Example 32 with GraphObject

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

the class ToExcelFunction method apply.

@Override
public Object apply(ActionContext ctx, Object caller, Object[] sources) throws FrameworkException {
    try {
        if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 8)) {
            if (!(sources[0] instanceof List)) {
                logParameterError(caller, sources, ctx.isJavaScriptContext());
                return "ERROR: First parameter must be a collection! ".concat(usage(ctx.isJavaScriptContext()));
            }
            final List<GraphObject> nodes = (List) sources[0];
            boolean includeHeader = true;
            boolean localizeHeader = false;
            String headerLocalizationDomain = null;
            String propertyView = null;
            List<String> properties = null;
            // we are using size() instead of isEmpty() because NativeArray.isEmpty() always returns true
            if (nodes.size() == 0) {
                logger.warn("to_csv(): Can not create Excel if no nodes are given!");
                logParameterError(caller, sources, ctx.isJavaScriptContext());
                return "";
            }
            switch(sources.length) {
                case 5:
                    headerLocalizationDomain = (String) sources[7];
                case 4:
                    localizeHeader = (Boolean) sources[6];
                case 3:
                    includeHeader = (Boolean) sources[5];
                case 2:
                    {
                        if (sources[1] instanceof String) {
                            // view is given
                            propertyView = (String) sources[1];
                        } else if (sources[1] instanceof List) {
                            // named properties are given
                            properties = (List) sources[1];
                            // we are using size() instead of isEmpty() because NativeArray.isEmpty() always returns true
                            if (properties.size() == 0) {
                                logger.warn("to_excel(): Can not create Excel if list of properties is empty!");
                                logParameterError(caller, sources, ctx.isJavaScriptContext());
                                return "";
                            }
                        } else {
                            logParameterError(caller, sources, ctx.isJavaScriptContext());
                            return "ERROR: Second parameter must be a collection of property names or a single property view!".concat(usage(ctx.isJavaScriptContext()));
                        }
                    }
            }
            try {
                final Workbook wb = writeExcel(nodes, propertyView, properties, includeHeader, localizeHeader, headerLocalizationDomain, ctx.getLocale());
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                wb.write(baos);
                return baos.toString("ISO-8859-1");
            } catch (Throwable t) {
                logger.warn("to_excel(): Exception occurred", t);
                return "";
            }
        } else {
            logParameterError(caller, sources, ctx.isJavaScriptContext());
            return usage(ctx.isJavaScriptContext());
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GraphObject(org.structr.core.GraphObject) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 33 with GraphObject

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

the class AbstractTabCompletionProvider method getTabCompletionForUUIDs.

protected List<TabCompletionResult> getTabCompletionForUUIDs(final SecurityContext securityContext, final String token, final String suffix) {
    final List<TabCompletionResult> results = new LinkedList<>();
    if (token.length() >= 3) {
        final App app = StructrApp.getInstance(securityContext);
        try (final Tx tx = app.tx()) {
            final String tenantIdentifier = app.getDatabaseService().getTenantIdentifier();
            final StringBuilder buf = new StringBuilder();
            buf.append("MATCH (n");
            if (tenantIdentifier != null) {
                buf.append(":");
                buf.append(tenantIdentifier);
            }
            buf.append(") WHERE n.id STARTS WITH {part} RETURN n");
            for (final GraphObject obj : app.cypher(buf.toString(), toMap("part", token))) {
                results.add(getCompletion(obj.getUuid(), token, suffix));
            }
            tx.success();
        } catch (FrameworkException ignore) {
        }
    }
    return results;
}
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) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList)

Example 34 with GraphObject

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

the class Scripting method replaceVariables.

public static String replaceVariables(final ActionContext actionContext, final GraphObject entity, final Object rawValue) throws FrameworkException {
    if (rawValue == null) {
        return null;
    }
    String value;
    if (rawValue instanceof String) {
        value = (String) rawValue;
        if (!actionContext.returnRawValue()) {
            final List<Tuple> replacements = new LinkedList<>();
            for (final String expression : extractScripts(value)) {
                try {
                    final Object extractedValue = evaluate(actionContext, entity, expression, "script source");
                    String partValue = extractedValue != null ? formatToDefaultDateOrString(extractedValue) : "";
                    if (partValue != null) {
                        replacements.add(new Tuple(expression, partValue));
                    } else {
                        // all browsers
                        if (!value.equals(expression)) {
                            replacements.add(new Tuple(expression, ""));
                        }
                    }
                } catch (UnlicensedException ex) {
                    ex.log(logger);
                }
            }
            // apply replacements
            for (final Tuple tuple : replacements) {
                // only replace a single occurrence at a time!
                value = StringUtils.replaceOnce(value, tuple.key, tuple.value);
            }
        }
    } else if (rawValue instanceof Boolean) {
        value = Boolean.toString((Boolean) rawValue);
    } else {
        value = rawValue.toString();
    }
    if (Functions.NULL_STRING.equals(value)) {
        // return literal null for a single ___NULL___
        return null;
    } else {
        // Replace ___NULL___ by empty string
        value = StringUtils.replaceAll(value, Functions.NULL_STRING, "");
    }
    return value;
}
Also used : UnlicensedException(org.structr.common.error.UnlicensedException) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList)

Example 35 with GraphObject

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

the class Scripting method evaluateScript.

// ----- private methods -----
private static Object evaluateScript(final ActionContext actionContext, final GraphObject entity, final String engineName, final String script) throws FrameworkException {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName(engineName);
    if (engine == null) {
        throw new RuntimeException(engineName + " script engine could not be initialized. Check class path.");
    }
    final ScriptContext scriptContext = engine.getContext();
    final Bindings bindings = new StructrScriptBindings(actionContext, entity);
    if (!(engine instanceof RenjinScriptEngine)) {
        scriptContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
    }
    StringWriter output = new StringWriter();
    scriptContext.setWriter(output);
    try {
        engine.eval(script);
        Object extractedValue = output.toString();
        return extractedValue;
    } catch (final ScriptException e) {
        logger.error("Error while processing {} script: {}", engineName, script, e);
    }
    return null;
}
Also used : ScriptException(javax.script.ScriptException) StringWriter(java.io.StringWriter) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptContext(javax.script.ScriptContext) GraphObject(org.structr.core.GraphObject) RenjinScriptEngine(org.renjin.script.RenjinScriptEngine) Bindings(javax.script.Bindings) RenjinScriptEngine(org.renjin.script.RenjinScriptEngine) ScriptEngine(javax.script.ScriptEngine)

Aggregations

GraphObject (org.structr.core.GraphObject)151 FrameworkException (org.structr.common.error.FrameworkException)58 PropertyKey (org.structr.core.property.PropertyKey)39 LinkedList (java.util.LinkedList)35 SecurityContext (org.structr.common.SecurityContext)25 App (org.structr.core.app.App)25 StructrApp (org.structr.core.app.StructrApp)24 Tx (org.structr.core.graph.Tx)23 List (java.util.List)22 PropertyConverter (org.structr.core.converter.PropertyConverter)22 AbstractNode (org.structr.core.entity.AbstractNode)18 GraphObjectMap (org.structr.core.GraphObjectMap)17 NodeInterface (org.structr.core.graph.NodeInterface)17 LinkedHashSet (java.util.LinkedHashSet)15 PropertyMap (org.structr.core.property.PropertyMap)13 Map (java.util.Map)12 RestMethodResult (org.structr.rest.RestMethodResult)11 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 Result (org.structr.core.Result)9 ArrayList (java.util.ArrayList)8