Search in sources :

Example 6 with JSDataSet

use of com.servoy.j2db.dataprocessing.JSDataSet in project servoy-client by Servoy.

the class JSSecurity method js_getUserGroups.

/**
 * Get all the groups for given user UID.
 *
 * @sample
 * //get all the users in the security settings (Returns a JSDataset)
 * var dsUsers = security.getUsers()
 *
 * //loop through each user to get their group
 * //The getValue call is (row,column) where column 1 == id and 2 == name
 * for(var i=1 ; i<=dsUsers.getMaxRowIndex() ; i++)
 * {
 * 	//print to the output debugger tab: "user: " and the username
 * 	application.output("user:" + dsUsers.getValue(i,2));
 *
 * 	//set p to the user group for the current user
 * 	/** @type {JSDataSet} *&#47;
 * 	var p = security.getUserGroups(dsUsers.getValue(i,1));
 *
 * 	for(k=1;k<=p.getMaxRowIndex();k++)
 * 	{
 * 		//print to the output debugger tab: "group" and the group(s)
 * 		//the user belongs to
 * 		application.output("group: " + p.getValue(k,2));
 * 	}
 * }
 *
 * @param userUID to retrieve the user groups
 *
 * @return dataset with groupnames
 */
public JSDataSet js_getUserGroups(Object userUID) throws ServoyException {
    checkAuthorized();
    if (userUID == null)
        return null;
    String n_userUID = normalizeUID(userUID);
    try {
        String[] groups = null;
        if (n_userUID.equals(application.getUserUID())) {
            groups = application.getClientInfo().getUserGroups();
        }
        if (groups == null) {
            groups = application.getUserManager().getUserGroups(application.getClientID(), n_userUID.toString());
        }
        if (groups != null) {
            List rows = new ArrayList();
            for (String element : groups) {
                // fetch the id.
                int groupId = getGroupId(element);
                rows.add(new Object[] { new Integer(groupId), element });
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            return new JSDataSet(application, new BufferedDataSet(new String[] { "group_id", "group_name" }, rows));
        }
    } catch (Exception e) {
        Debug.error(e);
    }
    return new JSDataSet();
}
Also used : BufferedDataSet(com.servoy.j2db.dataprocessing.BufferedDataSet) ArrayList(java.util.ArrayList) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) ArrayList(java.util.ArrayList) List(java.util.List) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 7 with JSDataSet

use of com.servoy.j2db.dataprocessing.JSDataSet in project servoy-client by Servoy.

the class ServoyWrapFactory method wrap.

/**
 * @see org.mozilla.javascript.WrapFactory#wrap(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, java.lang.Object, java.lang.Class)
 */
@Override
public Object wrap(Context cx, Scriptable scope, Object obj, Class staticType) {
    if (application.getSolution() == null) {
        throw new ExitScriptException("killing current script, client/solution already terminated");
    }
    if (obj == null || obj == Undefined.instance || obj instanceof Scriptable || obj instanceof String || obj instanceof CharSequence || obj instanceof Number || obj instanceof Boolean) {
        return obj;
    }
    if (obj instanceof Date) {
        return cx.newObject(scope, "Date", new Object[] { new Double(NativeDate.convertToUTCMillisFromJava(((Date) obj).getTime())) });
    }
    if (obj instanceof DbIdentValue || obj instanceof UUID) {
        return new NativeJavaObject(scope, obj, ScriptObjectRegistry.getJavaMembers(obj.getClass(), null));
    }
    if (cx != null) {
        if (obj instanceof JSConvertedMap<?, ?>) {
            Scriptable newObject = null;
            JSConvertedMap<?, ?> map = (JSConvertedMap<?, ?>) obj;
            if (map.getConstructorName() != null) {
                newObject = cx.newObject(scope, map.getConstructorName());
            } else {
                newObject = cx.newObject(scope);
            }
            Iterator<?> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<?, ?> next = (Entry<?, ?>) iterator.next();
                Object key = next.getKey();
                Object value = next.getValue();
                if (value != null) {
                    value = wrap(cx, newObject, value, value.getClass());
                }
                if (key instanceof Integer) {
                    newObject.put(((Integer) key).intValue(), newObject, value);
                } else if (key instanceof String) {
                    newObject.put((String) key, newObject, value);
                } else {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    Debug.error("Try to create a JSConvertedMap->NativeObject with a key that isnt a string or integer:" + key + " for value: " + value);
                }
            }
            return newObject;
        }
        if (obj.getClass() == JSONObject.class) {
            JSONObject json = (JSONObject) obj;
            Scriptable newObject = cx.newObject(scope);
            Iterator<String> iterator = json.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();
                Object value = null;
                try {
                    value = ServoyJSONObject.jsonNullToNull(json.get(key));
                } catch (JSONException e) {
                    Debug.error(e);
                }
                if (value != null) {
                    value = wrap(cx, newObject, value, value.getClass());
                }
                newObject.put(key, newObject, value);
            }
            return newObject;
        }
        if (obj.getClass() == JSONArray.class) {
            JSONArray array = (JSONArray) obj;
            Scriptable newObject = cx.newObject(scope, "Array");
            for (int i = 0; i < array.length(); i++) {
                Object value = null;
                try {
                    value = array.get(i);
                } catch (JSONException e) {
                    Debug.error(e);
                }
                if (value != null) {
                    value = wrap(cx, newObject, value, value.getClass());
                }
                newObject.put(i, newObject, value);
            }
            return newObject;
        }
    }
    if (obj instanceof IDataSet) {
        return new JSDataSet(application, (IDataSet) obj);
    }
    if (obj instanceof FormController) {
        throw new IllegalArgumentException(// $NON-NLS-1$
        "Bad practice: FormController cant be wrapped to javascript (for example not usable as argument in Scheduler plugin)");
    }
    // if it is a none primitive array
    if (obj.getClass().isArray() && !obj.getClass().getComponentType().isPrimitive()) {
        // then convert the array to a NativeArray, first convert all the elements of the array itself.
        Object[] source = (Object[]) obj;
        Object[] array = new Object[source.length];
        for (int i = 0; i < source.length; i++) {
            Object src = source[i];
            array[i] = wrap(cx, scope, src, src != null ? src.getClass() : null);
        }
        NativeArray result = obj.getClass().getComponentType() != Object.class ? new ServoyNativeArray(array, obj.getClass().getComponentType()) : new NativeArray(array);
        ScriptRuntime.setBuiltinProtoAndParent(result, scope, TopLevel.Builtins.Array);
        return result;
    }
    return super.wrap(cx, scope, obj, staticType);
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) Entry(java.util.Map.Entry) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) UUID(com.servoy.j2db.util.UUID) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) FormController(com.servoy.j2db.FormController) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Scriptable(org.mozilla.javascript.Scriptable) ExitScriptException(com.servoy.j2db.ExitScriptException) Date(java.util.Date) NativeDate(org.mozilla.javascript.NativeDate) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) Map(java.util.Map)

