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