Skip to content

Commit

Permalink
MAJOR: WEBUI_HU_Transform fully refactored; bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Jul 22, 2017
1 parent 8e280db commit b4e780e
Show file tree
Hide file tree
Showing 27 changed files with 1,395 additions and 572 deletions.
69 changes: 69 additions & 0 deletions src/main/java/de/metas/ui/web/handlingunits/HUEditorRowQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package de.metas.ui.web.handlingunits;

import com.google.common.collect.ImmutableSet;

import lombok.Builder;
import lombok.Singular;
import lombok.Value;

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

/**
* {@link HUEditorRow} query.
*
* @author metas-dev <dev@metasfresh.com>
*/
@Value
public class HUEditorRowQuery
{
/** User string filter (e.g. what user typed in the lookup field) */
private final String stringFilter;

/** If specified only the rows of given type are considered */
private final HUEditorRowType rowType;

/**
* 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.
*/
private final ImmutableSet<Integer> excludeHUIds;

@Builder
private HUEditorRowQuery(final String stringFilter,
final HUEditorRowType rowType,
@Singular final ImmutableSet<Integer> excludeHUIds)
{
this.stringFilter = stringFilter;
this.rowType = rowType;

if (excludeHUIds == null || excludeHUIds.isEmpty())
{
this.excludeHUIds = ImmutableSet.of();
}
else
{
this.excludeHUIds = excludeHUIds.stream().filter(huId -> huId > 0).collect(ImmutableSet.toImmutableSet());
}
}
}
118 changes: 107 additions & 11 deletions src/main/java/de/metas/ui/web/handlingunits/HUEditorView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.metas.ui.web.handlingunits;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -16,6 +18,7 @@
import org.compiere.util.Evaluatee;

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

import de.metas.handlingunits.model.I_M_HU;
Expand Down Expand Up @@ -80,6 +83,7 @@ public static HUEditorView cast(final IView view)
private final ViewId viewId;
private final JSONViewDataType viewType;

private final String referencingTableName;
private final Set<DocumentPath> referencingDocumentPaths;

private final ViewActionDescriptorsList actions;
Expand All @@ -90,6 +94,8 @@ public static HUEditorView cast(final IView view)

private final transient DocumentFilterDescriptorsProvider viewFilterDescriptors;
private final ImmutableList<DocumentFilter> filters;

private final ImmutableMap<String, Object> parameters;

private HUEditorView(final Builder builder)
{
Expand All @@ -107,6 +113,7 @@ private HUEditorView(final Builder builder)

//
// Build the repository
referencingTableName = builder.getReferencingTableName();
final HUEditorViewRepository huEditorRepo = HUEditorViewRepository.builder()
.windowId(builder.getWindowId())
.referencingTableName(builder.getReferencingTableName())
Expand All @@ -129,6 +136,8 @@ private HUEditorView(final Builder builder)

actions = builder.getActions();
additionalRelatedProcessDescriptors = builder.getAdditionalRelatedProcessDescriptors();

parameters = builder.getParameters();
}

private static final HUEditorViewBuffer createRowsBuffer( //
Expand All @@ -155,7 +164,7 @@ public ViewId getParentViewId()
{
return parentViewId;
}

@Override
public DocumentId getParentRowId()
{
Expand Down Expand Up @@ -231,6 +240,12 @@ public List<RelatedProcessDescriptor> getAdditionalRelatedProcessDescriptors()
{
return additionalRelatedProcessDescriptors;
}

public boolean getParameterAsBoolean(final String name, final boolean defaultValue)
{
final Boolean value = (Boolean)parameters.get(name);
return value != null ? value.booleanValue() : defaultValue;
}

@Override
public HUEditorRow getById(final DocumentId rowId) throws EntityNotFoundException
Expand All @@ -239,9 +254,9 @@ public HUEditorRow getById(final DocumentId rowId) throws EntityNotFoundExceptio
}

@Override
public List<HUEditorRow> getByIds(final DocumentIdsSelection rowId)
public List<HUEditorRow> getByIds(final DocumentIdsSelection rowIds)
{
return streamByIds(rowId).collect(ImmutableList.toImmutableList());
return streamByIds(rowIds).collect(ImmutableList.toImmutableList());
}

@Override
Expand Down Expand Up @@ -292,6 +307,11 @@ public boolean hasAttributesSupport()
return true;
}

public String getReferencingTableName()
{
return referencingTableName;
}