Example 8 with JSDataSet

use of com.servoy.j2db.dataprocessing.JSDataSet in project servoy-client by Servoy.

the class AbstractRuntimeValuelistComponent method setValueListItems.

public void setValueListItems(Object value) {
    if (getComponent() instanceof ISupportValueList) {
        IValueList list = ((ISupportValueList) getComponent()).getValueList();
        if (list != null && (value instanceof JSDataSet || value instanceof IDataSet)) {
            String name = list.getName();
            ValueList valuelist = application.getFlattenedSolution().getValueList(name);
            if (valuelist != null && valuelist.getValueListType() == ValueList.CUSTOM_VALUES) {
                ParsedFormat format = null;
                int type = 0;
                if (list instanceof CustomValueList) {
                    format = ((CustomValueList) list).getFormat();
                    type = ((CustomValueList) list).getValueType();
                }
                IValueList newVl = ValueListFactory.fillRealValueList(application, valuelist, ValueList.CUSTOM_VALUES, format, type, value);
                ((ISupportValueList) getComponent()).setValueList(newVl);
            }
        }
    }
}
Also used : ParsedFormat(com.servoy.j2db.util.FormatParser.ParsedFormat) CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) ISupportValueList(com.servoy.j2db.ui.ISupportValueList) ValueList(com.servoy.j2db.persistence.ValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList) ISupportValueList(com.servoy.j2db.ui.ISupportValueList) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) IValueList(com.servoy.j2db.dataprocessing.IValueList)

Example 9 with JSDataSet

use of com.servoy.j2db.dataprocessing.JSDataSet in project servoy-client by Servoy.

the class SwingForm method getFormContext.

