Skip to content

Commit

Permalink
Merge pull request #673 from metasfresh/gh668webui
Browse files Browse the repository at this point in the history
Gh668webui
  • Loading branch information
teosarca authored Nov 13, 2017
2 parents fa84659 + 010ae52 commit 0958391
Show file tree
Hide file tree
Showing 53 changed files with 2,449 additions and 1,651 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,20 +1,15 @@
package de.metas.ui.web.handlingunits;

import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;

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

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

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;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.util.stream.StreamUtils;
import lombok.NonNull;

/*
Expand All @@ -27,12 +22,12 @@
* 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>.
Expand All @@ -41,7 +36,7 @@

/**
* A {@link ViewBasedProcessTemplate} implementation template which add convenient functionalities around {@link HUEditorView}.
*
*
* @author metas-dev <dev@metasfresh.com>
*
*/
Expand All @@ -59,87 +54,49 @@ protected final HUEditorRow getSingleSelectedRow()
return HUEditorRow.cast(super.getSingleSelectedRow());
}

protected final List<HUEditorRow> getSelectedRows(@NonNull final HUEditorRowFilter filter)
protected final Stream<HUEditorRow> streamSelectedRows(@NonNull final HUEditorRowFilter filter)
{
final DocumentIdsSelection selectedDocumentIds = getSelectedDocumentIds();
final List<HUEditorRow> allRows = getView().getByIds(selectedDocumentIds);

return allRows.stream()
.filter(toPredicate(filter))
.collect(ImmutableList.toImmutableList());

if (selectedDocumentIds.isEmpty())
{
return Stream.empty();
}

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

/**
* Calls {@link #getSelectedRows(Select)} and returns the {@link HUEditorRow}s' {@code M_HU_ID}s.
*
* @param select
*/
protected final Set<Integer> getSelectedHUIds(@NonNull final Select select)
protected final Stream<Integer> streamSelectedHUIds(@NonNull final Select select)
{
return getSelectedHUIds(HUEditorRowFilter.select(select));
return streamSelectedHUIds(HUEditorRowFilter.select(select));
}

protected final Set<Integer> getSelectedHUIds(@NonNull final HUEditorRowFilter filter)
protected final Stream<Integer> streamSelectedHUIds(@NonNull final HUEditorRowFilter filter)
{
return getSelectedRows(filter)
.stream()
return streamSelectedRows(filter)
.map(HUEditorRow::getM_HU_ID)
.filter(huId -> huId > 0)
.collect(ImmutableSet.toImmutableSet());
.filter(huId -> huId > 0);
}

/**
* Gets <b>all</b> selected {@link HUEditorRow}s and loads the top level-HUs from those.
* I.e. this method does not rely on {@link HUEditorRow#isTopLevel()}, but checks the underlying HU.
*
*
* @param select
* @return
*/
protected final List<I_M_HU> getSelectedHUs(@NonNull final Select select)
protected final Stream<I_M_HU> streamSelectedHUs(@NonNull final Select select)
{
return getSelectedHUs(HUEditorRowFilter.select(select));
return streamSelectedHUs(HUEditorRowFilter.select(select));
}

protected final List<I_M_HU> getSelectedHUs(@NonNull final HUEditorRowFilter filter)
protected final Stream<I_M_HU> streamSelectedHUs(@NonNull final HUEditorRowFilter filter)
{
// get all IDs, we we will enforce the filter using HUQueryBuilder
final Set<Integer> huIds = getSelectedHUIds(HUEditorRowFilter.ALL);

return toHUQueryBuilder(filter)
.addOnlyHUIds(huIds)
.createQuery()
.list(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 Predicate<HUEditorRow> toPredicate(@NonNull final HUEditorRowFilter filter)
{
return row -> matches(row, filter);
}

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());
final Stream<Integer> huIds = streamSelectedHUIds(filter);
return StreamUtils.dice(huIds, 100)
.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));
}
}
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(toBuilder = true)
@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 0958391

Please sign in to comment.