Search in sources :

Example 1 with DocumentFieldDataBindingDescriptor

use of de.metas.ui.web.window.descriptor.DocumentFieldDataBindingDescriptor in project metasfresh-webui-api by metasfresh.

the class SqlDocumentsRepository method setPOValue.

/**
 * Sets PO's value from given <code>documentField</code>.
 *
 * @param po
 * @param documentField
 * @return true if value was set and really changed
 */
private static boolean setPOValue(final PO po, final IDocumentFieldView documentField) {
    final DocumentFieldDataBindingDescriptor dataBinding = documentField.getDescriptor().getDataBinding().orElse(null);
    if (dataBinding == null) {
        logger.trace("Skip setting PO's column because it has no databinding: {}", documentField);
        return false;
    }
    final POInfo poInfo = po.getPOInfo();
    final String columnName = dataBinding.getColumnName();
    final int poColumnIndex = poInfo.getColumnIndex(columnName);
    if (poColumnIndex < 0) {
        logger.trace("Skip setting PO's column because it's missing: {} -- PO={}", columnName, po);
        return false;
    }
    // Virtual column => skip setting it
    if (poInfo.isVirtualColumn(poColumnIndex)) {
        logger.trace("Skip setting PO's virtual column: {} -- PO={}", columnName, po);
        // no change
        return false;
    } else // ID
    if (poInfo.isKey(poColumnIndex)) {
        final int id = documentField.getValueAsInt(-1);
        if (id >= 0) {
            final int idOld = po.get_ValueAsInt(poColumnIndex);
            if (id == idOld) {
                logger.trace("Skip setting PO's key column because it's the same as the old value: {} (old={}), PO={}", columnName, idOld, po);
                // no change
                return false;
            }
            final boolean idSet = po.set_ValueNoCheck(columnName, id);
            if (!idSet) {
                throw new AdempiereException("Failed setting ID=" + id + " to " + po);
            }
            logger.trace("Setting PO ID: {}={} -- PO={}", columnName, id, po);
            return true;
        } else {
            logger.trace("Skip setting PO's key column: {} -- PO={}", columnName, po);
            // no change
            return false;
        }
    } else // Created/Updated columns
    if (WindowConstants.FIELDNAMES_CreatedUpdated.contains(columnName)) {
        logger.trace("Skip setting PO's created/updated column: {} -- PO={}", columnName, po);
        // no change
        return false;
    } else // 
    // Regular column
    {
        // 
        // Check if value was changed, compared with PO's current value
        final Object poValue = po.get_Value(poColumnIndex);
        final Class<?> poValueClass = poInfo.getColumnClass(poColumnIndex);
        final Object fieldValueConv = convertValueToPO(documentField.getValue(), columnName, documentField.getWidgetType(), poValueClass);
        if (poFieldValueEqual(fieldValueConv, poValue)) {
            logger.trace("Skip setting PO's column because it was not changed: {}={} (old={}) -- PO={}", columnName, fieldValueConv, poValue, po);
            // no change
            return false;
        }
        // Check if the field value was changed from when we last queried it
        if (!po.is_new()) {
            final Object fieldInitialValueConv = convertValueToPO(documentField.getInitialValue(), columnName, documentField.getWidgetType(), poValueClass);
            if (!poFieldValueEqual(fieldInitialValueConv, poValue)) {
                throw new AdempiereException("Document's field was changed from when we last queried it. Please re-query." + "\n Document field initial value: " + fieldInitialValueConv + "\n PO value: " + poValue + "\n Document field: " + documentField + "\n PO: " + po);
            }
        }
        // TODO: handle not updateable columns... i think we shall set them only if the PO is new
        // NOTE: at this point we shall not do any other validations like "mandatory but null", value min/max range check,
        // because we shall rely completely on Document level validations and not duplicate the logic here.
        // 
        // Try setting the value
        final boolean valueSet = po.set_ValueOfColumn(columnName, fieldValueConv);
        if (!valueSet) {
            logger.warn("Failed setting PO's column: {}={} (old={}) -- PO={}", columnName, fieldValueConv, poValue, po);
            // no change
            return false;
        }
        logger.trace("Setting PO value: {}={} (old={}) -- PO={}", columnName, fieldValueConv, poValue, po);
        return true;
    }
}
Also used : POInfo(org.compiere.model.POInfo) AdempiereException(org.adempiere.exceptions.AdempiereException) SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) DocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.DocumentFieldDataBindingDescriptor)

Aggregations

DocumentFieldDataBindingDescriptor (de.metas.ui.web.window.descriptor.DocumentFieldDataBindingDescriptor)1 SqlDocumentFieldDataBindingDescriptor (de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1 POInfo (org.compiere.model.POInfo)1