public JSDataSet getFormContext() {
    SwingForm current = this;
    FormLookupPanel currentLookupPanel = null;
    SpecialTabPanel currentTabPanel = null;
    String currentBeanName = null;
    SpecialSplitPane currentSplitPane = null;
    IDataSet set = new BufferedDataSet(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$//$NON-NLS-6$
    new String[] { "containername", "formname", "tabpanel/splitpane/accordion/beanname", "tabname", "tabindex", "tabindex1based" }, new ArrayList<Object[]>());
    set.addRow(new Object[] { null, current.formController.getName(), null, null, null, null });
    Container parent = getParent();
    while (parent != null) {
        if (parent instanceof SpecialTabPanel) {
            currentTabPanel = (SpecialTabPanel) parent;
        } else if (parent instanceof SpecialSplitPane) {
            currentSplitPane = (SpecialSplitPane) parent;
        } else if (parent instanceof FormLookupPanel) {
            currentLookupPanel = (FormLookupPanel) parent;
        } else if (parent instanceof IServoyAwareBean && parent instanceof IComponent) {
            currentBeanName = ((IComponent) parent).getName();
        } else if (parent instanceof SwingForm) {
            current = (SwingForm) parent;
            if (currentTabPanel != null) {
                ITabPaneAlike panel = currentTabPanel.getEnclosingComponent();
                int index = -1;
                String tabName = null;
                if (currentLookupPanel != null) {
                    index = panel.getTabIndex(currentLookupPanel);
                    if (index != -1) {
                        tabName = panel.getNameAt(index);
                    }
                }
                set.addRow(0, new Object[] { null, current.formController.getName(), currentTabPanel.getName(), tabName, new Integer(index), new Integer(index + 1) });
            } else if (currentBeanName != null) {
                set.addRow(0, new Object[] { null, current.formController.getName(), currentBeanName, null, null, null });
            } else if (currentSplitPane != null) {
                int idx = currentLookupPanel != null && currentLookupPanel.equals(currentSplitPane.getLeftForm()) ? 0 : 1;
                set.addRow(0, new Object[] { null, current.formController.getName(), currentSplitPane.getName(), currentSplitPane.getTabNameAt(idx), new Integer(idx + 1), new Integer(idx + 1) });
            } else {
                set.addRow(0, new Object[] { null, current.formController.getName(), null, null, null, null });
            }
            currentBeanName = null;
            currentTabPanel = null;
            currentLookupPanel = null;
            currentSplitPane = null;
        } else if (parent instanceof MainPanel) {
            String containerName = ((MainPanel) parent).getContainerName();
            if (containerName != null) {
                for (int i = 0; i < set.getRowCount(); i++) {
                    set.getRow(i)[0] = containerName;
                }
            }
            return new JSDataSet(formController.getApplication(), set);
        }
        parent = parent.getParent();
    }
    return new JSDataSet(formController.getApplication(), set);
}
Also used : IComponent(com.servoy.j2db.ui.IComponent) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) ITabPaneAlike(com.servoy.j2db.util.ITabPaneAlike) Point(java.awt.Point) Container(java.awt.Container) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) IServoyAwareBean(com.servoy.j2db.dataui.IServoyAwareBean) BufferedDataSet(com.servoy.j2db.dataprocessing.BufferedDataSet) SpecialSplitPane(com.servoy.j2db.smart.dataui.SpecialSplitPane) SpecialTabPanel(com.servoy.j2db.smart.dataui.SpecialTabPanel) TwoNativeJavaObject(com.servoy.j2db.smart.scripting.TwoNativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) FormLookupPanel(com.servoy.j2db.smart.dataui.FormLookupPanel)

Example 10 with JSDataSet

use of com.servoy.j2db.dataprocessing.JSDataSet in project servoy-client by Servoy.

the class ServoyApiObject method getDataSetByQuery.

/**
 * Performs a sql query with a query builder object.
 * Will throw an exception if anything did go wrong when executing the query.
 * Will use any data filter defined on table.
 *
 * @sample
 *  var dataset = servoyApi.getDataSetByQuery(qbselect, 10);
 *
 * @param query QBSelect query.
 * @param max_returned_rows The maximum number of rows returned by the query.
 *
 * @return The JSDataSet containing the results of the query.
 */
@JSFunction
public JSDataSet getDataSetByQuery(QBSelect query, Number max_returned_rows) {
    int _max_returned_rows = Utils.getAsInteger(max_returned_rows);
    String serverName = DataSourceUtils.getDataSourceServerName(query.getDataSource());
    if (serverName == null)
        throw new RuntimeException(new ServoyException(ServoyException.InternalCodes.SERVER_NOT_FOUND, new Object[] { query.getDataSource() }));
    QuerySelect select = query.build();
    try {
        return new JSDataSet(app, ((FoundSetManager) app.getFoundSetManager()).getDataSetByQuery(serverName, select, true, _max_returned_rows));
    } catch (ServoyException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) QuerySelect(com.servoy.j2db.query.QuerySelect) ServoyException(com.servoy.j2db.util.ServoyException) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Aggregations

JSDataSet (com.servoy.j2db.dataprocessing.JSDataSet)14 IDataSet (com.servoy.j2db.dataprocessing.IDataSet)9 BufferedDataSet (com.servoy.j2db.dataprocessing.BufferedDataSet)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 List (java.util.List)4 JSONObject (org.json.JSONObject)4 ScriptableObject (org.mozilla.javascript.ScriptableObject)4 ServoyException (com.servoy.j2db.util.ServoyException)3 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)3 Point (java.awt.Point)3 HashMap (java.util.HashMap)3 NativeArray (org.mozilla.javascript.NativeArray)3 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)3 ApplicationException (com.servoy.j2db.ApplicationException)2 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)2 IValueList (com.servoy.j2db.dataprocessing.IValueList)2 IServoyAwareBean (com.servoy.j2db.dataui.IServoyAwareBean)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 ValueList (com.servoy.j2db.persistence.ValueList)2