From 9a3933e7d462d250c94d53416193b7808b237d5a Mon Sep 17 00:00:00 2001 From: Teo Sarca Date: Tue, 2 May 2017 09:42:28 +0300 Subject: [PATCH] refactor DocumentFieldValueLoader(s) https://github.com/metasfresh/metasfresh-webui-api/issues/330 --- .../de/metas/ui/web/view/DefaultView.java | 1 + .../ui/web/view/SqlViewDataRepository.java | 10 +- .../de/metas/ui/web/view/SqlViewFactory.java | 31 +- .../metas/ui/web/view/ViewEvaluationCtx.java | 5 + .../descriptor/SqlViewRowFieldBinding.java | 2 +- .../sql/DocumentFieldValueLoader.java | 37 ++ .../sql/DocumentFieldValueLoaders.java | 336 ++++++++++++++++++ ...SqlDocumentFieldDataBindingDescriptor.java | 174 +-------- .../model/sql/SqlDocumentsRepository.java | 2 +- 9 files changed, 412 insertions(+), 186 deletions(-) create mode 100644 src/main/java/de/metas/ui/web/window/descriptor/sql/DocumentFieldValueLoader.java create mode 100644 src/main/java/de/metas/ui/web/window/descriptor/sql/DocumentFieldValueLoaders.java diff --git a/src/main/java/de/metas/ui/web/view/DefaultView.java b/src/main/java/de/metas/ui/web/view/DefaultView.java index f89a50975..2c063c3b9 100644 --- a/src/main/java/de/metas/ui/web/view/DefaultView.java +++ b/src/main/java/de/metas/ui/web/view/DefaultView.java @@ -353,6 +353,7 @@ public void notifyRecordsChanged(final Set recordRefs) rowIds.forEach(cache_rowsById::remove); // Collect event + // TODO: check which rowIds are contained in this view and fire events only for those ViewChangesCollector.getCurrentOrAutoflush().collectRowsChanged(this, rowIds); } diff --git a/src/main/java/de/metas/ui/web/view/SqlViewDataRepository.java b/src/main/java/de/metas/ui/web/view/SqlViewDataRepository.java index 5a6ca1039..80f035ef8 100644 --- a/src/main/java/de/metas/ui/web/view/SqlViewDataRepository.java +++ b/src/main/java/de/metas/ui/web/view/SqlViewDataRepository.java @@ -152,6 +152,7 @@ public IViewRow retrieveById(final ViewEvaluationCtx viewEvalCtx, final ViewId v { final WindowId windowId = viewId.getWindowId(); final String viewSelectionId = viewId.getViewId(); + final String adLanguage = viewEvalCtx.getAD_Language(); final String sql = sqlBindings.getSqlSelectById().evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail); @@ -169,7 +170,7 @@ public IViewRow retrieveById(final ViewEvaluationCtx viewEvalCtx, final ViewId v IViewRow firstDocument = null; while (rs.next()) { - final IViewRow document = loadViewRow(rs, windowId); + final IViewRow document = loadViewRow(rs, windowId, adLanguage); if (document == null) { continue; @@ -204,7 +205,7 @@ public IViewRow retrieveById(final ViewEvaluationCtx viewEvalCtx, final ViewId v } } - private IViewRow loadViewRow(final ResultSet rs, final WindowId windowId) throws SQLException + private IViewRow loadViewRow(final ResultSet rs, final WindowId windowId, final String adLanguage) throws SQLException { final ViewRow.Builder viewRowBuilder = ViewRow.builder(windowId); @@ -213,7 +214,7 @@ private IViewRow loadViewRow(final ResultSet rs, final WindowId windowId) throws final String fieldName = field.getFieldName(); final boolean keyColumn = field.isKeyColumn(); final SqlViewRowFieldLoader fieldLoader = field.getFieldLoader(); - final Object value = fieldLoader.retrieveValueAsJson(rs); + final Object value = fieldLoader.retrieveValueAsJson(rs, adLanguage); if (keyColumn) { @@ -249,6 +250,7 @@ public List retrievePage(final ViewEvaluationCtx viewEvalCtx, final Vi logger.debug("Using: {}", orderedSelection); final WindowId windowId = orderedSelection.getWindowId(); final String viewSelectionId = orderedSelection.getSelectionId(); + final String adLanguage = viewEvalCtx.getAD_Language(); final int firstSeqNo = firstRow + 1; // NOTE: firstRow is 0-based while SeqNo are 1-based final int lastSeqNo = firstRow + pageLength; @@ -268,7 +270,7 @@ public List retrievePage(final ViewEvaluationCtx viewEvalCtx, final Vi final ImmutableList.Builder pageBuilder = ImmutableList.builder(); while (rs.next()) { - final IViewRow row = loadViewRow(rs, windowId); + final IViewRow row = loadViewRow(rs, windowId, adLanguage); if (row == null) { continue; diff --git a/src/main/java/de/metas/ui/web/view/SqlViewFactory.java b/src/main/java/de/metas/ui/web/view/SqlViewFactory.java index 5a31fe25f..e66150f1f 100644 --- a/src/main/java/de/metas/ui/web/view/SqlViewFactory.java +++ b/src/main/java/de/metas/ui/web/view/SqlViewFactory.java @@ -1,10 +1,11 @@ package de.metas.ui.web.view; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Collection; import java.util.Set; import org.adempiere.ad.expression.api.NullStringExpression; -import org.adempiere.util.Check; import org.compiere.util.CCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -23,12 +24,13 @@ import de.metas.ui.web.window.descriptor.factory.DocumentDescriptorFactory; import de.metas.ui.web.window.descriptor.filters.DocumentFilterDescriptor; import de.metas.ui.web.window.descriptor.filters.DocumentFilterDescriptorsProvider; +import de.metas.ui.web.window.descriptor.sql.DocumentFieldValueLoader; import de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor; import de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor; -import de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor.DocumentFieldValueLoader; import de.metas.ui.web.window.model.DocumentReference; import de.metas.ui.web.window.model.DocumentReferencesService; import de.metas.ui.web.window.model.filters.DocumentFilter; +import lombok.NonNull; import lombok.Value; /* @@ -169,7 +171,6 @@ private static final SqlViewRowFieldBinding createViewFieldBinding(final SqlDocu { final String fieldName = documentField.getFieldName(); final boolean isDisplayColumnAvailable = documentField.isUsingDisplayColumn() && availableDisplayColumnNames.contains(fieldName); - final SqlViewRowFieldLoader fieldLoader = createViewRowFieldLoader(documentField.getDocumentFieldValueLoader(), isDisplayColumnAvailable); return SqlViewRowFieldBinding.builder() .fieldName(fieldName) @@ -185,24 +186,24 @@ private static final SqlViewRowFieldBinding createViewFieldBinding(final SqlDocu // .sqlOrderBy(documentField.getSqlOrderBy()) // - .fieldLoader(fieldLoader) + .fieldLoader(new DocumentFieldValueLoaderAsSqlViewRowFieldLoader(documentField.getDocumentFieldValueLoader(), isDisplayColumnAvailable)) // .build(); } - /** - * NOTE to developer: keep this method static and provide only primitive or lambda parameters - * - * @param fieldValueLoader - * @param isDisplayColumnAvailable - */ - private static SqlViewRowFieldLoader createViewRowFieldLoader(final DocumentFieldValueLoader fieldValueLoader, final boolean isDisplayColumnAvailable) + @Value + private static final class DocumentFieldValueLoaderAsSqlViewRowFieldLoader implements SqlViewRowFieldLoader { - Check.assumeNotNull(fieldValueLoader, "Parameter fieldValueLoader is not null"); - return rs -> { - final Object fieldValue = fieldValueLoader.retrieveFieldValue(rs, isDisplayColumnAvailable); + private final @NonNull DocumentFieldValueLoader fieldValueLoader; + private final boolean isDisplayColumnAvailable; + + @Override + public Object retrieveValueAsJson(ResultSet rs, String adLanguage) throws SQLException + { + final Object fieldValue = fieldValueLoader.retrieveFieldValue(rs, isDisplayColumnAvailable, adLanguage); return Values.valueToJsonObject(fieldValue); - }; + } + } } diff --git a/src/main/java/de/metas/ui/web/view/ViewEvaluationCtx.java b/src/main/java/de/metas/ui/web/view/ViewEvaluationCtx.java index 43ae69afc..944ca3910 100644 --- a/src/main/java/de/metas/ui/web/view/ViewEvaluationCtx.java +++ b/src/main/java/de/metas/ui/web/view/ViewEvaluationCtx.java @@ -64,6 +64,11 @@ public String toString() .add("permissionsKey", permissionsKey) .toString(); } + + public String getAD_Language() + { + return adLanguage; + } public UserRolePermissionsKey getPermissionsKey() { diff --git a/src/main/java/de/metas/ui/web/view/descriptor/SqlViewRowFieldBinding.java b/src/main/java/de/metas/ui/web/view/descriptor/SqlViewRowFieldBinding.java index 0796275ef..1b469ff1d 100644 --- a/src/main/java/de/metas/ui/web/view/descriptor/SqlViewRowFieldBinding.java +++ b/src/main/java/de/metas/ui/web/view/descriptor/SqlViewRowFieldBinding.java @@ -40,7 +40,7 @@ public class SqlViewRowFieldBinding implements SqlEntityFieldBinding @FunctionalInterface public static interface SqlViewRowFieldLoader { - Object retrieveValueAsJson(ResultSet rs) throws SQLException; + Object retrieveValueAsJson(ResultSet rs, String adLanguage) throws SQLException; } private final String fieldName; diff --git a/src/main/java/de/metas/ui/web/window/descriptor/sql/DocumentFieldValueLoader.java b/src/main/java/de/metas/ui/web/window/descriptor/sql/DocumentFieldValueLoader.java new file mode 100644 index 000000000..996eb6f5f --- /dev/null +++ b/src/main/java/de/metas/ui/web/window/descriptor/sql/DocumentFieldValueLoader.java @@ -0,0 +1,37 @@ +package de.metas.ui.web.window.descriptor.sql; + +import java.sql.ResultSet; +import java.sql.SQLException; + +/* + * #%L + * metasfresh-webui-api + * %% + * Copyright (C) 2017 metas GmbH + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ + +/** + * Retrieves a particular field from given {@link ResultSet}. + * + * To create specific instances of this interface, please use {@link DocumentFieldValueLoaders}. + */ +@FunctionalInterface +public interface DocumentFieldValueLoader +{ + Object retrieveFieldValue(ResultSet rs, boolean isDisplayColumnAvailable, String adLanguage) throws SQLException; +} diff --git a/src/main/java/de/metas/ui/web/window/descriptor/sql/DocumentFieldValueLoaders.java b/src/main/java/de/metas/ui/web/window/descriptor/sql/DocumentFieldValueLoaders.java new file mode 100644 index 000000000..a17a53782 --- /dev/null +++ b/src/main/java/de/metas/ui/web/window/descriptor/sql/DocumentFieldValueLoaders.java @@ -0,0 +1,336 @@ +package de.metas.ui.web.window.descriptor.sql; + +import java.math.BigDecimal; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; + +import org.adempiere.util.Check; +import org.adempiere.util.NumberUtils; +import org.compiere.util.DisplayType; +import org.compiere.util.SecureEngine; +import org.slf4j.Logger; + +import de.metas.i18n.ImmutableTranslatableString; +import de.metas.logging.LogManager; +import de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue; +import de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue; +import lombok.Value; + +/* + * #%L + * metasfresh-webui-api + * %% + * Copyright (C) 2017 metas GmbH + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ + +/** + * Factory methods to create specific {@link DocumentFieldValueLoader} instances. + * + * @author metas-dev + * + */ +public final class DocumentFieldValueLoaders +{ + public static final DocumentFieldValueLoader toString(final String sqlColumnName, final boolean encrypted) + { + if (encrypted) + { + return new EncryptedStringDocumentFieldValueLoader(sqlColumnName); + } + else + { + return new StringDocumentFieldValueLoader(sqlColumnName); + } + } + + public static final DocumentFieldValueLoader toByteArray(final String sqlColumnName, final boolean encrypted) + { + return new ByteArrayDocumentFieldValueLoader(sqlColumnName, encrypted); + } + + public static final DocumentFieldValueLoader toBoolean(final String sqlColumnName, final boolean encrypted) + { + return new BooleanDocumentFieldValueLoader(sqlColumnName, encrypted); + } + + public static final DocumentFieldValueLoader toDate(final String sqlColumnName, final boolean encrypted) + { + return new DateDocumentFieldValueLoader(sqlColumnName, encrypted); + } + + public static final DocumentFieldValueLoader toBigDecimal(final String sqlColumnName, final boolean encrypted, final Integer precision) + { + if (precision != null) + { + return new BigDecimalWithPrecisionDocumentFieldValueLoader(sqlColumnName, encrypted, precision); + } + else + { + return new BigDecimalDocumentFieldValueLoader(sqlColumnName, encrypted); + } + } + + public static final DocumentFieldValueLoader toInteger(final String sqlColumnName, final boolean encrypted) + { + return new IntegerDocumentFieldValueLoader(sqlColumnName, encrypted); + } + + public static final DocumentFieldValueLoader toLookupValue(final String sqlColumnName, final String sqlDisplayColumnName, final boolean numericKey) + { + if (Check.isEmpty(sqlDisplayColumnName, true)) + { + throw new IllegalArgumentException("sqlDisplayColumnName shall not be null or empty"); + } + + if (numericKey) + { + return new IntegerLookupValueDocumentFieldValueLoader(sqlColumnName, sqlDisplayColumnName); + } + else + { + return new StringLookupValueDocumentFieldValueLoader(sqlColumnName, sqlDisplayColumnName); + } + } + + // + // + // + // + // + + private static final transient Logger logger = LogManager.getLogger(DocumentFieldValueLoaders.class); + + private DocumentFieldValueLoaders() + { + } + + private static final Object decrypt(final Object value) + { + if (value == null) + { + return null; + } + return SecureEngine.decrypt(value); + } + + @Value + private static final class StringDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + final String value = rs.getString(sqlColumnName); + return value; + } + } + + @Value + private static final class EncryptedStringDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + final String value = rs.getString(sqlColumnName); + return decrypt(value); + } + } + + @Value + private static final class ByteArrayDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + private final boolean encrypted; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + final Object valueObj = rs.getObject(sqlColumnName); + final byte[] valueBytes; + if (rs.wasNull()) + { + valueBytes = null; + } + else if (valueObj instanceof Clob) + { + final Clob lob = (Clob)valueObj; + final long length = lob.length(); + valueBytes = lob.getSubString(1, (int)length).getBytes(); + } + else if (valueObj instanceof Blob) + { + final Blob lob = (Blob)valueObj; + final long length = lob.length(); + valueBytes = lob.getBytes(1, (int)length); + } + else if (valueObj instanceof String) + { + valueBytes = ((String)valueObj).getBytes(); + } + else if (valueObj instanceof byte[]) + { + valueBytes = (byte[])valueObj; + } + else + { + logger.warn("Unknown LOB value '{}' for {}. Considering it null.", valueObj, sqlColumnName); + valueBytes = null; + } + // + return valueBytes; + } + } + + @Value + private static final class BooleanDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + private final boolean encrypted; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + String valueStr = rs.getString(sqlColumnName); + if (encrypted) + { + valueStr = valueStr == null ? null : decrypt(valueStr).toString(); + } + + return DisplayType.toBoolean(valueStr); + } + } + + @Value + private static final class DateDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + private final boolean encrypted; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + final Timestamp valueTS = rs.getTimestamp(sqlColumnName); + final java.util.Date value = valueTS == null ? null : new java.util.Date(valueTS.getTime()); + return encrypted ? decrypt(value) : value; + } + } + + @Value + private static final class BigDecimalDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + private final boolean encrypted; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + final BigDecimal value = rs.getBigDecimal(sqlColumnName); + return encrypted ? decrypt(value) : value; + } + } + + @Value + private static final class BigDecimalWithPrecisionDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + private final boolean encrypted; + private final int precision; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + BigDecimal value = rs.getBigDecimal(sqlColumnName); + value = value == null ? null : NumberUtils.setMinimumScale(value, precision); + return encrypted ? decrypt(value) : value; + } + } + + @Value + private static final class IntegerDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + private final boolean encrypted; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + final int valueInt = rs.getInt(sqlColumnName); + final Integer value = rs.wasNull() ? null : valueInt; + return encrypted ? decrypt(value) : value; + } + } + + @Value + private static final class IntegerLookupValueDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + private final String sqlDisplayColumnName; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + final int id = rs.getInt(sqlColumnName); + if (rs.wasNull()) + { + return null; + } + if (isDisplayColumnAvailable) + { + final String displayName = rs.getString(sqlDisplayColumnName); + return IntegerLookupValue.of(id, ImmutableTranslatableString.singleLanguage(adLanguage, displayName)); + } + else + { + return IntegerLookupValue.unknown(id); + } + } + } + + @Value + private static final class StringLookupValueDocumentFieldValueLoader implements DocumentFieldValueLoader + { + private final String sqlColumnName; + private final String sqlDisplayColumnName; + + @Override + public Object retrieveFieldValue(final ResultSet rs, final boolean isDisplayColumnAvailable, final String adLanguage) throws SQLException + { + final String key = rs.getString(sqlColumnName); + if (rs.wasNull()) + { + return null; + } + if (isDisplayColumnAvailable) + { + final String displayName = rs.getString(sqlDisplayColumnName); + return StringLookupValue.of(key, ImmutableTranslatableString.singleLanguage(adLanguage, displayName)); + } + else + { + return StringLookupValue.unknown(key); + } + } + } + +} diff --git a/src/main/java/de/metas/ui/web/window/descriptor/sql/SqlDocumentFieldDataBindingDescriptor.java b/src/main/java/de/metas/ui/web/window/descriptor/sql/SqlDocumentFieldDataBindingDescriptor.java index 016a34f6e..ff82215fc 100644 --- a/src/main/java/de/metas/ui/web/window/descriptor/sql/SqlDocumentFieldDataBindingDescriptor.java +++ b/src/main/java/de/metas/ui/web/window/descriptor/sql/SqlDocumentFieldDataBindingDescriptor.java @@ -1,28 +1,14 @@ package de.metas.ui.web.window.descriptor.sql; -import java.math.BigDecimal; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Timestamp; import java.util.Optional; import org.adempiere.ad.expression.api.IStringExpression; import org.adempiere.ad.expression.api.NullStringExpression; import org.adempiere.ad.expression.api.impl.ConstantStringExpression; import org.adempiere.util.Check; -import org.adempiere.util.NumberUtils; -import org.compiere.util.DisplayType; -import org.compiere.util.SecureEngine; -import org.slf4j.Logger; import com.google.common.base.MoreObjects; -import de.metas.i18n.ImmutableTranslatableString; -import de.metas.logging.LogManager; -import de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue; -import de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue; import de.metas.ui.web.window.descriptor.DocumentFieldDataBindingDescriptor; import de.metas.ui.web.window.descriptor.DocumentFieldWidgetType; import de.metas.ui.web.window.descriptor.LookupDescriptor; @@ -81,8 +67,6 @@ public static final SqlDocumentFieldDataBindingDescriptor castOrNull(final Docum return null; } - private static final transient Logger logger = LogManager.getLogger(SqlDocumentFieldDataBindingDescriptor.class); - private final String fieldName; private final String sqlTableName; @@ -291,23 +275,7 @@ public IStringExpression getSqlOrderBy() final IStringExpression orderByExpr = isUsingDisplayColumn() ? getDisplayColumnSqlExpression() : ConstantStringExpression.of(getColumnSql()); return orderByExpr; } - - /** - * Retrieves a particular field from given {@link ResultSet}. - */ - @FunctionalInterface - public static interface DocumentFieldValueLoader - { - Object retrieveFieldValue(ResultSet rs, boolean isDisplayColumnAvailable, String adLanguage) throws SQLException; - - default Object retrieveFieldValue(ResultSet rs, boolean isDisplayColumnAvailable) throws SQLException - { - String adLanguage = null; - return retrieveFieldValue(rs, isDisplayColumnAvailable, adLanguage); - } - - } - + public static final class Builder { private String fieldName; @@ -439,165 +407,41 @@ private static final DocumentFieldValueLoader createDocumentFieldValueLoader( , final Boolean numericKey // ) { - final Logger logger = SqlDocumentFieldDataBindingDescriptor.logger; // yes, we can share the static logger - if (!Check.isEmpty(displayColumnName)) { - if (numericKey) - { - return (rs, isDisplayColumnAvailable, adLanguage) -> { - final int id = rs.getInt(sqlColumnName); - if (rs.wasNull()) - { - return null; - } - if (isDisplayColumnAvailable) - { - final String displayName = rs.getString(displayColumnName); - return IntegerLookupValue.of(id, ImmutableTranslatableString.singleLanguage(adLanguage, displayName)); - } - else - { - return IntegerLookupValue.unknown(id); - } - }; - } - else - { - return (rs, isDisplayColumnAvailable, adLanguage) -> { - final String key = rs.getString(sqlColumnName); - if (rs.wasNull()) - { - return null; - } - if (isDisplayColumnAvailable) - { - final String displayName = rs.getString(displayColumnName); - return StringLookupValue.of(key, ImmutableTranslatableString.singleLanguage(adLanguage, displayName)); - } - else - { - return StringLookupValue.unknown(key); - } - - }; - } + return DocumentFieldValueLoaders.toLookupValue(sqlColumnName, displayColumnName, numericKey); } else if (java.lang.String.class == valueClass) { - if (encrypted) - { - return (rs, isDisplayColumnAvailable, adLanguage) -> decrypt(rs.getString(sqlColumnName)); - } - else - { - return (rs, isDisplayColumnAvailable, adLanguage) -> rs.getString(sqlColumnName); - } + return DocumentFieldValueLoaders.toString(sqlColumnName, encrypted); } else if (java.lang.Integer.class == valueClass) { - return (rs, isDisplayColumnAvailable, adLanguage) -> { - final int valueInt = rs.getInt(sqlColumnName); - final Integer value = rs.wasNull() ? null : valueInt; - return encrypted ? decrypt(value) : value; - }; + return DocumentFieldValueLoaders.toInteger(sqlColumnName, encrypted); } else if (java.math.BigDecimal.class == valueClass) { final Integer precision = widgetType.getStandardNumberPrecision(); - if (precision != null) - { - final int precisionInt = precision; - return (rs, isDisplayColumnAvailable, adLanguage) -> { - BigDecimal value = rs.getBigDecimal(sqlColumnName); - value = value == null ? null : NumberUtils.setMinimumScale(value, precisionInt); - return encrypted ? decrypt(value) : value; - }; - } - else - { - return (rs, isDisplayColumnAvailable, adLanguage) -> { - final BigDecimal value = rs.getBigDecimal(sqlColumnName); - return encrypted ? decrypt(value) : value; - }; - } + return DocumentFieldValueLoaders.toBigDecimal(sqlColumnName, encrypted, precision); } else if (java.util.Date.class.isAssignableFrom(valueClass)) { - return (rs, isDisplayColumnAvailable, adLanguage) -> { - final Timestamp valueTS = rs.getTimestamp(sqlColumnName); - final java.util.Date value = valueTS == null ? null : new java.util.Date(valueTS.getTime()); - return encrypted ? decrypt(value) : value; - }; + return DocumentFieldValueLoaders.toDate(sqlColumnName, encrypted); } // YesNo else if (Boolean.class == valueClass) { - return (rs, isDisplayColumnAvailable, adLanguage) -> { - String valueStr = rs.getString(sqlColumnName); - if (encrypted) - { - valueStr = valueStr == null ? null : decrypt(valueStr).toString(); - } - - return DisplayType.toBoolean(valueStr); - }; + return DocumentFieldValueLoaders.toBoolean(sqlColumnName, encrypted); } // LOB else if (byte[].class == valueClass) { - return (rs, isDisplayColumnAvailable, adLanguage) -> { - final Object valueObj = rs.getObject(sqlColumnName); - final byte[] valueBytes; - if (rs.wasNull()) - { - valueBytes = null; - } - else if (valueObj instanceof Clob) - { - final Clob lob = (Clob)valueObj; - final long length = lob.length(); - valueBytes = lob.getSubString(1, (int)length).getBytes(); - } - else if (valueObj instanceof Blob) - { - final Blob lob = (Blob)valueObj; - final long length = lob.length(); - valueBytes = lob.getBytes(1, (int)length); - } - else if (valueObj instanceof String) - { - valueBytes = ((String)valueObj).getBytes(); - } - else if (valueObj instanceof byte[]) - { - valueBytes = (byte[])valueObj; - } - else - { - logger.warn("Unknown LOB value '{}' for {}. Considering it null.", valueObj, sqlColumnName); - valueBytes = null; - } - // - return valueBytes; - }; + return DocumentFieldValueLoaders.toByteArray(sqlColumnName, encrypted); } else { - return (rs, isDisplayColumnAvailable, adLanguage) -> { - final String value = rs.getString(sqlColumnName); - return encrypted ? decrypt(value) : value; - }; - } - } - - private static final Object decrypt(final Object value) - { - if (value == null) - { - return null; + return DocumentFieldValueLoaders.toString(sqlColumnName, encrypted); } - return SecureEngine.decrypt(value); } public Builder setFieldName(final String fieldName) diff --git a/src/main/java/de/metas/ui/web/window/model/sql/SqlDocumentsRepository.java b/src/main/java/de/metas/ui/web/window/model/sql/SqlDocumentsRepository.java index d449154c4..2da52d0f5 100644 --- a/src/main/java/de/metas/ui/web/window/model/sql/SqlDocumentsRepository.java +++ b/src/main/java/de/metas/ui/web/window/model/sql/SqlDocumentsRepository.java @@ -42,9 +42,9 @@ import de.metas.ui.web.window.descriptor.DocumentFieldDataBindingDescriptor; import de.metas.ui.web.window.descriptor.DocumentFieldDescriptor; import de.metas.ui.web.window.descriptor.DocumentFieldWidgetType; +import de.metas.ui.web.window.descriptor.sql.DocumentFieldValueLoader; import de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor; import de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor; -import de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor.DocumentFieldValueLoader; import de.metas.ui.web.window.exceptions.DocumentNotFoundException; import de.metas.ui.web.window.model.Document; import de.metas.ui.web.window.model.Document.DocumentValuesSupplier;