Skip to content

Commit

Permalink
merge HUEditorRowQuery to HUEditorRowFilter; refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Nov 12, 2017
1 parent d9e7a32 commit 70fb3b0
Show file tree
Hide file tree
Showing 17 changed files with 376 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import org.adempiere.ad.dao.IQueryFilter;
import org.adempiere.ad.dao.impl.TypedSqlQueryFilter;
import org.compiere.util.DB;

import de.metas.printing.esb.base.util.Check;
Expand Down Expand Up @@ -80,4 +82,11 @@ default String getSql(

return sqlWhereClauseBuilder.toString();
}

default <T> IQueryFilter<T> createQueryFilter(@NonNull final List<DocumentFilter> filters, @NonNull final SqlOptions sqlOpts)
{
final SqlParamsCollector sqlFilterParams = SqlParamsCollector.newInstance();
final String sqlFilter = getSql(sqlFilterParams, filters, SqlOptions.defaults());
return TypedSqlQueryFilter.of(sqlFilter, sqlFilterParams.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package de.metas.ui.web.handlingunits;

import java.util.Set;
import java.util.stream.Stream;

import org.adempiere.ad.dao.IQueryBL;
import org.adempiere.util.Services;

import de.metas.handlingunits.IHUQueryBuilder;
import de.metas.handlingunits.IHandlingUnitsDAO;
import de.metas.handlingunits.model.I_M_HU;
import de.metas.ui.web.handlingunits.HUEditorRowFilter.Select;
import de.metas.ui.web.process.adprocess.ViewBasedProcessTemplate;
Expand Down Expand Up @@ -59,9 +57,12 @@ protected final HUEditorRow getSingleSelectedRow()
protected final Stream<HUEditorRow> streamSelectedRows(@NonNull final HUEditorRowFilter filter)
{
final DocumentIdsSelection selectedDocumentIds = getSelectedDocumentIds();
return getView()
.streamByIds(selectedDocumentIds)
.filter(row -> matches(row, filter));
if (selectedDocumentIds.isEmpty())
{
return Stream.empty();
}

return getView().streamByIds(filter.andOnlyRowIds(selectedDocumentIds));
}

protected final Stream<Integer> streamSelectedHUIds(@NonNull final Select select)
Expand Down Expand Up @@ -90,37 +91,12 @@ protected final Stream<I_M_HU> streamSelectedHUs(@NonNull final Select select)

protected final Stream<I_M_HU> streamSelectedHUs(@NonNull final HUEditorRowFilter filter)
{
final Stream<Integer> huIds = streamSelectedHUIds(HUEditorRowFilter.ALL);
final Stream<Integer> huIds = streamSelectedHUIds(filter);
return StreamUtils.dice(huIds, 100)
.flatMap(huIdsChunk -> toHUQueryBuilder(filter)
.addOnlyHUIds(huIdsChunk)
.createQuery()
.flatMap(huIdsChunk -> Services.get(IQueryBL.class)
.createQueryBuilder(I_M_HU.class)
.addInArrayFilter(I_M_HU.COLUMNNAME_M_HU_ID, huIdsChunk)
.create()
.stream(I_M_HU.class));
}

private static final IHUQueryBuilder toHUQueryBuilder(final HUEditorRowFilter filter)
{
return Services.get(IHandlingUnitsDAO.class)
.createHUQueryBuilder()
.setOnlyTopLevelHUs(filter.getSelect() == Select.ONLY_TOPLEVEL)
.addHUStatusesToInclude(filter.getOnlyHUStatuses());
}

private static final boolean matches(final HUEditorRow row, final HUEditorRowFilter filter)
{
final Select select = filter.getSelect();
if (select == Select.ONLY_TOPLEVEL && !row.isTopLevel())
{
return false;
}

final Set<String> onlyHUStatuses = filter.getOnlyHUStatuses();
final boolean huStatusDoesntMatter = onlyHUStatuses.isEmpty();
if (huStatusDoesntMatter)
{
return true;
}

return onlyHUStatuses.contains(row.getHUStatusKey());
}
}
120 changes: 105 additions & 15 deletions src/main/java/de/metas/ui/web/handlingunits/HUEditorRowFilter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package de.metas.ui.web.handlingunits;

import java.util.Set;

import javax.annotation.Nullable;

import org.adempiere.exceptions.AdempiereException;

import com.google.common.collect.ImmutableSet;

import de.metas.handlingunits.model.X_M_HU;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import lombok.NonNull;
import lombok.Singular;

Expand All @@ -16,46 +23,129 @@
* 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
* 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
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@lombok.Builder
@lombok.Value
public final class HUEditorRowFilter
{
@NonNull
private final Select select;
public static final HUEditorRowFilter ALL = builder().select(Select.ALL).build();

@Singular
private final ImmutableSet<String> onlyHUStatuses;
public static final HUEditorRowFilter select(final Select select)
{
return builder().select(select).build();
}

public static final HUEditorRowFilter onlyRowIds(DocumentIdsSelection rowIds)
{
if (rowIds.isAll())
{
return ALL;
}
else
{
return builder().onlyRowIdsFromSelection(rowIds).build();
}
}

private final Select select;

public enum Select
{
ONLY_TOPLEVEL, ALL
ONLY_TOPLEVEL, ALL, LU, TU
}

public static final HUEditorRowFilter ALL = builder().select(Select.ALL).build();
private final ImmutableSet<HUEditorRowId> onlyRowIds;

private final ImmutableSet<String> onlyHUStatuses;

public static final HUEditorRowFilter select(Select select)
/**
* Note: this list is never {@code null}, empty means "no restriction".
*/
private final ImmutableSet<String> excludeHUStatuses;

/**
* M_HU_IDs to exclude.
* The HUs are excluded AFTER they are exploded,
* so if you exclude an M_HU_ID then you will not get it in the result but it's children will be part of the result.
*
* Note: this list is never {@code null}, empty means "no restriction".
*/
private final ImmutableSet<Integer> excludeHUIds;

/** User string filter (e.g. what user typed in the lookup field) */
private final String userInputFilter;

@lombok.Builder(builderClassName = "Builder", toBuilder = true)
private HUEditorRowFilter(
@Nullable final Select select,
@NonNull @Singular final Set<HUEditorRowId> onlyRowIds,
@NonNull @Singular final ImmutableSet<String> onlyHUStatuses,
@NonNull @Singular final ImmutableSet<String> excludeHUStatuses,
@NonNull @Singular final ImmutableSet<Integer> excludeHUIds,
@Nullable final String userInputFilter)
{
return builder().select(select).build();
this.select = select != null ? select : Select.ALL;
this.onlyRowIds = ImmutableSet.copyOf(onlyRowIds);
this.onlyHUStatuses = onlyHUStatuses;
this.excludeHUStatuses = excludeHUStatuses;
this.userInputFilter = userInputFilter;

if (excludeHUIds == null || excludeHUIds.isEmpty())
{
this.excludeHUIds = ImmutableSet.of();
}
else
{
this.excludeHUIds = excludeHUIds.stream().filter(huId -> huId > 0).collect(ImmutableSet.toImmutableSet());
}
}

public static final class HUEditorRowFilterBuilder
public HUEditorRowFilter andOnlyRowIds(final DocumentIdsSelection rowIds)
{
public HUEditorRowFilterBuilder onlyActiveHUs()
if (rowIds.isAll())
{
// nothing to do
return this;
}
else
{
return toBuilder().onlyRowIdsFromSelection(rowIds).build();
}
}

public static final class Builder
{
public Builder onlyActiveHUs()
{
onlyHUStatus(X_M_HU.HUSTATUS_Active);
return this;
}

public Builder onlyRowIdsFromSelection(DocumentIdsSelection rowIds)
{
if (rowIds.isAll())
{
// nothing
}
else if (rowIds.isEmpty())
{
throw new AdempiereException("Empty rowIds is not allowed");
}
else
{
onlyRowIds(rowIds.toSet(HUEditorRowId::ofDocumentId));
}
return this;
}
}
}
Loading

0 comments on commit 70fb3b0

Please sign in to comment.