Skip to content

Commit

Permalink
decouple SqlViewBinding from SqlDocumentEntityDataBindingDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Apr 29, 2017
1 parent 8dc23a1 commit 079f428
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 192 deletions.
74 changes: 67 additions & 7 deletions src/main/java/de/metas/ui/web/view/SqlViewFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
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;

import de.metas.ui.web.view.descriptor.SqlViewBinding;
import de.metas.ui.web.view.descriptor.SqlViewRowFieldBinding;
import de.metas.ui.web.view.descriptor.SqlViewRowFieldBinding.SqlViewRowFieldLoader;
import de.metas.ui.web.view.descriptor.ViewLayout;
import de.metas.ui.web.view.json.JSONViewDataType;
import de.metas.ui.web.window.datatypes.DocumentPath;
import de.metas.ui.web.window.datatypes.Values;
import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.descriptor.DocumentEntityDescriptor;
import de.metas.ui.web.window.descriptor.DocumentFieldDescriptor.Characteristic;
import de.metas.ui.web.window.descriptor.DocumentLayoutDescriptor;
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.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;
Expand Down Expand Up @@ -45,6 +52,12 @@
* #L%
*/

/**
* View factory which is based on {@link DocumentEntityDescriptor} having SQL repository.
*
* @author metas-dev <dev@metasfresh.com>
*
*/
@Service
public class SqlViewFactory implements IViewFactory
{
Expand All @@ -53,6 +66,14 @@ public class SqlViewFactory implements IViewFactory
@Autowired
private DocumentReferencesService documentReferencesService;

@Value
private static final class SqlViewBindingKey
{
private final WindowId windowId;
private final Characteristic requiredFieldCharacteristic;
}

//
private final transient CCache<SqlViewBindingKey, SqlViewBinding> viewBindings = CCache.newCache("SqlViewBindings", 20, 0);

@Override
Expand Down Expand Up @@ -125,20 +146,59 @@ private SqlViewBinding createViewBinding(final SqlViewBindingKey key)

final SqlDocumentEntityDataBindingDescriptor entityBinding = SqlDocumentEntityDataBindingDescriptor.cast(entityDescriptor.getDataBinding());

return SqlViewBinding.builder()
final SqlViewBinding.Builder builder = SqlViewBinding.builder()
.setTableName(entityBinding.getTableName())
.setTableAlias(entityBinding.getTableAlias())
.setDisplayFieldNames(displayFieldNames)
.addFields(entityBinding.getFields())
.setFilterDescriptors(entityDescriptor.getFiltersProvider())
.setOrderBys(entityBinding.getOrderBys())
.setOrderBys(entityBinding.getDefaultOrderBys());

entityBinding.getFields()
.stream()
.map(documentField -> createViewFieldBinding(documentField, displayFieldNames))
.forEach(fieldBinding -> builder.addField(fieldBinding));

return builder.build();
}

private static final SqlViewRowFieldBinding createViewFieldBinding(final SqlDocumentFieldDataBindingDescriptor documentField, final Collection<String> availableDisplayColumnNames)
{
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)
.columnName(documentField.getColumnName())
.columnSql(documentField.getColumnSql())
.keyColumn(documentField.isKeyColumn())
.widgetType(documentField.getWidgetType())
//
.sqlValueClass(documentField.getSqlValueClass())
.sqlSelectValue(documentField.getSqlSelectValue())
.usingDisplayColumn(isDisplayColumnAvailable)
.sqlSelectDisplayValue(isDisplayColumnAvailable ? documentField.getSqlSelectDisplayValue() : NullStringExpression.instance)
//
.sqlOrderBy(documentField.getSqlOrderBy())
//
.fieldLoader(fieldLoader)
//
.build();

}