@Override
public Set<DocumentPath> getReferencingDocumentPaths()
{
Expand All @@ -315,26 +335,41 @@ private void invalidateAllNoNotify()

public void addHUsAndInvalidate(final Collection<I_M_HU> husToAdd)
{
if (rowsBuffer.addHUIds(extractHUIds(husToAdd)))
addHUIdsAndInvalidate(extractHUIds(husToAdd));
}

public void addHUIdsAndInvalidate(final Collection<Integer> huIdsToAdd)
{
if (addHUIds(huIdsToAdd))
{
invalidateAll();
}
}

public void removesHUsAndInvalidate(final Collection<I_M_HU> husToRemove)
public boolean addHUIds(final Collection<Integer> huIdsToAdd)
{
return rowsBuffer.addHUIds(huIdsToAdd);
}

public void removeHUsAndInvalidate(final Collection<I_M_HU> husToRemove)
{
final Set<Integer> huIdsToRemove = extractHUIds(husToRemove);
removesHUIdsAndInvalidate(huIdsToRemove);
removeHUIdsAndInvalidate(huIdsToRemove);
}

public void removesHUIdsAndInvalidate(final Collection<Integer> huIdsToRemove)
public void removeHUIdsAndInvalidate(final Collection<Integer> huIdsToRemove)
{
if (rowsBuffer.removeHUIds(huIdsToRemove))
if (removeHUIds(huIdsToRemove))
{
invalidateAll();
}
}

public boolean removeHUIds(final Collection<Integer> huIdsToRemove)
{
return rowsBuffer.removeHUIds(huIdsToRemove);
}

private static final Set<Integer> extractHUIds(final Collection<I_M_HU> hus)
{
if (hus == null || hus.isEmpty())
Expand Down Expand Up @@ -383,6 +418,18 @@ public Stream<HUEditorRow> streamAllRecursive()
return rowsBuffer.streamAllRecursive();
}

/** @return top level rows and included rows recursive stream which are matching the given query */
public Stream<HUEditorRow> streamAllRecursive(final HUEditorRowQuery query)
{
return rowsBuffer.streamAllRecursive(query);
}

/** @return true if there is any top level or included row which is matching given query */
public boolean matchesAnyRowRecursive(final HUEditorRowQuery query)
{
return rowsBuffer.matchesAnyRowRecursive(query);
}

@Override
public <T> List<T> retrieveModelsByIds(final DocumentIdsSelection rowIds, final Class<T> modelClass)
{
Expand All @@ -404,6 +451,8 @@ public <T> List<T> retrieveModelsByIds(final DocumentIdsSelection rowIds, final
return InterfaceWrapperHelper.createList(hus, modelClass);
}

//
//
//
//
//
Expand All @@ -425,6 +474,8 @@ public static final class Builder
private List<DocumentFilter> stickyFilters;
private List<DocumentFilter> filters;

private LinkedHashMap<String, Object> parameters;

private Builder(@NonNull final SqlViewBinding sqlViewBinding)
{
this.sqlViewBinding = sqlViewBinding;
Expand Down Expand Up @@ -456,7 +507,7 @@ public Builder setParentRowId(final DocumentId parentRowId)
this.parentRowId = parentRowId;
return this;
}

private DocumentId getParentRowId()
{
return parentRowId;
Expand Down Expand Up @@ -491,7 +542,7 @@ public Builder setReferencingDocumentPaths(final String referencingTableName, fi
return this;
}

private String getReferencingTableName()
public String getReferencingTableName()
{
return referencingTableName;
}
Expand All @@ -514,7 +565,26 @@ private ViewActionDescriptorsList getActions()

public Builder setAdditionalRelatedProcessDescriptors(@NonNull final List<RelatedProcessDescriptor> additionalRelatedProcessDescriptors)
{
this.additionalRelatedProcessDescriptors = additionalRelatedProcessDescriptors;
if (additionalRelatedProcessDescriptors == null || additionalRelatedProcessDescriptors.isEmpty())
{
this.additionalRelatedProcessDescriptors = null;
}
else
{
this.additionalRelatedProcessDescriptors = new ArrayList<>(additionalRelatedProcessDescriptors);
}

return this;
}

public Builder addAdditionalRelatedProcessDescriptor(@NonNull final RelatedProcessDescriptor descriptor)
{
if (this.additionalRelatedProcessDescriptors == null)
{
additionalRelatedProcessDescriptors = new ArrayList<>();
}
additionalRelatedProcessDescriptors.add(descriptor);

return this;
}

Expand Down Expand Up @@ -545,5 +615,31 @@ private List<DocumentFilter> getFilters()
return filters != null ? filters : ImmutableList.of();
}

public Builder setParameter(final String name, final Object value)
{
if (value == null)
{
if (parameters != null)
{
parameters.remove(name);
}
}
else
{
if (parameters == null)
{
parameters = new LinkedHashMap<>();
parameters.put(name, value);
}
}

return this;
}

private ImmutableMap<String, Object> getParameters()
{
return parameters != null ? ImmutableMap.copyOf(parameters) : ImmutableMap.of();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import com.google.common.collect.ImmutableSet;

import de.metas.printing.esb.base.util.Check;
import de.metas.ui.web.document.filter.DocumentFilter;
import de.metas.ui.web.exceptions.EntityNotFoundException;
import de.metas.ui.web.view.ViewId;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import lombok.NonNull;

/*
* #%L
Expand Down Expand Up @@ -59,6 +64,42 @@ interface HUEditorViewBuffer

/** @return top level rows and included rows recursive stream */
Stream<HUEditorRow> streamAllRecursive() throws UnsupportedOperationException;

/** @return top level rows and included rows recursive stream which are matching the given query */
default Stream<HUEditorRow> streamAllRecursive(@NonNull final HUEditorRowQuery query)
{
Stream<HUEditorRow> result = streamAllRecursive();

// Filter by row type
final HUEditorRowType rowType = query.getRowType();
if (rowType != null)
{
result = result.filter(row -> Objects.equals(row.getType(), rowType));
}

// Filter by string filter
final String stringFilter = query.getStringFilter();
if (!Check.isEmpty(stringFilter, true))
{
result = result.filter(row -> row.matchesStringFilter(stringFilter));
}

// Exclude M_HU_IDs
final ImmutableSet<Integer> excludeHUIds = query.getExcludeHUIds();
if(!excludeHUIds.isEmpty())
{
result = result.filter(row -> !excludeHUIds.contains(row.getM_HU_ID()));
}

return result;
}

/** @return true if there is any top level or included row which is matching given query */
default boolean matchesAnyRowRecursive(final HUEditorRowQuery query)
{
return streamAllRecursive(query).findAny().isPresent();
}


/**
* Stream all rows (including children) which match any of given <code>rowIds</code>.
Expand Down
Loading

0 comments on commit b4e780e

Please sign in to comment.