Search in sources :

Example 61 with Column

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

the class FoundSetManager method getTableFilterParams.

/**
 * Get the table filters that are applicable on the sql for the server. Returns an array of table filters, the resulting array may be modified by the
 * caller.
 *
 * @param serverName
 * @param sql
 * @return
 */
public ArrayList<TableFilter> getTableFilterParams(String serverName, IQueryElement sql) {
    final List<TableFilter> serverFilters = tableFilterParams.get(serverName);
    Object[] tenantValue = application.getTenantValue();
    if (serverFilters == null && tenantValue == null) {
        return null;
    }
    // get the sql table names in the query
    final Set<String> tableSqlNames = new HashSet<String>();
    // find the filters for the tables found in the query
    final ArrayList<TableFilter>[] filters = new ArrayList[] { null };
    sql.acceptVisitor(o -> {
        try {
            if (o instanceof QueryTable && ((QueryTable) o).getDataSource() != null && ((QueryTable) o).getName() != null && tableSqlNames.add(((QueryTable) o).getName())) {
                QueryTable qTable = (QueryTable) o;
                Table table = (Table) getTable(qTable.getDataSource());
                if (table == null) {
                    // should never happen
                    throw new RuntimeException("Could not find table '" + qTable.getDataSource() + "' for table filters");
                }
                if (tenantValue != null) {
                    for (Column tenantColumn : table.getTenantColumns()) {
                        addFilter(filters, createTenantFilter(table, tenantColumn, tenantValue));
                    }
                }
                for (TableFilter filter : iterate(serverFilters)) {
                    TableFilterdefinition tableFilterdefinition = filter.getTableFilterdefinition();
                    if (filter.getTableName() == null && tableFilterdefinition instanceof DataproviderTableFilterdefinition) {
                        DataproviderTableFilterdefinition dataproviderTableFilterdefinition = (DataproviderTableFilterdefinition) tableFilterdefinition;
                        // filter is on all tables with specified dataProvider as column
                        Column column = table.getColumn(dataproviderTableFilterdefinition.getDataprovider());
                        if (column != null) {
                            // Use filter with table name filled in.
                            // When table was null value was not yet converted, convert now.
                            Object value = convertFilterValue(table, column, dataproviderTableFilterdefinition.getValue());
                            TableFilter useFilter = new TableFilter(filter.getName(), filter.getServerName(), table.getName(), table.getSQLName(), dataproviderTableFilterdefinition.getDataprovider(), dataproviderTableFilterdefinition.getOperator(), value);
                            addFilter(filters, useFilter);
                        }
                    } else if (filter.getTableSQLName().equals(qTable.getName())) {
                        addFilter(filters, filter);
                    }
                }
            }
        } catch (Exception e) {
            // big trouble, this is security filtering, so bail out on error
            throw new RuntimeException(e);
        }
        return o;
    });
    return filters[0];
}
Also used : BaseQueryTable(com.servoy.base.query.BaseQueryTable) Table(com.servoy.j2db.persistence.Table) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) MarshallException(org.jabsorb.serializer.MarshallException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) HashSet(java.util.HashSet)

Example 62 with Column

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

the class FoundSetManager method getConvertedTypeForColumn.

@Override
public int getConvertedTypeForColumn(IColumn column, boolean mapToDefaultType) {
    int type = mapToDefaultType ? column.getDataProviderType() : (column instanceof Column ? ((Column) column).getType() : column.getDataProviderType());
    ColumnInfo ci = column.getColumnInfo();
    if (ci != null && ci.getConverterName() != null && ci.getConverterName().trim().length() != 0) {
        IColumnConverter columnConverter = ((FoundSetManager) application.getFoundSetManager()).getColumnConverterManager().getConverter(ci.getConverterName());
        if (columnConverter instanceof ITypedColumnConverter) {
            try {
                int convType = ((ITypedColumnConverter) columnConverter).getToObjectType(ComponentFactory.<String>parseJSonProperties(ci.getConverterProperties()));
                if (convType != Integer.MAX_VALUE) {
                    type = Column.mapToDefaultType(convType);
                }
            } catch (IOException e) {
                Debug.error("Exception loading properties for converter " + columnConverter.getName() + ", properties: " + ci.getConverterProperties(), e);
            }
        }
    }
    return type;
}
Also used : Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) IOException(java.io.IOException)

Example 63 with Column

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

the class RelatedFoundSet method notifyChange_checkForNewRow.

