use of com.servoy.j2db.util.ServoyException in project servoy-client by Servoy.
the class JSDatabaseManager method convertFoundSet.
public FoundSet convertFoundSet(Object foundset, Object related) throws ServoyException {
checkAuthorized();
if (foundset instanceof FoundSet && ((FoundSet) foundset).getTable() != null) {
FoundSet fs_old = (FoundSet) foundset;
try {
String relationName;
if (related instanceof RelatedFoundSet) {
relationName = ((RelatedFoundSet) related).getRelationName();
} else if (related instanceof String) {
relationName = (String) related;
} else {
// $NON-NLS-1$
Debug.warn("convertFoundSet: invalid argument " + related);
return null;
}
Relation relation = application.getFlattenedSolution().getRelation(relationName);
if (relation == null || relation.isMultiServer() || fs_old.getTable() == null || !fs_old.getTable().equals(application.getFlattenedSolution().getTable(relation.getPrimaryDataSource()))) {
// $NON-NLS-1$
Debug.warn("convertFoundSet: cannot use relation " + relationName);
return null;
}
ITable ft = application.getFlattenedSolution().getTable(relation.getForeignDataSource());
FoundSet fs_new = (FoundSet) application.getFoundSetManager().getNewFoundSet(ft, null, application.getFoundSetManager().getDefaultPKSortColumns(ft.getDataSource()));
QuerySelect sql = fs_old.getPksAndRecords().getQuerySelectForModification();
SQLSheet sheet_new = fs_old.getSQLSheet().getRelatedSheet(relation, ((FoundSetManager) application.getFoundSetManager()).getSQLGenerator());
if (sheet_new != null) {
BaseQueryTable oldTable = sql.getTable();
ISQLTableJoin join = (ISQLTableJoin) sql.getJoin(oldTable, relation.getName());
if (join == null) {
join = SQLGenerator.createJoin(application.getFlattenedSolution(), relation, oldTable, new QueryTable(ft.getSQLName(), ft.getDataSource(), ft.getCatalog(), ft.getSchema()), true, fs_old);
sql.addJoin(join);
}
BaseQueryTable mainTable = join.getForeignTable();
// invert the join
sql.setTable(mainTable);
// $NON-NLS-1$
join.invert("INVERTED." + join.getName());
// set the columns to be the PKs from the related table
ArrayList<IQuerySelectValue> pkColumns = new ArrayList<IQuerySelectValue>();
Iterator<Column> pks = sheet_new.getTable().getRowIdentColumns().iterator();
while (pks.hasNext()) {
Column column = pks.next();
pkColumns.add(column.queryColumn(mainTable));
}
sql.setColumns(pkColumns);
// sorting will be on the original columns, when distinct is set, this will conflict with the related pk columns
sql.setDistinct(false);
fs_new.setSQLSelect(sql);
return fs_new;
}
} catch (Exception e) {
Debug.error(e);
}
}
return null;
}
use of com.servoy.j2db.util.ServoyException in project servoy-client by Servoy.
the class FoundSetManager method getSortColumns.
public List<SortColumn> getSortColumns(ITable t, String options) {
List<SortColumn> list = new ArrayList<SortColumn>(3);
if (t == null)
return list;
if (options != null) {
try {
// $NON-NLS-1$
StringTokenizer tk = new StringTokenizer(options, ",");
while (tk.hasMoreTokens()) {
String columnName = null;
String order = null;
String def = tk.nextToken().trim();
// $NON-NLS-1$
int index = def.indexOf(" ");
if (index != -1) {
columnName = def.substring(0, index);
order = def.substring(index + 1);
} else {
columnName = def;
}
if (columnName != null) {
SortColumn sc = getSortColumn(t, Utils.toEnglishLocaleLowerCase(columnName), true);
if (sc != null) {
if (// $NON-NLS-1$
order != null && order.trim().toLowerCase().startsWith("desc")) {
sc.setSortOrder(SortColumn.DESCENDING);
}
list.add(sc);
}
}
}
} catch (Exception ex) {
Debug.error(ex);
}
}
if (list.size() == 0) {
// default pk sort
try {
return getDefaultPKSortColumns(t.getDataSource());
} catch (ServoyException e) {
Debug.error(e);
}
}
return list;
}
use of com.servoy.j2db.util.ServoyException in project servoy-client by Servoy.
the class FoundSetManager method notifyDataChange.
/*
* _____________________________________________________________ dataNotification
*/
public void notifyDataChange(final String ds, IDataSet pks, final int action, Object[] insertColumnData) {
RowManager rm = rowManagers.get(ds);
if (rm != null) {
List<Row> insertedRows = null;
if (action == ISQLActionTypes.INSERT_ACTION && insertColumnData == null) {
// in this case the insert notification is probably triggered by rawSQL; so we need to read the new rows from DB to get correct newly inserted content
try {
insertedRows = rm.getRows(pks, 0, pks.getRowCount(), false);
if (insertedRows.size() != pks.getRowCount()) {
insertedRows = rm.getRows(pks, 0, pks.getRowCount(), true);
}
} catch (ServoyException e) {
Debug.error("Cannot get newly inserted rows.", e);
}
}
boolean didHaveRowAndIsUpdated = false;
IDataSet newPks = pks;
try {
// Convert the pk dataset to the column type of the pk columns
newPks = BufferedDataSetInternal.convertPksToRightType(pks, getTable(ds));
} catch (RepositoryException e) {
Debug.error(e);
}
final IDataSet fnewPks = newPks;
for (int i = 0; i < fnewPks.getRowCount(); i++) {
boolean b = rm.changeByOther(RowManager.createPKHashKey(fnewPks.getRow(i)), action, insertColumnData, insertedRows == null ? null : insertedRows.get(i));
didHaveRowAndIsUpdated = (didHaveRowAndIsUpdated || b);
}
// changed by other calls don't notify the table change for every row, call it once now.
notifyChange(rm.getSQLSheet().getTable());
final boolean didHaveDataCached = didHaveRowAndIsUpdated;
Runnable r = new Runnable() {
public void run() {
Solution solution = application.getSolution();
if (solution != null) {
ScriptMethod sm = application.getFlattenedSolution().getScriptMethod(solution.getOnDataBroadcastMethodID());
if (sm != null) {
try {
application.getScriptEngine().getScopesScope().executeGlobalFunction(sm.getScopeName(), sm.getName(), arrayMerge(new Object[] { ds, new Integer(action), new JSDataSet(application, fnewPks), Boolean.valueOf(didHaveDataCached) }, // $NON-NLS-1$
parseJSExpressions(solution.getFlattenedMethodArguments("onDataBroadcastMethodID"))), false, false);
} catch (Exception e1) {
application.reportError(Messages.getString("servoy.foundsetManager.error.ExecutingDataBroadcastMethod", new Object[] { sm.getName() }), // $NON-NLS-1$
e1);
}
}
}
}
};
application.invokeLater(r);
if (didHaveRowAndIsUpdated) {
if (infoListener != null)
infoListener.showDataChange();
} else // TODO if(action == INSERT) This is called to often now.
{
fireTableEvent(rm.getSQLSheet().getTable());
}
}
}
Aggregations