Search in sources :

Example 1 with Relation

use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.

the class FlattenedSolution method getGlobalDataProviderEx.

private IDataProvider getGlobalDataProviderEx(String id, boolean quiet) throws RepositoryException {
    if (id == null)
        return null;
    Pair<String, String> scope = ScopesUtils.getVariableScope(id);
    if (scope.getLeft() != null) /* global scope */
    {
        // search all objects,will return globals
        ScriptVariable global = AbstractBase.selectByName(getScriptVariables(scope.getLeft(), false), scope.getRight());
        if (global != null) {
            return global;
        }
        // try @enum global variables
        return getEnumDataProvider(id);
    }
    // in case of multi-level relations we have more that 1 dot
    int indx = id.lastIndexOf('.');
    if (indx > 0) {
        String rel_name = id.substring(0, indx);
        String col = id.substring(indx + 1);
        Relation[] relations = getRelationSequence(rel_name);
        if (relations == null) {
            return null;
        }
        Relation r = relations[relations.length - 1];
        if (quiet) {
            boolean missingSrv = true;
            String ds = r.getForeignDataSource();
            if (ds != null) {
                String[] st = DataSourceUtilsBase.getDBServernameTablename(ds);
                if (st != null && st.length == 2) {
                    try {
                        missingSrv = (r.getRootObject().getServer(st[0]) == null);
                    } catch (RemoteException e) {
                    // we are in developer here - shouldn't happen
                    }
                }
            }
            if (missingSrv)
                return null;
        }
        // TODO if this is refactord out to be resolved outside the relation also look at the DataProviderConverter
        // the call from that class to flattenedSolution.getGlobalDataProvider(value);
        Column[] cols = r.getForeignColumns(this);
        if (cols == null || cols.length == 0)
            return null;
        IDataProvider c = getDataProviderForTable(getTable(r.getForeignDataSource()), col);
        if (r != null && c instanceof IColumn) {
            return new ColumnWrapper((IColumn) c, relations);
        }
        return c;
    }
    return null;
}
Also used : ColumnWrapper(com.servoy.j2db.persistence.ColumnWrapper) IDataProvider(com.servoy.j2db.persistence.IDataProvider) Relation(com.servoy.j2db.persistence.Relation) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IColumn(com.servoy.j2db.persistence.IColumn) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) RemoteException(java.rmi.RemoteException)

Example 2 with Relation

use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.

the class FlattenedSolution method getRelationSequence.

/**
 * Get relations from string rel1.rel2. ... .reln
 *
 * @param name
 * @return
 */
public Relation[] getRelationSequence(String name) {
    if (name == null) {
        return null;
    }
    // $NON-NLS-1$
    String[] parts = name.split("\\.");
    Relation[] seq = new Relation[parts.length];
    Relation prev = null;
    for (int i = 0; i < parts.length; i++) {
        Relation relation = getRelation(parts[i]);
        if (relation == null || (prev != null && (prev.getForeignDataSource() == null || !prev.getForeignDataSource().equals(relation.getPrimaryDataSource())))) {
            return null;
        }
        seq[i] = relation;
        prev = relation;
    }
    return seq;
}
Also used : Relation(com.servoy.j2db.persistence.Relation)

Example 3 with Relation

use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.

the class FlattenedSolution method getValuelistSortRelation.

/**
 * Get the internal relation that can be used to sort on this value list using the display values.
 * @param valueList
 * @param callingTable
 * @param dataProviderID
 * @param foundSetManager
 * @return
 * @throws RepositoryException
 */