private void notifyChange_checkForNewRow(Row row) {
    // if already in state for new query then don't test anything.
    if (mustQueryForUpdates) {
        return;
    }
    // check if sql where is still the same, if there is search in it do nothing
    AndOrCondition createCondition = creationSqlSelect.getCondition(SQLGenerator.CONDITION_RELATION);
    AndOrCondition condition = getPksAndRecords().getQuerySelectForReading().getCondition(SQLGenerator.CONDITION_RELATION);
    if (// does not include placeholder values in comparison
    (createCondition == null && condition == null) || createCondition != null && createCondition.equals(condition)) {
        try {
            boolean doCheck = true;
            Relation relation = fsm.getApplication().getFlattenedSolution().getRelation(relationName);
            if (relation == null) {
                // this may happen when the relation was removed using solution model
                return;
            }
            // check the foreign key if they match, if so it will fall in this foundset
            Placeholder ph = creationSqlSelect.getPlaceholder(SQLGenerator.createRelationKeyPlaceholderKey(creationSqlSelect.getTable(), relation.getName()));
            if (ph == null || !ph.isSet()) {
                Column[] cols = relation.getForeignColumns(fsm.getApplication().getFlattenedSolution());
                StringBuilder columns = new StringBuilder();
                columns.append("(");
                if (cols != null && cols.length > 0) {
                    for (Column col : cols) {
                        columns.append(col.getName());
                        columns.append(",");
                    }
                    columns.setLength(columns.length() - 1);
                }
                columns.append(")");
                // $NON-NLS-1$
                Debug.error("RelatedFoundset check for relation:" + relationName + " for a new row, creation args " + columns + "not found!!");
                // how can this happen??
                return;
            }
            // foreignData is a matrix as wide as the relation keys and 1 deep
            // Really get only the where params not all of them (like table filter)
            Object[][] foreignData = (Object[][]) ph.getValue();
            Column[] cols = relation.getForeignColumns(fsm.getApplication().getFlattenedSolution());
            if (foreignData.length != cols.length) {
                StringBuilder columns = new StringBuilder();
                columns.append("(");
                if (cols.length > 0) {
                    for (Column col : cols) {
                        columns.append(col.getName());
                        columns.append(",");
                    }
                    columns.setLength(columns.length() - 1);
                }
                columns.append(")");
                StringBuilder data = new StringBuilder();
                data.append("(");
                if (foreignData.length > 0) {
                    for (Object[] d : foreignData) {
                        data.append("[");
                        if (d.length > 0) {
                            for (Object object : d) {
                                data.append(object);
                                data.append(",");
                            }
                            data.setLength(data.length() - 1);
                        }
                        data.append("]");
                    }
                    data.setLength(data.length() - 1);
                }
                data.append(")");
                Debug.error(// $NON-NLS-1$
                "RelatedFoundset check for relation:" + relationName + " for new row, creation args " + columns + " and relation args " + data + "  are not the same!!");
                // how can this happen??
                return;
            }
            int[] operators = relation.getOperators();
            for (int i = 0; i < cols.length; i++) {
                // compare unconverted values
                Object obj = row.getRawValue(cols[i].getDataProviderID());
                if (!checkForeignKeyValue(obj, foreignData[i][0], operators[i])) {
                    doCheck = false;
                    break;
                }
            }
            if (doCheck) {
                invalidateFoundset();
                getFoundSetManager().getEditRecordList().fireEvents();
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) AndOrCondition(com.servoy.j2db.query.AndOrCondition) ServoyException(com.servoy.j2db.util.ServoyException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Relation(com.servoy.j2db.persistence.Relation) Column(com.servoy.j2db.persistence.Column)

Example 64 with Column

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

the class RelatedValueList method getDisplayFormat.

private String[] getDisplayFormat(Table table) {
    if (table != null && hasRealValues()) {
        String[] displayFormats = new String[3];
        Column col1 = table.getColumn(valueList.getDataProviderID1());
        if (col1 != null && col1.getColumnInfo() != null)
            displayFormats[0] = col1.getColumnInfo().getDefaultFormat();
        Column col2 = table.getColumn(valueList.getDataProviderID2());
        if (col2 != null && col2.getColumnInfo() != null)
            displayFormats[1] = col2.getColumnInfo().getDefaultFormat();
        Column col3 = table.getColumn(valueList.getDataProviderID3());
        if (col3 != null && col3.getColumnInfo() != null)
            displayFormats[2] = col3.getColumnInfo().getDefaultFormat();
        return displayFormats;
    }
    return null;
}
Also used : Column(com.servoy.j2db.persistence.Column)

Example 65 with Column

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

the class Row method setValue.

// returns the oldvalue, or value if no change
public Object setValue(IRowChangeListener src, String dataProviderID, Object value) {
    Object o = getRawValue(dataProviderID);
    // this column is controlled by the database - so do not allow sets until the database chose a value
    if (o instanceof DbIdentValue)
        return o;
    Object convertedValue = value;
    SQLSheet sheet = parent.getSQLSheet();
    int columnIndex = sheet.getColumnIndex(dataProviderID);
    VariableInfo variableInfo = sheet.getCalculationOrColumnVariableInfo(dataProviderID, columnIndex);
    if (// do not convert null to 0 incase of numbers, this means the calcs the value whould change each time //$NON-NLS-1$
    convertedValue != null && !("".equals(convertedValue) && Column.mapToDefaultType(variableInfo.type) == IColumnTypes.TEXT)) {
        convertedValue = sheet.convertObjectToValue(dataProviderID, convertedValue, parent.getFoundsetManager().getColumnConverterManager(), parent.getFoundsetManager().getColumnValidatorManager(), src);
    } else if (parent.getFoundsetManager().isNullColumnValidatorEnabled() && // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Settings.getInstance().getProperty("servoy.execute.column.validators.only.on.validate_and_save", "true").equals("false")) {
        // check for not null constraint
        Column c = null;
        try {
            c = sheet.getTable().getColumn(dataProviderID);
        } catch (Exception e) {
            Debug.error(e);
        }
        if (c != null && !c.getAllowNull()) {
            // $NON-NLS-1$
            throw new IllegalArgumentException(Messages.getString("servoy.record.error.validation", new Object[] { dataProviderID, convertedValue }));
        }
    }
    boolean wasUNINITIALIZED = false;
    if (o == UNINITIALIZED) {
        o = null;
        wasUNINITIALIZED = true;
    }
    boolean isCalculation = containsCalculation(dataProviderID);
    // if we receive NULL from the db for Empty strings in Servoy calcs, return value
    if (// $NON-NLS-1$
    o == null && "".equals(convertedValue) && isCalculation) {
        mustRecalculate(dataProviderID, false);
        return convertedValue;
    }
    if (!Utils.equalObjects(o, convertedValue)) {
        boolean mustStop = false;
        if (columnIndex != -1 && columnIndex < columndata.length) {
            mustStop = !parent.getFoundsetManager().getEditRecordList().isEditing();
            if (// if not yet existInDB, leave startEdit to Foundset new/duplicateRecord code!
            src != null && existInDB && !wasUNINITIALIZED) {
                src.startEditing(false);
            }
            createOldValuesIfNeeded();
            columndata[columnIndex] = convertedValue;
        } else if (isCalculation) {
            unstoredCalcCache.put(dataProviderID, convertedValue);
        }
        lastException = null;
        // Reset the mustRecalculate here, before setValue fires events, so if it is an every time changing calculation it will not be calculated again and again
        if (isCalculation) {
            mustRecalculate(dataProviderID, false);
            threadCalculationComplete(dataProviderID);
        }
        handleCalculationDependencies(sheet.getTable().getColumn(dataProviderID), dataProviderID);
        FireCollector collector = FireCollector.getFireCollector();
        try {
            fireNotifyChange(dataProviderID, convertedValue, collector);
        } finally {
            collector.done();
        }
        if (src != null && mustStop && existInDB && !wasUNINITIALIZED) {
            try {
                src.stopEditing();
            } catch (Exception e) {
                Debug.error(e);
            }
        }
        return o;
    } else if (isCalculation) {
        // Reset the mustRecalculate here, before setValue fires events, so if it is an every time changing calculation it will not be calculated again and again
        mustRecalculate(dataProviderID, false);
    }
    // is same so return
    return convertedValue;
}
Also used : IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) VariableInfo(com.servoy.j2db.dataprocessing.SQLSheet.VariableInfo)

Aggregations

Column (com.servoy.j2db.persistence.Column)76 QueryColumn (com.servoy.j2db.query.QueryColumn)44 IColumn (com.servoy.j2db.persistence.IColumn)37 RepositoryException (com.servoy.j2db.persistence.RepositoryException)32 IBaseColumn (com.servoy.base.persistence.IBaseColumn)31 QuerySelect (com.servoy.j2db.query.QuerySelect)29 ArrayList (java.util.ArrayList)29 QueryTable (com.servoy.j2db.query.QueryTable)27 ITable (com.servoy.j2db.persistence.ITable)23 BaseQueryTable (com.servoy.base.query.BaseQueryTable)22 Table (com.servoy.j2db.persistence.Table)22 ServoyException (com.servoy.j2db.util.ServoyException)21 SafeArrayList (com.servoy.j2db.util.SafeArrayList)19 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)18 RemoteException (java.rmi.RemoteException)17 ColumnInfo (com.servoy.j2db.persistence.ColumnInfo)16 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)14 IDataProvider (com.servoy.j2db.persistence.IDataProvider)12 Relation (com.servoy.j2db.persistence.Relation)12 Placeholder (com.servoy.j2db.query.Placeholder)12