Search in sources :

Example 1 with UsedDataProviderTracker

use of com.servoy.j2db.scripting.UsedDataProviderTracker in project servoy-client by Servoy.

the class Record method getValue.

public Object getValue(String dataProviderID, boolean converted) {
    if (dataProviderID == null || parent == null)
        return null;
    if (// $NON-NLS-1$
    "currentRecordIndex".equals(dataProviderID)) {
        // deprecated
        return new Integer(parent.getRecordIndex(this) + 1);
    }
    if (// $NON-NLS-1$
    "foundset".equals(dataProviderID)) {
        return parent;
    }
    if (// $NON-NLS-1$
    "exception".equals(dataProviderID)) {
        return row.getLastException();
    }
    FireJSCollector jsFireCollector = null;
    boolean threadExecutionRegistered = false;
    try {
        boolean containsCalc = row.containsCalculation(dataProviderID);
        boolean mustRecalc = containsCalc && row.mustRecalculate(dataProviderID, true);
        if (mustRecalc) {
            threadExecutionRegistered = true;
            row.threadWillExecuteCalculation(dataProviderID);
        }
        mustRecalc = containsCalc && row.mustRecalculate(dataProviderID, true);
        if ((containsCalc || row.containsDataprovider(dataProviderID)) && !mustRecalc) {
            // also stored calcs are always calculated ones(required due to use of plugin methods in calc);
            return converted ? row.getValue(dataProviderID) : row.getRawValue(dataProviderID);
        }
        if (// check if calculation
        containsCalc) {
            jsFireCollector = FireJSCollector.createOrGetJSFireCollector();
            UsedDataProviderTracker usedDataProviderTracker = new UsedDataProviderTracker(getParentFoundSet().getFoundSetManager().getApplication().getFlattenedSolution());
            // do real calc
            Object value = parent.getCalculationValue(this, dataProviderID, null, usedDataProviderTracker);
            if (!(value instanceof Undefined)) {
                value = Utils.mapToNullIfUnmanageble(value);
                row.setValue(this, dataProviderID, value);
            }
            // re get it so that we do have the right type if the calc didn't return the type it specifies.
            // and that a converter is also applied.
            value = converted ? row.getValue(dataProviderID) : row.getRawValue(dataProviderID);
            manageCalcDependency(dataProviderID, usedDataProviderTracker);
            return value;
        }
    } finally {
        if (threadExecutionRegistered)
            row.threadCalculationComplete(dataProviderID);
        if (jsFireCollector != null)
            jsFireCollector.done();
    }
    if (// as shared (global or aggregate)
    parent.containsDataProvider(dataProviderID)) {
        return parent.getDataProviderValue(dataProviderID);
    }
    if (// $NON-NLS-1$
    "recordMarkers".equals(dataProviderID)) {
        return recordMarkers;
    }
    if (ScopesUtils.isVariableScope(dataProviderID)) {
        return Utils.mapToNullIfUnmanageble(parent.getDataProviderValue(dataProviderID));
    }
    int index = dataProviderID.lastIndexOf('.');
    if (// check if is related value request
    index > 0) {
        String partName = dataProviderID.substring(0, index);
        String restName = dataProviderID.substring(index + 1);
        if (// $NON-NLS-1$
        "lazyMaxRecordIndex".equals(restName)) {
            if (!isRelatedFoundSetLoaded(partName, restName)) {
                // $NON-NLS-1$
                return "?";
            }
            // $NON-NLS-1$
            restName = "maxRecordIndex";
        }
        // partName may be multiple levels deep; check substate, will return null if not found
        IFoundSetInternal foundSet = getRelatedFoundSet(partName);
        if (foundSet != null) {
            // related data
            int selected = foundSet.getSelectedIndex();
            // in printing selected row will be set to -1, but if data is retrieved we need to use the first record again for use after printing...
            if (selected == -1 && foundSet.getSize() > 0)
                selected = 0;
            IRecordInternal state = foundSet.getRecord(selected);
            if (state != null) {
                return state.getValue(restName);
            }
            if (foundSet.containsDataProvider(restName)) {
                return foundSet.getDataProviderValue(restName);
            }
        }
        return null;
    }
    if (parent.isValidRelation(dataProviderID)) {
        return getRelatedFoundSet(dataProviderID);
    }
    return Scriptable.NOT_FOUND;
}
Also used : Undefined(org.mozilla.javascript.Undefined) UsedDataProviderTracker(com.servoy.j2db.scripting.UsedDataProviderTracker)

