Skip to content

Commit

Permalink
refactored + rename SqlView to DefaultView
Browse files Browse the repository at this point in the history
note: the DefaultView is free of SQL

#330
  • Loading branch information
teosarca committed May 1, 2017
1 parent 0b0b691 commit 55e6b6b
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ViewLayout getViewLayout(final WindowId windowId, final JSONViewDataType
}

@Override
public Collection<DocumentFilterDescriptor> getViewFilters(final WindowId windowId, final JSONViewDataType viewType)
public Collection<DocumentFilterDescriptor> getViewFilterDescriptors(final WindowId windowId, final JSONViewDataType viewType)
{
return null; // not supported
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ViewLayout getViewLayout(final WindowId windowId, final JSONViewDataType
}

@Override
public Collection<DocumentFilterDescriptor> getViewFilters(final WindowId windowId, final JSONViewDataType viewType)
public Collection<DocumentFilterDescriptor> getViewFilterDescriptors(final WindowId windowId, final JSONViewDataType viewType)
{
return null; // not supported
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@

import javax.annotation.Nullable;

import org.adempiere.ad.dao.IQueryBL;
import org.adempiere.ad.dao.impl.TypedSqlQueryFilter;
import org.adempiere.exceptions.DBException;
import org.adempiere.model.PlainContextAware;
import org.adempiere.util.GuavaCollectors;
import org.adempiere.util.Services;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.compiere.util.CCache;
import org.compiere.util.Env;
Expand All @@ -27,7 +22,6 @@

import de.metas.logging.LogManager;
import de.metas.ui.web.exceptions.EntityNotFoundException;
import de.metas.ui.web.view.descriptor.SqlViewBinding;
import de.metas.ui.web.view.event.ViewChangesCollector;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.LookupValuesList;
Expand Down Expand Up @@ -60,35 +54,38 @@
* #L%
*/

class SqlView implements IView
/**
* Default {@link IView} implementation.
*
* @author metas-dev <dev@metasfresh.com>
*
*/
class DefaultView implements IView
{
public static final Builder builder(final SqlViewBinding sqlViewBinding)
public static final Builder builder(final IViewDataRepository viewDataRepository)
{
return new Builder(sqlViewBinding);
return new Builder(viewDataRepository);
}

private static final Logger logger = LogManager.getLogger(SqlView.class);
private static final Logger logger = LogManager.getLogger(DefaultView.class);

private final IViewDataRepository viewDataRepository;

private final AtomicBoolean closed = new AtomicBoolean(false);
private final ViewId parentViewId;
private final SqlViewRepository viewRowsRepo;

private final transient IViewRowIdsOrderedSelectionFactory orderedSelectionFactory;
private final transient ViewRowIdsOrderedSelection defaultSelection;
private final transient ConcurrentHashMap<ImmutableList<DocumentQueryOrderBy>, ViewRowIdsOrderedSelection> selectionsByOrderBys = new ConcurrentHashMap<>();

//
// Filters
private final transient DocumentFilterDescriptorsProvider filterDescriptors;
private final transient DocumentFilterDescriptorsProvider viewFilterDescriptors;
/** Sticky filters (i.e. active filters which cannot be changed) */
private final List<DocumentFilter> stickyFilters;
/** Active filters */
private final List<DocumentFilter> filters;

//
// Attributes
private final transient IViewRowAttributesProvider attributesProvider;

//
// Misc
private transient String _toString;
Expand All @@ -97,39 +94,35 @@ public static final Builder builder(final SqlViewBinding sqlViewBinding)
// Caching
private final transient CCache<DocumentId, IViewRow> cache_rowsById;

private SqlView(final Builder builder)
private DefaultView(final Builder builder)
{
viewRowsRepo = new SqlViewRepository(builder.getSqlViewBinding());
viewDataRepository = builder.getViewDataRepository();
parentViewId = builder.getParentViewId();

//
// Filters
filterDescriptors = builder.getFilterDescriptors();
viewFilterDescriptors = builder.getViewFilterDescriptors();
stickyFilters = ImmutableList.copyOf(builder.getStickyFilters());
filters = ImmutableList.copyOf(builder.getFilters());

//
// Selection
{
final SqlViewEvaluationCtx evalCtx = SqlViewEvaluationCtx.of(Env.getCtx());
orderedSelectionFactory = viewRowsRepo.createOrderedSelectionFactory(evalCtx);
final ViewEvaluationCtx evalCtx = ViewEvaluationCtx.of(Env.getCtx());
orderedSelectionFactory = viewDataRepository.createOrderedSelectionFactory(evalCtx);

defaultSelection = viewRowsRepo.createOrderedSelection(
defaultSelection = viewDataRepository.createOrderedSelection(
evalCtx //
, builder.getWindowId() //
, ImmutableList.<DocumentFilter> builder().addAll(stickyFilters).addAll(filters).build() //
);
selectionsByOrderBys.put(defaultSelection.getOrderBys(), defaultSelection);
}

//
// Attributes
attributesProvider = ViewRowAttributesProviderFactory.instance.createProviderOrNull(defaultSelection.getWindowId());

//
// Cache
cache_rowsById = CCache.newLRUCache( //
viewRowsRepo.getTableName() + "#rowById#viewId=" + defaultSelection.getSelectionId() // cache name
viewDataRepository.getTableName() + "#rowById#viewId=" + defaultSelection.getSelectionId() // cache name
, 100 // maxSize
, 2 // expireAfterMinutes
);
Expand All @@ -142,14 +135,13 @@ public String toString()
{
if (_toString == null)
{
// NOTE: keep it short
_toString = MoreObjects.toStringHelper(this)
.omitNullValues()
.add("viewId", defaultSelection.getViewId())
.add("tableName", viewRowsRepo.getTableName())
.add("tableName", viewDataRepository.getTableName())
.add("parentViewId", parentViewId)
.add("defaultSelection", defaultSelection)
// .add("sql", sqlSelectPage) // too long..
// .add("fieldLoaders", fieldLoaders) // no point to show them because all are lambdas
.toString();
}
return _toString;
Expand All @@ -170,7 +162,7 @@ public ViewId getViewId()
@Override
public String getTableName()
{
return viewRowsRepo.getTableName();
return viewDataRepository.getTableName();
}

@Override
Expand Down Expand Up @@ -232,14 +224,14 @@ private final void assertNotClosed()
}

@Override
public ViewResult getPage(final int firstRow, final int pageLength, final List<DocumentQueryOrderBy> orderBys) throws DBException
public ViewResult getPage(final int firstRow, final int pageLength, final List<DocumentQueryOrderBy> orderBys)
{
assertNotClosed();

final SqlViewEvaluationCtx evalCtx = SqlViewEvaluationCtx.of(Env.getCtx());
final ViewEvaluationCtx evalCtx = ViewEvaluationCtx.of(Env.getCtx());
final ViewRowIdsOrderedSelection orderedSelection = getOrderedSelection(orderBys);

final List<IViewRow> page = viewRowsRepo.retrievePage(evalCtx, orderedSelection, firstRow, pageLength);
final List<IViewRow> page = viewDataRepository.retrievePage(evalCtx, orderedSelection, firstRow, pageLength);

// Add to cache
page.forEach(row -> cache_rowsById.put(row.getId(), row));
Expand All @@ -257,12 +249,12 @@ public IViewRow getById(final DocumentId rowId)
private final IViewRow getOrRetrieveById(final DocumentId rowId)
{
return cache_rowsById.getOrLoad(rowId, () -> {
final SqlViewEvaluationCtx evalCtx = SqlViewEvaluationCtx.of(Env.getCtx());
return viewRowsRepo.retrieveById(evalCtx, getViewId(), rowId);
final ViewEvaluationCtx evalCtx = ViewEvaluationCtx.of(Env.getCtx());
return viewDataRepository.retrieveById(evalCtx, getViewId(), rowId);
});
}

private ViewRowIdsOrderedSelection getOrderedSelection(final List<DocumentQueryOrderBy> orderBys) throws DBException
private ViewRowIdsOrderedSelection getOrderedSelection(final List<DocumentQueryOrderBy> orderBys)
{
if (orderBys == null || orderBys.isEmpty())
{
Expand All @@ -274,22 +266,21 @@ private ViewRowIdsOrderedSelection getOrderedSelection(final List<DocumentQueryO
return defaultSelection;
}

final ImmutableList<DocumentQueryOrderBy> orderBysImmutable = ImmutableList.copyOf(orderBys);
return selectionsByOrderBys.computeIfAbsent(orderBysImmutable, (newOrderBys) -> orderedSelectionFactory.createFromView(defaultSelection, orderBysImmutable));
return selectionsByOrderBys.computeIfAbsent(ImmutableList.copyOf(orderBys), orderBysImmutable -> orderedSelectionFactory.createFromView(defaultSelection, orderBysImmutable));
}

@Override
public String getSqlWhereClause(final Collection<DocumentId> rowIds)
{
return viewRowsRepo.getSqlWhereClause(getViewId(), rowIds);
return viewDataRepository.getSqlWhereClause(getViewId(), rowIds);
}

@Override
public LookupValuesList getFilterParameterDropdown(final String filterId, final String filterParameterName, final Evaluatee ctx)
{
assertNotClosed();

return filterDescriptors.getByFilterId(filterId)
return viewFilterDescriptors.getByFilterId(filterId)
.getParameterByName(filterParameterName)
.getLookupDataSource()
.findEntities(ctx);
Expand All @@ -300,7 +291,7 @@ public LookupValuesList getFilterParameterTypeahead(final String filterId, final
{
assertNotClosed();

return filterDescriptors.getByFilterId(filterId)
return viewFilterDescriptors.getByFilterId(filterId)
.getParameterByName(filterParameterName)
.getLookupDataSource()
.findEntities(ctx, query);
Expand All @@ -309,7 +300,7 @@ public LookupValuesList getFilterParameterTypeahead(final String filterId, final
@Override
public boolean hasAttributesSupport()
{
return attributesProvider != null;
return false;
}

@Override
Expand Down Expand Up @@ -340,17 +331,7 @@ public Stream<? extends IViewRow> streamByIds(final Collection<DocumentId> rowId
@Override
public <T> List<T> retrieveModelsByIds(final Collection<DocumentId> rowIds, final Class<T> modelClass)
{
if (rowIds.isEmpty())
{
return ImmutableList.of();
}

final String sqlWhereClause = getSqlWhereClause(rowIds);

return Services.get(IQueryBL.class).createQueryBuilder(modelClass, getTableName(), PlainContextAware.createUsingOutOfTransaction())
.filter(new TypedSqlQueryFilter<>(sqlWhereClause))
.create()
.list(modelClass);
return viewDataRepository.retrieveModelsByIds(getViewId(), rowIds, modelClass);
}

@Override
Expand Down Expand Up @@ -385,19 +366,19 @@ public static final class Builder
{
private WindowId windowId;
private ViewId parentViewId;
private final SqlViewBinding sqlViewBinding;
private final IViewDataRepository viewDataRepository;

private List<DocumentFilter> _stickyFilters;
private List<DocumentFilter> _filters;

private Builder(@NonNull final SqlViewBinding sqlViewBinding)
private Builder(@NonNull final IViewDataRepository viewDataRepository)
{
this.sqlViewBinding = sqlViewBinding;
this.viewDataRepository = viewDataRepository;
}

public SqlView build()
public DefaultView build()
{
return new SqlView(this);
return new DefaultView(this);
}

public Builder setParentViewId(final ViewId parentViewId)
Expand All @@ -422,14 +403,14 @@ private ViewId getParentViewId()
return parentViewId;
}

private SqlViewBinding getSqlViewBinding()
private IViewDataRepository getViewDataRepository()
{
return sqlViewBinding;
return viewDataRepository;
}

private DocumentFilterDescriptorsProvider getFilterDescriptors()
private DocumentFilterDescriptorsProvider getViewFilterDescriptors()
{
return sqlViewBinding.getFilterDescriptors();
return viewDataRepository.getViewFilterDescriptors();
}

public Builder setStickyFilter(@Nullable final DocumentFilter stickyFilter)
Expand All @@ -451,7 +432,7 @@ public Builder setFilters(final List<DocumentFilter> filters)

public Builder setFiltersFromJSON(final List<JSONDocumentFilter> jsonFilters)
{
setFilters(JSONDocumentFilter.unwrapList(jsonFilters, getFilterDescriptors()));
setFilters(JSONDocumentFilter.unwrapList(jsonFilters, getViewFilterDescriptors()));
return this;
}

Expand Down
59 changes: 59 additions & 0 deletions src/main/java/de/metas/ui/web/view/IViewDataRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.metas.ui.web.view;

import java.util.Collection;
import java.util.List;

import org.adempiere.exceptions.DBException;

import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.descriptor.filters.DocumentFilterDescriptorsProvider;
import de.metas.ui.web.window.model.filters.DocumentFilter;

/*
* #%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%
*/

/**
* View data repository.
* This repository is responsible for fetching {@link IViewRow} or even their models.
*
* @author metas-dev <dev@metasfresh.com>
*
*/
interface IViewDataRepository
{
String getTableName();

String getSqlWhereClause(ViewId viewId, Collection<DocumentId> rowIds);

DocumentFilterDescriptorsProvider getViewFilterDescriptors();

IViewRow retrieveById(ViewEvaluationCtx viewEvalCtx, ViewId viewId, DocumentId rowId);

List<IViewRow> retrievePage(ViewEvaluationCtx viewEvalCtx, ViewRowIdsOrderedSelection orderedSelection, int firstRow, int pageLength) throws DBException;

<T> List<T> retrieveModelsByIds(ViewId viewId, Collection<DocumentId> rowIds, Class<T> modelClass);

IViewRowIdsOrderedSelectionFactory createOrderedSelectionFactory(ViewEvaluationCtx viewEvalCtx);

ViewRowIdsOrderedSelection createOrderedSelection(ViewEvaluationCtx viewEvalCtx, WindowId windowId, List<DocumentFilter> filters);
}
2 changes: 1 addition & 1 deletion src/main/java/de/metas/ui/web/view/IViewFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IViewFactory
{
ViewLayout getViewLayout(WindowId windowId, JSONViewDataType viewDataType);

Collection<DocumentFilterDescriptor> getViewFilters(WindowId windowId, JSONViewDataType viewDataType);
Collection<DocumentFilterDescriptor> getViewFilterDescriptors(WindowId windowId, JSONViewDataType viewDataType);

IView createView(ViewCreateRequest request);

Expand Down
Loading

0 comments on commit 55e6b6b

Please sign in to comment.