Search in sources :

Example 1 with JSReadonlyProperty

use of com.servoy.j2db.scripting.annotations.JSReadonlyProperty in project servoy-client by Servoy.

the class InstanceJavaMembers method makeBeanProperties.

/**
 * @see org.mozilla.javascript.JavaMembers#makeBeanProperties(Scriptable, boolean)
 */
@SuppressWarnings("nls")
@Override
protected void makeBeanProperties(boolean isStatic, boolean includePrivate) {
    Map<String, Object> ht = isStatic ? staticMembers : members;
    Map<String, Object> copy = new HashMap<String, Object>(ht);
    for (Entry<String, Object> entry : ht.entrySet()) {
        String name = entry.getKey();
        String newName = null;
        if (// $NON-NLS-1$
        name.startsWith("js_")) {
            newName = name.substring(3);
        } else {
            Object member = entry.getValue();
            if (member instanceof NativeJavaMethod) {
                if (((NativeJavaMethod) member).getMethods().length == 1) {
                    MemberBox mb = ((NativeJavaMethod) member).getMethods()[0];
                    if (mb.isMethod()) {
                        if (AnnotationManagerReflection.getInstance().isAnnotationPresent(mb.method(), cl, JSReadonlyProperty.class)) {
                            newName = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), cl, JSReadonlyProperty.class).property();
                            if (newName == null || newName.length() == 0 && (entry.getKey().startsWith("get") || entry.getKey().startsWith("is"))) {
                                newName = entry.getKey().substring(entry.getKey().startsWith("get") ? 3 : 2);
                                // Make the bean property name.
                                char ch0 = newName.charAt(0);
                                if (Character.isUpperCase(ch0)) {
                                    if (newName.length() == 1) {
                                        newName = newName.toLowerCase();
                                    } else {
                                        char ch1 = newName.charAt(1);
                                        if (!Character.isUpperCase(ch1)) {
                                            newName = Character.toLowerCase(ch0) + newName.substring(1);
                                        }
                                    }
                                }
                            }
                        } else if (AnnotationManagerReflection.getInstance().isAnnotationPresent(mb.method(), cl, JSGetter.class)) {
                            newName = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), cl, JSGetter.class).value();
                        } else if (AnnotationManagerReflection.getInstance().isAnnotationPresent(mb.method(), cl, JSSetter.class)) {
                            newName = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), cl, JSSetter.class).value();
                        }
                    }
                }
                for (MemberBox mb : ((NativeJavaMethod) member).getMethods()) {
                    if (mb.isMethod() && AnnotationManagerReflection.getInstance().isAnnotationPresent(mb.method(), cl, JSFunction.class)) {
                        String funcName = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), cl, JSFunction.class).value();
                        if (funcName == null || funcName.length() == 0) {
                            funcName = entry.getKey();
                        }
                        // $NON-NLS-1$
                        newName = "jsFunction_".concat(funcName);
                        break;
                    }
                }
            }
        }
        if (newName != null && newName.length() > 0 && !newName.equals(name) && !Ident.checkIfJavascriptKeyword(newName)) {
            putNewValueMergeForDuplicates(copy, name, newName);
        }
    }
    ht = copy;
    if (isStatic) {
        staticMembers = ht;
    } else {
        members = ht;
    }
    super.makeBeanProperties(isStatic, includePrivate);
    copy = new HashMap<String, Object>(ht);
    for (Entry<String, Object> entry : ht.entrySet()) {
        String name = entry.getKey();
        if (// $NON-NLS-1$
        name.startsWith("jsFunction_")) {
            String newName = name.substring(11);
            if (!Ident.checkIfJavascriptKeyword(newName)) {
                putNewValueMergeForDuplicates(copy, name, newName);
            }
        } else {
            Object member = entry.getValue();
            if (member instanceof NativeJavaMethod && ((NativeJavaMethod) member).getMethods().length == 1) {
                MemberBox mb = ((NativeJavaMethod) member).getMethods()[0];
                if (mb.isMethod()) {
                    // make bean property
                    JSReadonlyProperty jsReadonlyProperty = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), mb.getDeclaringClass(), JSReadonlyProperty.class);
                    if (jsReadonlyProperty != null) {
                        String propertyName = jsReadonlyProperty.property();
                        if (propertyName == null || propertyName.length() == 0) {
                            propertyName = name;
                        }
                        Object oldValue = copy.put(propertyName, new BeanProperty(mb, null, null));
                        if (oldValue instanceof NativeJavaMethod) {
                            // allow the method to be called directly as well
                            String functionName = ((NativeJavaMethod) oldValue).getFunctionName();
                            if (!functionName.equals(propertyName)) {
                                copy.put(functionName, oldValue);
                                // but do not show it
                                addMethodToDelete(functionName);
                            }
                        }
                    }
                }
            }
        }
    }
    if (isStatic) {
        staticMembers = copy;
    } else {
        members = copy;
    }
}
Also used : MemberBox(org.mozilla.javascript.MemberBox) HashMap(java.util.HashMap) JSReadonlyProperty(com.servoy.j2db.scripting.annotations.JSReadonlyProperty) NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) JSFunction(org.mozilla.javascript.annotations.JSFunction) JSGetter(org.mozilla.javascript.annotations.JSGetter)

