use of com.servoy.j2db.util.SafeArrayList in project servoy-client by Servoy.
the class TestNGClient method createDataServer.
@Override
protected IDataServer createDataServer() {
return new IDataServer() {
private final HashMap<String, IDataSet> dataSetMap = new HashMap<String, IDataSet>();
@Override
public void setServerMaintenanceMode(boolean maintenanceMode) throws RemoteException {
}
@Override
public void logMessage(String msg) throws RemoteException {
}
@Override
public boolean isInServerMaintenanceMode() throws RemoteException {
return false;
}
@Override
public boolean releaseLocks(String client_id, String server_name, String table_name, Set<Object> pkhashkeys) throws RemoteException, RepositoryException {
return false;
}
@Override
public IDataSet acquireLocks(String client_id, String server_name, String table_name, Set<Object> pkhashkeys, QuerySelect lockSelect, String transaction_id, ArrayList<TableFilter> filters, int chunkSize) throws RemoteException, RepositoryException {
return null;
}
@Override
public String startTransaction(String clientId, String server_name) throws RepositoryException, RemoteException {
return null;
}
@Override
public Object[] performUpdates(String clientId, ISQLStatement[] statements) throws ServoyException, RemoteException {
return statements;
}
@Override
public IDataSet[] performQuery(String client_id, String server_name, String transaction_id, QueryData[] array) throws ServoyException, RemoteException {
if (array.length > 0) {
String ds = array[0].getSqlSelect().getTable().getDataSource();
if ("mem:relatedtest".equals(ds)) {
IDataSet set = dataSetMap.get(ds);
IDataSet[] returnDataSet = new IDataSet[array.length];
for (int i = 0; i < array.length; i++) {
returnDataSet[i] = new BufferedDataSet();
for (int k = 0; k < set.getRowCount(); k++) {
Object[][] value = (Object[][]) ((Placeholder) ((SetCondition) ((QuerySelect) array[i].getSqlSelect()).getConditions().values().iterator().next().getConditions().get(0)).getValues()).getValue();
if (set.getRow(k)[1].equals(value[0][0])) {
returnDataSet[i].addRow(new Object[] { set.getRow(k)[0], set.getRow(k)[1], set.getRow(k)[2], set.getRow(k)[3] });
}
}
}
return returnDataSet;
}
}
return null;
}
@Override
public IDataSet performQuery(String client_id, String server_name, String driverTableName, String transaction_id, String sql, Object[] questiondata, int startRow, int rowsToRetrieve, boolean updateIdleTimestamp) throws ServoyException, RemoteException {
// don't know the
return dataSetMap.values().iterator().next();
// datasource,
// just return
// the first
// dataset
}
@Override
public IDataSet performQuery(String client_id, String server_name, String transaction_id, ISQLSelect sqlSelect, ColumnType[] resultTypes, ArrayList<TableFilter> filters, boolean distinctInMemory, int startRow, int rowsToRetrieve, boolean updateIdleTimestamp) throws ServoyException, RemoteException {
return dataSetMap.get(sqlSelect.getTable().getDataSource());
}
@Override
public IDataSet performQuery(String client_id, String server_name, String driverTableName, String transaction_id, String sql, Object[] questiondata, int startRow, int rowsToRetrieve, int type) throws ServoyException, RemoteException {
// don't know the
return dataSetMap.values().iterator().next();
// datasource,
// just return
// the first
// dataset
}
@Override
public IDataSet performQuery(String client_id, String server_name, String transaction_id, ISQLSelect sqlSelect, ColumnType[] resultTypes, ArrayList<TableFilter> filters, boolean distinctInMemory, int startRow, int rowsToRetrieve, int type, ITrackingSQLStatement trackingInfo) throws ServoyException, RemoteException {
return dataSetMap.get(sqlSelect.getTable().getDataSource());
}
@Override
public IDataSet performQuery(String client_id, String server_name, String transaction_id, ISQLSelect sqlSelect, ColumnType[] resultTypes, ArrayList<TableFilter> filters, boolean distinctInMemory, int startRow, int rowsToRetrieve, int type) throws ServoyException, RemoteException {
IDataSet set = dataSetMap.get(sqlSelect.getTable().getDataSource());
if (sqlSelect instanceof QuerySelect && ((QuerySelect) sqlSelect).getColumns().size() == 1) {
// pk select
int lastRow = Math.min(set.getRowCount(), startRow + rowsToRetrieve);
BufferedDataSet ds = BufferedDataSetInternal.createBufferedDataSet(null, null, new SafeArrayList<Object[]>(0), lastRow < set.getRowCount());
for (int i = startRow; i < lastRow; i++) {
ds.addRow(new Object[] { set.getRow(i)[0] });
}
return ds;
}
return set;
}
@Override
public IDataSet performQuery(String client_id, String server_name, String driverTableName, String transaction_id, String sql, Object[] questiondata, int startRow, int rowsToRetrieve) throws ServoyException, RemoteException {
// don't know the
return dataSetMap.values().iterator().next();
// datasource,
// just return
// the first
// dataset
}
@Override
public IDataSet performQuery(String client_id, String server_name, String transaction_id, ISQLSelect sqlSelect, ColumnType[] resultTypes, ArrayList<TableFilter> filters, boolean distinctInMemory, int startRow, int rowsToRetrieve) throws ServoyException, RemoteException {
return dataSetMap.get(sqlSelect.getTable().getDataSource());
}
@Override
public IDataSet performCustomQuery(String client_id, String server_name, String driverTableName, String transaction_id, ISQLSelect sqlSelect, ArrayList<TableFilter> filters, int startRow, int rowsToRetrieve) throws ServoyException, RemoteException {
return dataSetMap.get(sqlSelect.getTable().getDataSource());
}
@Override
public boolean notifyDataChange(String client_id, String server_name, String tableName, IDataSet pks, int action, String transaction_id) throws RemoteException {
return false;
}
@Override
public ITable insertQueryResult(String client_id, String queryServerName, String queryTid, ISQLSelect sqlSelect, ArrayList<TableFilter> filters, boolean distinctInMemory, int startRow, int rowsToRetrieve, int type, String dataSource, String targetServerName, String targetTableName, String targetTid, ColumnType[] columnTypes, String[] pkNames) throws ServoyException, RemoteException {
return null;
}
@Override
public InsertResult insertDataSet(String client_id, IDataSet set, final String dataSource, String serverName, String tableName, String tid, ColumnType[] columnTypes, String[] pkNames, HashMap<String, ColumnInfoDef> columnInfoDefinitions) throws ServoyException, RemoteException {
dataSetMap.put(dataSource, set);
Table table = new Table(serverName, serverName, true, ITable.TABLE, null, null);
table.setDataSource(dataSource);
for (int i = 0; i < set.getColumnCount(); i++) {
Column col = new Column(table, set.getColumnNames()[i], set.getColumnTypes()[i], 50, 50, true);
table.addColumn(col);
if (Arrays.binarySearch(pkNames, col.getName()) >= 0) {
col.setDatabasePK(true);
}
}
return new InsertResult(table, new Object[0]);
}
@Override
public QuerySet getSQLQuerySet(String serverName, ISQLQuery sqlQuery, ArrayList<TableFilter> filters, int startRow, int rowsToRetrieve, boolean forceQualifyColumns, boolean disableUseArrayForIn) throws RepositoryException, RemoteException {
QuerySet qs = new QuerySet();
qs.setSelect(new QueryString("select from test", false));
return qs;
}
@Override
public Object getNextSequence(String serverName, String tableName, String columnName, int columnInfoID, String columnInfoServer) throws RepositoryException, RemoteException {
return null;
}
@Override
public Blob getBlob(String clientId, String serverName, ISQLSelect blobSelect, ArrayList<TableFilter> filters, String tid) throws RepositoryException, RemoteException {
return null;
}
@Override
public boolean endTransactions(String client_id, String[] transaction_id, boolean commit) throws RepositoryException, RemoteException {
return false;
}
@Override
public void dropTemporaryTable(String client_id, String serverName, String tableName) throws RemoteException, RepositoryException {
}
@Override
public ISQLStatement createSQLStatement(int action, String server_name, String tableName, Object[] pkColumnData, String tid, ISQLUpdate sqlUpdate, ArrayList<TableFilter> filters) throws RemoteException {
return null;
}
@Override
public ISQLStatement createSQLStatement(int action, String server_name, String tableName, Object[] pkColumnData, String tid, String sql, Object[] questiondata) throws RemoteException, RepositoryException {
return null;
}
@Override
public void addClientAsTableUser(String client_id, String serverName, String tableName) throws RemoteException, RepositoryException {
}
@Override
public IDataSet[] executeProcedure(String clientId, String server_name, String tid, Procedure procedure, Object[] arguments) throws RepositoryException, RemoteException {
return null;
}
};
}
use of com.servoy.j2db.util.SafeArrayList in project servoy-client by Servoy.
the class MediaURLStreamHandler method getBlobLoaderMedia.
public static byte[] getBlobLoaderMedia(IServiceProvider application, String urlQueryPart) throws IOException {
// cannot work without a solution
if (application.getSolution() == null)
return null;
String datasource = null;
String serverName = null;
String tableName = null;
try {
if (urlQueryPart == null)
return null;
String dataProvider = null;
List<String> pks = new SafeArrayList<String>();
// $NON-NLS-1$
StringTokenizer tk = new StringTokenizer(urlQueryPart, "?&");
while (tk.hasMoreTokens()) {
String token = tk.nextToken();
if (// $NON-NLS-1$
token.startsWith("global=")) {
// $NON-NLS-1$
String globalName = token.substring("global=".length());
Object obj = application.getScriptEngine().getScopesScope().get(null, globalName);
if (obj instanceof byte[]) {
return (byte[]) obj;
}
if (obj instanceof String) {
// TODO check can we always just convert to the default encoding of this machine (server if web)
return ((String) obj).getBytes();
}
return null;
}
if (// $NON-NLS-1$
token.startsWith("servername=")) {
// $NON-NLS-1$
serverName = token.substring("servername=".length());
} else if (// $NON-NLS-1$
token.startsWith("tablename=")) {
// $NON-NLS-1$
tableName = token.substring("tablename=".length());
} else if (// $NON-NLS-1$
token.startsWith("datasource=")) {
// $NON-NLS-1$
datasource = token.substring("datasource=".length());
} else if (// $NON-NLS-1$
token.startsWith("dataprovider=")) {
// $NON-NLS-1$
dataProvider = token.substring("dataprovider=".length());
} else if (// $NON-NLS-1$
token.startsWith("rowid")) {
// get id
int index = Utils.getAsInteger(token.substring(5, 6));
if (index > 0) {
// get value after 'rowidX='
pks.add(index - 1, token.substring(7));
} else {
// get value after 'rowid='
pks.add(0, token.substring(6));
}
}
}
if (datasource == null && serverName != null && tableName != null) {
datasource = DataSourceUtils.createDBTableDataSource(serverName, tableName);
}
if (application.getFoundSetManager().getTable(datasource) == null) {
// $NON-NLS-1$
throw new IOException(Messages.getString("servoy.exception.serverAndTableNotFound", DataSourceUtils.getDBServernameTablename(datasource)));
}
FoundSet fs = (FoundSet) application.getFoundSetManager().getNewFoundSet(datasource);
// use mutable list here, elements are overwritten with Column.getAsRightType equivalent
List<Object[]> rows = new ArrayList<Object[]>(1);
rows.add(pks.toArray());
if (!fs.loadExternalPKList(new BufferedDataSet(null, null, rows))) {
return null;
}
IRecordInternal rec = fs.getRecord(0);
if (rec == null) {
return null;
}
Object blob_value = rec.getValue(dataProvider);
if (blob_value instanceof byte[]) {
return (byte[]) blob_value;
}
if (blob_value instanceof String) {
// TODO check can we always just convert to the default encoding of this machine (server if web)
return ((String) blob_value).getBytes();
}
} catch (Exception e) {
throw new IOException(e.getMessage());
}
return null;
}
use of com.servoy.j2db.util.SafeArrayList in project servoy-client by Servoy.
the class FoundSet method copyFrom.
public boolean copyFrom(FoundSet fs) {
if (fs == null || fs.getTable() == null || fs == this)
return false;
if (!fs.getTable().equals(getTable()))
return false;
if (relationName != null)
return false;
int oldNumberOfRows = getSize();
fireSelectionAdjusting();
omittedPKs = null;
List<TableFilter> myOwnFilters = null;
// look for filters in this foundset that have not been applied to the other foundset
for (TableFilter filter : iterate(foundSetFilters)) {
if (fs.foundSetFilters == null || !fs.foundSetFilters.contains(filter)) {
if (myOwnFilters == null)
myOwnFilters = new ArrayList<TableFilter>(foundSetFilters.size());
myOwnFilters.add(filter);
}
}
sheet = fs.sheet;
IFoundSetChanges changes = pksAndRecords.setPksAndQuery(new BufferedDataSet(fs.pksAndRecords.getPks()), fs.pksAndRecords.getDbIndexLastPk(), addFilterconditions(fs.pksAndRecords.getQuerySelectForModification(), myOwnFilters));
if (fs.foundSetFilters != null) {
// copy over the foundset filters from the other fs, merged with the filters this foundset had
foundSetFilters = new ArrayList<>();
fs.foundSetFilters.forEach(filter -> {
if (filter.getTableFilterdefinition() instanceof QueryTableFilterdefinition) {
QuerySelect select = AbstractBaseQuery.deepClone(((QueryTableFilterdefinition) filter.getTableFilterdefinition()).getQuerySelect());
select.relinkTable(select.getTable(), creationSqlSelect.getTable());
foundSetFilters.add(new TableFilter(filter.getName(), sheet.getServerName(), sheet.getTable().getName(), sheet.getTable().getSQLName(), new QueryTableFilterdefinition(select)));
} else {
foundSetFilters.add(filter);
}
});
if (myOwnFilters != null) {
foundSetFilters.addAll(myOwnFilters);
}
resetFiltercondition(foundSetFilters);
}
initialized = fs.initialized;
clearInternalState(true);
SafeArrayList<IRecordInternal> fsCachedRecords = fs.pksAndRecords.getCachedRecords();
synchronized (fsCachedRecords) {
SafeArrayList<IRecordInternal> cachedRecords = pksAndRecords.getCachedRecords();
for (int i = 0; i < fsCachedRecords.size(); i++) {
IRecordInternal record = fsCachedRecords.get(i);
if (record != null && !record.existInDataSource()) {
cachedRecords.set(i, new Record(this, record.getRawData()));
}
}
}
lastSortColumns = ((FoundSetManager) getFoundSetManager()).getSortColumns(getTable(), fs.getSort());
fireDifference(oldNumberOfRows, getSize(), changes);
setMultiSelect(fs.isMultiSelect());
if (fs.isMultiSelect()) {
int[] selectedIndexes = fs.getSelectedIndexes();
if (selectedIndexes != null && selectedIndexes.length > 0)
setSelectedIndexes(selectedIndexes);
else
setSelectedIndex(fs.getSelectedIndex());
} else
setSelectedIndex(fs.getSelectedIndex());
if (myOwnFilters != null) {
// my own filters have been added, have to refresh
refresh();
}
return true;
}
use of com.servoy.j2db.util.SafeArrayList in project servoy-client by Servoy.
the class RowManager method getRows.
synchronized List<Row> getRows(IDataSet pks, int row, int sizeHint, boolean queryAll) throws ServoyException {
List<Row> retval = new SafeArrayList<Row>();
if (row >= pks.getRowCount())
return retval;
Object[] pk = pks.getRow(row);
Row rowData = queryAll ? null : getCachedRow(pk).getLeft();
if (rowData == null) {
String transaction_id = null;
GlobalTransaction gt = fsm.getGlobalTransaction();
if (gt != null) {
transaction_id = gt.getTransactionID(sheet.getServerName());
}
IDataSet formdata = null;
QuerySelect select = (QuerySelect) sheet.getSQL(SQLSheet.SELECT);
int maxRow = Math.min(row + sizeHint, pks.getRowCount());
// get the PK array
int ncols = pks.getColumnCount();
int nvals = 0;
@SuppressWarnings("unchecked") List<Object>[] valueLists = new List[ncols];
for (int c = 0; c < ncols; c++) {
valueLists[c] = new ArrayList<Object>();
}
for (int i = 0; i < maxRow - row; i++) {
Object[] data = pks.getRow(row + i);
if (data != null) {
if (data.length != ncols) {
// $NON-NLS-1$
throw new RuntimeException("Inconsistent PK set width");
}
boolean add = true;
for (int c = 0; add && c < ncols; c++) {
add = !(data[c] instanceof DbIdentValue);
}
if (add) {
nvals++;
for (int c = 0; c < ncols; c++) {
valueLists[c].add(data[c]);
}
}
}
}
Object[][] values = new Object[ncols][];
for (int c = 0; c < ncols; c++) {
values[c] = valueLists[c].toArray();
}
if (!select.setPlaceholderValue(new TablePlaceholderKey(select.getTable(), SQLGenerator.PLACEHOLDER_PRIMARY_KEY), values)) {
Debug.error(new RuntimeException(// $NON-NLS-1$
"Could not set placeholder " + new TablePlaceholderKey(select.getTable(), SQLGenerator.PLACEHOLDER_PRIMARY_KEY) + " in query " + select + // $NON-NLS-1$//$NON-NLS-2$
"-- continuing"));
}
long time = System.currentTimeMillis();
try {
SQLStatement trackingInfo = null;
if (fsm.getEditRecordList().hasAccess(sheet.getTable(), IRepository.TRACKING_VIEWS)) {
trackingInfo = new SQLStatement(ISQLActionTypes.SELECT_ACTION, sheet.getServerName(), sheet.getTable().getName(), pks, null);
trackingInfo.setTrackingData(sheet.getColumnNames(), new Object[][] {}, new Object[][] {}, fsm.getApplication().getUserUID(), fsm.getTrackingInfo(), fsm.getApplication().getClientID());
}
formdata = fsm.getDataServer().performQuery(fsm.getApplication().getClientID(), sheet.getServerName(), transaction_id, select, null, /* use types as reported by the db */
fsm.getTableFilterParams(sheet.getServerName(), select), false, 0, nvals, IDataServer.FOUNDSET_LOAD_QUERY, trackingInfo);
if (Debug.tracing()) {
Debug.trace(// $NON-NLS-1$ //$NON-NLS-2$
Thread.currentThread().getName() + ": getting RowData time: " + (System.currentTimeMillis() - time) + ", SQL: " + select.toString());
}
} catch (RemoteException e) {
throw new RepositoryException(e);
}
// construct Rows
for (int k = row; k < maxRow; k++) {
String pkHash = createPKHashKey(pks.getRow(k));
// reorder based on pk in mem,cannot do related sort icw SELECT_IN
for (int r = 0; r < formdata.getRowCount(); r++) {
Object[] columndata = formdata.getRow(r);
rowData = getRowBasedonPKFromEntireColumnArray(columndata);
if (pkHash.equals(createPKHashKey(rowData.getPK()))) {
retval.set(k - row, rowData);
break;
}
}
}
if (retval.size() < maxRow - row) {
retval.set(maxRow - row - 1, null);
}
} else {
retval.add(rowData);
if (sizeHint > 1) {
int maxRow = Math.min(row + fsm.config.chunkSize(), pks.getRowCount());
for (int r = row + 1; r < maxRow; r++) {
Object[] data = pks.getRow(r);
Row r2 = getCachedRow(data).getLeft();
if (r2 == null) {
// if there is no row te be found, just break and return the currently found retval.
break;
}
retval.add(r2);
}
}
}
return retval;
}
use of com.servoy.j2db.util.SafeArrayList in project servoy-client by Servoy.
the class GlobalMethodValueList method fill.
public void fill(IRecordInternal state, String filter, Object real, boolean force) {
String display = filter == null ? null : filter.toLowerCase();
if (filling) {
return;
}
try {
filling = true;
if (force || this.record != state || !Utils.equalObjects(display, this.displayString) || !Utils.equalObjects(real, this.realObject)) {
this.displayString = display;
this.realObject = real;
this.record = state;
super.fill(state);
GlobalScope globalScope = application.getScriptEngine().getScopesScope().getGlobalScope(globalFunction.getLeft());
if (globalScope != null) {
Function function = globalScope.getFunctionByName(globalFunction.getRight());
try {
if (// $NON-NLS-1$
real == null || "".equals(real)) {
application.invokeAndWait(new Runnable() {
public void run() {
realValues = new SafeArrayList<Object>();
removeAllElements();
}
});
} else if (realValues == null) {
realValues = new SafeArrayList<Object>();
}
Object[] args = null;
if (// $NON-NLS-1$
display != null && !"".equals(display)) {
args = new Object[] { display, null, state, valueList.getName(), Boolean.valueOf(state instanceof FindState), filter };
} else if (// $NON-NLS-1$
real != null && !"".equals(real)) {
args = new Object[] { null, real, state, valueList.getName(), Boolean.valueOf(state instanceof FindState), null };
} else {
args = new Object[] { null, null, state, valueList.getName(), Boolean.valueOf(state instanceof FindState), null };
}
final Object retValue = application.getScriptEngine().executeFunction(function, globalScope, globalScope, args, false, true);
application.invokeAndWait(new Runnable() {
public void run() {
// add empty row
if (valueList.getAddEmptyValue() == IValueListConstants.EMPTY_VALUE_ALWAYS) {
// $NON-NLS-1$
addElement("");
realValues.add(null);
}
if (retValue instanceof IDataSet && ((IDataSet) retValue).getRowCount() > 0) {
startBundlingEvents();
try {
hasRealValue = false;
IDataSet dataSet = (IDataSet) retValue;
for (int i = 0; i < dataSet.getRowCount(); i++) {
Object[] row = dataSet.getRow(i);
if (row.length == 1) {
realValues.add(CustomValueList.handleRowData(valueList, false, 1, row, application));
} else {
hasRealValue = true;
realValues.add(CustomValueList.handleRowData(valueList, false, 2, row, application));
}
addElement(CustomValueList.handleRowData(valueList, false, 1, row, application));
}
} finally {
stopBundlingEvents();
}
}
}
});
} catch (Exception e) {
// $NON-NLS-1$
application.reportError("error getting data from global method valuelist", e);
}
}
}
} finally {
filling = false;
}
}
Aggregations