use of com.servoy.j2db.persistence.RepositoryException in project servoy-client by Servoy.
the class FoundSet method performFind.
/**
* Execute the find sql, returns the number of records found, returns -1 when the call was blocked by a trigger
* @param clearLastResult
* @param reduceSearch
* @param clearIfZero
* @param cancelFind
* @param returnInvalidRangeConditions
* @return
* @throws ServoyException
*/
public int performFind(boolean clearLastResult, boolean reduceSearch, boolean clearIfZero, boolean cancelFind, List<String> returnInvalidRangeConditions) throws // perform the find
ServoyException {
int numberOfFindStates = getSize();
if (cancelFind) {
// ignore find states
pksAndRecords.setPks(null, 0);
setSelectedIndex(-1);
} else {
try {
if (!executeFoundsetTriggerBreakOnFalse(new Object[] { Boolean.valueOf(clearLastResult), Boolean.valueOf(reduceSearch) }, PROPERTY_ONSEARCHMETHODID, true)) {
// $NON-NLS-1$
Debug.trace("Foundset search was denied by onSearchFoundset method");
// blocked
return -1;
}
} catch (ServoyException e) {
Debug.error(e);
// blocked
return -1;
}
if (clearLastResult)
removeLastFound();
setSelectedIndex(numberOfFindStates > 0 ? 0 : -1);
}
try {
QuerySelect findSqlSelect = getCurrentStateQuery(reduceSearch, false);
if (returnInvalidRangeConditions != null) {
ISQLCondition sqlCondition = findSqlSelect.getCondition(SQLGenerator.CONDITION_SEARCH);
returnInvalidRangeConditions.addAll(AbstractBaseQuery.getInvalidRangeConditions(sqlCondition));
}
// cache pks
String transaction_id = fsm.getTransactionID(sheet);
long time = System.currentTimeMillis();
IDataSet findPKs = null;
try {
findPKs = performQuery(transaction_id, findSqlSelect, getRowIdentColumnTypes(), 0, fsm.config.pkChunkSize(), IDataServer.FIND_BROWSER_QUERY);
} catch (RemoteException e) {
throw new RepositoryException(e);
}
if (Debug.tracing()) {
Debug.trace(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
"Find executed, time: " + (System.currentTimeMillis() - time) + " thread: " + Thread.currentThread().getName() + ", sql: " + findSqlSelect.toString());
}
IFoundSetChanges changes = null;
if (findPKs.getRowCount() == 0) {
if (clearIfZero) {
changes = pksAndRecords.setPksAndQuery(null, 0, findSqlSelect);
clearInternalState(true);
setSelectedIndex(-1);
}
} else {
fireSelectionAdjusting();
changes = pksAndRecords.setPksAndQuery(findPKs, findPKs.getRowCount(), findSqlSelect);
clearInternalState(true);
// notify about aggregate change,because the find has cleared them all.
fireAggregateChangeWithEvents(null);
}
initialized = true;
fireDifference(numberOfFindStates, getSize(), changes);
if (getSelectedIndex() == -1 && getSize() > 0) {
setSelectedIndex(0);
}
int nfound = findPKs.getRowCount();
try {
executeFoundsetTrigger(null, PROPERTY_ONAFTERSEARCHMETHODID, false);
} catch (ServoyException e) {
Debug.error(e);
}
return nfound;
} catch (ServoyException e) {
pksAndRecords.setPks(null, 0);
clearInternalState(true);
setSelectedIndex(-1);
fireDifference(numberOfFindStates, 0, null);
throw e;
}
}
use of com.servoy.j2db.persistence.RepositoryException in project servoy-client by Servoy.
the class FoundSet method invert.
public void invert() throws ServoyException {
int sizeBefore;
QuerySelect sqlSelect;
ArrayList<String> invertConditionNames = new ArrayList<String>();
IFoundSetChanges changes = null;
synchronized (pksAndRecords) {
sizeBefore = getSize();
sqlSelect = pksAndRecords.getQuerySelectForReading();
Map<String, AndCondition> conditions = sqlSelect.getConditions();
if (conditions != null) {
Iterator<String> conditionNamesIte = conditions.keySet().iterator();
String conditionName;
while (conditionNamesIte.hasNext()) {
conditionName = conditionNamesIte.next();
if (conditionName != null && (conditionName.equals(SQLGenerator.CONDITION_SEARCH) || !conditionName.startsWith(SQLGenerator.SERVOY_CONDITION_PREFIX))) {
invertConditionNames.add(conditionName);
}
}
}
if (invertConditionNames.size() == 0) {
changes = pksAndRecords.setPksAndQuery(new BufferedDataSet(), 0, sqlSelect);
} else {
sqlSelect = pksAndRecords.getQuerySelectForModification();
for (String inverConditionName : invertConditionNames) {
sqlSelect.setCondition(inverConditionName, conditions.get(inverConditionName).negate());
}
clearOmit(sqlSelect);
// set pks here in case a refresh comes along
changes = pksAndRecords.setPksAndQuery(pksAndRecords.getPks(), pksAndRecords.getDbIndexLastPk(), sqlSelect);
}
}
if (invertConditionNames.size() > 0) {
// cache pks
String transaction_id = fsm.getTransactionID(sheet);
try {
IDataSet pks = performQuery(transaction_id, sqlSelect, getRowIdentColumnTypes(), 0, fsm.config.pkChunkSize(), IDataServer.FOUNDSET_LOAD_QUERY);
synchronized (pksAndRecords) {
// optimistic locking, if the query has been changed in the mean time forget about the refresh
if (sqlSelect != pksAndRecords.getQuerySelectForReading()) {
// $NON-NLS-1$
Debug.log("invert: query was changed during refresh, not resetting old query");
return;
}
changes = pksAndRecords.setPksAndQuery(pks, pks.getRowCount(), sqlSelect);
}
} catch (RemoteException e) {
changes = pksAndRecords.setPksAndQuery(new BufferedDataSet(), 0, sqlSelect);
throw new RepositoryException(e);
}
}
if (aggregateCache.size() > 0) {
fireAggregateChangeWithEvents(null);
}
fireDifference(sizeBefore, getSize(), changes);
}
use of com.servoy.j2db.persistence.RepositoryException 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.persistence.RepositoryException in project servoy-client by Servoy.
the class FoundSet method reloadWithCurrentQuery.
/**
* @throws ServoyException
* @throws RepositoryException
*/
protected boolean reloadWithCurrentQuery(int rowsToRetrieve, boolean reuse, boolean clearInternalState) throws ServoyException {
QuerySelect sqlSelect;
IDataSet pks;
synchronized (pksAndRecords) {
sqlSelect = getPksAndRecords().getQuerySelectForReading();
pks = pksAndRecords.getPks();
}
// always keep selection when reloading
Object[][] selectedPKs = null;
int[] selectedIndexes = getSelectedIndexes();
// if single selected and first record is selected we ignore selection
if (pks != null && selectedIndexes != null && (selectedIndexes.length > 1 || (selectedIndexes.length == 1 && selectedIndexes[0] > 0))) {
selectedPKs = new Object[selectedIndexes.length][];
int i = 0;
for (int selectedIndex : selectedIndexes) {
selectedPKs[i++] = pks.getRow(selectedIndex);
}
}
IFoundSetChanges changes = null;
int oldSize = getRawSize();
// cache pks
String transaction_id = fsm.getTransactionID(sheet);
try {
pks = performQuery(transaction_id, sqlSelect, getRowIdentColumnTypes(), 0, rowsToRetrieve, IDataServer.FOUNDSET_LOAD_QUERY);
synchronized (pksAndRecords) {
// optimistic locking, if the query has been changed in the mean time forget about the refresh
if (sqlSelect != pksAndRecords.getQuerySelectForReading()) {
// $NON-NLS-1$
Debug.log("sort: query was changed during refresh, not resetting old query");
return false;
}
changes = pksAndRecords.setPksAndQuery(pks, pks.getRowCount(), sqlSelect, reuse);
}
} catch (RemoteException e) {
throw new RepositoryException(e);
}
initialized = true;
if (clearInternalState) {
// do kind of browseAll/refresh from db.
// differences: selected record isn't tried to keep in sync with pk, new records are handled different.
// boolean must be false before clear (that fires a aggregate change so again a getRecord())
clearInternalState(true);
}
int newSize = getRawSize();
fireDifference(oldSize, newSize, changes);
boolean selectedPKsRecPresent = false;
if (selectedPKs != null) {
selectedPKsRecPresent = selectedPKs.length == 1 ? selectRecord(selectedPKs[0]) : selectRecords(selectedPKs);
}
if (!selectedPKsRecPresent) {
if (fsm.getApplication().isEventDispatchThread()) {
setSelectedIndex(newSize > 0 ? 0 : -1);
} else {
fsm.getApplication().invokeLater(() -> setSelectedIndex(newSize > 0 ? 0 : -1));
}
}
return true;
}
use of com.servoy.j2db.persistence.RepositoryException in project servoy-client by Servoy.
the class FoundSet method deleteAllInternal.
public void deleteAllInternal() throws ServoyException {
Table table = sheet.getTable();
if (table == null) {
return;
}
fireSelectionAdjusting();
boolean partOfBiggerDelete = false;
// does have access, does not have join to other table and doesn't have a on delete method
QuerySelect sqlSelect;
IDataSet currentPKs;
synchronized (pksAndRecords) {
sqlSelect = pksAndRecords.getQuerySelectForReading();
currentPKs = pksAndRecords.getPks();
}
if (!hasAccess(IRepository.TRACKING) && sqlSelect.getJoins() == null && !tableHasOnDeleteMethods()) {
if (!hasAccess(IRepository.DELETE)) {
throw new ApplicationException(ServoyException.NO_DELETE_ACCESS, new Object[] { table.getName() });
}
boolean hasRelationsWithDelete = false;
Iterator<Relation> it = fsm.getApplication().getFlattenedSolution().getRelations(table, true, false);
while (it.hasNext()) {
Relation element = it.next();
if ((element.getDeleteRelatedRecords() || !element.getAllowParentDeleteWhenHavingRelatedRecords()) && !element.isGlobal()) {
// $NON-NLS-1$ //$NON-NLS-2$
Debug.trace("Foundset deleted per-record because relation '" + element.getName() + "' requires some checks");
hasRelationsWithDelete = true;
break;
}
}
if (!hasRelationsWithDelete) {
getFoundSetManager().getEditRecordList().removeEditedRecords(this);
// do sql delete all at once
QueryDelete delete_sql = new QueryDelete(sqlSelect.getTable());
delete_sql.setCondition(sqlSelect.getWhereClone());
IDataSet deletePKs;
boolean allFoundsetRecordsLoaded = currentPKs != null && pksAndRecords.getCachedRecords().size() == getSize() && !hadMoreRows();
if (allFoundsetRecordsLoaded) {
// clone because this will be used in a separate thread by performUpdates while it will be altered in this one (deletes all records at the end of the method)
deletePKs = currentPKs.clone();
} else {
deletePKs = new BufferedDataSet();
deletePKs.addRow(new Object[] { ValueFactory.createTableFlushValue() });
}
String tid = fsm.getTransactionID(table.getServerName());
SQLStatement statement = new SQLStatement(ISQLActionTypes.DELETE_ACTION, table.getServerName(), table.getName(), deletePKs, tid, delete_sql, fsm.getTableFilterParams(table.getServerName(), delete_sql));
try {
Object[] results = fsm.getDataServer().performUpdates(fsm.getApplication().getClientID(), new ISQLStatement[] { statement });
for (int i = 0; results != null && i < results.length; i++) {
if (results[i] instanceof ServoyException) {
throw (ServoyException) results[i];
}
}
if (!allFoundsetRecordsLoaded) {
fsm.flushCachedDatabaseData(fsm.getDataSource(table));
}
partOfBiggerDelete = true;
} catch (ApplicationException aex) {
if (allFoundsetRecordsLoaded || aex.getErrorCode() != ServoyException.RECORD_LOCKED) {
throw aex;
}
// a record was locked by another client, try per-record
// $NON-NLS-1$
Debug.log("Could not delete all records in 1 statement (a record may be locked), trying per-record");
} catch (RemoteException e) {
throw new RepositoryException(e);
}
}
}
// Need to get all the PKs, recursive delete may not actually remove the PK from the list because it is already being deleted.
if (!partOfBiggerDelete) {
PksAndRecordsHolder pksAndRecordsCopy;
int rowCount;
synchronized (pksAndRecords) {
pksAndRecordsCopy = pksAndRecords.shallowCopy();
IDataSet pks = pksAndRecordsCopy.getPks();
rowCount = pks == null ? 0 : pks.getRowCount();
}
queryForMorePKs(pksAndRecordsCopy, rowCount, -1, false);
}
try {
for (int i = getSize() - 1; i >= 0; i--) {
deleteRecord(i, partOfBiggerDelete);
}
} finally {
int correctedSize = getCorrectedSizeForFires();
if (correctedSize > -1)
fireFoundSetEvent(0, correctedSize, FoundSetEvent.CHANGE_DELETE);
}
}
Aggregations