use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.
the class JavaToDocumentedJSTypeTranslator method splitArrayIfNeeded.
/**
* If the class is an array, split it up into array dimension and element type (class).
* @return a pair containing the element type and the array's dimension. The array's dimension is 0 and element type is the given type if it's not an array.
*/
private Pair<Class<?>, Integer> splitArrayIfNeeded(Class<?> javaClass) {
int arrDim = 0;
Class<?> c = javaClass;
while (c.isArray()) {
arrDim++;
c = c.getComponentType();
}
return new Pair<Class<?>, Integer>(c, Integer.valueOf(arrDim));
}
use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.
the class RuntimeWebComponent method put.
@Override
public void put(String name, Scriptable start, Object value) {
if (isInvalidValue(name, value))
return;
List<Pair<String, String>> oldVisibleForms = getVisibleForms();
if (specProperties != null && specProperties.contains(name)) {
Object previousVal = null;
PropertyDescription pd = webComponentSpec.getProperties().get(name);
if (pd.getType() instanceof ISabloComponentToRhino && !(pd.getType() instanceof IRhinoToSabloComponent)) {
// the it has sablo to rhino conversion but not the other way around then we should just use the
// value from the conversion so call get(String,Scriptable)
previousVal = get(name, start);
} else
previousVal = component.getProperty(name);
Object val = NGConversions.INSTANCE.convertRhinoToSabloComponentValue(value, previousVal, pd, component);
if (val != previousVal)
component.setProperty(name, val);
if (pd != null && pd.getType() instanceof VisiblePropertyType) {
// search all labelfor elements
for (WebComponent siblingComponent : component.getParent().getComponents()) {
Collection<PropertyDescription> labelFors = siblingComponent.getSpecification().getProperties(LabelForPropertyType.INSTANCE);
if (labelFors != null) {
for (PropertyDescription labelForProperty : labelFors) {
if (Utils.equalObjects(component.getName(), siblingComponent.getProperty(labelForProperty.getName()))) {
// sibling component is labelfor, so set value to all its visible properties
Collection<PropertyDescription> visibleProperties = siblingComponent.getSpecification().getProperties(VisiblePropertyType.INSTANCE);
if (visibleProperties != null) {
for (PropertyDescription visibleProperty : visibleProperties) {
previousVal = siblingComponent.getProperty(visibleProperty.getName());
val = NGConversions.INSTANCE.convertRhinoToSabloComponentValue(value, previousVal, visibleProperty, siblingComponent);
if (val != previousVal)
siblingComponent.setProperty(name, val);
}
}
break;
}
}
}
}
}
} else if (prototypeScope != null) {
if (!apiFunctions.containsKey(name)) {
// check if we have a setter for this property
if (name != null && name.length() > 0) {
String uName = new StringBuffer(name.substring(0, 1).toUpperCase()).append(name.substring(1)).toString();
if (apiFunctions.containsKey("set" + uName) && apiFunctions.containsKey("get" + uName)) {
// call setter
Function propertySetter = apiFunctions.get("set" + uName);
propertySetter.call(Context.getCurrentContext(), start, start, new Object[] { value });
} else {
prototypeScope.put(name, start, value);
}
}
}
}
updateVisibleContainers(oldVisibleForms);
}
use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.
the class RuntimeWebComponent method get.
@Override
public Object get(final String name, final Scriptable start) {
if (specProperties != null && specProperties.contains(name)) {
PropertyDescription pd = webComponentSpec.getProperties().get(name);
if (WebFormComponent.isDesignOnlyProperty(pd))
return Scriptable.NOT_FOUND;
return NGConversions.INSTANCE.convertSabloComponentToRhinoValue(component.getProperty(name), pd, component, start);
}
if ("getFormName".equals(name)) {
return new Callable() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
IWebFormUI parent = component.findParent(IWebFormUI.class);
if (parent != null) {
return parent.getController().getName();
}
return null;
}
};
}
if ("getDesignTimeProperty".equals(name) && component.getFormElement().getPersistIfAvailable() instanceof AbstractBase) {
return new Callable() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
return Utils.parseJSExpression(((AbstractBase) component.getFormElement().getPersistIfAvailable()).getCustomDesignTimeProperty((String) args[0]));
}
};
}
if ("getDesignProperties".equals(name) && component.getFormElement().getPersistIfAvailable() instanceof AbstractBase) {
return new Callable() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Map<String, Object> designProperties = ((AbstractBase) component.getFormElement().getPersistIfAvailable()).getMergedCustomDesignTimeProperties();
Map<String, Object> parsedMap = new HashMap<String, Object>();
designProperties.entrySet().forEach(entry -> {
parsedMap.put(entry.getKey(), Utils.parseJSExpression(entry.getValue()));
});
return parsedMap;
}
};
}
final Function func = apiFunctions.get(name);
if (func != null && isApiFunctionEnabled(name)) {
final List<Pair<String, String>> oldVisibleForms = getVisibleForms();
return new BaseFunction() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Object retValue;
cx.putThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE, Boolean.TRUE);
try {
retValue = func.call(cx, scope, thisObj, args);
} finally {
cx.removeThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE);
}
if (!(func instanceof WebComponentFunction)) {
WebObjectFunctionDefinition def = webComponentSpec.getApiFunctions().get(name);
retValue = NGConversions.INSTANCE.convertServerSideRhinoToRhinoValue(retValue, def.getReturnType(), component, null);
}
updateVisibleContainers(oldVisibleForms);
return retValue;
}
};
}
// check if we have a setter/getter for this property
if (name != null && name.length() > 0) {
String uName = new StringBuffer(name.substring(0, 1).toUpperCase()).append(name.substring(1)).toString();
if (apiFunctions.containsKey("set" + uName) && apiFunctions.containsKey("get" + uName)) {
// call getter
Function propertyGetter = apiFunctions.get("get" + uName);
return propertyGetter.call(Context.getCurrentContext(), start, start, new Object[] {});
}
}
if ("svyMarkupId".equals(name)) {
String formName = null;
IWebFormUI parent = component.findParent(IWebFormUI.class);
if (parent != null) {
formName = parent.getController().getName();
} else {
formName = component.getFormElement().getForm().getName();
}
return ComponentFactory.getMarkupId(formName, component.getName());
}
// is this really needed? will not prototype be looked at automatically by Rhino code?
if (prototypeScope != null) {
return prototypeScope.get(name, start);
}
return Scriptable.NOT_FOUND;
}
use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.
the class RuntimeWebComponent method getVisibleForms.
private List<Pair<String, String>> getVisibleForms() {
List<Pair<String, String>> visibleContainedForms = new ArrayList<Pair<String, String>>();
// legacy for now, should we do it more general, from the spec
if (component.getFormElement() != null && component.getFormElement().getPersistIfAvailable() instanceof TabPanel) {
Object visibleTabPanel = component.getProperty("visible");
if (visibleTabPanel instanceof Boolean && !((Boolean) visibleTabPanel).booleanValue())
return visibleContainedForms;
Object tabIndex = component.getProperty("tabIndex");
Object tabs = component.getProperty("tabs");
if (tabs instanceof List && ((List<?>) tabs).size() > 0) {
@SuppressWarnings("unchecked") List<Map<String, Object>> tabsList = (List<Map<String, Object>>) tabs;
TabPanel tabpanel = (TabPanel) component.getFormElement().getPersistIfAvailable();
if (tabpanel.getTabOrientation() == TabPanel.SPLIT_HORIZONTAL || tabpanel.getTabOrientation() == TabPanel.SPLIT_VERTICAL) {
for (Map<String, Object> element : tabsList) {
Map<String, Object> tab = element;
if (tab != null) {
String relationName = tab.get("relationName") != null ? tab.get("relationName").toString() : null;
Object form = tab.get("containsFormId");
if (form != null) {
visibleContainedForms.add(new Pair<String, String>(form.toString(), relationName));
}
}
}
} else {
Map<String, Object> visibleTab = null;
if (tabIndex instanceof Number && tabsList.size() > 0 && ((Number) tabIndex).intValue() <= tabsList.size()) {
int index = ((Number) tabIndex).intValue() - 1;
if (index < 0) {
index = 0;
}
visibleTab = (tabsList.get(index));
} else if (tabIndex instanceof String || tabIndex instanceof CharSequence) {
for (Map<String, Object> element : tabsList) {
Map<String, Object> tab = element;
if (Utils.equalObjects(tabIndex, tab.get("name"))) {
visibleTab = tab;
break;
}
}
}
if (visibleTab != null) {
String relationName = visibleTab.get("relationName") != null ? visibleTab.get("relationName").toString() : null;
Object form = visibleTab.get("containsFormId");
if (form != null) {
visibleContainedForms.add(new Pair<String, String>(form.toString(), relationName));
}
}
}
}
}
return visibleContainedForms;
}
use of com.servoy.j2db.util.Pair in project servoy-client by Servoy.
the class NGFoundSetManager method executeMethod.
@SuppressWarnings("nls")
@Override
public Object executeMethod(String methodName, JSONObject args) throws Exception {
if ("getFoundSet".equals(methodName)) {
IFoundSetInternal foundset = FoundsetReferencePropertyTypeOld.INSTANCE.fromJSON(args, null, null, null, null);
String sort = args.optString("sort");
if (!"".equals(sort)) {
foundset.setSort(sort);
}
FoundsetTypeSabloValue value = getFoundsetTypeSabloValue(foundset, args.optJSONObject("dataproviders"));
ChangeAwareList<ChangeAwareMap<String, Object>, Object> foundsets = (ChangeAwareList<ChangeAwareMap<String, Object>, Object>) ((NGClient) getApplication()).getWebsocketSession().getClientService("foundset_manager").getProperty("foundsets");
if (foundsets == null) {
foundsets = new ChangeAwareList<ChangeAwareMap<String, Object>, Object>(new ArrayList<ChangeAwareMap<String, Object>>());
((NGClient) getApplication()).getWebsocketSession().getClientService("foundset_manager").setProperty("foundsets", foundsets);
}
boolean newFoundsetInfo = true;
for (ChangeAwareMap<String, Object> foundsetInfoMap : foundsets) {
if (foundsetInfoMap.containsValue(value)) {
newFoundsetInfo = false;
break;
}
}
if (newFoundsetInfo) {
HashMap<String, Object> foundsetinfoMap = new HashMap<String, Object>();
foundsetinfoMap.put("foundset", value);
foundsetinfoMap.put("foundsethash", args.optString("foundsethash"));
String childrelation = args.optString("childrelation");
if (childrelation != null) {
JSONObject childrelationinfo = new JSONObject();
childrelationinfo.put("name", childrelation);
for (int i = 0; i < foundset.getSize(); i++) {
IRecordInternal record = foundset.getRecord(i);
Object o = record.getValue(childrelation);
if (o instanceof IFoundSetInternal) {
childrelationinfo.put(record.getPKHashKey() + "_" + record.getParentFoundSet().getRecordIndex(record), ((IFoundSetInternal) o).getSize());
}
}
foundsetinfoMap.put("childrelationinfo", childrelationinfo);
}
CustomJSONObjectType dummyCustomObjectTypeForChildRelationInfo = (CustomJSONObjectType) TypesRegistry.createNewType(CustomJSONObjectType.TYPE_NAME, "svy__dummyCustomObjectTypeForDeprecatedFMServiceChildRelationInfo");
PropertyDescription dummyPD = new PropertyDescriptionBuilder().withType(dummyCustomObjectTypeForChildRelationInfo).build();
dummyCustomObjectTypeForChildRelationInfo.setCustomJSONDefinition(dummyPD);
foundsets.add(new ChangeAwareMap(foundsetinfoMap, null, dummyPD));
}
} else if ("getRelatedFoundSetHash".equals(methodName)) {
IFoundSetInternal foundset = FoundsetReferencePropertyTypeOld.INSTANCE.fromJSON(args, null, null, null, null);
String rowid = args.optString("rowid");
String relation = args.optString("relation");
Pair<String, Integer> splitHashAndIndex = FoundsetTypeSabloValue.splitPKHashAndIndex(rowid);
int recordIndex = foundset.getRecordIndex(splitHashAndIndex.getLeft(), splitHashAndIndex.getRight().intValue());
if (recordIndex != -1) {
IRecordInternal record = foundset.getRecord(recordIndex);
Object o = record.getValue(relation);
if (o instanceof IFoundSetInternal) {
IFoundSetInternal relatedFoundset = (IFoundSetInternal) o;
PropertyDescription foundsetRefProperty = new PropertyDescriptionBuilder().withType(FoundsetReferencePropertyTypeOld.INSTANCE).build();
return new TypedData<IFoundSetInternal>(relatedFoundset, foundsetRefProperty);
}
}
} else if ("updateFoundSetRow".equals(methodName)) {
IFoundSetInternal foundset = FoundsetReferencePropertyTypeOld.INSTANCE.fromJSON(args, null, null, null, null);
String rowid = args.optString("rowid");
String dataproviderid = args.optString("dataproviderid");
Object value = args.get("value");
Pair<String, Integer> splitHashAndIndex = FoundsetTypeSabloValue.splitPKHashAndIndex(rowid);
int recordIndex = foundset.getRecordIndex(splitHashAndIndex.getLeft(), splitHashAndIndex.getRight().intValue());
if (recordIndex != -1) {
IRecordInternal record = foundset.getRecord(recordIndex);
if (record.startEditing()) {
record.setValue(dataproviderid, value);
return Boolean.TRUE;
}
}
} else if ("removeFoundSetFromCache".equals(methodName)) {
IFoundSetInternal foundset = FoundsetReferencePropertyTypeOld.INSTANCE.fromJSON(args, null, null, null, null);
removeFoundSetTypeSabloValue(foundset);
} else if ("removeFoundSetsFromCache".equals(methodName)) {
ChangeAwareList<ChangeAwareMap<String, Object>, Object> foundsets = (ChangeAwareList<ChangeAwareMap<String, Object>, Object>) ((NGClient) getApplication()).getWebsocketSession().getClientService("foundset_manager").getProperty("foundsets");
if (foundsets != null) {
foundsets.clear();
}
foundsetTypeSabloValueMap.clear();
}
return null;
}
Aggregations