Example usage for jdk.nashorn.api.scripting JSObject hasMember

List of usage examples for jdk.nashorn.api.scripting JSObject hasMember

Introduction

In this page you can find the example usage for jdk.nashorn.api.scripting JSObject hasMember.

Prototype

public boolean hasMember(final String name);

Source Link

Document

Does this object have a named member?

Usage

From source file:com.eas.client.queries.LocalQueriesProxy.java

private void readScriptFields(String aQueryName, JSObject sSchema, Fields fields, Scripts.Space aSpace) {
    Object oLength = sSchema.getMember("length");
    if (oLength instanceof Number) {
        int length = ((Number) oLength).intValue();
        for (int i = 0; i < length; i++) {
            Object oElement = sSchema.getSlot(i);
            if (oElement instanceof JSObject) {
                JSObject sElement = (JSObject) oElement;
                Object oFieldName = aSpace
                        .toJava(sElement.hasMember("name") ? sElement.getMember("name") : null);
                if (oFieldName instanceof String && !((String) oFieldName).isEmpty()) {
                    String sFieldName = (String) oFieldName;
                    Field field = fields instanceof Parameters ? new Parameter() : new Field();
                    field.setTypeInfo(DataTypeInfo.OTHER);
                    fields.add(field);//from   www  . j  a v  a  2 s . c om
                    field.setName(sFieldName);
                    field.setOriginalName(sFieldName);
                    Object oEntity = aSpace
                            .toJava(sElement.hasMember("entity") ? sElement.getMember("entity") : null);
                    if (oEntity instanceof String && !((String) oEntity).isEmpty()) {
                        field.setTableName((String) oEntity);
                    } else {
                        field.setTableName(aQueryName);
                    }
                    Object oDescription = aSpace.toJava(
                            sElement.hasMember("description") ? sElement.getMember("description") : null);
                    if (oDescription instanceof String && !((String) oDescription).isEmpty()) {
                        field.setDescription((String) oDescription);
                    }
                    Object oType = sElement.getMember("type");
                    if (oType instanceof JSObject && ((JSObject) oType).isFunction()) {
                        Object ofName = aSpace.toJava(((JSObject) oType).getMember("name"));
                        if (ofName instanceof String) {
                            String fName = (String) ofName;
                            if (String.class.getSimpleName().equals(fName)) {
                                field.setTypeInfo(DataTypeInfo.VARCHAR.copy());
                            } else if (Number.class.getSimpleName().equals(fName)) {
                                field.setTypeInfo(DataTypeInfo.DECIMAL.copy());
                            } else if (Boolean.class.getSimpleName().equals(fName)) {
                                field.setTypeInfo(DataTypeInfo.BOOLEAN.copy());
                            } else if (Date.class.getSimpleName().equals(fName)) {
                                field.setTypeInfo(DataTypeInfo.TIMESTAMP.copy());
                            }
                        }
                    }
                    Object oRequired = aSpace
                            .toJava(sElement.hasMember("required") ? sElement.getMember("required") : null);
                    if (oRequired instanceof Boolean) {
                        boolean bRequired = (Boolean) oRequired;
                        field.setNullable(!bRequired);
                    }
                    Object oKey = aSpace.toJava(sElement.hasMember("key") ? sElement.getMember("key") : null);
                    if (oKey instanceof Boolean) {
                        boolean bKey = (Boolean) oKey;
                        field.setPk(bKey);
                        field.setNullable(false);
                    }
                    Object oRef = sElement.hasMember("ref") ? sElement.getMember("ref") : null;
                    if (oRef instanceof JSObject) {
                        JSObject sRef = (JSObject) oRef;
                        Object oProperty = aSpace
                                .toJava(sRef.hasMember("property") ? sRef.getMember("property") : null);
                        if (oProperty instanceof String) {
                            String sProperty = (String) oProperty;
                            if (!sProperty.isEmpty()) {
                                Object oRefEntity = sRef.hasMember("entity") ? sRef.getMember("entity") : null;
                                String sRefEntity;
                                if (oRefEntity instanceof String && !((String) oRefEntity).isEmpty()) {
                                    sRefEntity = (String) oRefEntity;
                                } else {
                                    sRefEntity = aQueryName;
                                }
                                field.setFk(new ForeignKeySpec(null, aQueryName, field.getName(), null,
                                        ForeignKeySpec.ForeignKeyRule.CASCADE,
                                        ForeignKeySpec.ForeignKeyRule.CASCADE, false, null, sRefEntity,
                                        sProperty, null));
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:com.eas.client.queries.LocalQueriesProxy.java

protected SqlQuery queryFromModule(String aModuleName, Scripts.Space aSpace) throws Exception {
    SqlQuery query = new ScriptedQuery(core, aModuleName);
    JSObject schemaContainer = createModule(aModuleName, aSpace);
    if (schemaContainer != null) {
        Fields fields = new Fields();
        query.setFields(fields);// w w w .java2 s  . c om
        Object oSchema = schemaContainer.hasMember("schema") ? schemaContainer.getMember("schema") : null;
        if (oSchema instanceof JSObject) {
            readScriptFields(aModuleName, (JSObject) oSchema, fields, aSpace);
            Parameters params;
            Object oParams = schemaContainer.hasMember("params") ? schemaContainer.getMember("params") : null;
            if (oParams instanceof JSObject) {
                params = new Parameters();
                readScriptFields(aModuleName, (JSObject) oParams, params, aSpace);
                params.toCollection().stream().forEach((p) -> {
                    query.putParameter(p.getName(), p.getTypeInfo(), null);
                });
            }
            return query;
        } else {
            throw new IllegalStateException(" datasource module: " + aModuleName + " doesn't contain a schema");
        }
    } else {
        throw new IllegalStateException(" datasource module: " + aModuleName + " is not found");
    }
}

From source file:com.eas.client.queries.ScriptedQuery.java

@Override
public JSObject execute(Scripts.Space aSpace, Consumer<JSObject> onSuccess, Consumer<Exception> onFailure)
        throws Exception {
    assert Scripts.getSpace() == aSpace : "Scripts.Space TLS assumption failed";
    if (onSuccess != null) {
        ScriptedResource._require(new String[] { entityName }, null, aSpace, new HashSet<>(), (Void v) -> {
            JSObject source = aSpace.createModule(entityName);
            if (source.hasMember("fetch")) {
                Object oFetch = source.getMember("fetch");
                if (oFetch instanceof JSObject) {
                    JSObject jsFetch = (JSObject) oFetch;
                    if (jsFetch.isFunction()) {
                        JSObject jsParams = aSpace.makeObj();
                        for (int i = 0; i < params.getParametersCount(); i++) {
                            Parameter p = params.get(i + 1);
                            jsParams.setMember(p.getName(), aSpace.toJs(p.getValue()));
                        }//from   w w  w . jav a 2s  . c o  m
                        final ExecutionChecker exChecker = new ExecutionChecker();
                        Object oRowset = jsFetch.call(source,
                                aSpace.toJs(new Object[] { jsParams, new AbstractJSObject() {

                                    @Override
                                    public Object call(final Object thiz, final Object... args) {
                                        if (exChecker.isExecutionNeeded()) {
                                            try {
                                                JSObject jsRowset = args.length > 0
                                                        ? (JSObject) aSpace.toJava(args[0])
                                                        : null;
                                                try {
                                                    onSuccess.accept(jsRowset);
                                                } catch (Exception ex) {
                                                    Logger.getLogger(ScriptedQuery.class.getName())
                                                            .log(Level.SEVERE, null, ex);
                                                }
                                            } catch (Exception ex) {
                                                if (onFailure != null) {
                                                    onFailure.accept(ex);
                                                }
                                            }
                                        }
                                        return null;
                                    }
                                }, new AbstractJSObject() {

                                    @Override
                                    public Object call(final Object thiz, final Object... args) {
                                        if (exChecker.isExecutionNeeded()) {
                                            if (onFailure != null) {
                                                if (args.length > 0) {
                                                    if (args[0] instanceof Exception) {
                                                        onFailure.accept((Exception) args[0]);
                                                    } else {
                                                        onFailure.accept(new Exception(
                                                                String.valueOf(aSpace.toJava(args[0]))));
                                                    }
                                                } else {
                                                    onFailure.accept(new Exception(
                                                            "No error information from fetch method"));
                                                }
                                            }
                                        }
                                        return null;
                                    }
                                } }));
                        if (!JSType.nullOrUndefined(oRowset)) {
                            onSuccess.accept((JSObject) aSpace.toJava(oRowset));
                            exChecker.setExecutionNeeded(false);
                        }
                    }
                }
            }
        }, onFailure);
        return null;
    } else {
        JSObject source = aSpace.createModule(entityName);
        if (source.hasMember("fetch")) {
            Object oFetch = source.getMember("fetch");
            if (oFetch instanceof JSObject) {
                JSObject jsFetch = (JSObject) oFetch;
                if (jsFetch.isFunction()) {
                    JSObject jsParams = aSpace.makeObj();
                    Object oRowset = jsFetch.call(source, aSpace.toJs(new Object[] { jsParams }));
                    if (!JSType.nullOrUndefined(oRowset)) {
                        return (JSObject) aSpace.toJava(oRowset);
                    }
                }
            }
        }
        return null;
    }
}

From source file:com.eas.client.threetier.json.FieldsJSONReader.java

public static void readFields(JSObject pa, Fields aFields) {
    int length = JSType.toInteger(pa.getMember("length"));
    for (int i = 0; i < length; i++) {
        JSObject po = (JSObject) pa.getSlot(i);
        assert po != null;
        String name = JSType.toString(po.getMember(NAME_PROP_NAME));
        String desc = JSType.toString(po.getMember(DESCRIPTION_PROP_NAME));

        String type = po.hasMember(TYPE_PROP_NAME) && po.getMember(TYPE_PROP_NAME) != null
                ? JSType.toString(po.getMember(TYPE_PROP_NAME))
                : null;/*from   w  ww  .ja v a 2s  .c o  m*/
        boolean pk = JSType.toBoolean(po.getMember(PK_PROP_NAME));
        boolean nullable = JSType.toBoolean(po.getMember(NULLABLE_PROP_NAME));
        Field f = aFields instanceof Parameters ? new Parameter(name) : new Field(name);
        f.setDescription(desc);
        f.setType(type);
        f.setPk(pk);
        f.setNullable(nullable);
        aFields.add(f);
    }
}

From source file:de.axelfaust.alfresco.nashorn.repo.utils.SpecialModuleHandler.java

License:Open Source License

protected static boolean getBoolean(final JSObject delegate, final String member) {
    boolean result;

    if (delegate.hasMember(member)) {
        final Object memberValue = delegate.getMember(member);
        result = Boolean.TRUE.equals(memberValue);
    } else {/*from  w w w  .j  ava  2s.co  m*/
        result = false;
    }

    return result;
}