Search in sources :

Example 1 with SqlDocumentFieldDataBindingDescriptor

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);
}
Also used : SqlDocumentEntityDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor) IExpression(org.adempiere.ad.expression.api.IExpression) LookupDescriptorProvider(de.metas.ui.web.window.descriptor.LookupDescriptorProvider) ButtonFieldActionDescriptor(de.metas.ui.web.window.descriptor.ButtonFieldActionDescriptor) SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) DocumentFieldDescriptor(de.metas.ui.web.window.descriptor.DocumentFieldDescriptor) ILogicExpression(org.adempiere.ad.expression.api.ILogicExpression) ButtonFieldActionType(de.metas.ui.web.window.descriptor.ButtonFieldActionDescriptor.ButtonFieldActionType) DocumentFieldWidgetType(de.metas.ui.web.window.descriptor.DocumentFieldWidgetType) IADTableDAO(org.adempiere.ad.table.api.IADTableDAO) SqlLookupDescriptor(de.metas.ui.web.window.descriptor.sql.SqlLookupDescriptor) LookupDescriptor(de.metas.ui.web.window.descriptor.LookupDescriptor)

Example 2 with SqlDocumentFieldDataBindingDescriptor

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);
}
Also used : SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) DocumentFieldDescriptor(de.metas.ui.web.window.descriptor.DocumentFieldDescriptor) IModelTranslationMap(de.metas.i18n.IModelTranslationMap) SqlDocumentEntityDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor) LabelsLookup(de.metas.ui.web.window.model.lookup.LabelsLookup)

Example 3 with SqlDocumentFieldDataBindingDescriptor

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;
}
Also used : SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) IQueryBL(org.adempiere.ad.dao.IQueryBL) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 4 with SqlDocumentFieldDataBindingDescriptor

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()));
}
Also used : IPair(org.adempiere.util.lang.IPair) SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) Env(org.compiere.util.Env) ArrayList(java.util.ArrayList) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) Evaluatee(org.compiere.util.Evaluatee) SqlDocumentFilterConverters(de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverters) ImmutableList(com.google.common.collect.ImmutableList) SqlEntityFieldBinding(de.metas.ui.web.window.descriptor.sql.SqlEntityFieldBinding) SqlDocumentEntityDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor) IDocumentFieldView(de.metas.ui.web.window.model.IDocumentFieldView) Map(java.util.Map) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor) DocumentFilter(de.metas.ui.web.document.filter.DocumentFilter) DocumentQueryOrderBy(de.metas.ui.web.window.model.DocumentQueryOrderBy) OnVariableNotFound(org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound) Properties(java.util.Properties) WindowConstants(de.metas.ui.web.window.WindowConstants) UserRolePermissionsKey(org.adempiere.ad.security.UserRolePermissionsKey) Evaluatees(org.compiere.util.Evaluatees) ImmutableMap(com.google.common.collect.ImmutableMap) AccessSqlStringExpression(org.adempiere.ad.security.impl.AccessSqlStringExpression) MoreObjects(com.google.common.base.MoreObjects) Objects(java.util.Objects) DocumentFieldDescriptor(de.metas.ui.web.window.descriptor.DocumentFieldDescriptor) List(java.util.List) ImmutablePair(org.adempiere.util.lang.ImmutablePair) AdempiereException(org.adempiere.exceptions.AdempiereException) Check(org.adempiere.util.Check) DocumentFieldWidgetType(de.metas.ui.web.window.descriptor.DocumentFieldWidgetType) DocumentQuery(de.metas.ui.web.window.model.DocumentQuery) LookupValueByIdSupplier(de.metas.ui.web.window.model.lookup.LookupValueByIdSupplier) SqlParamsCollector(de.metas.ui.web.document.filter.sql.SqlParamsCollector) Collections(java.util.Collections) Document(de.metas.ui.web.window.model.Document) CompositeStringExpression(org.adempiere.ad.expression.api.impl.CompositeStringExpression) CompositeStringExpression(org.adempiere.ad.expression.api.impl.CompositeStringExpression) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) Document(de.metas.ui.web.window.model.Document) IDocumentFieldView(de.metas.ui.web.window.model.IDocumentFieldView) SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) DocumentFieldWidgetType(de.metas.ui.web.window.descriptor.DocumentFieldWidgetType) AdempiereException(org.adempiere.exceptions.AdempiereException) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SqlParamsCollector(de.metas.ui.web.document.filter.sql.SqlParamsCollector)

Example 5 with SqlDocumentFieldDataBindingDescriptor

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();
}
Also used : SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) DocumentFieldDescriptor(de.metas.ui.web.window.descriptor.DocumentFieldDescriptor) AdempiereException(org.adempiere.exceptions.AdempiereException)

Aggregations

SqlDocumentFieldDataBindingDescriptor (de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor)6 DocumentFieldDescriptor (de.metas.ui.web.window.descriptor.DocumentFieldDescriptor)5 DocumentFieldWidgetType (de.metas.ui.web.window.descriptor.DocumentFieldWidgetType)3 SqlDocumentEntityDataBindingDescriptor (de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor)3 AdempiereException (org.adempiere.exceptions.AdempiereException)3 LookupDescriptor (de.metas.ui.web.window.descriptor.LookupDescriptor)2 SqlLookupDescriptor (de.metas.ui.web.window.descriptor.sql.SqlLookupDescriptor)2 MoreObjects (com.google.common.base.MoreObjects)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ICurrencyDAO (de.metas.currency.ICurrencyDAO)1 DateTimeTranslatableString (de.metas.i18n.DateTimeTranslatableString)1 IModelTranslationMap (de.metas.i18n.IModelTranslationMap)1 ITranslatableString (de.metas.i18n.ITranslatableString)1 ImmutableTranslatableString (de.metas.i18n.ImmutableTranslatableString)1 NumberTranslatableString (de.metas.i18n.NumberTranslatableString)1 BoardFieldLoader (de.metas.ui.web.board.BoardCardFieldDescriptor.BoardFieldLoader)1 DocumentFilter (de.metas.ui.web.document.filter.DocumentFilter)1 SqlDocumentFilterConverters (de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverters)1 SqlParamsCollector (de.metas.ui.web.document.filter.sql.SqlParamsCollector)1