Adding associated records to Get, Query.

This commit is contained in:
2023-03-30 16:56:18 -05:00
parent 3df4513cd1
commit 7e368c6ff9
9 changed files with 576 additions and 23 deletions

View File

@ -70,8 +70,6 @@ public class QRecordApiAdapter
{
ApiFieldMetaData apiFieldMetaData = ApiFieldMetaData.of(field);
// todo - what about display values / possible values?
String apiFieldName = ApiFieldMetaData.getEffectiveApiFieldName(field);
if(StringUtils.hasContent(apiFieldMetaData.getReplacedByFieldName()))
{
@ -82,6 +80,23 @@ public class QRecordApiAdapter
outputRecord.put(apiFieldName, record.getValue(field.getName()));
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// todo - should probably define in meta-data if an association is included in the api or not!! //
// and what its name is too... //
//////////////////////////////////////////////////////////////////////////////////////////////////
QTableMetaData table = QContext.getQInstance().getTable(tableName);
for(Association association : CollectionUtils.nonNullList(table.getAssociations()))
{
ArrayList<Map<String, Serializable>> associationList = new ArrayList<>();
outputRecord.put(association.getName(), associationList);
for(QRecord associatedRecord : CollectionUtils.nonNullList(CollectionUtils.nonNullMap(record.getAssociatedRecords()).get(association.getName())))
{
associationList.add(qRecordToApiMap(associatedRecord, association.getAssociatedTableName(), apiVersion));
}
}
return (outputRecord);
}

View File

@ -567,6 +567,7 @@ public class QJavalinApiHandler
// and throw a 400-series error (tell the user bad-request), rather than, we're doing a 500 (server error)
getInput.setPrimaryKey(primaryKey);
getInput.setIncludeAssociations(true);
GetAction getAction = new GetAction();
GetOutput getOutput = getAction.execute(getInput);
@ -616,6 +617,7 @@ public class QJavalinApiHandler
QJavalinAccessLogger.logStart("apiQuery", logPair("table", tableName));
queryInput.setTableName(tableName);
queryInput.setIncludeAssociations(true);
PermissionsHelper.checkTablePermissionThrowing(queryInput, TablePermissionSubType.READ);
@ -795,6 +797,16 @@ public class QJavalinApiHandler
output.put("pageNo", pageNo);
output.put("pageSize", pageSize);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// map record fields for api //
// note - don't put them in the output until after the count, just because that looks a little nicer, i think //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ArrayList<Map<String, Serializable>> records = new ArrayList<>();
for(QRecord record : queryOutput.getRecords())
{
records.add(QRecordApiAdapter.qRecordToApiMap(record, tableName, version));
}
/////////////////////////////
// optionally do the count //
/////////////////////////////
@ -807,14 +819,6 @@ public class QJavalinApiHandler
output.put("count", countOutput.getCount());
}
///////////////////////////////
// map record fields for api //
///////////////////////////////
ArrayList<Map<String, Serializable>> records = new ArrayList<>();
for(QRecord record : queryOutput.getRecords())
{
records.add(QRecordApiAdapter.qRecordToApiMap(record, tableName, version));
}
output.put("records", records);
QJavalinAccessLogger.logEndSuccess(logPair("recordCount", queryOutput.getRecords().size()), QJavalinAccessLogger.logPairIfSlow("filter", filter, SLOW_LOG_THRESHOLD_MS));