public Relation getValuelistSortRelation(ValueList valueList, Table callingTable, String dataProviderID, IFoundSetManagerInternal foundSetManager) throws RepositoryException {
    if (callingTable == null || valueList == null) {
        return null;
    }
    String destDataSource;
    Relation[] relationSequence;
    String relationPrefix;
    switch(valueList.getDatabaseValuesType()) {
        case IValueListConstants.TABLE_VALUES:
            // create an internal relation
            relationSequence = null;
            // $NON-NLS-1$
            relationPrefix = "";
            destDataSource = valueList.getDataSource();
            break;
        case IValueListConstants.RELATED_VALUES:
            // replace the last relation in the sequence with an internal relation
            relationSequence = getRelationSequence(valueList.getRelationName());
            if (relationSequence == null) {
                return null;
            }
            if (relationSequence.length > 1) {
                for (Relation r : relationSequence) {
                    if (r.getJoinType() != INNER_JOIN) {
                        // outer join on the intermediate tables causes extra results that influence the sorting result
                        return null;
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            for (Relation r : relationSequence) {
                sb.append('-').append(r.getName());
            }
            relationPrefix = sb.toString();
            destDataSource = relationSequence[relationSequence.length - 1].getForeignDataSource();
            break;
        default:
            return null;
    }
    if (destDataSource == null || !DataSourceUtils.isSameServer(callingTable.getDataSource(), destDataSource)) {
        // do not create a cross-server relation
        return null;
    }
    Table destTable = (Table) foundSetManager.getTable(destDataSource);
    if (destTable == null) {
        return null;
    }
    String relationName = // $NON-NLS-1$
    Relation.INTERNAL_PREFIX + "VL-" + callingTable.getDataSource() + '-' + dataProviderID + relationPrefix + '-' + valueList.getName() + '-';
    synchronized (this) {
        Column callingColumn = callingTable.getColumn(dataProviderID);
        if (callingColumn == null) {
            return null;
        }
        Relation relation = getRelation(relationName);
        if (relation == null) {
            // create in internal relation
            String dp;
            int returnValues = valueList.getReturnDataProviders();
            if ((returnValues & 1) != 0) {
                dp = valueList.getDataProviderID1();
            } else if ((returnValues & 2) != 0) {
                dp = valueList.getDataProviderID2();
            } else if ((returnValues & 4) != 0) {
                dp = valueList.getDataProviderID3();
            } else {
                return null;
            }
            Column destColumn = destTable.getColumn(dp);
            if (destColumn == null) {
                return null;
            }
            // create internal value list relation
            QueryTable callingQTable = new QueryTable(callingTable.getSQLName(), callingTable.getDataSource(), callingTable.getCatalog(), callingTable.getSchema());
            QueryTable destQTable = new QueryTable(destTable.getSQLName(), destTable.getDataSource(), destTable.getCatalog(), destTable.getSchema());
            List<ISQLTableJoin> joins = new ArrayList<ISQLTableJoin>();
            ISQLTableJoin lastJoin = null;
            if (relationSequence == null) {
                // table values
                joins.add(lastJoin = new QueryJoin(relationName, callingQTable, destQTable, new AndCondition(), LEFT_OUTER_JOIN, false));
                if (// apply name as filter on column valuelist_name
                valueList.getUseTableFilter()) {
                    lastJoin.getCondition().addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, new QueryColumn(destQTable, DBValueList.NAME_COLUMN), valueList.getName()));
                }
            } else {
                // related values
                QueryTable primaryQTable = callingQTable;
                for (int i = 0; i < relationSequence.length; i++) {
                    Relation r = relationSequence[i];
                    QueryTable foreignQTable;
                    if (i == relationSequence.length - 1) {
                        // last one
                        foreignQTable = destQTable;
                    } else {
                        ITable relForeignTable = getTable(r.getForeignDataSource());
                        if (relForeignTable == null) {
                            return null;
                        }
                        foreignQTable = new QueryTable(relForeignTable.getSQLName(), relForeignTable.getDataSource(), relForeignTable.getCatalog(), relForeignTable.getSchema());
                    }
                    lastJoin = SQLGenerator.createJoin(this, r, primaryQTable, foreignQTable, false, new IGlobalValueEntry() {

                        public Object setDataProviderValue(String dpid, Object value) {
                            return null;
                        }

                        public Object getDataProviderValue(String dpid) {
                            // A value will be added when the relation is used, see SQLGenerator.createJoin
                            return new Placeholder(new ObjectPlaceholderKey<int[]>(null, dpid));
                        }

                        public boolean containsDataProvider(String dpid) {
                            return false;
                        }
                    });
                    joins.add(lastJoin);
                    primaryQTable = foreignQTable;
                }
            }
            // add condition for return dp id
            lastJoin.getCondition().addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, destColumn.queryColumn(destQTable), callingColumn.queryColumn(callingQTable)));
            relation = getSolutionCopy().createNewRelation(new ScriptNameValidator(this), relationName, callingTable.getDataSource(), destDataSource, LEFT_OUTER_JOIN);
            ISQLTableJoin join;
            if (joins.size() == 1) {
                join = lastJoin;
            } else {
                // combine joins
                join = new QueryCompositeJoin(relationName, joins);
            }
            relation.setRuntimeProperty(Relation.RELATION_JOIN, join);
        }
        return relation;
    }
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) Table(com.servoy.j2db.persistence.Table) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) ArrayList(java.util.ArrayList) QueryJoin(com.servoy.j2db.query.QueryJoin) QueryTable(com.servoy.j2db.query.QueryTable) AndCondition(com.servoy.j2db.query.AndCondition) Relation(com.servoy.j2db.persistence.Relation) IGlobalValueEntry(com.servoy.j2db.dataprocessing.IGlobalValueEntry) QueryCompositeJoin(com.servoy.j2db.query.QueryCompositeJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) CompareCondition(com.servoy.j2db.query.CompareCondition) ITable(com.servoy.j2db.persistence.ITable) JSONObject(org.json.JSONObject) IRootObject(com.servoy.j2db.persistence.IRootObject) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator)

