Search in sources :

Example 6 with IDataSet

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

the class JSSecurity method js_getElementUUIDs.

/**
 * Returns the form elements UUID's as dataset, the one with no name is the form itself.
 *
 * @sample var formElementsUUIDDataSet = security.getElementUUIDs('orders_form');
 *
 * @param formname the formname to retieve the dataset for
 * @return dataset with element info
 */
public // return dataset with name, uuid (note: null name is form uuid)
JSDataSet js_getElementUUIDs(// return dataset with name, uuid (note: null name is form uuid)
String formname) {
    Form f = application.getFlattenedSolution().getForm(formname);
    if (f == null)
        f = application.getFormManager().getPossibleForm(formname);
    if (f != null) {
        List elements = new ArrayList();
        elements.add(new Object[] { null, f.getUUID() });
        Iterator<? extends IPersist> it = f.isResponsiveLayout() ? f.getFlattenedObjects(NameComparator.INSTANCE).iterator() : f.getAllObjects();
        while (it.hasNext()) {
            IPersist elem = it.next();
            int type = elem.getTypeID();
            if (type == IRepository.GRAPHICALCOMPONENTS || type == IRepository.FIELDS || type == IRepository.PORTALS || type == IRepository.RECTSHAPES || type == IRepository.SHAPES || type == IRepository.BEANS || type == IRepository.TABPANELS || type == IRepository.WEBCOMPONENTS) {
                if (elem instanceof ISupportName && ((ISupportName) elem).getName() != null) {
                    elements.add(new Object[] { ((ISupportName) elem).getName(), elem.getUUID() });
                }
            }
        }
        IDataSet set = new BufferedDataSet(new String[] { "name", "uuid" }, elements);
        return new JSDataSet(application, set);
    }
    return new JSDataSet(application);
}
Also used : BufferedDataSet(com.servoy.j2db.dataprocessing.BufferedDataSet) Form(com.servoy.j2db.persistence.Form) IPersist(com.servoy.j2db.persistence.IPersist) ISupportName(com.servoy.j2db.persistence.ISupportName) ArrayList(java.util.ArrayList) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) ArrayList(java.util.ArrayList) List(java.util.List) IDataSet(com.servoy.j2db.dataprocessing.IDataSet)

Example 7 with IDataSet

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

the class JSSecurity method js_setSecuritySettings.

/**
 * Sets the security settings; the entries contained in the given dataset will override those contained in the current security settings.
 *
 * NOTE: The security.getElementUUIDs and security.setSecuritySettings functions can be used to define custom security that overrides Servoy security.
 * For additional information see the function security.getElementUUIDs.
 *
 * @sample
 * var colNames = new Array();
 * colNames[0] = 'uuid';
 * colNames[1] = 'flags';
 * var dataset = databaseManager.createEmptyDataSet(0,colNames);
 *
 * var row = new Array();
 * row[0] = '413a4d69-becb-4ae4-8fdd-980755d6a7fb';//normally retreived via security.getElementUUIDs(...)
 * row[1] = JSSecurity.VIEWABLE|JSSecurity.ACCESSIBLE; // use bitwise 'or' for both
 * dataset.addRow(row);//setting element security
 *
 * row = new Array();
 * row[0] = 'example_data.orders';
 * row[1] = JSSecurity.READ|JSSecurity.INSERT|JSSecurity.UPDATE|JSSecurity.DELETE|JSSecurity.TRACKING; //use bitwise 'or' for multiple flags
 * dataset.addRow(row);//setting table security
 *
 * security.setSecuritySettings(dataset);//to be called in solution startup method
 *
 * @param dataset the dataset with security settings
 */
