Search in sources :

Example 21 with Relation

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

the class UsedDataProviderTracker method usedFromRecord.

/**
 * Used name from Record,
 * @param record may be null (for global relations)
 * @param name
 */
protected void usedFromRecord(IRecordInternal record, String name) {
    IRecordInternal currentRecord = record;
    try {
        // $NON-NLS-1$
        String[] parts = name.split("\\.");
        for (String part : parts) {
            Relation relation = flattenedSolution.getRelation(part);
            if (relation != null) {
                // calc depends on the relation, add a dependency for the primary data providers for the relation
                IDataProvider[] primaryDataProviders = relation.getPrimaryDataProviders(flattenedSolution);
                for (IDataProvider prim : primaryDataProviders) {
                    if (prim instanceof LiteralDataprovider)
                        continue;
                    String primdp = prim.getDataProviderID();
                    if (ScopesUtils.isVariableScope(primdp)) {
                        // global
                        usedGlobal(primdp);
                    } else {
                        // column
                        if (currentRecord != null) {
                            if (currentRecord.getRawData() == null) {
                                if (currentRecord instanceof PrototypeState) {
                                    Debug.trace("Calculation '" + name + "' depends on field '" + part + "' from PrototypeState " + currentRecord);
                                } else {
                                    // should not happen
                                    Debug.error("Unexpected state: calculation '" + name + "' depends on column '" + part + "' from record without pk: " + currentRecord, new IllegalStateException());
                                }
                            } else {
                                usedColumn(currentRecord.getParentFoundSet().getDataSource(), currentRecord.getRawData().getPKHashKey(), primdp);
                            }
                        }
                    }
                }
                IFoundSetInternal foundSet = null;
                if (currentRecord != null)
                    foundSet = currentRecord.getRelatedFoundSet(relation.getName());
                currentRecord = null;
                if (foundSet instanceof RelatedFoundSet) {
                    usedRelatedFoundSet(relation.getName(), (RelatedFoundSet) foundSet);
                    currentRecord = foundSet.getRecord(foundSet.getSelectedIndex());
                }
            } else {
                if (currentRecord != null) {
                    IFoundSetInternal foundSet = currentRecord.getParentFoundSet();
                    if (foundSet.getSQLSheet().containsAggregate(part)) {
                        // aggregate
                        usedAggregate(foundSet, part);
                    } else {
                        // field or calc
                        if (currentRecord.has(part)) {
                            if (currentRecord.getRawData() == null) {
                                if (currentRecord instanceof PrototypeState) {
                                    Debug.trace("Calculation '" + name + "' depends on field '" + part + "' from PrototypeState " + currentRecord);
                                } else {
                                    // should not happen
                                    Debug.error("Unexpected state: calculation '" + name + "' depends on field '" + part + "' from record without pk: " + currentRecord, new IllegalStateException());
                                }
                            } else {
                                usedColumn(foundSet.getDataSource(), currentRecord.getRawData().getPKHashKey(), part);
                            }
                        }
                    }
                }
                return;
            }
            if (currentRecord == null) {
                return;
            }
        }
    } catch (RepositoryException e) {
        Debug.error(e);
    }
}
Also used : Relation(com.servoy.j2db.persistence.Relation) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) RelatedFoundSet(com.servoy.j2db.dataprocessing.RelatedFoundSet) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IDataProvider(com.servoy.j2db.persistence.IDataProvider) LiteralDataprovider(com.servoy.j2db.persistence.LiteralDataprovider) PrototypeState(com.servoy.j2db.dataprocessing.PrototypeState)

Example 22 with Relation

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

the class ViewFoundSet method getRelatedFoundSet.

/*
	 * (non-Javadoc)
	 *
	 * @see com.servoy.j2db.dataprocessing.IFoundSetInternal#getRelatedFoundSet(com.servoy.j2db.dataprocessing.IRecordInternal, java.lang.String,
	 * java.util.List)
	 */