Example 4 with Relation

use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.

the class DisplaysAdapter method startEdit.

public static void startEdit(DataAdapterList dal, IDisplay display, IRecordInternal state) {
    final IApplication application = dal.getApplication();
    dal.setCurrentDisplay(display);
    boolean isGlobal = false;
    boolean isColumn = true;
    if (display instanceof IDisplayData) {
        String dataProviderID = ((IDisplayData) display).getDataProviderID();
        isGlobal = dataProviderID != null && ScopesUtils.isVariableScope(dataProviderID);
        if (!isGlobal && dataProviderID != null) {
            // $NON-NLS-1$
            String[] parts = dataProviderID.split("\\.");
            IRecordInternal currState = state;
            for (int i = 0; i < parts.length - 1; i++) {
                IFoundSetInternal foundset = currState.getRelatedFoundSet(parts[i]);
                if (foundset == null) {
                    break;
                }
                Relation r = application.getFoundSetManager().getApplication().getFlattenedSolution().getRelation(parts[i]);
                currState = foundset.getRecord(foundset.getSelectedIndex());
                if (currState == null) {
                    if (r != null && r.getAllowCreationRelatedRecords()) {
                        try {
                            currState = foundset.getRecord(foundset.newRecord(0, true));
                        } catch (ServoyException se) {
                            application.reportError(se.getLocalizedMessage(), se);
                        }
                    } else {
                        final ApplicationException ae = new ApplicationException(ServoyException.NO_RELATED_CREATE_ACCESS, new Object[] { parts[i] });
                        ae.setContext(dal.getFormController().toString());
                        // unfocus the current field, otherwise when the dialog is closed focus is set back to this field and the same error recurs ad infinitum.
                        application.looseFocus();
                        application.invokeLater(new Runnable() {

                            public void run() {
                                // ApplicationException knows how to translate this null into an i18n message
                                application.handleException(null, ae);
                            }
                        });
                    }
                }
                if (currState == null)
                    return;
            }
            isColumn = currState.getParentFoundSet().getColumnIndex(parts[parts.length - 1]) != -1 || currState.getParentFoundSet().containsCalculation(dataProviderID);
        }
    }
    if (// globals are always allowed to set in datarenderers
    isGlobal || !isColumn || state.startEditing()) {
        // bit ugly should use property event here
        if (application instanceof ISmartClientApplication)
            ((ISmartClientApplication) application).updateInsertModeIcon(display);
    } else {
        // loose focus first
        // don't transfer focus to menu bar.. (macosx)
        // application.getMainApplicationFrame().getJMenuBar().requestFocus();
        application.looseFocus();
        application.reportWarningInStatus(// $NON-NLS-1$
        application.getI18NMessage("servoy.foundSet.error.noModifyAccess", new Object[] { state.getParentFoundSet().getDataSource() }));
    }
}
Also used : ISmartClientApplication(com.servoy.j2db.ISmartClientApplication) ServoyException(com.servoy.j2db.util.ServoyException) IApplication(com.servoy.j2db.IApplication) Relation(com.servoy.j2db.persistence.Relation) ApplicationException(com.servoy.j2db.ApplicationException)

