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);
});
}
}
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);
}
}
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;
}
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);
}
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;
}
Aggregations