Search in sources :

Example 16 with Relation

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

the class SQLSheet method getNewRowData.

/**
 * Returns raw (not using column converters) row data for a new record
 * @param app
 * @param fs
 * @return
 */
Object[] getNewRowData(IServiceProvider app, FoundSet fs) {
    Object[][] creationArgs = null;
    // INSERT
    SQLDescription desc = getSQLDescription(SELECT);
    // RequiredDataProviderIDs();
    List<?> list = desc.getDataProviderIDsDilivery();
    Column[] fcols = null;
    Relation relation = null;
    String relationName = fs.getRelationName();
    if (relationName != null) {
        try {
            relation = app.getFlattenedSolution().getRelation(relationName);
            if (relation != null) {
                fcols = relation.getForeignColumns(app.getFlattenedSolution());
                QuerySelect creationSQLString = fs.getCreationSqlSelect();
                Placeholder ph = creationSQLString.getPlaceholder(SQLGenerator.createRelationKeyPlaceholderKey(creationSQLString.getTable(), relation.getName()));
                if (ph != null && ph.isSet()) {
                    // a matrix as wide as the relation keys and 1 deep
                    creationArgs = (Object[][]) ph.getValue();
                }
            }
        } catch (RepositoryException e) {
            Debug.error(e);
        }
    }
    Object[] array = new Object[list.size()];
    for (int i = 0; i < list.size(); i++) {
        try {
            boolean filled = false;
            Column c = table.getColumn((String) list.get(i));
            if (c.isDBIdentity()) {
                array[i] = ValueFactory.createDbIdentValue();
                filled = true;
            } else {
                ColumnInfo ci = c.getColumnInfo();
                if (c.getRowIdentType() != IBaseColumn.NORMAL_COLUMN && ci != null && ci.hasSequence()) {
                    // this is here for safety, it can happen that a form has (unwanted) still a related foundset which is created by relation based on primary key
                    array[i] = c.getNewRecordValue(app);
                    filled = true;
                } else {
                    if (// created via relation, so fill the foreign key with foreign value
                    creationArgs != null && creationArgs.length != 0 && fcols != null) {
                        for (int j = 0; j < fcols.length; j++) {
                            if (c.equals(fcols[j]) && ((relation.getOperators()[j] & IBaseSQLCondition.OPERATOR_MASK) == IBaseSQLCondition.EQUALS_OPERATOR)) {
                                // creationArgs is a matrix as wide as the relation keys and 1 deep
                                array[i] = creationArgs[j][0];
                                filled = true;
                                break;
                            }
                        }
                    }
                }
            }
            if (!filled) {
                array[i] = c.getNewRecordValue(app);
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
    return array;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QuerySelect(com.servoy.j2db.query.QuerySelect) ServoyException(com.servoy.j2db.util.ServoyException) IOException(java.io.IOException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Relation(com.servoy.j2db.persistence.Relation) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column)

Example 17 with Relation

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

the class RelatedFoundSet method getWhereArgs.

public Object[] getWhereArgs(boolean onlyEqualsConditions) {
    Placeholder ph = creationSqlSelect.getPlaceholder(SQLGenerator.createRelationKeyPlaceholderKey(creationSqlSelect.getTable(), getRelationName()));
    if (ph == null || !ph.isSet()) {
        if (!findMode) {
            Debug.error(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
            "RelatedFoundset, creation args not found\nplaceholder=" + ph + "\nrelation=" + getRelationName() + "\ncreationSqlSelect=" + creationSqlSelect, new RuntimeException("RelatedFoundset, creation args not found!!"));
        }
        // how can this happen (other then in find mode) ??
        return null;
    }
    Relation relation = fsm.getApplication().getFlattenedSolution().getRelation(relationName);
    if (relation == null) {
        // $NON-NLS-1$
        throw new IllegalStateException("Relation not found for related foundset: " + relationName);
    }
    Object[][] foreignData = (Object[][]) ph.getValue();
    Column[] columns;
    try {
        columns = relation.getForeignColumns(fsm.getApplication().getFlattenedSolution());
    } catch (RepositoryException e) {
        Debug.error(e);
        // $NON-NLS-1$
        throw new IllegalStateException("Relation columns not found for related foundset: " + relationName);
    }
    if (columns.length != foreignData.length) {
        // $NON-NLS-1$
        throw new IllegalStateException("Relation where-args inconsistent with columns for relation" + relationName);
    }
    IntStream columnIndexesStream;
    if (onlyEqualsConditions) {
        int[] columnIndexes = getIndexesEqualsEntries();
        if (columnIndexes.length == 0) {
            return null;
        }
        columnIndexesStream = IntStream.of(columnIndexes);
    } else {
        columnIndexesStream = IntStream.range(0, columns.length);
    }
    return columnIndexesStream.mapToObj(i -> {
        // Use converted value for hash
        int colindex = getSQLSheet().getColumnIndex(columns[i].getDataProviderID());
        return getSQLSheet().convertValueToObject(foreignData[i][0], colindex, fsm.getColumnConverterManager());
    }).toArray();
}
Also used : IntStream(java.util.stream.IntStream) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) Builder(java.util.stream.IntStream.Builder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Placeholder(com.servoy.j2db.query.Placeholder) Debug(com.servoy.j2db.util.Debug) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) HashMap(java.util.HashMap) NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) ArrayList(java.util.ArrayList) Utils(com.servoy.j2db.util.Utils) StringTokenizer(java.util.StringTokenizer) SafeArrayList(com.servoy.j2db.util.SafeArrayList) Map(java.util.Map) ISQLSelect(com.servoy.j2db.query.ISQLSelect) Relation(com.servoy.j2db.persistence.Relation) ObjectOutputStream(java.io.ObjectOutputStream) QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) ServoyException(com.servoy.j2db.util.ServoyException) IBaseSQLCondition(com.servoy.base.query.IBaseSQLCondition) AbstractBaseQuery(com.servoy.j2db.query.AbstractBaseQuery) Iterator(java.util.Iterator) IRepository(com.servoy.j2db.persistence.IRepository) ServoyClientSupport(com.servoy.base.scripting.annotations.ServoyClientSupport) QuerySelect(com.servoy.j2db.query.QuerySelect) RemoteException(java.rmi.RemoteException) List(java.util.List) Scriptable(org.mozilla.javascript.Scriptable) Column(com.servoy.j2db.persistence.Column) RepositoryException(com.servoy.j2db.persistence.RepositoryException) PackVisitor(com.servoy.j2db.util.visitor.PackVisitor) AndOrCondition(com.servoy.j2db.query.AndOrCondition) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) Placeholder(com.servoy.j2db.query.Placeholder) Relation(com.servoy.j2db.persistence.Relation) Column(com.servoy.j2db.persistence.Column) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IntStream(java.util.stream.IntStream)