Example 5 with Relation

use of com.servoy.j2db.persistence.Relation in project servoy-client by Servoy.

the class FindState method createFindStateJoins.

/**
 * Find all processable related find states and create joins. A find state is processable when it has changed or when a related find state has changed.
 * @param sqlSelect
 * @param relations path to this state
 * @param selectTable
 * @param provider
 * @return
 * @throws RepositoryException
 */
public List<RelatedFindState> createFindStateJoins(QuerySelect sqlSelect, List<IRelation> relations, BaseQueryTable selectTable, IGlobalValueEntry provider) throws RepositoryException {
    List<RelatedFindState> relatedFindStates = null;
    List<Relation> searchRelations = getValidSearchRelations();
    // find processable find states of related find states
    for (Relation relation : searchRelations) {
        if (relation != null) {
            IFoundSetInternal set = relatedStates.get(relation.getName());
            if (set != null && set.getSize() > 0) {
                ISQLTableJoin existingJoin = (ISQLTableJoin) sqlSelect.getJoin(selectTable, relation.getName());
                BaseQueryTable foreignQTable;
                if (existingJoin == null) {
                    ITable foreignTable = parent.getFoundSetManager().getApplication().getFlattenedSolution().getTable(relation.getForeignDataSource());
                    foreignQTable = new QueryTable(foreignTable.getSQLName(), foreignTable.getDataSource(), foreignTable.getCatalog(), foreignTable.getSchema());
                } else {
                    foreignQTable = existingJoin.getForeignTable();
                }
                FindState fs = (FindState) set.getRecord(0);
                List<IRelation> nextRelations = new ArrayList<IRelation>(relations);
                nextRelations.add(relation);
                List<RelatedFindState> rfs = fs.createFindStateJoins(sqlSelect, nextRelations, foreignQTable, provider);
                if (rfs != null && rfs.size() > 0) {
                    // changed related findstate, add self with join
                    if (relatedFindStates == null) {
                        relatedFindStates = rfs;
                    } else {
                        relatedFindStates.addAll(rfs);
                    }
                    if (existingJoin == null) {
                        sqlSelect.addJoin(SQLGenerator.createJoin(parent.getFoundSetManager().getApplication().getFlattenedSolution(), relation, selectTable, foreignQTable, false, provider));
                    }
                }
            }
        }
    }
    // add yourself if you have changed or one or more related states has changed
    if (isChanged() || (relatedFindStates != null && relatedFindStates.size() > 0)) {
        if (relatedFindStates == null) {
            relatedFindStates = new ArrayList<RelatedFindState>();
        }
        relatedFindStates.add(new RelatedFindState(this, relations, selectTable));
    }
    return relatedFindStates;
}
Also used : ArrayList(java.util.ArrayList) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) Relation(com.servoy.j2db.persistence.Relation) IRelation(com.servoy.j2db.persistence.IRelation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) IRelation(com.servoy.j2db.persistence.IRelation) ITable(com.servoy.j2db.persistence.ITable)

Aggregations

Relation (com.servoy.j2db.persistence.Relation)53 ServoyException (com.servoy.j2db.util.ServoyException)16 ArrayList (java.util.ArrayList)16 FlattenedSolution (com.servoy.j2db.FlattenedSolution)15 RepositoryException (com.servoy.j2db.persistence.RepositoryException)15 Column (com.servoy.j2db.persistence.Column)11 ITable (com.servoy.j2db.persistence.ITable)11 RemoteException (java.rmi.RemoteException)10 BaseQueryTable (com.servoy.base.query.BaseQueryTable)9 IDataProvider (com.servoy.j2db.persistence.IDataProvider)8 QuerySelect (com.servoy.j2db.query.QuerySelect)8 QueryTable (com.servoy.j2db.query.QueryTable)8 IColumn (com.servoy.j2db.persistence.IColumn)7 Placeholder (com.servoy.j2db.query.Placeholder)7 ApplicationException (com.servoy.j2db.ApplicationException)6 Table (com.servoy.j2db.persistence.Table)6 QueryColumn (com.servoy.j2db.query.QueryColumn)6 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)5 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)5 SafeArrayList (com.servoy.j2db.util.SafeArrayList)5