use of com.servoy.j2db.dataprocessing.IRecordInternal in project servoy-client by Servoy.
the class DataLookupField method dependencyChanged.
public void dependencyChanged(IRecordInternal record) {
this.parentState = record;
if (list != null) {
Object o = getValue();
int index = -1;
if (dataProviderID != null && !ScopesUtils.isVariableScope(dataProviderID)) {
index = dataProviderID.lastIndexOf('.');
}
if (index == -1 || parentState == null || dataProviderID == null) {
list.fill(parentState);
} else {
IFoundSetInternal relatedFoundSet = parentState.getRelatedFoundSet(dataProviderID.substring(0, index));
if (relatedFoundSet == null) {
list.fill(parentState.getParentFoundSet().getPrototypeState());
} else if (relatedFoundSet.getSize() == 0) {
list.fill(relatedFoundSet.getPrototypeState());
} else {
IRecordInternal relatedRecord = relatedFoundSet.getRecord(relatedFoundSet.getSelectedIndex());
list.fill(relatedRecord);
}
}
if (editProvider != null) {
editProvider.setAdjusting(true);
}
try {
setValue(null);
setValue(o);
} finally {
if (editProvider != null) {
editProvider.setAdjusting(false);
}
}
}
}
use of com.servoy.j2db.dataprocessing.IRecordInternal in project servoy-client by Servoy.
the class SpecialSplitPane method registerSelectionListeners.
private void registerSelectionListeners(IRecordInternal parentState, String relationName) {
// $NON-NLS-1$
String[] parts = relationName.split("\\.");
IRecordInternal currentRecord = parentState;
for (int i = 0; currentRecord != null && i < parts.length - 1; i++) {
IFoundSetInternal fs = currentRecord.getRelatedFoundSet(parts[i]);
if (fs instanceof ISwingFoundSet) {
related.add((ISwingFoundSet) fs);
((ISwingFoundSet) fs).getSelectionModel().addListSelectionListener(this);
}
currentRecord = (fs == null) ? null : fs.getRecord(fs.getSelectedIndex());
}
}
use of com.servoy.j2db.dataprocessing.IRecordInternal 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;
}
use of com.servoy.j2db.dataprocessing.IRecordInternal in project servoy-client by Servoy.
the class FoundsetTypeSabloValue method browserUpdatesReceived.
public void browserUpdatesReceived(Object jsonValue, PropertyDescription pd, IBrowserConverterContext dataConverterContext) {
PushToServerEnum pushToServer = BrowserConverterContext.getPushToServerValue(dataConverterContext);
if (getFoundset() == null)
return;
try {
if (jsonValue instanceof JSONArray) {
JSONArray arr = (JSONArray) jsonValue;
for (int i = 0; i < arr.length(); i++) {
JSONObject update = (JSONObject) arr.get(i);
// {newViewPort: {startIndex : startIndex, size : size}}
if (update.has("newViewPort")) {
JSONObject newViewport = update.getJSONObject("newViewPort");
int requestID = update.getInt(ID_KEY);
viewPort.clearSendingInitialPreferredViewport();
viewPort.setBounds(newViewport.getInt(START_INDEX), newViewport.getInt(SIZE));
changeMonitor.requestIdHandled(requestID, true);
}
if (update.has(PREFERRED_VIEWPORT_SIZE)) {
viewPort.setPreferredViewportSize(update.getInt(PREFERRED_VIEWPORT_SIZE));
if (update.has(FoundsetPropertyTypeConfig.SEND_SELECTION_VIEWPORT_INITIALLY))
viewPort.setSendSelectionViewportInitially(update.getBoolean(FoundsetPropertyTypeConfig.SEND_SELECTION_VIEWPORT_INITIALLY));
if (update.has(INITIAL_SELECTION_VIEWPORT_CENTERED))
viewPort.setInitialSelectionViewportCentered(update.getBoolean(INITIAL_SELECTION_VIEWPORT_CENTERED));
} else // {loadExtraRecords: negativeOrPositiveCount}
if (update.has("loadExtraRecords")) {
int requestID = update.getInt(ID_KEY);
viewPort.clearSendingInitialPreferredViewport();
viewPort.loadExtraRecords(update.getInt("loadExtraRecords"));
changeMonitor.requestIdHandled(requestID, true);
} else // {loadLessRecords: negativeOrPositiveCount}
if (update.has("loadLessRecords")) {
int requestID = update.getInt(ID_KEY);
viewPort.clearSendingInitialPreferredViewport();
viewPort.loadLessRecords(update.getInt("loadLessRecords"));
changeMonitor.requestIdHandled(requestID, true);
} else if (update.has("sort")) {
int requestID = update.getInt(ID_KEY);
JSONArray columns = update.getJSONArray("sort");
StringBuilder sort = new StringBuilder();
Map<String, String> dp = dataproviders.size() > 0 ? dataproviders : recordDataLinkedPropertyIDToColumnDP;
String dataProviderID = null;
boolean sortAscending = true;
for (int j = 0; j < columns.length(); j++) {
JSONObject sortColumn = columns.getJSONObject(j);
String name = sortColumn.getString("name");
if (dp.containsKey(name)) {
sort.append(dp.get(name));
sort.append(" " + sortColumn.getString("direction"));
if (dataProviderID == null) {
dataProviderID = dp.get(name);
sortAscending = "asc".equalsIgnoreCase(sortColumn.getString("direction"));
}
if (j < columns.length() - 1)
sort.append(",");
}
}
IWebFormUI formUI = getFormUI();
IWebFormController fc = (formUI != null ? formUI.getController() : null);
if (fc != null && fc.getForm().getOnSortCmdMethodID() > 0 && dataProviderID != null) {
// our api only supports one dataproviderid sort at a time
JSEvent event = new JSEvent();
event.setFormName(fc.getName());
fc.executeFunction(String.valueOf(fc.getForm().getOnSortCmdMethodID()), Utils.arrayMerge((new Object[] { dataProviderID, Boolean.valueOf(sortAscending), event }), // $NON-NLS-1$
Utils.parseJSExpressions(fc.getForm().getFlattenedMethodArguments("onSortCmdMethodID"))), true, null, false, // $NON-NLS-1$
"onSortCmdMethodID");
} else {
try {
String currentSort = foundset.getSort();
String newSort = sort.toString();
foundset.setSort(newSort);
if (// really a new sort
!Utils.equalObjects(currentSort, newSort) || // not sorted, send back to client
!Utils.equalObjects(foundset.getSort(), newSort)) {
changeMonitor.foundsetSortChanged();
}
} catch (ServoyException e) {
Debug.error("Cannot sort foundset by " + sort.toString(), e);
}
}
changeMonitor.requestIdHandled(requestID, true);
} else // {newClientSelection: newSelectedIndexesArray}
if (update.has("newClientSelection")) {
JSONArray jsonSelectedIndexes = update.getJSONArray("newClientSelection");
int[] newSelectedIndexes = new int[jsonSelectedIndexes.length()];
for (int j = newSelectedIndexes.length - 1; j >= 0; j--) {
newSelectedIndexes[j] = jsonSelectedIndexes.getInt(j);
}
// this !Arrays.equals check in conjunction with pause()/resume() is needed to avoid an effect on the client that server always sends back changed selection in which case
// if the user quickly changes selection multiple times and the connection is slow, selection will jump all over
// the place until it stabilizes correctly
getListSelectionListener().pause();
try {
if (newSelectedIndexes.length == 1) {
foundset.setSelectedIndex(newSelectedIndexes[0]);
} else {
foundset.setSelectedIndexes(newSelectedIndexes);
}
} finally {
getListSelectionListener().resume();
// if server denies the new selection as invalid and doesn't change selection, send it to the client so that it doesn't keep invalid selection
if (!Arrays.equals(foundset.getSelectedIndexes(), newSelectedIndexes)) {
changeMonitor.selectionChanged(false);
}
}
} else // {newClientSelectionRequest: newSelectedIndexesArray}
if (update.has("newClientSelectionRequest")) {
int requestID = update.getInt(ID_KEY);
JSONArray jsonSelectedIndexes = update.getJSONArray("newClientSelectionRequest");
int[] newSelectedIndexes = new int[jsonSelectedIndexes.length()];
for (int j = newSelectedIndexes.length - 1; j >= 0; j--) {
newSelectedIndexes[j] = jsonSelectedIndexes.getInt(j);
}
int[] oldSelection = foundset.getSelectedIndexes();
// this !Arrays.equals check in conjunction with pause()/resume() is needed to avoid an effect on the client that server always sends back changed selection in which case
// if the user quickly changes selection multiple times and the connection is slow, selection will jump all over
// the place until it stabilizes correctly
getListSelectionListener().pause();
try {
if (newSelectedIndexes.length == 1) {
foundset.setSelectedIndex(newSelectedIndexes[0]);
} else {
foundset.setSelectedIndexes(newSelectedIndexes);
}
} finally {
getListSelectionListener().resume();
if (!Arrays.equals(oldSelection, foundset.getSelectedIndexes())) {
// if the selection is changed, send it back to the client so that its model is also updated
changeMonitor.selectionChanged(false);
changeMonitor.requestIdHandled(requestID, true);
} else {
if (!Arrays.equals(oldSelection, newSelectedIndexes)) {
// it was supposed to change but the server did not allow it
changeMonitor.requestIdHandled(requestID, false);
} else
changeMonitor.requestIdHandled(requestID, true);
}
}
} else if (update.has(ViewportDataChangeMonitor.VIEWPORT_CHANGED)) {
if (PushToServerEnum.allow.compareTo(pushToServer) <= 0) {
// {dataChanged: { ROW_ID_COL_KEY: rowIDValue, dataproviderName: value }}
JSONObject dataChangeJSON = (JSONObject) update.get(ViewportDataChangeMonitor.VIEWPORT_CHANGED);
String rowIDValue = dataChangeJSON.getString(ROW_ID_COL_KEY);
String dpKey = dataChangeJSON.getString(DATAPROVIDER_KEY);
String dataProviderName;
if (dataproviders.containsKey(dpKey)) {
dataProviderName = dataproviders.get(dpKey);
} else {
dataProviderName = recordDataLinkedPropertyIDToColumnDP.get(dpKey);
}
Object value = dataChangeJSON.get(VALUE_KEY);
if (foundset != null) {
Pair<String, Integer> splitHashAndIndex = splitPKHashAndIndex(rowIDValue);
int recordIndex = foundset.getRecordIndex(splitHashAndIndex.getLeft(), splitHashAndIndex.getRight().intValue());
if (recordIndex != -1) {
IRecordInternal record = foundset.getRecord(recordIndex);
// convert Dates where it's needed
// this should be enough for when only foundset dataproviders are used
PropertyDescription dataProviderPropDesc = getDataProviderPropertyDescription(dataProviderName);
ValueReference<Boolean> returnValueAdjustedIncommingValueForRow = new ValueReference<Boolean>(Boolean.FALSE);
value = JSONUtils.fromJSONUnwrapped(null, value, dataProviderPropDesc, dataConverterContext, returnValueAdjustedIncommingValueForRow);
try {
if (record.startEditing()) {
try {
record.setValue(dataProviderName, value);
} catch (IllegalArgumentException e) {
// TODO handle the validaton errors.
IWebFormUI formUI = getFormUI();
formUI.getController().getApplication().reportError("Validation for " + dataProviderName + " for value: " + value + " failed.", e);
}
}
// else cannot start editing; finally block will deal with it (send old value back to client as new one can't be pushed)
} finally {
// if server denies the new value as invalid and doesn't change it, send it to the client so that it doesn't keep invalid value; the same if for example a double was rounded to an int
if (!Utils.equalObjects(record.getValue(dataProviderName), value) || returnValueAdjustedIncommingValueForRow.value.booleanValue()) {
changeMonitor.recordsUpdated(recordIndex, recordIndex, foundset.getSize(), viewPort, Arrays.asList(new String[] { dataProviderName }));
}
}
} else {
Debug.error("Cannot set foundset record (" + rowIDValue + ") dataprovider '" + dataProviderName + "' to value '" + value + ". Record not found.");
}
}
} else {
log.error("Property (" + pd + ") that doesn't define a suitable pushToServer value (allow/shallow/deep) tried to modify foundset dataprovider value serverside. Denying and sending back full viewport!");
changeMonitor.viewPortCompletelyChanged();
}
}
}
}
} catch (JSONException e) {
Debug.error("Error when getting browser updates for property (" + this.toString() + ") for " + jsonValue, e);
}
}
use of com.servoy.j2db.dataprocessing.IRecordInternal in project servoy-client by Servoy.
the class UsedDataProviderTracker method usedFromRecord.
/**
* Used name from Record,
* @param record may be null (for global relations)
* @param name
*/
protected void usedFromRecord(IRecordInternal record, String name) {
IRecordInternal currentRecord = record;
try {
// $NON-NLS-1$
String[] parts = name.split("\\.");
for (String part : parts) {
Relation relation = flattenedSolution.getRelation(part);
if (relation != null) {
// calc depends on the relation, add a dependency for the primary data providers for the relation
IDataProvider[] primaryDataProviders = relation.getPrimaryDataProviders(flattenedSolution);
for (IDataProvider prim : primaryDataProviders) {
if (prim instanceof LiteralDataprovider)
continue;
String primdp = prim.getDataProviderID();
if (ScopesUtils.isVariableScope(primdp)) {
// global
usedGlobal(primdp);
} else {
// column
if (currentRecord != null) {
if (currentRecord.getRawData() == null) {
if (currentRecord instanceof PrototypeState) {
Debug.trace("Calculation '" + name + "' depends on field '" + part + "' from PrototypeState " + currentRecord);
} else {
// should not happen
Debug.error("Unexpected state: calculation '" + name + "' depends on column '" + part + "' from record without pk: " + currentRecord, new IllegalStateException());
}
} else {
usedColumn(currentRecord.getParentFoundSet().getDataSource(), currentRecord.getRawData().getPKHashKey(), primdp);
}
}
}
}
IFoundSetInternal foundSet = null;
if (currentRecord != null)
foundSet = currentRecord.getRelatedFoundSet(relation.getName());
currentRecord = null;
if (foundSet instanceof RelatedFoundSet) {
usedRelatedFoundSet(relation.getName(), (RelatedFoundSet) foundSet);
currentRecord = foundSet.getRecord(foundSet.getSelectedIndex());
}
} else {
if (currentRecord != null) {
IFoundSetInternal foundSet = currentRecord.getParentFoundSet();
if (foundSet.getSQLSheet().containsAggregate(part)) {
// aggregate
usedAggregate(foundSet, part);
} else {
// field or calc
if (currentRecord.has(part)) {
if (currentRecord.getRawData() == null) {
if (currentRecord instanceof PrototypeState) {
Debug.trace("Calculation '" + name + "' depends on field '" + part + "' from PrototypeState " + currentRecord);
} else {
// should not happen
Debug.error("Unexpected state: calculation '" + name + "' depends on field '" + part + "' from record without pk: " + currentRecord, new IllegalStateException());
}
} else {
usedColumn(foundSet.getDataSource(), currentRecord.getRawData().getPKHashKey(), part);
}
}
}
}
return;
}
if (currentRecord == null) {
return;
}
}
} catch (RepositoryException e) {
Debug.error(e);
}
}
Aggregations