Search in sources :

Example 1 with RelationItem

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

the class JSRelation method getRelationItems.

/**
 * Returns an array of JSRelationItem objects representing the relation criteria defined for this relation.
 *
 * @sample
 * var criteria = relation.getRelationItems();
 * for (var i=0; i<criteria.length; i++)
 * {
 *	var item = criteria[i];
 *	application.output('relation item no. ' + i);
 *	application.output('primary column: ' + item.primaryDataProviderID);
 *	application.output('operator: ' + item.operator);
 *	application.output('foreign column: ' + item.foreignColumnName);
 * }
 *
 * @return An array of JSRelationItem instances representing the relation criteria of this relation.
 */
@JSFunction
public JSRelationItem[] getRelationItems() {
    ArrayList<JSRelationItem> al = new ArrayList<JSRelationItem>();
    Iterator<IPersist> allObjects = relation.getAllObjects();
    while (allObjects.hasNext()) {
        IPersist persist = allObjects.next();
        if (persist instanceof RelationItem) {
            al.add(new JSRelationItem((RelationItem) persist, this, isCopy));
        }
    }
    return al.toArray(new JSRelationItem[al.size()]);
}
Also used : RelationItem(com.servoy.j2db.persistence.RelationItem) IPersist(com.servoy.j2db.persistence.IPersist) ArrayList(java.util.ArrayList) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 2 with RelationItem

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

the class JSRelation method removeRelationItem.

/**
 * Removes the desired relation item from the specified relation.
 *
 * @sample
 * var relation = solutionModel.newRelation('myRelation', 'db:/myServer/parentTable', 'db:/myServer/childTable', JSRelation.INNER_JOIN);
 * relation.newRelationItem('someColumn1', '=', 'someColumn2');
 * relation.newRelationItem('anotherColumn', '=', 'someOtherColumn');
 * relation.removeRelationItem('someColumn1', '=', 'someColumn2');
 * var criteria = relation.getRelationItems();
 * for (var i = 0; i < criteria.length; i++) {
 * 	var item = criteria[i];
 * 	application.output('primary column: ' + item.primaryDataProviderID);
 * 	application.output('operator: ' + item.operator);
 * 	application.output('foreign column: ' + item.foreignColumnName);
 * }
 *
 * @param primaryDataProviderID the primary data provider (column) name
 * @param operator the operator
 * @param foreignColumnName the foreign column name
 */
@JSFunction
public void removeRelationItem(String primaryDataProviderID, String operator, String foreignColumnName) {
    checkModification();
    Iterator<IPersist> allObjects = relation.getAllObjects();
    while (allObjects.hasNext()) {
        IPersist persist = allObjects.next();
        if (persist instanceof RelationItem) {
            RelationItem ri = (RelationItem) persist;
            int validOperator = RelationItem.getValidOperator(operator, RelationItem.RELATION_OPERATORS, null);
            if (ri.getPrimaryDataProviderID().equals(primaryDataProviderID) && ri.getOperator() == validOperator && ri.getForeignColumnName().equals(foreignColumnName)) {
                relation.removeChild(persist);
                relation.setValid(true);
                break;
            }
        }
    }
}
Also used : RelationItem(com.servoy.j2db.persistence.RelationItem) IPersist(com.servoy.j2db.persistence.IPersist) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 3 with RelationItem

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

the class JSRelation method newRelationItem.

/**
 * Creates a new relation item for this relation. The primary dataprovider, the foreign data provider
 * and one relation operators (like '=' '!=' '>' '<') must be provided.
 *
 * @sample
 * var relation = solutionModel.newRelation('parentToChild', 'db:/example_data/parent_table', 'db:/example_data/child_table', JSRelation.INNER_JOIN);
 * relation.newRelationItem('another_parent_table_id', '=', 'another_child_table_parent_id');
 * // for literals use a prefix
 * relation.newRelationItem(JSRelationItem.LITERAL_PREFIX + "'hello'",'=', 'mytextfield');
 *
 * @param dataprovider The name of the primary dataprovider.
 *
 * @param operator The operator used to relate the primary and the foreign dataproviders.
 *
 * @param foreinColumnName The name of the foreign dataprovider.
 *
 * @return A JSRelationItem instance representing the newly added relation item.
 */
@JSFunction
public JSRelationItem newRelationItem(String dataprovider, String operator, String foreinColumnName) {
    if (dataprovider == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("dataprovider cannot be null");
    }
    if (foreinColumnName == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("foreinColumnName cannot be null");
    }
    int validOperator = RelationItem.getValidOperator(operator, RelationItem.RELATION_OPERATORS, null);
    if (validOperator == -1) {
        // $NON-NLS-1$//$NON-NLS-2$
        throw new IllegalArgumentException("operator " + operator + " is not a valid relation operator");
    }
    checkModification();
    try {
        IDataProvider primaryDataProvider = null;
        if (ScopesUtils.isVariableScope(dataprovider)) {
            primaryDataProvider = application.getFlattenedSolution().getGlobalDataProvider(dataprovider);
        } else if (dataprovider.startsWith(LiteralDataprovider.LITERAL_PREFIX)) {
            primaryDataProvider = new LiteralDataprovider(dataprovider);
            if (((LiteralDataprovider) primaryDataProvider).getValue() == null) {
                // $NON-NLS-1$
                throw new IllegalArgumentException("primary data provider " + dataprovider + " is not a valid relation primary data provider");
            }
        } else {
            primaryDataProvider = application.getFlattenedSolution().getDataProviderForTable((Table) application.getFoundSetManager().getTable(relation.getPrimaryDataSource()), dataprovider);
        }
        if (primaryDataProvider == null) {
            // $NON-NLS-1$
            throw new IllegalArgumentException("cant create relation item primary dataprovider not found: " + dataprovider);
        }
        IDataProvider dp = application.getFlattenedSolution().getDataProviderForTable((Table) application.getFoundSetManager().getTable(relation.getForeignDataSource()), foreinColumnName);
        if (!(dp instanceof Column)) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new IllegalArgumentException("Foreign columnname " + foreinColumnName + " is not a valid column");
        }
        RelationItem result = relation.createNewRelationItem(application.getFoundSetManager(), primaryDataProvider, validOperator, (Column) dp);
        if (result != null)
            return new JSRelationItem(result, this, isCopy);
        return null;
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : RelationItem(com.servoy.j2db.persistence.RelationItem) Column(com.servoy.j2db.persistence.Column) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IDataProvider(com.servoy.j2db.persistence.IDataProvider) LiteralDataprovider(com.servoy.j2db.persistence.LiteralDataprovider) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Aggregations

RelationItem (com.servoy.j2db.persistence.RelationItem)3 JSFunction (org.mozilla.javascript.annotations.JSFunction)3 IPersist (com.servoy.j2db.persistence.IPersist)2 Column (com.servoy.j2db.persistence.Column)1 IDataProvider (com.servoy.j2db.persistence.IDataProvider)1 LiteralDataprovider (com.servoy.j2db.persistence.LiteralDataprovider)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ArrayList (java.util.ArrayList)1