Example 2 with JSReadonlyProperty

use of com.servoy.j2db.scripting.annotations.JSReadonlyProperty in project servoy-client by Servoy.

the class DefaultJavaScope method getJsFunctions.

public static Map<String, NativeJavaMethod> getJsFunctions(Class<?> clazz) {
    Map<String, NativeJavaMethod> jsFunctions = new HashMap<String, NativeJavaMethod>();
    try {
        for (Method method : clazz.getMethods()) {
            String name = null;
            if (// $NON-NLS-1$
            method.getName().startsWith("js_")) {
                name = method.getName().substring(3);
            } else if (// $NON-NLS-1$
            method.getName().startsWith("jsFunction_")) {
                name = method.getName().substring(11);
            } else {
                AnnotationManagerReflection annotationManager = AnnotationManagerReflection.getInstance();
                JSReadonlyProperty jsReadonlyProperty = annotationManager.getAnnotation(method, clazz, JSReadonlyProperty.class);
                if (jsReadonlyProperty != null) {
                    name = jsReadonlyProperty.property();
                    if (name == null || name.length() == 0) {
                        name = method.getName();
                    }
                } else if (annotationManager.isAnnotationPresent(method, clazz, JSFunction.class)) {
                    name = method.getName();
                }
            }
            if (name != null) {
                NativeJavaMethod nativeJavaMethod = jsFunctions.get(name);
                if (nativeJavaMethod == null) {
                    nativeJavaMethod = new NativeJavaMethod(method, name);
                } else {
                    nativeJavaMethod = new NativeJavaMethod(Utils.arrayAdd(nativeJavaMethod.getMethods(), new MemberBox(method), true), name);
                }
                jsFunctions.put(name, nativeJavaMethod);
            }
        }
    } catch (Exception e) {
        Debug.error(e);
    }
    return jsFunctions;
}
Also used : NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) MemberBox(org.mozilla.javascript.MemberBox) HashMap(java.util.HashMap) AnnotationManagerReflection(com.servoy.j2db.scripting.annotations.AnnotationManagerReflection) JSReadonlyProperty(com.servoy.j2db.scripting.annotations.JSReadonlyProperty) NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) Method(java.lang.reflect.Method)

Example 3 with JSReadonlyProperty

use of com.servoy.j2db.scripting.annotations.JSReadonlyProperty in project servoy-client by Servoy.

the class FoundSet method alldataproviders.

/**
 * Get all dataproviders of the foundset.
 *
 * @sample
 * var dataprovidersNames = %%prefix%%alldataproviders;
 * application.output("This foundset has " + dataprovidersNames.length + " data providers.")
 * for (var i=0; i<dataprovidersNames.length; i++)
 * 	application.output(dataprovidersNames[i]);
 *
 * @special
 */