public // uuid/server.tablename , integer(flags)
void js_setSecuritySettings(// uuid/server.tablename , integer(flags)
Object dataset) {
    if (dataset instanceof JSDataSet) {
        dataset = ((JSDataSet) dataset).getDataSet();
    }
    if (dataset instanceof IDataSet) {
        application.getFoundSetManager().getEditRecordList().clearSecuritySettings();
        Map<Object, Integer> sp = new HashMap<Object, Integer>();
        IDataSet ds = (IDataSet) dataset;
        if (ds.getColumnCount() < 2)
            return;
        for (int i = 0; i < ds.getRowCount(); i++) {
            Object[] row = ds.getRow(i);
            if (row[0] != null && row[1] != null) {
                Integer val = new Integer(Utils.getAsInteger(row[1]));
                try {
                    boolean matched = false;
                    if (row[0] instanceof UUID) {
                        sp.put(row[0], val);
                        matched = true;
                    } else if (row[0].toString().indexOf('-') > 0) {
                        UUID uuid = UUID.fromString(row[0].toString());
                        sp.put(uuid, val);
                        matched = true;
                    } else {
                        String datasource = row[0].toString();
                        if (datasource.indexOf('.') != -1) {
                            String[] server_table = datasource.split("\\.");
                            if (server_table.length == 2) {
                                IServer server = application.getSolution().getServer(server_table[0]);
                                if (server != null) {
                                    ITable table = server.getTable(server_table[1]);
                                    if (table != null) {
                                        Iterator<String> it = table.getRowIdentColumnNames();
                                        if (it.hasNext()) {
                                            sp.put(Utils.getDotQualitfied(table.getServerName(), table.getName(), it.next()), val);
                                            matched = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!matched) {
                        Debug.error("security.setSecuritySettings: could not apply security settings for '" + row[0] + "'");
                    }
                } catch (Exception e) {
                    Debug.error(e);
                }
            }
        }
        application.getFlattenedSolution().overrideSecurityAccess(sp);
    }
}
Also used : IServer(com.servoy.j2db.persistence.IServer) HashMap(java.util.HashMap) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Iterator(java.util.Iterator) ITable(com.servoy.j2db.persistence.ITable) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) UUID(com.servoy.j2db.util.UUID)

Example 8 with IDataSet

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

the class ScriptVariableScope method put.

public Object put(String name, Object val) {
    Object value = Utils.mapToNullIfUnmanageble(val);
    Integer variableType = nameType.get(name);
    int type = 0;
    boolean xmlType = false;
    if (variableType == null) {
        // global doesn't exist. dynamic new one.. so MEDIA type
        type = IColumnTypes.MEDIA;
        nameType.put(name, new Integer(type));
        // $NON-NLS-1$//$NON-NLS-2$
        Debug.trace("Warning: " + name + " is not defined in the variables, a dynamic (media type) variable is created");
    } else {
        if (variableType.intValue() == Types.OTHER) {
            type = IColumnTypes.MEDIA;
            xmlType = true;
        } else {
            type = Column.mapToDefaultType(variableType.intValue());
        }
    }
    if (type == IColumnTypes.TEXT) {
        Object txt = value;
        while (txt instanceof IDelegate<?>) {
            txt = ((IDelegate<?>) txt).getDelegate();
        }
        if (txt instanceof IDataSet) {
            IDataSet set = (IDataSet) txt;
            StringBuilder sb = new StringBuilder();
            sb.append('\n');
            for (int i = 0; i < set.getRowCount(); i++) {
                sb.append(set.getRow(i)[0]);
                sb.append('\n');
            }
            value = sb.toString();
        } else if (txt instanceof FoundSet) {
            StringBuilder sb = new StringBuilder();
            sb.append('\n');
            FoundSet fs = (FoundSet) txt;
            for (int i = 0; i < fs.getSize(); i++) {
                IRecordInternal record = fs.getRecord(i);
                sb.append(record.getPKHashKey());
                sb.append('\n');
            }
            value = sb.toString();
        }
    }
    if (value != null && variableType != null) {
        Object unwrapped = value;
        while (unwrapped instanceof Wrapper) {
            unwrapped = ((Wrapper) unwrapped).unwrap();
            if (unwrapped == value) {
                break;
            }
        }
        if (type == IColumnTypes.MEDIA) {
            if (!(unwrapped instanceof UUID)) {
                Object previousValue = get(name);
                if (previousValue instanceof UUID || previousValue == null) {
                    Iterator<ScriptVariable> scriptVariablesIte = getScriptLookup().getScriptVariables(false);
                    ScriptVariable sv;
                    while (scriptVariablesIte.hasNext()) {
                        sv = scriptVariablesIte.next();
                        if (name.equals(sv.getName())) {
                            if (UUID.class.getSimpleName().equals(getDeclaredType(sv))) {
                                value = Utils.getAsUUID(unwrapped, false);
                            }
                            break;
                        }
                    }
                }
            }
        } else {
            value = unwrapped;
            try {
                // dont convert with timezone here, its not ui but from scripting
                value = Column.getAsRightType(variableType.intValue(), IBaseColumn.NORMAL_COLUMN, value, null, Integer.MAX_VALUE, null, true, false);
            } catch (Exception e) {
                throw new IllegalArgumentException(// $NON-NLS-1$
                Messages.getString(// $NON-NLS-1$
                "servoy.conversion.error.global", new Object[] { name, Column.getDisplayTypeString(variableType.intValue()), value }));
            }
        }
    }
    if (value instanceof Date) {
        // make copy so then when it is further used in js it won't change this one.
        value = new Date(((Date) value).getTime());
    } else if (xmlType && value instanceof String) {
        // $NON-NLS-1$
        value = evalValue(name, (String) value, "internal_anon", -1);
    }
    Object oldVar = allVars.get(name);
    allVars.put(name, value);
    if (variableType != null && !Utils.equalObjects(oldVar, value)) {
        fireModificationEvent(name, value);
    }
    return oldVar;
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) WrappedException(org.mozilla.javascript.WrappedException) Date(java.util.Date) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) XMLObject(org.mozilla.javascript.xml.XMLObject) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) UUID(com.servoy.j2db.util.UUID) IDelegate(com.servoy.j2db.util.IDelegate)

Example 9 with IDataSet

use of com.servoy.j2db.dataprocessing.IDataSet 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 10 with IDataSet

use of com.servoy.j2db.dataprocessing.IDataSet 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)

Aggregations

IDataSet (com.servoy.j2db.dataprocessing.IDataSet)17 JSDataSet (com.servoy.j2db.dataprocessing.JSDataSet)9 BufferedDataSet (com.servoy.j2db.dataprocessing.BufferedDataSet)5 QuerySelect (com.servoy.j2db.query.QuerySelect)5 Column (com.servoy.j2db.persistence.Column)4 CompareCondition (com.servoy.j2db.query.CompareCondition)4 QueryColumn (com.servoy.j2db.query.QueryColumn)4 ArrayList (java.util.ArrayList)4 IDataServer (com.servoy.j2db.dataprocessing.IDataServer)3 QueryColumnValue (com.servoy.j2db.query.QueryColumnValue)3 QueryTable (com.servoy.j2db.query.QueryTable)3 UUID (com.servoy.j2db.util.UUID)3 Point (java.awt.Point)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 ApplicationException (com.servoy.j2db.ApplicationException)2 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)2 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)2