use of de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor in project metasfresh-webui-api by metasfresh.
the class GridTabVOBasedDocumentEntityDescriptorFactory method createAndAddDocumentField.
private final void createAndAddDocumentField(final DocumentEntityDescriptor.Builder entityDescriptor, final GridFieldVO gridFieldVO, final boolean keyColumn) {
// From entry data-binding:
final SqlDocumentEntityDataBindingDescriptor.Builder entityBindings = entityDescriptor.getDataBindingBuilder(SqlDocumentEntityDataBindingDescriptor.Builder.class);
// From GridFieldVO:
final String fieldName = gridFieldVO.getColumnName();
final String sqlColumnName = fieldName;
//
final boolean isParentLinkColumn = sqlColumnName.equals(entityBindings.getSqlParentLinkColumnName());
//
//
DocumentFieldWidgetType widgetType;
final Class<?> valueClass;
final Optional<IExpression<?>> defaultValueExpression;
final boolean alwaysUpdateable;
final LookupDescriptorProvider lookupDescriptorProvider;
final LookupDescriptor lookupDescriptor;
ILogicExpression readonlyLogic;
if (isParentLinkColumn) {
widgetType = DocumentFieldWidgetType.Integer;
valueClass = widgetType.getValueClass();
alwaysUpdateable = false;
lookupDescriptorProvider = LookupDescriptorProvider.NULL;
lookupDescriptor = null;
defaultValueExpression = Optional.empty();
readonlyLogic = ConstantLogicExpression.TRUE;
} else {
final int displayType = gridFieldVO.getDisplayType();
widgetType = DescriptorsFactoryHelper.extractWidgetType(sqlColumnName, displayType);
alwaysUpdateable = extractAlwaysUpdateable(gridFieldVO);
final String ctxTableName = Services.get(IADTableDAO.class).retrieveTableName(gridFieldVO.getAD_Table_ID());
lookupDescriptorProvider = SqlLookupDescriptor.builder().setCtxTableName(ctxTableName).setCtxColumnName(sqlColumnName).setWidgetType(widgetType).setDisplayType(displayType).setAD_Reference_Value_ID(gridFieldVO.getAD_Reference_Value_ID()).setAD_Val_Rule_ID(gridFieldVO.getAD_Val_Rule_ID()).buildProvider();
lookupDescriptor = lookupDescriptorProvider.provideForScope(LookupDescriptorProvider.LookupScope.DocumentField);
valueClass = DescriptorsFactoryHelper.getValueClass(widgetType, lookupDescriptor);
defaultValueExpression = defaultValueExpressionsFactory.extractDefaultValueExpression(gridFieldVO.getDefaultValue(), fieldName, widgetType, valueClass, gridFieldVO.isMandatory(), gridFieldVO.isUseDocSequence());
if (keyColumn) {
readonlyLogic = ConstantLogicExpression.TRUE;
} else if (gridFieldVO.isReadOnly()) {
readonlyLogic = ConstantLogicExpression.TRUE;
} else // and we want to allow the user setting it.
if (!gridFieldVO.isUpdateable() && !gridFieldVO.isParentLink()) {
readonlyLogic = ConstantLogicExpression.FALSE;
} else {
readonlyLogic = gridFieldVO.getReadOnlyLogic();
}
}
//
// Button action
final ButtonFieldActionDescriptor buttonAction;
if (!isParentLinkColumn && widgetType.isButton()) {
buttonAction = extractButtonFieldActionDescriptor(entityDescriptor.getTableNameOrNull(), fieldName, gridFieldVO.AD_Process_ID);
if (buttonAction != null) {
final ButtonFieldActionType actionType = buttonAction.getActionType();
if (actionType == ButtonFieldActionType.processCall) {
widgetType = DocumentFieldWidgetType.ProcessButton;
} else if (actionType == ButtonFieldActionType.genericZoomInto) {
widgetType = DocumentFieldWidgetType.ZoomIntoButton;
// allow pressing the button
readonlyLogic = ConstantLogicExpression.FALSE;
}
}
} else {
buttonAction = null;
}
//
// ORDER BY SortNo
int orderBySortNo = gridFieldVO.getSortNo();
if (orderBySortNo == 0 && keyColumn) {
orderBySortNo = Integer.MAX_VALUE;
}
final String sqlColumnSql = gridFieldVO.getColumnSQL(false);
final SqlDocumentFieldDataBindingDescriptor fieldBinding = SqlDocumentFieldDataBindingDescriptor.builder().setFieldName(sqlColumnName).setTableName(entityBindings.getTableName()).setTableAlias(entityBindings.getTableAlias()).setColumnName(sqlColumnName).setColumnSql(sqlColumnSql).setVirtualColumn(gridFieldVO.isVirtualColumn()).setMandatory(gridFieldVO.isMandatoryDB()).setWidgetType(widgetType).setValueClass(valueClass).setSqlValueClass(entityBindings.getPOInfo().getColumnClass(sqlColumnName)).setLookupDescriptor(lookupDescriptor).setKeyColumn(keyColumn).setEncrypted(gridFieldVO.isEncryptedColumn()).setDefaultOrderBy(orderBySortNo).build();
final DocumentFieldDescriptor.Builder fieldBuilder = DocumentFieldDescriptor.builder(sqlColumnName).setCaption(gridFieldVO.getHeaderTrls(), gridFieldVO.getHeader()).setDescription(gridFieldVO.getDescriptionTrls(), gridFieldVO.getDescription()).setKey(keyColumn).setParentLink(isParentLinkColumn).setWidgetType(widgetType).setButtonActionDescriptor(buttonAction).setLookupDescriptorProvider(lookupDescriptorProvider).setValueClass(fieldBinding.getValueClass()).setVirtualField(fieldBinding.isVirtualColumn()).setCalculated(gridFieldVO.isCalculated()).setDefaultValueExpression(defaultValueExpression).addCharacteristicIfTrue(keyColumn, Characteristic.SideListField).addCharacteristicIfTrue(keyColumn, Characteristic.GridViewField).setReadonlyLogic(readonlyLogic).setAlwaysUpdateable(alwaysUpdateable).setMandatoryLogic(extractMandatoryLogic(gridFieldVO)).setDisplayLogic(gridFieldVO.getDisplayLogic()).setDefaultFilterInfo(createDefaultFilterDescriptor(gridFieldVO.getDefaultFilterDescriptor(), sqlColumnName, widgetType)).setDataBinding(fieldBinding);
//
// Add Field builder to document entity
entityDescriptor.addField(fieldBuilder);
//
// Add Field's data binding to entity data binding
entityBindings.addField(fieldBinding);
//
// Collect special field
collectSpecialField(fieldBuilder);
}
use of de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor in project metasfresh-webui-api by metasfresh.
the class GridTabVOBasedDocumentEntityDescriptorFactory method createAndAddLabelsDocumentField.
private final void createAndAddLabelsDocumentField(final DocumentEntityDescriptor.Builder entityDescriptor, final I_AD_UI_Element labelsUIElement) {
final String labelsFieldName = getLabelsFieldName(labelsUIElement);
final String tablename = entityDescriptor.getTableName().get();
final LabelsLookup lookupDescriptor = createLabelsLookup(labelsUIElement, tablename);
final SqlDocumentEntityDataBindingDescriptor.Builder entityBindings = entityDescriptor.getDataBindingBuilder(SqlDocumentEntityDataBindingDescriptor.Builder.class);
final SqlDocumentFieldDataBindingDescriptor fieldBinding = SqlDocumentFieldDataBindingDescriptor.builder().setFieldName(labelsFieldName).setVirtualColumn(false).setMandatory(false).setWidgetType(DocumentFieldWidgetType.Labels).setValueClass(DocumentFieldWidgetType.Labels.getValueClass()).setSqlValueClass(DocumentFieldWidgetType.Labels.getValueClass()).setLookupDescriptor(lookupDescriptor).build();
entityBindings.addField(fieldBinding);
final IModelTranslationMap trlMap = InterfaceWrapperHelper.getModelTranslationMap(labelsUIElement);
final DocumentFieldDescriptor.Builder fieldBuilder = DocumentFieldDescriptor.builder(labelsFieldName).setCaption(trlMap.getColumnTrl(I_AD_UI_Element.COLUMNNAME_Name, labelsUIElement.getName())).setDescription(trlMap.getColumnTrl(I_AD_UI_Element.COLUMNNAME_Description, labelsUIElement.getDescription())).setKey(false).setParentLink(false).setWidgetType(DocumentFieldWidgetType.Labels).setValueClass(fieldBinding.getValueClass()).setLookupDescriptorProvider(lookupDescriptor).setDefaultFilterInfo(createLabelsDefaultFilterInfo(labelsUIElement)).setDataBinding(fieldBinding);
//
// Add Field builder to document entity
entityDescriptor.addField(fieldBuilder);
//
// Collect special field
collectSpecialField(fieldBuilder);
}
use of de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor in project metasfresh-webui-api by metasfresh.
the class SqlDocumentsRepository method toQueryBuilder.
private static final IQueryBuilder<Object> toQueryBuilder(final SqlDocumentEntityDataBindingDescriptor dataBinding, final DocumentId documentId) {
final String tableName = dataBinding.getTableName();
final List<SqlDocumentFieldDataBindingDescriptor> keyFields = dataBinding.getKeyFields();
final int keyFieldsCount = keyFields.size();
if (keyFieldsCount == 0) {
throw new AdempiereException("No primary key defined for " + tableName);
}
final List<Object> keyParts = documentId.toComposedKeyParts();
if (keyFieldsCount != keyParts.size()) {
throw new AdempiereException("Invalid documentId '" + documentId + "'. It shall have " + keyFieldsCount + " parts but it has " + keyParts.size());
}
final IQueryBL queryBL = Services.get(IQueryBL.class);
final IQueryBuilder<Object> queryBuilder = queryBL.createQueryBuilder(tableName, PlainContextAware.newWithThreadInheritedTrx());
for (int i = 0; i < keyFieldsCount; i++) {
final SqlDocumentFieldDataBindingDescriptor keyField = keyFields.get(i);
final String keyColumnName = keyField.getColumnName();
final Object keyValue = convertValueToPO(keyParts.get(i), keyColumnName, keyField.getWidgetType(), keyField.getSqlValueClass());
queryBuilder.addEqualsFilter(keyColumnName, keyValue);
}
return queryBuilder;
}
use of de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor in project metasfresh-webui-api by metasfresh.
the class SqlDocumentQueryBuilder method buildSqlWhereClause.
private IPair<IStringExpression, List<Object>> buildSqlWhereClause() {
final SqlParamsCollector sqlParams = SqlParamsCollector.newInstance();
final CompositeStringExpression.Builder sqlWhereClauseBuilder = IStringExpression.composer();
//
// Entity's WHERE clause
{
final IStringExpression entityWhereClauseExpression = entityBinding.getSqlWhereClause();
if (!entityWhereClauseExpression.isNullExpression()) {
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* entity where clause */ (").append(entityWhereClauseExpression).append(")");
}
}
//
// Key column
// FIXME: handle AD_Reference/AD_Ref_List(s). In that case the recordId will be AD_Ref_List.Value,
// so the SQL where clause which is currently build is AD_Ref_List_ID=<the AD_Ref_List.Value>.
// The build SQL where clause shall be something like AD_Reference_ID=<the reference, i think we shall fetch it somehow from Lookup> AND Value=<the value, which currently is the recordId>
final DocumentId recordId = getRecordId();
if (recordId != null) {
final List<SqlDocumentFieldDataBindingDescriptor> keyFields = entityBinding.getKeyFields();
if (keyFields.isEmpty()) {
throw new AdempiereException("Failed building where clause because there is no Key Column defined in " + entityBinding);
} else // Single primary key
if (keyFields.size() == 1) {
final String keyColumnName = keyFields.get(0).getColumnName();
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* key */ ").append(keyColumnName).append("=").append(sqlParams.placeholder(recordId.toInt()));
} else // Composed primary key
{
final Map<String, Object> keyColumnName2value = extractComposedKey(recordId, keyFields);
keyColumnName2value.forEach((keyColumnName, value) -> {
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* key */ ").append(keyColumnName).append("=").append(sqlParams.placeholder(value));
});
}
}
//
// Parent link where clause (if any)
final Document parentDocument = getParentDocument();
if (parentDocument != null) {
final String parentLinkColumnName = entityBinding.getParentLinkColumnName();
final String linkColumnName = entityBinding.getLinkColumnName();
if (parentLinkColumnName != null && linkColumnName != null) {
final IDocumentFieldView parentLinkField = parentDocument.getFieldView(parentLinkColumnName);
final Object parentLinkValue = parentLinkField.getValue();
final DocumentFieldWidgetType parentLinkWidgetType = parentLinkField.getWidgetType();
final Class<?> targetClass = entityBinding.getFieldByFieldName(linkColumnName).getSqlValueClass();
final Object sqlParentLinkValue = SqlDocumentsRepository.convertValueToPO(parentLinkValue, parentLinkColumnName, parentLinkWidgetType, targetClass);
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* parent link */ ").append(linkColumnName).append("=").append(sqlParams.placeholder(sqlParentLinkValue));
}
}
//
// Document filters
{
final String sqlFilters = SqlDocumentFilterConverters.createEntityBindingEffectiveConverter(entityBinding).getSql(sqlParams, getDocumentFilters(), SqlOptions.usingTableAlias(entityBinding.getTableAlias()));
if (!Check.isEmpty(sqlFilters, true)) {
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* filters */ (\n").append(sqlFilters).append(")\n");
}
}
// Build the final SQL where clause
return ImmutablePair.of(sqlWhereClauseBuilder.build(), Collections.unmodifiableList(sqlParams.toList()));
}
use of de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor in project metasfresh-webui-api by metasfresh.
the class SqlDocumentQueryBuilder method extractSingleKeyColumnName.
/**
* @return SQL key column name; never returns null
*/
private static final String extractSingleKeyColumnName(final DocumentEntityDescriptor entityDescriptor) {
final DocumentFieldDescriptor idField = entityDescriptor.getSingleIdField();
final SqlDocumentFieldDataBindingDescriptor idFieldBinding = SqlDocumentFieldDataBindingDescriptor.castOrNull(idField.getDataBinding());
if (idFieldBinding == null) {
throw new AdempiereException("Entity's ID field does not have SQL bindings: " + idField);
}
return idFieldBinding.getColumnName();
}
Aggregations