Search in sources :

Example 16 with UUID

use of com.servoy.j2db.util.UUID in project servoy-client by Servoy.

the class WebObjectImpl method updatePersistMappedPropertyFromJSON.

protected void updatePersistMappedPropertyFromJSON(String beanJSONKey, Object object) {
    List<IChildWebObject> newChildPersistPropertiesToFire = null;
    try {
        synchronized (this) {
            if (object != null && getChildPropertyDescription(beanJSONKey) != null) {
                PropertyDescription childPd = getChildPropertyDescription(beanJSONKey);
                IPropertyType<?> propertyType = childPd.getType();
                String simpleTypeName = PropertyUtils.getSimpleNameOfCustomJSONTypeProperty(propertyType);
                if (isPersistMappedProperty(beanJSONKey)) {
                    if (ServoyJSONObject.isJavascriptUndefined(object)) {
                        persistMappedProperties.remove(beanJSONKey);
                        persistMappedPropetiesByUUID = null;
                    } else if (ServoyJSONObject.isJavascriptNull(object)) {
                        persistMappedProperties.put(beanJSONKey, null);
                        persistMappedPropetiesByUUID = null;
                    } else {
                        boolean arrayReturnType = PropertyUtils.isCustomJSONArrayPropertyType(propertyType);
                        if (!arrayReturnType) {
                            IChildWebObject childWebObject;
                            IChildWebObject existingWebObject = null;
                            // find the UUID that is stored for that key already in the .frm file - if any
                            UUID childChildWebObjectUUID = null;
                            JSONObject childWebObjectJSON = WebObjectImpl.getFullJSONInFrmFile(webObject, beanJSONKey, -1, false);
                            if (childWebObjectJSON != null && childWebObjectJSON.has(IChildWebObject.UUID_KEY)) {
                                try {
                                    childChildWebObjectUUID = UUID.fromString(childWebObjectJSON.getString(IChildWebObject.UUID_KEY));
                                } catch (IllegalArgumentException ex) {
                                    Debug.error(ex);
                                }
                                if (childChildWebObjectUUID != null) {
                                    IChildWebObject currentPersist = null;
                                    if (persistMappedProperties.containsKey(beanJSONKey) && persistMappedProperties.get(beanJSONKey) instanceof IChildWebObject) {
                                        currentPersist = (IChildWebObject) persistMappedProperties.get(beanJSONKey);
                                        if (currentPersist != null && childChildWebObjectUUID.equals(currentPersist.getUUID())) {
                                            existingWebObject = currentPersist;
                                        }
                                    }
                                }
                            }
                            if (existingWebObject == null) {
                                // "object" is not null/undefined here - there are checks for that above; so create the needed persists
                                if (isComponent(propertyType)) {
                                    childWebObject = ChildWebComponent.createNewInstance(webObject, childPd, beanJSONKey, -1, false, childChildWebObjectUUID);
                                    persistMappedProperties.put(beanJSONKey, childWebObject);
                                    if (newChildPersistPropertiesToFire == null)
                                        newChildPersistPropertiesToFire = new ArrayList<>();
                                    newChildPersistPropertiesToFire.add(childWebObject);
                                    persistMappedPropetiesByUUID = null;
                                } else if (PropertyUtils.isCustomJSONObjectProperty(propertyType)) {
                                    childWebObject = WebCustomType.createNewInstance(webObject, childPd, beanJSONKey, -1, false, childChildWebObjectUUID);
                                    childWebObject.setTypeName(simpleTypeName);
                                    persistMappedProperties.put(beanJSONKey, childWebObject);
                                    if (newChildPersistPropertiesToFire == null)
                                        newChildPersistPropertiesToFire = new ArrayList<>();
                                    newChildPersistPropertiesToFire.add(childWebObject);
                                    persistMappedPropetiesByUUID = null;
                                }
                            }
                        } else if (object instanceof JSONArray) {
                            PropertyDescription elementPD = (propertyType instanceof ICustomType<?>) ? ((ICustomType<?>) propertyType).getCustomJSONTypeDefinition() : null;
                            if (elementPD != null) {
                                ArrayList<IChildWebObject> persistMappedPropertyArray = new ArrayList<IChildWebObject>();
                                if (PropertyUtils.isCustomJSONObjectProperty(elementPD.getType()) || isComponent(propertyType)) {
                                    ArrayList<IChildWebObject> currentPersistMappedPropertyArray = new ArrayList<IChildWebObject>();
                                    if (persistMappedProperties.containsKey(beanJSONKey) && persistMappedProperties.get(beanJSONKey) instanceof IChildWebObject[]) {
                                        currentPersistMappedPropertyArray.addAll(Arrays.asList((IChildWebObject[]) persistMappedProperties.get(beanJSONKey)));
                                    }
                                    for (int i = 0; i < ((JSONArray) object).length(); i++) {
                                        IChildWebObject existingWebObject = null;
                                        UUID childChildWebObjectUUID = null;
                                        JSONObject childWebObjectJSON = WebObjectImpl.getFullJSONInFrmFile(webObject, beanJSONKey, i, false);
                                        if (childWebObjectJSON != null && childWebObjectJSON.has(IChildWebObject.UUID_KEY)) {
                                            try {
                                                childChildWebObjectUUID = UUID.fromString(childWebObjectJSON.getString(IChildWebObject.UUID_KEY));
                                            } catch (IllegalArgumentException ex) {
                                                Debug.error(ex);
                                            }
                                            if (childChildWebObjectUUID != null) {
                                                for (IChildWebObject wo : currentPersistMappedPropertyArray) {
                                                    if (wo != null && childChildWebObjectUUID.equals(wo.getUUID())) {
                                                        persistMappedPropertyArray.add(wo);
                                                        existingWebObject = wo;
                                                        // just to be sure we didn't find it at some other index
                                                        existingWebObject.setIndex(i);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        if (existingWebObject == null) {
                                            // we couldn't find an old array item with the same UUID; add a fresh value
                                            Object newJSNOValAtIdx = ((JSONArray) object).opt(i);
                                            if (ServoyJSONObject.isJavascriptNullOrUndefined(newJSNOValAtIdx)) {
                                                persistMappedPropertyArray.add(null);
                                            } else {
                                                if (PropertyUtils.isCustomJSONObjectProperty(elementPD.getType())) {
                                                    WebCustomType webCustomType = WebCustomType.createNewInstance(webObject, elementPD, beanJSONKey, i, false, childChildWebObjectUUID);
                                                    webCustomType.setTypeName(simpleTypeName);
                                                    persistMappedPropertyArray.add(webCustomType);
                                                    if (newChildPersistPropertiesToFire == null)
                                                        newChildPersistPropertiesToFire = new ArrayList<>();
                                                    newChildPersistPropertiesToFire.add(webCustomType);
                                                } else if (isComponent(propertyType)) {
                                                    ChildWebComponent childComponent = ChildWebComponent.createNewInstance(webObject, elementPD, beanJSONKey, i, false, childChildWebObjectUUID);
                                                    persistMappedPropertyArray.add(childComponent);
                                                    if (newChildPersistPropertiesToFire == null)
                                                        newChildPersistPropertiesToFire = new ArrayList<>();
                                                    newChildPersistPropertiesToFire.add(childComponent);
                                                }
                                            }
                                        }
                                    }
                                }
                                persistMappedProperties.put(beanJSONKey, persistMappedPropertyArray.toArray(new IChildWebObject[persistMappedPropertyArray.size()]));
                                persistMappedPropetiesByUUID = null;
                            }
                        } else {
                            Debug.error("Typed property value ('" + beanJSONKey + "') is not JSONArray although in spec it is defined as array... " + this + " - " + object);
                        }
                    }
                }
            } else {
                if (persistMappedProperties.remove(beanJSONKey) != null)
                    persistMappedPropetiesByUUID = null;
            }
        }
    } finally {
        // fire these later outside the synchronized block to avoid potential deadlocks - who knows what code will execute in listeners
        if (newChildPersistPropertiesToFire != null)
            newChildPersistPropertiesToFire.forEach((IChildWebObject p) -> {
                fireChildCreated(p);
            });
    }
}
Also used : ArrayList(java.util.ArrayList) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) JSONArray(org.json.JSONArray) PropertyDescription(org.sablo.specification.PropertyDescription) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ICustomType(org.sablo.specification.property.ICustomType) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) UUID(com.servoy.j2db.util.UUID)

Example 17 with UUID

use of com.servoy.j2db.util.UUID 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 18 with UUID

use of com.servoy.j2db.util.UUID 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 19 with UUID

use of com.servoy.j2db.util.UUID 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 20 with UUID

use of com.servoy.j2db.util.UUID in project servoy-client by Servoy.

the class FormComponentPropertyType method getForm.

public Form getForm(Object formElementValue, FlattenedSolution fs) {
    Object formId = formElementValue;
    if (formId instanceof JSONObject) {
        formId = ((JSONObject) formId).optString("svy_form");
    }
    Form form = null;
    if (formId instanceof Integer) {
        form = fs.getForm(((Integer) formId).intValue());
    } else if (formId instanceof String || formId instanceof UUID) {
        // try first by name or uuid (FS caches by both)
        form = fs.getForm(formId.toString());
        if (form == null) {
            form = (Form) fs.searchPersist(formId.toString());
        }
    } else if (formId instanceof JSForm) {
        return ((JSForm) formId).getSupportChild();
    }
    return form;
}
Also used : JSONObject(org.json.JSONObject) JSForm(com.servoy.j2db.scripting.solutionmodel.JSForm) Form(com.servoy.j2db.persistence.Form) JSForm(com.servoy.j2db.scripting.solutionmodel.JSForm) JSONObject(org.json.JSONObject) UUID(com.servoy.j2db.util.UUID)

Aggregations

UUID (com.servoy.j2db.util.UUID)23 RepositoryException (com.servoy.j2db.persistence.RepositoryException)6 RemoteException (java.rmi.RemoteException)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 JSONObject (org.json.JSONObject)5 IPersist (com.servoy.j2db.persistence.IPersist)4 ServoyException (com.servoy.j2db.util.ServoyException)4 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)4 IDataSet (com.servoy.j2db.dataprocessing.IDataSet)3 Form (com.servoy.j2db.persistence.Form)3 Date (java.util.Date)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ApplicationException (com.servoy.j2db.ApplicationException)2 FlattenedSolution (com.servoy.j2db.FlattenedSolution)2 JSDataSet (com.servoy.j2db.dataprocessing.JSDataSet)2 AbstractBase (com.servoy.j2db.persistence.AbstractBase)2 ChangeHandler (com.servoy.j2db.persistence.ChangeHandler)2 ISupportChilds (com.servoy.j2db.persistence.ISupportChilds)2