Example 18 with Relation

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

the class RelatedFoundSet method getIndexesEqualsEntries.

private int[] getIndexesEqualsEntries() {
    if (equalsOpsIndexes == null) {
        Relation relation = fsm.getApplication().getFlattenedSolution().getRelation(relationName);
        Builder indexBuilder = IntStream.builder();
        int[] operators = relation.getOperators();
        for (int i = 0; i < operators.length; i++) {
            if (operators[i] == IBaseSQLCondition.EQUALS_OPERATOR) {
                indexBuilder.add(i);
            }
        }
        equalsOpsIndexes = indexBuilder.build().toArray();
    }
    return equalsOpsIndexes;
}
Also used : Relation(com.servoy.j2db.persistence.Relation) Builder(java.util.stream.IntStream.Builder)

Example 19 with Relation

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

the class LookupValueList method fill.

private void fill(Object display, Object real) throws ServoyException, RemoteException {
    if (dontQuery || table == null)
        return;
    Object value = null;
    int values = 0;
    if (display != null) {
        values = showValues;
        value = display;
    } else {
        values = returnValues;
        value = real;
    }
    QuerySelect select = null;
    BaseQueryTable qTable = null;
    if (valueList.getDatabaseValuesType() == IValueListConstants.TABLE_VALUES) {
        select = DBValueList.createValuelistQuery(application, valueList, table);
        if (select != null) {
            qTable = select.getTable();
        }
    } else {
        Relation[] relations = application.getFlattenedSolution().getRelationSequence(valueList.getRelationName());
        Pair<QuerySelect, BaseQueryTable> pair = RelatedValueList.createRelatedValuelistQuery(application, valueList, relations, parentState);
        if (pair != null) {
            select = pair.getLeft();
            qTable = pair.getRight();
        }
    }
    if (select == null) {
        return;
    }
    String[] displayValues = null;
    String separator = valueList.getSeparator();
    if (// $NON-NLS-1$
    values == showValues && value != null && separator != null && !separator.equals("")) {
        if (values != 1 && values != 2 && values != 4) {
            // its a combination
            displayValues = Utils.stringSplit(value.toString(), separator);
        }
    }
    OrCondition where = new OrCondition();
    if ((values & 1) != 0) {
        String dp1 = valueList.getDataProviderID1();
        if (displayValues != null) {
            for (String displayValue : displayValues) {
                where.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, DBValueList.getQuerySelectValue(table, qTable, dp1), getAsRightType(dp1, displayValue)));
            }
        }
        // also just add the complete value, for the possibility that it was a value with a separator.
        value = getAsRightType(dp1, value);
        where.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, DBValueList.getQuerySelectValue(table, qTable, dp1), value));
    }
    if ((values & 2) != 0) {
        String dp2 = valueList.getDataProviderID2();
        if (displayValues != null) {
            for (String displayValue : displayValues) {
                where.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, DBValueList.getQuerySelectValue(table, qTable, dp2), getAsRightType(dp2, displayValue)));
            }
        }
        value = getAsRightType(dp2, value);
        where.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, DBValueList.getQuerySelectValue(table, qTable, dp2), value));
    }
    if ((values & 4) != 0) {
        String dp3 = valueList.getDataProviderID3();
        if (displayValues != null) {
            for (String displayValue : displayValues) {
                where.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, DBValueList.getQuerySelectValue(table, qTable, dp3), getAsRightType(dp3, displayValue)));
            }
        }
        value = getAsRightType(dp3, value);
        where.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, DBValueList.getQuerySelectValue(table, qTable, dp3), value));
    }
    select.setCondition(SQLGenerator.CONDITION_SEARCH, where);
    FoundSetManager foundSetManager = ((FoundSetManager) application.getFoundSetManager());
    String transaction_id = foundSetManager.getTransactionID(table.getServerName());
    ArrayList<TableFilter> tableFilterParams = foundSetManager.getTableFilterParams(table.getServerName(), select);
    if (// apply name as filter on column valuelist_name
    valueList.getUseTableFilter()) {
        if (tableFilterParams == null) {
            tableFilterParams = new ArrayList<TableFilter>();
        }
        tableFilterParams.add(new // $NON-NLS-1$
        TableFilter(// $NON-NLS-1$
        "lookupValueList.nameFilter", // $NON-NLS-1$
        table.getServerName(), // $NON-NLS-1$
        table.getName(), // $NON-NLS-1$
        table.getSQLName(), DBValueList.NAME_COLUMN, IBaseSQLCondition.EQUALS_OPERATOR, valueList.getName()));
    }
    SQLStatement trackingInfo = null;
    if (foundSetManager.getEditRecordList().hasAccess(table, IRepository.TRACKING_VIEWS)) {
        trackingInfo = new SQLStatement(ISQLActionTypes.SELECT_ACTION, table.getServerName(), table.getName(), null, null);
        trackingInfo.setTrackingData(select.getColumnNames(), new Object[][] {}, new Object[][] {}, application.getUserUID(), foundSetManager.getTrackingInfo(), application.getClientID());
    }
    IDataSet set = application.getDataServer().performQuery(application.getClientID(), table.getServerName(), transaction_id, select, null, tableFilterParams, !select.isUnique(), 0, maxValuelistRows, IDataServer.VALUELIST_QUERY, trackingInfo);
    String[] displayFormats = getDisplayFormat();
    for (int i = 0; i < set.getRowCount(); i++) {
        Object[] row = CustomValueList.processRow(set.getRow(i), showValues, returnValues);
        DisplayString obj = CustomValueList.handleDisplayData(valueList, displayFormats, concatShowValues, showValues, row, application);
        if (obj != null) {
            alDisplay.add(obj);
            alReal.add(CustomValueList.handleRowData(valueList, concatReturnValues, returnValues, row, application));
        }
    }
}
Also used : DisplayString(com.servoy.j2db.dataprocessing.CustomValueList.DisplayString) QuerySelect(com.servoy.j2db.query.QuerySelect) Relation(com.servoy.j2db.persistence.Relation) BaseQueryTable(com.servoy.base.query.BaseQueryTable) DisplayString(com.servoy.j2db.dataprocessing.CustomValueList.DisplayString) CompareCondition(com.servoy.j2db.query.CompareCondition) OrCondition(com.servoy.j2db.query.OrCondition)

Example 20 with Relation

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

the class JSSolutionModel method getRelation.

/**
 * Gets an existing relation by the specified name and returns a JSRelation Object.
 *
 * @sample
 * var relation = solutionModel.getRelation('name');
 * application.output("The primary server name is " + relation.primaryServerName);
 * application.output("The primary table name is " + relation.primaryTableName);
 * application.output("The foreign table name is " + relation.foreignTableName);
 * application.output("The relation items are " + relation.getRelationItems());
 *
 * @param name the specified name of the relation
 *
 * @return a JSRelation
 */
@JSFunction
public JSRelation getRelation(String name) {
    if (name == null)
        return null;
    FlattenedSolution fs = application.getFlattenedSolution();
    Relation relation = fs.getRelation(name);
    if (relation != null) {
        return new JSRelation(relation, application, false);
    }
    return null;
}
Also used : ISMRelation(com.servoy.j2db.solutionmodel.ISMRelation) Relation(com.servoy.j2db.persistence.Relation) FlattenedSolution(com.servoy.j2db.FlattenedSolution) 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