@Override
public IFoundSetInternal getRelatedFoundSet(IRecordInternal record, String fullRelationName, List<SortColumn> defaultSortColumns) throws ServoyException {
    if (fullRelationName == null) {
        return null;
    }
    IFoundSetInternal retval = null;
    IRecordInternal currentRecord = record;
    // $NON-NLS-1$
    String[] parts = fullRelationName.split("\\.");
    for (int i = 0; i < parts.length; i++) {
        // if this is a findstate and that is not the source record then leave the relation lookup to the findstate itself.
        if (currentRecord instanceof FindState && i != 0) {
            String leftPart = parts[i];
            for (int k = i + 1; k < parts.length; k++) {
                // $NON-NLS-1$
                leftPart += "." + parts[k];
            }
            return currentRecord.getRelatedFoundSet(leftPart);
        }
        RowManager rowManager = manager.getRowManager(table.getDataSource());
        SQLSheet relatedSheet = rowManager.getSQLSheet().getRelatedSheet(getFoundSetManager().getApplication().getFlattenedSolution().getRelation(parts[i]), ((FoundSetManager) getFoundSetManager()).getSQLGenerator());
        if (relatedSheet == null) {
            retval = getFoundSetManager().getGlobalRelatedFoundSet(parts[i]);
        } else {
            retval = ((FoundSetManager) getFoundSetManager()).getRelatedFoundSet(currentRecord, relatedSheet, parts[i], defaultSortColumns);
            if (retval != null) {
                if (retval.getSize() == 0 && !currentRecord.existInDataSource()) {
                    Relation r = getFoundSetManager().getApplication().getFlattenedSolution().getRelation(parts[i]);
                    if (// TODO add unique column test instead of pk requirement
                    r != null && r.isExactPKRef(getFoundSetManager().getApplication().getFlattenedSolution())) {
                        ((FoundSet) retval).newRecord(record.getRawData(), 0, true, false);
                    }
                }
                retval.addParent(currentRecord);
            }
        }
        if (retval == null) {
            return null;
        }
        if (i < parts.length - 1) {
            currentRecord = retval.getRecord(retval.getSelectedIndex());
            if (currentRecord == null) {
                return null;
            }
        }
    }
    return retval;
}
Also used : Relation(com.servoy.j2db.persistence.Relation)

Example 23 with Relation

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

the class LookupValueList method deregister.

public void deregister() {
    clear();
    if (tableListener != null && table != null) {
        FlattenedSolution fs = application.getFlattenedSolution();
        ((FoundSetManager) application.getFoundSetManager()).removeTableListener(table, tableListener);
        Relation[] relations = application.getFlattenedSolution().getRelationSequence(valueList.getRelationName());
        for (int i = 0; relations != null && i < relations.length - 1; i++) {
            ((FoundSetManager) application.getFoundSetManager()).removeTableListener(fs.getTable(relations[i].getForeignDataSource()), tableListener);
        }
        tableListener = null;
    }
}
Also used : Relation(com.servoy.j2db.persistence.Relation) FlattenedSolution(com.servoy.j2db.FlattenedSolution)

Example 24 with Relation

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

the class PrototypeState method getRelatedFoundSet.

/**
 * Get related foundset, relationName may be multiple-levels deep
 */
@Override
public IFoundSetInternal getRelatedFoundSet(String relationName, List<SortColumn> defaultSortColumns) {
    if (parent != null) {
        if (relationName != null) {
            int dot = relationName.indexOf('.');
            String firstRelation = (dot > 0) ? relationName.substring(0, dot) : relationName;
            Relation relation = parent.getFoundSetManager().getApplication().getFlattenedSolution().getRelation(firstRelation);
            if (// only do handle global relations or pass the same foundset
            relation != null && (relation.isGlobal() || relation.isParentRef())) {
                return super.getRelatedFoundSet(relationName, defaultSortColumns);
            }
        }
    }
    return null;
}
Also used : Relation(com.servoy.j2db.persistence.Relation)

Example 25 with Relation

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

the class JSSolutionModel method getRelations.

/**
 * @clonedesc getRelations(String)
 * @sampleas getRelations(String)
 * @param servername the specified name of the server for the specified table
 * @param tablename the specified name of the table
 *
 * @return an array of all relations (all elements in the array are of type JSRelation)
 */
@JSFunction
public JSRelation[] getRelations(String servername, String tablename) {
    FlattenedSolution fs = application.getFlattenedSolution();
    try {
        Table primaryTable = null;
        if (servername != null && tablename != null) {
            IServer primaryServer = fs.getSolution().getServer(servername);
            if (primaryServer == null)
                throw new RuntimeException("can't list relations, primary server not found: " + servername);
            primaryTable = (Table) primaryServer.getTable(tablename);
            if (primaryTable == null)
                throw new RuntimeException("can't list relations, primary table not found: " + tablename);
        }
        List<JSRelation> relations = new ArrayList<JSRelation>();
        Iterator<Relation> iterator = fs.getRelations(primaryTable, true, true);
        while (iterator.hasNext()) {
            Relation relation = iterator.next();
            if (((primaryTable == null) == relation.isGlobal()) && !relation.isInternal()) {
                relations.add(new JSRelation(relation, application, false));
            }
        }
        return relations.toArray(new JSRelation[relations.size()]);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : IServer(com.servoy.j2db.persistence.IServer) ISMRelation(com.servoy.j2db.solutionmodel.ISMRelation) Relation(com.servoy.j2db.persistence.Relation) Table(com.servoy.j2db.persistence.Table) ArrayList(java.util.ArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSFunction(org.mozilla.javascript.annotations.JSFunction)

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