@Value
private static final class SqlViewBindingKey
/**
* 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)
{
private final WindowId windowId;
private final Characteristic requiredFieldCharacteristic;
Check.assumeNotNull(fieldValueLoader, "Parameter fieldValueLoader is not null");
return rs -> {
final Object fieldValue = fieldValueLoader.retrieveFieldValue(rs, isDisplayColumnAvailable);
return Values.valueToJsonObject(fieldValue);
};
}
}
60 changes: 16 additions & 44 deletions src/main/java/de/metas/ui/web/view/descriptor/SqlViewBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.descriptor.filters.DocumentFilterDescriptorsProvider;
import de.metas.ui.web.window.descriptor.filters.NullDocumentFilterDescriptorsProvider;
import de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor;
import de.metas.ui.web.window.descriptor.sql.SqlEntityBinding;
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import de.metas.ui.web.window.model.filters.DocumentFilter;
import de.metas.ui.web.window.model.sql.SqlDocumentFiltersBuilder;
import de.metas.ui.web.window.model.sql.SqlDocumentOrderByBuilder;
import lombok.NonNull;

/*
Expand Down Expand Up @@ -97,7 +97,7 @@ public static final Builder builder()
private final IStringExpression sqlSelectById;
private final List<SqlViewRowFieldLoader> rowFieldLoaders;

private final List<DocumentQueryOrderBy> orderBys;
private final List<DocumentQueryOrderBy> defaultOrderBys;
private final DocumentFilterDescriptorsProvider filterDescriptors;

private SqlViewBinding(final Builder builder)
Expand Down Expand Up @@ -144,7 +144,7 @@ private SqlViewBinding(final Builder builder)
}
this.rowFieldLoaders = ImmutableList.copyOf(rowFieldLoaders);

orderBys = ImmutableList.copyOf(builder.getOrderBys());
defaultOrderBys = ImmutableList.copyOf(builder.getDefaultOrderBys());
filterDescriptors = builder.getFilterDescriptors();
}

Expand Down Expand Up @@ -260,22 +260,6 @@ private static IStringExpression buildSqlSelect( //
}
}

private final IStringExpression buildSqlOrderBy(final List<DocumentQueryOrderBy> orderBys)
{
if (orderBys.isEmpty())
{
return null;
}

final IStringExpression sqlOrderByFinal = orderBys
.stream()
.map(orderBy -> getFieldByFieldName(orderBy.getFieldName()).getSqlOrderBy(orderBy.isAscending()))
.filter(sql -> sql != null && !sql.isNullExpression())
.collect(IStringExpression.collectJoining(", "));

return sqlOrderByFinal;
}

public IStringExpression getSqlSelectByPage()
{
return sqlSelectByPage;
Expand Down Expand Up @@ -313,11 +297,6 @@ public List<SqlViewRowFieldLoader> getRowFieldLoaders()
return rowFieldLoaders;
}

public List<DocumentQueryOrderBy> getOrderBys()
{
return orderBys;
}

public DocumentFilterDescriptorsProvider getFilterDescriptors()
{
return filterDescriptors;
Expand Down Expand Up @@ -349,7 +328,7 @@ public ViewRowIdsOrderedSelection createOrderedSelection( //
//
// SELECT ... FROM ... WHERE 1=1
{
IStringExpression sqlOrderBy = buildSqlOrderBy(orderBys);
IStringExpression sqlOrderBy = SqlDocumentOrderByBuilder.newInstance(this).buildSqlOrderBy(defaultOrderBys);
if (sqlOrderBy.isNullExpression())
{
sqlOrderBy = ConstantStringExpression.of(keyColumnNameFQ);
Expand Down Expand Up @@ -415,7 +394,7 @@ public ViewRowIdsOrderedSelection createOrderedSelection( //
return ViewRowIdsOrderedSelection.builder()
.setViewId(viewId)
.setSize(rowsCount)
.setOrderBys(orderBys)
.setOrderBys(defaultOrderBys)
.setQueryLimit(queryLimit, queryLimitHit)
.build();
}
Expand Down Expand Up @@ -490,7 +469,7 @@ public static final class Builder
private final Map<String, SqlViewRowFieldBinding> _fieldsByFieldName = new LinkedHashMap<>();
private SqlViewRowFieldBinding _keyField;

private List<DocumentQueryOrderBy> orderBys;
private List<DocumentQueryOrderBy> defaultOrderBys;
private DocumentFilterDescriptorsProvider filterDescriptors = NullDocumentFilterDescriptorsProvider.instance;

private Builder()
Expand Down Expand Up @@ -549,35 +528,28 @@ private Map<String, SqlViewRowFieldBinding> getFieldsByFieldName()
{
return _fieldsByFieldName;
}

private final Builder addField(final SqlDocumentFieldDataBindingDescriptor field)
public final Builder addField(SqlViewRowFieldBinding field)
{
final boolean isDisplayColumnAvailable = getDisplayFieldNames().contains(field.getFieldName());
final SqlViewRowFieldBinding rowFieldBinding = SqlViewRowFieldBinding.of(field, isDisplayColumnAvailable);
Check.assumeNotNull(field, "Parameter field is not null");

_fieldsByFieldName.put(rowFieldBinding.getFieldName(), rowFieldBinding);
if (rowFieldBinding.isKeyColumn())
_fieldsByFieldName.put(field.getFieldName(), field);
if (field.isKeyColumn())
{
_keyField = rowFieldBinding;
_keyField = field;
}
return this;
}

public Builder addFields(final Collection<SqlDocumentFieldDataBindingDescriptor> fields)
{
fields.forEach(this::addField);
return this;
}

public Builder setOrderBys(final List<DocumentQueryOrderBy> orderBys)
public Builder setOrderBys(final List<DocumentQueryOrderBy> defaultOrderBys)
{
this.orderBys = orderBys;
this.defaultOrderBys = defaultOrderBys;
return this;
}

private List<DocumentQueryOrderBy> getOrderBys()
private List<DocumentQueryOrderBy> getDefaultOrderBys()
{
return orderBys == null ? ImmutableList.of() : orderBys;
return defaultOrderBys == null ? ImmutableList.of() : defaultOrderBys;
}

public Builder setFilterDescriptors(@NonNull final DocumentFilterDescriptorsProvider filterDescriptors)
Expand Down
Loading

0 comments on commit 079f428

Please sign in to comment.