use of de.metas.ui.web.window.model.DocumentQueryOrderByList in project metasfresh-webui-api by metasfresh.
the class HUEditorViewBuffer_HighVolume method huIdsPageFetcher.
private PageFetcher<HuId> huIdsPageFetcher(final DocumentQueryOrderByList orderBys) {
final ViewEvaluationCtx viewEvalCtx = getViewEvaluationCtx();
final ViewRowIdsOrderedSelection selection = getSelection(orderBys);
return (firstRow, maxRows) -> huEditorRepo.retrieveHUIdsPage(viewEvalCtx, selection, firstRow, maxRows);
}
use of de.metas.ui.web.window.model.DocumentQueryOrderByList in project metasfresh-webui-api by metasfresh.
the class WindowRestController method getIncludedTabRows.
@GetMapping("/{windowId}/{documentId}/{tabId}")
public List<JSONDocument> getIncludedTabRows(@PathVariable("windowId") final String windowIdStr, @PathVariable("documentId") final String documentIdStr, @PathVariable("tabId") final String tabIdStr, @RequestParam(name = "ids", required = false) @ApiParam("comma separated rowIds") final String rowIdsListStr, @RequestParam(name = PARAM_FieldsList, required = false) @ApiParam("comma separated field names") final String fieldsListStr, @RequestParam(name = PARAM_Advanced, required = false, defaultValue = PARAM_Advanced_DefaultValue) final boolean advanced, @RequestParam(name = "orderBy", required = false) final String orderBysListStr) {
final WindowId windowId = WindowId.fromJson(windowIdStr);
final DocumentId documentId = DocumentId.of(documentIdStr);
final DetailId tabId = DetailId.fromJson(tabIdStr);
final DocumentIdsSelection onlyRowIds = DocumentIdsSelection.ofCommaSeparatedString(rowIdsListStr);
final DocumentPath documentPath;
if (onlyRowIds.isEmpty() || onlyRowIds.isAll()) {
documentPath = DocumentPath.includedDocumentPath(windowId, documentId, tabId);
} else {
documentPath = DocumentPath.includedDocumentPath(windowId, documentId, tabId, onlyRowIds);
}
final DocumentQueryOrderByList orderBys = DocumentQueryOrderByList.parse(orderBysListStr);
final JSONDocumentOptions jsonOpts = newJSONDocumentOptions().showOnlyFieldsListStr(fieldsListStr).showAdvancedFields(advanced).build();
return getData(documentPath, orderBys, jsonOpts);
}
use of de.metas.ui.web.window.model.DocumentQueryOrderByList in project metasfresh-webui-api by metasfresh.
the class HUEditorViewBuffer_HighVolume method streamByIds.
private Stream<HUEditorRow> streamByIds(@NonNull final HUEditorRowFilter filter) {
final Stream<HUEditorRowId> huEditorRowIds;
final ImmutableSet<HUEditorRowId> onlyRowIds = filter.getOnlyRowIds();
if (onlyRowIds.isEmpty()) {
final DocumentQueryOrderByList defaultOrderBys = getDefaultSelection().getOrderBys();
huEditorRowIds = streamHUIdsByPage(0, Integer.MAX_VALUE, defaultOrderBys).map(HUEditorRowId::ofTopLevelHU);
} else {
huEditorRowIds = onlyRowIds.stream();
}
return HUEditorRowsPagedLoadingIterator.builder().huEditorRepo(huEditorRepo).cache(cache_huRowsById).rowIds(huEditorRowIds.iterator()).filter(filter).build().stream();
}
use of de.metas.ui.web.window.model.DocumentQueryOrderByList in project metasfresh-webui-api by metasfresh.
the class SqlViewSelectionQueryBuilder method buildSqlCreateSelectionFromSelection.
/**
* @return
*
* <pre>
* INSERT INTO T_WEBUI_ViewSelection (UUID, Line, keys)
* SELECT ... FROM T_WEBUI_ViewSelection sel INNER JOIN ourTable WHERE sel.UUID=[fromUUID]
* </pre>
*/
public SqlAndParams buildSqlCreateSelectionFromSelection(@NonNull final ViewEvaluationCtx viewEvalCtx, @NonNull final ViewId newViewId, @NonNull final String fromSelectionId, @NonNull final DocumentFilterList filters, @NonNull final DocumentQueryOrderByList orderBys, @NonNull final SqlDocumentFilterConverterContext filterConverterCtx) {
final String sqlTableAlias = getTableAlias();
final SqlViewKeyColumnNamesMap keyColumnNamesMap = getSqlViewKeyColumnNamesMap();
final DocumentQueryOrderByList orderBysEffective = orderBys.stream().flatMap(this::flatMapEffectiveFieldNames).collect(DocumentQueryOrderByList.toDocumentQueryOrderByList());
//
// Build the table we will join.
final SqlAndParams sqlSourceTable;
{
final Set<String> addedFieldNames = new HashSet<>();
final StringBuilder sqlKeyColumnNames;
{
sqlKeyColumnNames = new StringBuilder();
for (final String keyColumnName : keyColumnNamesMap.getKeyColumnNames()) {
if (!addedFieldNames.add(keyColumnName)) {
continue;
}
if (sqlKeyColumnNames.length() > 0) {
sqlKeyColumnNames.append("\n, ");
}
sqlKeyColumnNames.append(getSqlSelectValue(keyColumnName).withColumnNameAlias(keyColumnName).toSqlStringWithColumnNameAlias());
}
}
final SqlAndParams.Builder sqlSourceTableBuilder = SqlAndParams.builder();
sqlSourceTableBuilder.append("(SELECT ").append(sqlKeyColumnNames);
for (final DocumentQueryOrderBy orderBy : orderBysEffective.toList()) {
final String fieldName = orderBy.getFieldName();
final SqlSelectDisplayValue sqlSelectDisplayValue = getSqlSelectDisplayValue(fieldName);
if (sqlSelectDisplayValue != null && addedFieldNames.add(sqlSelectDisplayValue.getColumnNameAlias())) {
sqlSourceTableBuilder.append("\n, ").append(sqlSelectDisplayValue.withJoinOnTableNameOrAlias(getTableName()).toSqlStringWithColumnNameAlias(viewEvalCtx.toEvaluatee()));
}
final SqlSelectValue sqlSelectValue = getSqlSelectValue(fieldName);
if (sqlSelectValue != null && addedFieldNames.add(sqlSelectValue.getColumnNameAlias())) {
sqlSourceTableBuilder.append("\n, ").append(sqlSelectValue.withJoinOnTableNameOrAlias(getTableName()).toSqlStringWithColumnNameAlias());
}
}
sqlSourceTableBuilder.append("\n FROM ").append(getTableName());
final SqlAndParams sqlFilters = buildSqlFiltersOrNull(filters, filterConverterCtx, SqlOptions.usingTableName(getTableName()));
if (sqlFilters != null) {
sqlSourceTableBuilder.append("\n WHERE ").append(sqlFilters);
}
sqlSourceTableBuilder.append(")");
sqlSourceTable = sqlSourceTableBuilder.build();
}
//
// Order BY
final String sqlOrderBys = SqlDocumentOrderByBuilder.newInstance(this::getFieldOrderBy).joinOnTableNameOrAlias(sqlTableAlias).useColumnNameAlias(true).buildSqlOrderBy(orderBysEffective).map(sqlOrderBysExpr -> sqlOrderBysExpr.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail)).map(sql -> _viewBinding.replaceTableNameWithTableAlias(sql, sqlTableAlias)).orElse(null);
//
final String sqlJoinCondition = keyColumnNamesMap.getSqlJoinCondition(sqlTableAlias, "sel");
//
return SqlAndParams.builder().append("INSERT INTO " + I_T_WEBUI_ViewSelection.Table_Name + " (" + " " + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + ", " + I_T_WEBUI_ViewSelection.COLUMNNAME_Line + ", " + keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated() + ")").append("\n SELECT ").append("\n ?", // newUUID
newViewId.getViewId()).append("\n, ").append("row_number() OVER (").append(sqlOrderBys != null ? "ORDER BY " + sqlOrderBys : "").append(// Line
")").append("\n, ").append(// keys
keyColumnNamesMap.getKeyColumnNamesCommaSeparated()).append("\n FROM ").append(I_T_WEBUI_ViewSelection.Table_Name).append(" sel").append("\n INNER JOIN ").append(sqlSourceTable).append(" ").append(sqlTableAlias).append(" ON (").append(sqlJoinCondition).append(")").append("\n WHERE sel.").append(I_T_WEBUI_ViewSelection.COLUMNNAME_UUID).append("=?", // fromUUID
fromSelectionId).build();
}
use of de.metas.ui.web.window.model.DocumentQueryOrderByList in project metasfresh-webui-api by metasfresh.
the class SqlViewSelectionQueryBuilder method buildSqlCreateSelectionFromSelectionLines.
public SqlAndParams buildSqlCreateSelectionFromSelectionLines(@NonNull final ViewEvaluationCtx viewEvalCtx, @NonNull final ViewId newViewId, @NonNull final DocumentQueryOrderByList orderBys) {
final String lineTableName = getTableName();
final String lineTableAlias = getTableAlias();
final SqlViewKeyColumnNamesMap keyColumnNamesMap = getSqlViewKeyColumnNamesMap();
final String lineKeyColumnName = keyColumnNamesMap.getSingleKeyColumnName();
final SqlOrderByBindings sqlOrderByBindings = fieldName -> {
if (keyColumnNamesMap.isKeyPartFieldName(fieldName)) {
return SqlOrderByValue.ofColumnName("sl", keyColumnNamesMap.getWebuiSelectionColumnNameForKeyColumnName(fieldName));
} else if (isGroupBy(fieldName)) {
return getFieldOrderBy(fieldName);
} else if (isAggregated(fieldName)) {
return SqlOrderByValue.builder().sqlSelectValue(getSqlAggregatedColumn(fieldName)).build();
} else {
// shall not happen
return null;
}
};
final DocumentQueryOrderByList orderBysEffective = orderBys.stream().flatMap(this::flatMapEffectiveFieldNames).filter(orderBy -> keyColumnNamesMap.isKeyPartFieldName(orderBy.getFieldName()) || isGroupBy(orderBy.getFieldName()) || isAggregated(orderBy.getFieldName())).collect(DocumentQueryOrderByList.toDocumentQueryOrderByList());
final String sqlOrderBy = SqlDocumentOrderByBuilder.newInstance(sqlOrderByBindings).joinOnTableNameOrAlias(lineTableAlias).useColumnNameAlias(false).buildSqlOrderBy(orderBysEffective).map(sqlOrderByExpr -> sqlOrderByExpr.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail)).orElseGet(() -> keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated("sl"));
final String sqlFrom = "SELECT " + "\n sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_UUID + "\n, " + keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated("sl") + "\n FROM " + I_T_WEBUI_ViewSelectionLine.Table_Name + " sl " + "\n INNER JOIN " + lineTableName + " " + lineTableAlias + " ON (" + lineTableAlias + "." + lineKeyColumnName + " = sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_Line_ID + // join lines
")" + "\n WHERE " + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_UUID + "=?" + "\n GROUP BY " + "\n sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_UUID + "\n, " + keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated("sl") + "\n, " + getGroupByFieldNamesCommaSeparated() + "\n ORDER BY " + sqlOrderBy;
final String sqlCreateSelectionFromLines = "INSERT INTO " + I_T_WEBUI_ViewSelection.Table_Name + "(" + "\n " + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + "\n, " + keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated() + "\n, " + // SeqNo
I_T_WEBUI_ViewSelection.COLUMNNAME_Line + "\n)" + "\n SELECT " + "\n sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_UUID + "\n , " + keyColumnNamesMap.getWebuiSelectionColumnNamesCommaSeparated("sl") + // SeqNo
"\n , row_number() OVER ()" + "\n FROM (" + sqlFrom + ") sl";
final List<Object> sqlCreateSelectionFromLinesParams = Arrays.asList(newViewId.getViewId());
return SqlAndParams.of(sqlCreateSelectionFromLines, sqlCreateSelectionFromLinesParams);
}
Aggregations