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;
}
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();
}
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;
}
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));
}
}
}
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;
}
Aggregations