Search in sources :

Example 71 with FlattenedSolution

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();
        }
    }
}
Also used : Relation(com.servoy.j2db.persistence.Relation) ApplicationException(com.servoy.j2db.ApplicationException) FlattenedSolution(com.servoy.j2db.FlattenedSolution)

Example 72 with FlattenedSolution

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;
}
Also used : TableNode(com.servoy.j2db.persistence.TableNode) FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 73 with FlattenedSolution

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);
}
Also used : Relation(com.servoy.j2db.persistence.Relation) FlattenedSolution(com.servoy.j2db.FlattenedSolution) ServoyException(com.servoy.j2db.util.ServoyException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 74 with FlattenedSolution

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);
}
Also used : TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) BaseQueryTable(com.servoy.base.query.BaseQueryTable) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) ITable(com.servoy.j2db.persistence.ITable) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) Pair(com.servoy.j2db.util.Pair)

Example 75 with FlattenedSolution

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;
}
Also used : Relation(com.servoy.j2db.persistence.Relation) FlattenedSolution(com.servoy.j2db.FlattenedSolution)

Aggregations

FlattenedSolution (com.servoy.j2db.FlattenedSolution)79 JSFunction (org.mozilla.javascript.annotations.JSFunction)27 Form (com.servoy.j2db.persistence.Form)20 RepositoryException (com.servoy.j2db.persistence.RepositoryException)17 ArrayList (java.util.ArrayList)17 Relation (com.servoy.j2db.persistence.Relation)15 Media (com.servoy.j2db.persistence.Media)10 Solution (com.servoy.j2db.persistence.Solution)10 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)9 IBaseSMForm (com.servoy.base.solutionmodel.IBaseSMForm)7 AbstractActiveSolutionHandler (com.servoy.j2db.AbstractActiveSolutionHandler)7 ISMForm (com.servoy.j2db.solutionmodel.ISMForm)7 FormController (com.servoy.j2db.FormController)6 TableNode (com.servoy.j2db.persistence.TableNode)6 ValueList (com.servoy.j2db.persistence.ValueList)6 JSForm (com.servoy.j2db.scripting.solutionmodel.JSForm)6 IApplicationServer (com.servoy.j2db.server.shared.IApplicationServer)6 ITable (com.servoy.j2db.persistence.ITable)5 QueryTable (com.servoy.j2db.query.QueryTable)5 SafeArrayList (com.servoy.j2db.util.SafeArrayList)5