Example 2 with UsedDataProviderTracker

use of com.servoy.j2db.scripting.UsedDataProviderTracker in project servoy-client by Servoy.

the class FoundSet method getCalculationValue.

/**
 * Found set is using scriptengine to recalculate the specified calculation,check first with containsCalculation before calling
 */
public Object getCalculationValue(IRecordInternal state, String dataProviderID, Object[] vargs, UsedDataProviderTracker usedDataProviderTracker) {
    UsedDataProviderTracker current = null;
    try {
        Object obj;
        TableScope tableScope = (TableScope) fsm.getScriptEngine().getTableScope(sheet.getTable());
        tableScope.setArguments(vargs);
        current = tableScope.setUsedDataProviderTracker(usedDataProviderTracker);
        Scriptable previous = tableScope.getPrototype();
        try {
            // make sure its set correctly
            tableScope.setPrototype((Scriptable) state);
            obj = tableScope.getCalculationValue(dataProviderID, tableScope);
            if (// fix for postgress
            obj instanceof Byte) {
                obj = Integer.valueOf(((Byte) obj).intValue());
            } else {
                Column column = sheet.getTable().getColumn(dataProviderID);
                if (column != null) {
                    // TODO: check case with stored calc on column with column converter
                    if (column.getScale() > 0 && column.getDataProviderType() == IColumnTypes.NUMBER && obj != null) {
                        // if rounding results in the old value we do not have to save.
                        try {
                            obj = Utils.roundNumber(obj, column.getLength(), true);
                        } catch (Exception e) {
                            // $NON-NLS-1$
                            Debug.error("Could not round stored calculation '" + dataProviderID + '\'', e);
                        }
                    }
                }
            }
        // TODO: in developer we must check if the return type matches the one specified on a calculation otherwise relations will not work in some cases
        } finally {
            tableScope.setPrototype(previous);
            tableScope.setUsedDataProviderTracker(current);
        }
        return obj;
    } catch (Exception ex) {
        // fsm.getApplication().reportJSError(Messages.getString("servoy.error.executingCalculation",new Object[] {dataProviderID,getTable().getName(),ex.getMessage()}),ex) ;	 //$NON-NLS-1$
        fsm.getApplication().reportJSError(ex.getMessage(), ex);
        // $NON-NLS-1$
        Debug.error("error executing calc: " + dataProviderID, ex);
        return null;
    }
}
Also used : TableScope(com.servoy.j2db.scripting.TableScope) UsedDataProviderTracker(com.servoy.j2db.scripting.UsedDataProviderTracker) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) Scriptable(org.mozilla.javascript.Scriptable) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Aggregations

UsedDataProviderTracker (com.servoy.j2db.scripting.UsedDataProviderTracker)2 ApplicationException (com.servoy.j2db.ApplicationException)1 Column (com.servoy.j2db.persistence.Column)1 IColumn (com.servoy.j2db.persistence.IColumn)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 QueryColumn (com.servoy.j2db.query.QueryColumn)1 TableScope (com.servoy.j2db.scripting.TableScope)1 ServoyException (com.servoy.j2db.util.ServoyException)1 RemoteException (java.rmi.RemoteException)1 Scriptable (org.mozilla.javascript.Scriptable)1 Undefined (org.mozilla.javascript.Undefined)1