Skip to content

Commit

Permalink
refactor DocumentFieldValueLoader(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed May 2, 2017
1 parent c5cf47f commit 9a3933e
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 186 deletions.
1 change: 1 addition & 0 deletions src/main/java/de/metas/ui/web/view/DefaultView.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ public void notifyRecordsChanged(final Set<TableRecordReference> 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);
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/de/metas/ui/web/view/SqlViewDataRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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)
{
Expand Down Expand Up @@ -249,6 +250,7 @@ public List<IViewRow> 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;
Expand All @@ -268,7 +270,7 @@ public List<IViewRow> retrievePage(final ViewEvaluationCtx viewEvalCtx, final Vi
final ImmutableList.Builder<IViewRow> pageBuilder = ImmutableList.builder();
while (rs.next())
{
final IViewRow row = loadViewRow(rs, windowId);
final IViewRow row = loadViewRow(rs, windowId, adLanguage);
if (row == null)
{
continue;
Expand Down
31 changes: 16 additions & 15 deletions src/main/java/de/metas/ui/web/view/SqlViewFactory.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/*
Expand Down Expand Up @@ -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)
Expand All @@ -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);
};
}

}
}
5 changes: 5 additions & 0 deletions src/main/java/de/metas/ui/web/view/ViewEvaluationCtx.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public String toString()
.add("permissionsKey", permissionsKey)
.toString();
}

public String getAD_Language()
{
return adLanguage;
}

public UserRolePermissionsKey getPermissionsKey()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #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;
}
Loading

0 comments on commit 9a3933e

Please sign in to comment.