Search in sources :

Example 56 with ServoyException

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;
}
Also used : ArrayList(java.util.ArrayList) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) QueryString(com.servoy.j2db.persistence.QueryString) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) SQLException(java.sql.SQLException) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Relation(com.servoy.j2db.persistence.Relation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) QBColumn(com.servoy.j2db.querybuilder.impl.QBColumn) ITable(com.servoy.j2db.persistence.ITable) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 57 with ServoyException

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;
}
Also used : StringTokenizer(java.util.StringTokenizer) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) MarshallException(org.jabsorb.serializer.MarshallException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ServoyException(com.servoy.j2db.util.ServoyException)

Example 58 with ServoyException

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());
        }
    }
}
Also used : RepositoryException(com.servoy.j2db.persistence.RepositoryException) ServoyException(com.servoy.j2db.util.ServoyException) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) MarshallException(org.jabsorb.serializer.MarshallException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Solution(com.servoy.j2db.persistence.Solution)

Aggregations

ServoyException (com.servoy.j2db.util.ServoyException)58 RepositoryException (com.servoy.j2db.persistence.RepositoryException)31 ApplicationException (com.servoy.j2db.ApplicationException)25 RemoteException (java.rmi.RemoteException)25 ArrayList (java.util.ArrayList)16 ITable (com.servoy.j2db.persistence.ITable)14 QuerySelect (com.servoy.j2db.query.QuerySelect)13 BaseQueryTable (com.servoy.base.query.BaseQueryTable)11 QueryTable (com.servoy.j2db.query.QueryTable)10 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)10 IOException (java.io.IOException)9 SQLException (java.sql.SQLException)9 JavaScriptException (org.mozilla.javascript.JavaScriptException)9 Column (com.servoy.j2db.persistence.Column)8 Table (com.servoy.j2db.persistence.Table)8 IBaseColumn (com.servoy.base.persistence.IBaseColumn)7 QueryColumn (com.servoy.j2db.query.QueryColumn)7 HashMap (java.util.HashMap)7 MarshallException (org.jabsorb.serializer.MarshallException)7 NativeObject (org.mozilla.javascript.NativeObject)7