@JSReadonlyProperty
public String[] alldataproviders() {
    List<String> al = new ArrayList<String>();
    Table table = (Table) getTable();
    if (table != null) {
        try {
            Iterator<Column> columnsIt = table.getColumnsSortedByName();
            while (columnsIt.hasNext()) {
                IColumn c = columnsIt.next();
                al.add(c.getDataProviderID());
            }
            Iterator<AggregateVariable> aggIt = fsm.getApplication().getFlattenedSolution().getAggregateVariables(table, true);
            while (aggIt.hasNext()) {
                AggregateVariable av = aggIt.next();
                al.add(av.getDataProviderID());
            }
            Iterator<ScriptCalculation> scriptIt = fsm.getApplication().getFlattenedSolution().getScriptCalculations(table, true);
            while (scriptIt.hasNext()) {
                ScriptCalculation sc = scriptIt.next();
                if (al.contains(sc.getDataProviderID()))
                    al.remove(sc.getDataProviderID());
                al.add(sc.getDataProviderID());
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
    return al.toArray(new String[al.size()]);
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) Table(com.servoy.j2db.persistence.Table) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IColumn(com.servoy.j2db.persistence.IColumn) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSReadonlyProperty(com.servoy.j2db.scripting.annotations.JSReadonlyProperty)

Example 4 with JSReadonlyProperty

use of com.servoy.j2db.scripting.annotations.JSReadonlyProperty in project servoy-client by Servoy.

the class QBSelect method having.

/**
 * Get the having-part of the query, used to add conditions.
 * The conditions added here are AND-ed.
 * @sample
 * var query = datasources.db.example_data.orders.createSelect();
 * query.groupBy.addPk() // have to group by on pk when using having-conditions in (foundset) pk queries
 * .root.having.add(query.joins.orders_to_order_details.columns.quantity.count.eq(0))
 * foundset.loadRecords(query)
 */
@JSReadonlyProperty
public QBLogicalCondition having() throws RepositoryException {
    if (having == null) {
        ISQLCondition c = getQuery().getHaving();
        if (!(c instanceof AndOrCondition)) {
            getQuery().setHaving(c = AndCondition.and(c, new AndCondition()));
        }
        having = new QBLogicalCondition(this, this, (AndOrCondition) c);
    }
    return having;
}
Also used : AndOrCondition(com.servoy.j2db.query.AndOrCondition) ISQLCondition(com.servoy.j2db.query.ISQLCondition) AndCondition(com.servoy.j2db.query.AndCondition) JSReadonlyProperty(com.servoy.j2db.scripting.annotations.JSReadonlyProperty)

Aggregations

JSReadonlyProperty (com.servoy.j2db.scripting.annotations.JSReadonlyProperty)4 HashMap (java.util.HashMap)2 MemberBox (org.mozilla.javascript.MemberBox)2 NativeJavaMethod (org.mozilla.javascript.NativeJavaMethod)2 BaseQueryTable (com.servoy.base.query.BaseQueryTable)1 ApplicationException (com.servoy.j2db.ApplicationException)1 AggregateVariable (com.servoy.j2db.persistence.AggregateVariable)1 Column (com.servoy.j2db.persistence.Column)1 IColumn (com.servoy.j2db.persistence.IColumn)1 ITable (com.servoy.j2db.persistence.ITable)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptCalculation (com.servoy.j2db.persistence.ScriptCalculation)1 Table (com.servoy.j2db.persistence.Table)1 AndCondition (com.servoy.j2db.query.AndCondition)1 AndOrCondition (com.servoy.j2db.query.AndOrCondition)1 ISQLCondition (com.servoy.j2db.query.ISQLCondition)1 QueryColumn (com.servoy.j2db.query.QueryColumn)1 QueryTable (com.servoy.j2db.query.QueryTable)1 AnnotationManagerReflection (com.servoy.j2db.scripting.annotations.AnnotationManagerReflection)1 SafeArrayList (com.servoy.j2db.util.SafeArrayList)1