use of com.servoy.j2db.FlattenedSolution in project servoy-client by Servoy.
the class FoundSet method deleteRecord.
private void deleteRecord(IRecordInternal state, int row, boolean partOfBiggerDelete) throws ServoyException {
if (sheet.getTable() == null) {
return;
}
if (!hasAccess(IRepository.DELETE) && (state == null || state.existInDataSource())) {
throw new ApplicationException(ServoyException.NO_DELETE_ACCESS, new Object[] { sheet.getTable().getName() });
}
if (state != null && !(state instanceof PrototypeState) && !findMode) {
if (!fsm.getRowManager(fsm.getDataSource(sheet.getTable())).addRowToDeleteSet(state.getPKHashKey())) {
// already being deleted in recursion
return;
}
if (!partOfBiggerDelete) {
try {
// see EditRecordList.stopEditing
if (state.existInDataSource() && !executeFoundsetTriggerBreakOnFalse(new Object[] { state }, PROPERTY_ONDELETEMETHODID, true)) {
// trigger returned false
// $NON-NLS-1$
Debug.log("Delete not granted for the table " + getTable());
throw new ApplicationException(ServoyException.DELETE_NOT_GRANTED);
}
} catch (DataException e) {
// trigger threw exception
state.getRawData().setLastException(e);
getFoundSetManager().getEditRecordList().markRecordAsFailed(state);
// $NON-NLS-1$ //$NON-NLS-2$
Debug.log("Delete not granted for the table " + getTable() + ", pre-delete trigger threw exception");
throw new ApplicationException(ServoyException.DELETE_NOT_GRANTED);
}
// check for related data
FlattenedSolution flattenedSolution = fsm.getApplication().getFlattenedSolution();
Iterator<Relation> it = flattenedSolution.getRelations(sheet.getTable(), true, false);
while (it.hasNext()) {
Relation rel = it.next();
if (Relation.isValid(rel, flattenedSolution) && !rel.getAllowParentDeleteWhenHavingRelatedRecords() && !rel.isExactPKRef(fsm.getApplication().getFlattenedSolution()) && !rel.isGlobal()) {
IFoundSetInternal set = state.getRelatedFoundSet(rel.getName());
if (set != null && set.getSize() > 0) {
fsm.getApplication().reportJSError("Delete not granted due to AllowParentDeleteWhenHavingRelatedRecords size: " + set.getSize() + " from record with PK: " + state.getPKHashKey() + " index in foundset: " + row + " blocked by relation: " + rel.getName(), null);
throw new ApplicationException(ServoyException.NO_PARENT_DELETE_WITH_RELATED_RECORDS, new Object[] { rel.getName() }).setContext(this.toString());
}
}
}
// so only delete related data if the record is already in the db
if (state.existInDataSource()) {
// delete the related data
it = flattenedSolution.getRelations(sheet.getTable(), true, false);
while (it.hasNext()) {
Relation rel = it.next();
if (// if completely global never delete do cascade delete
Relation.isValid(rel, flattenedSolution) && rel.getDeleteRelatedRecords() && !rel.isGlobal()) {
IFoundSetInternal set = state.getRelatedFoundSet(rel.getName());
if (set != null) {
Debug.trace(// $NON-NLS-1$//$NON-NLS-2$
"******************************* delete related set size: " + set.getSize() + " from record with PK: " + state.getPKHashKey() + " index in foundset: " + // $NON-NLS-1$
row);
set.deleteAllInternal();
}
}
}
}
}
if (state.existInDataSource()) {
Row data = state.getRawData();
rowManager.deleteRow(this, data, hasAccess(IRepository.TRACKING), partOfBiggerDelete);
executeFoundsetTrigger(new Object[] { state }, PROPERTY_ONAFTERDELETEMETHODID, false);
GlobalTransaction gt = fsm.getGlobalTransaction();
if (gt != null) {
gt.addDeletedRecord(state);
}
// really remove the state from the edited records, can't be saved at all anymore after delete.
fsm.getEditRecordList().removeEditedRecord(state);
} else {
rowManager.clearRow(state.getRawData());
}
}
if (!(state instanceof PrototypeState)) {
removeRecordInternalEx(state, row);
// delete the row data so it won't be updated by other foundsets also having records to this rowdata.
if (state != null && state.getRawData() != null) {
state.getRawData().flagExistInDB();
}
}
}
use of com.servoy.j2db.FlattenedSolution in project servoy-client by Servoy.
the class FoundSet method tableHasOnDeleteMethods.
private boolean tableHasOnDeleteMethods() {
try {
FlattenedSolution solutionRoot = fsm.getApplication().getFlattenedSolution();
Iterator<TableNode> tableNodes = solutionRoot.getTableNodes(getTable());
List<ScriptMethod> foundsetMethods = solutionRoot.getFoundsetMethods(getTable(), false);
while (tableNodes.hasNext()) {
TableNode node = tableNodes.next();
int methodId = node.getOnDeleteMethodID();
if (methodId > 0 && solutionRoot.getScriptMethod(methodId) != null || AbstractBase.selectById(foundsetMethods.iterator(), methodId) != null) {
return true;
}
methodId = node.getOnAfterDeleteMethodID();
if (methodId > 0 && solutionRoot.getScriptMethod(methodId) != null || AbstractBase.selectById(foundsetMethods.iterator(), methodId) != null) {
return true;
}
}
} catch (RepositoryException e) {
Debug.error(e);
}
return false;
}
use of com.servoy.j2db.FlattenedSolution in project servoy-client by Servoy.
the class RelatedValueList method fill.
@Override
public // to create all the rows
void fill(// to create all the rows
IRecordInternal ps) {
this.parentState = ps;
Relation[] relations = application.getFlattenedSolution().getRelationSequence(valueList.getRelationName());
if (relations != null) {
try {
defaultSort = ((FoundSetManager) application.getFoundSetManager()).getSortColumns(relations[relations.length - 1].getForeignDataSource(), valueList.getSortOptions());
// check if all relations go to the same server
boolean sameServer = true;
String serverName = relations[0].getForeignServerName();
FlattenedSolution fs = application.getFlattenedSolution();
for (Relation relation : relations) {
// if changes are performed on the data refresh this list.
if (!relation.isGlobal()) {
if (!registered) {
((FoundSetManager) application.getFoundSetManager()).addTableListener(fs.getTable(relation.getForeignDataSource()), this);
}
if (sameServer) {
sameServer = serverName.equals(relation.getForeignServerName());
}
}
}
registered = true;
// performance optimalization, look when it is not filled by query (related list is not filled in) that the first related foundset is still the same
if (this.parentState instanceof Record && related.size() > 0) {
IFoundSetInternal relatedFoundSet = parentState.getRelatedFoundSet(relations[0].getName());
if (related.indexOf(relatedFoundSet) == 0)
return;
}
int size = super.getSize();
// to be on the safe side
stopBundlingEvents();
removeAllElements();
for (IFoundSetInternal fsi : related) {
fsi.removeFoundSetEventListener(this);
}
related.clear();
realValues = new SafeArrayList<Object>();
fireIntervalRemoved(this, 0, size);
if (sameServer && relations.length > 1 && !containsCalculation) {
// multiple-level relation on same server, use query
fillWithQuery(relations);
} else {
// 1-level relation or different servers, use foundsets
fillFromFoundset(relations);
}
} catch (Exception e) {
// due to buggy swing ui on macosx 131 this is needed, throws nullp when filled and not selected
Debug.error(e);
}
}
super.fill(ps);
// to be on the safe side
stopBundlingEvents();
fireContentsChanged(this, -1, -1);
}
use of com.servoy.j2db.FlattenedSolution in project servoy-client by Servoy.
the class RelatedValueList method createRelatedValuelistQuery.
public static Pair<QuerySelect, BaseQueryTable> createRelatedValuelistQuery(IServiceProvider application, ValueList valueList, Relation[] relations, IRecordInternal parentState) throws ServoyException {
if (parentState == null) {
return null;
}
FoundSetManager foundSetManager = (FoundSetManager) application.getFoundSetManager();
SQLGenerator sqlGenerator = foundSetManager.getSQLGenerator();
IGlobalValueEntry scopesScopeProvider = foundSetManager.getScopesScopeProvider();
SQLSheet childSheet = sqlGenerator.getCachedTableSQLSheet(relations[0].getPrimaryDataSource());
// this returns quickly if it already has a sheet for that relation, but optimize further?
sqlGenerator.makeRelatedSQL(childSheet, relations[0]);
QuerySelect select = AbstractBaseQuery.deepClone((QuerySelect) childSheet.getRelatedSQLDescription(relations[0].getName()).getSQLQuery());
Object[] relationWhereArgs = foundSetManager.getRelationWhereArgs(parentState, relations[0], false);
if (relationWhereArgs == null) {
return null;
}
TablePlaceholderKey placeHolderKey = SQLGenerator.createRelationKeyPlaceholderKey(select.getTable(), relations[0].getName());
if (!select.setPlaceholderValue(placeHolderKey, relationWhereArgs)) {
// $NON-NLS-1$//$NON-NLS-2$
Debug.error(new RuntimeException("Could not set relation placeholder " + placeHolderKey + " in query " + select));
return null;
}
FlattenedSolution fs = application.getFlattenedSolution();
BaseQueryTable lastTable = select.getTable();
ITable foreignTable = fs.getTable(relations[0].getForeignDataSource());
for (int i = 1; i < relations.length; i++) {
foreignTable = fs.getTable(relations[i].getForeignDataSource());
ISQLTableJoin join = SQLGenerator.createJoin(application.getFlattenedSolution(), relations[i], lastTable, new QueryTable(foreignTable.getSQLName(), foreignTable.getDataSource(), foreignTable.getCatalog(), foreignTable.getSchema()), true, scopesScopeProvider);
select.addJoin(join);
lastTable = join.getForeignTable();
}
List<SortColumn> defaultSort = foundSetManager.getSortColumns(relations[relations.length - 1].getForeignDataSource(), valueList.getSortOptions());
foundSetManager.getSQLGenerator().addSorts(select, lastTable, scopesScopeProvider, foreignTable, defaultSort, true, true);
int showValues = valueList.getShowDataProviders();
int returnValues = valueList.getReturnDataProviders();
int total = (showValues | returnValues);
ArrayList<IQuerySelectValue> columns = new ArrayList<IQuerySelectValue>();
if ((total & 1) != 0) {
columns.add(getQuerySelectValue(foreignTable, lastTable, valueList.getDataProviderID1()));
}
if ((total & 2) != 0) {
columns.add(getQuerySelectValue(foreignTable, lastTable, valueList.getDataProviderID2()));
}
if ((total & 4) != 0) {
columns.add(getQuerySelectValue(foreignTable, lastTable, valueList.getDataProviderID3()));
}
select.setColumns(columns);
// not allowed in all situations
select.setDistinct(false);
return new Pair<QuerySelect, BaseQueryTable>(select, lastTable);
}
use of com.servoy.j2db.FlattenedSolution in project servoy-client by Servoy.
the class RelatedValueList method deregister.
@Override
public void deregister() {
if (registered && valueList.getDatabaseValuesType() == IValueListConstants.RELATED_VALUES) {
FlattenedSolution fs = application.getFlattenedSolution();
Relation[] relations = fs.getRelationSequence(valueList.getRelationName());
if (relations != null) {
for (Relation relation : relations) {
if (!relation.isGlobal()) {
// if changes are performed on the data refresh this list.
((FoundSetManager) application.getFoundSetManager()).removeTableListener(fs.getTable(relation.getForeignDataSource()), this);
}
}
}
}
registered = false;
}
Aggregations