Skip to content

Commit

Permalink
Fixes for material cockpit
Browse files Browse the repository at this point in the history
  • Loading branch information
metas-ts committed Feb 23, 2021
1 parent e7cb4f2 commit a11f822
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ private DimensionSpec(final I_DIM_Dimension_Spec dimensionSpecRecord)
* Create a new {@link I_M_AttributeSetInstance} containing instances for relevant attributes in dimensionSpec and values from the given asi.<br>
* In other words, create a "projection" of the given asi, with respect to the given dimensionSpec.
*
* @param asi
* @param dimensionSpec
* @return the new ASI if at least one of the relevant attribute/value couple in the given ASI, null otherwise
*
* @deprecated this method does not correctly handle dimensions with multiple M_AttributeValue_IDs in one group and is also only used by an oboslete feature.
Expand Down Expand Up @@ -154,10 +152,6 @@ public I_M_AttributeSetInstance createASIForDimensionSpec(final I_M_AttributeSet
* Create {@link KeyNamePair}s of attribute IDs and values taken from the given <code>asi</code> that are relevant for the given dimensionSpec.
* In case of <code>null</code> asi or attributes not found or attributes with non relevant values, their values will be set to {@link DimensionConstants#DIM_EMPTY}.
*
* @param asi
* @param dimensionSpec
* @return
*
* @deprecated this method does not correctly handle dimensions with multiple M_AttributeValue_IDs in one group and is also only used by an oboslete feature.
*/
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static <ET, CT extends Collection<ET>> String toString(final CT collectio
*/
public static <ET, CT extends Collection<ET>> String toString(final CT collection,
final String separator,
final Converter<String, ET> elementStringConverter)
@Nullable final Converter<String, ET> elementStringConverter)
{
if (collection == null)
{
Expand Down Expand Up @@ -119,7 +119,7 @@ public static Set<Integer> asIntSet(final int... arr)
}

final Set<Integer> set = new HashSet<>(arr.length);
for (int i : arr)
for (final int i : arr)
{
set.add(i);
}
Expand All @@ -135,10 +135,7 @@ public static <T> Set<T> asSet(@SuppressWarnings("unchecked") final T... arr)
}

final Set<T> set = new HashSet<>(arr.length);
for (final T e : arr)
{
set.add(e);
}
Collections.addAll(set, arr);

return set;
}
Expand All @@ -148,7 +145,6 @@ public static <T> Set<T> asSet(@SuppressWarnings("unchecked") final T... arr)
*
* If there were more elements matching or no element was matching an exception will be thrown.
*
* @param collection
* @param filter filter used to match the element
* @return matching element; returns null ONLY if the element is null
* @see de.metas.util.reducers.Reducers#singleValue()
Expand All @@ -157,10 +153,8 @@ public static <T> T singleElement(@NonNull final Collection<T> collection, @NonN
{
final List<T> result = new ArrayList<>();

final Iterator<T> it = collection.iterator();
while (it.hasNext())
for (final T e : collection)
{
final T e = it.next();
if (filter.test(e))
{
result.add(e);
Expand All @@ -176,7 +170,6 @@ public static <T> T singleElement(@NonNull final Collection<T> collection, @NonN
*
* If the collection has more elements or no element then an exception will be thrown.
*
* @param collection
* @return element; returns null ONLY if the element is null
* @see de.metas.util.reducers.Reducers#singleValue()
*/
Expand All @@ -191,10 +184,9 @@ public static <T> T singleElement(@NonNull final Collection<T> collection)
*
* If the collection has more elements or no element then <code>null</code> will be returned.
*
* @param collection
* @return element
* @see de.metas.util.reducers.Reducers#singleValue()
*/
@Nullable
public static <T> T singleElementOrNull(final Collection<T> collection)
{
final T defaultValue = null;
Expand All @@ -209,7 +201,8 @@ public static <T> T singleElementOrNull(final Collection<T> collection)
* @param defaultValue value to be returned in case there are more then one elements or no element
* @see de.metas.util.reducers.Reducers#singleValue()
*/
public static <T> T singleElementOrDefault(final Collection<T> collection, final T defaultValue)
@Nullable
public static <T> T singleElementOrDefault(final Collection<T> collection, @Nullable final T defaultValue)
{
if (collection == null)
{
Expand Down Expand Up @@ -242,6 +235,7 @@ public static <T, R> R extractSingleElement(
/**
* @see de.metas.util.reducers.Reducers#singleValue()
*/
@Nullable
public static <T, R> R extractSingleElementOrDefault(
@NonNull final Collection<T> collection,
@NonNull final Function<T, R> extractFuntion,
Expand Down Expand Up @@ -293,6 +287,7 @@ public static <R, T> ImmutableSet<R> extractDistinctElementsIntoSet(
* @param extractFuntion converter to be used to convert elements
* @return list of OutputTypes.
*/
@Nullable
public static <R, T> ImmutableList<R> convert(
@Nullable final Collection<T> collection,
@NonNull final Function<T, R> extractFuntion)
Expand All @@ -313,10 +308,9 @@ public static <R, T> ImmutableList<R> convert(
*
* NOTE: this method is NOT checking if the set is null or empty.
*
* @param set
* @return firt element
* @return the removed first element
*/
public static <T> T removeFirst(final Set<T> set)
public static <T> T removeFirst(@NonNull final Set<T> set)
{
final Iterator<T> it = set.iterator();
final T element = it.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;

import java.util.Objects;

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

Expand Down Expand Up @@ -83,7 +85,9 @@
@EqualsAndHashCode(of = "documentId")
public class MaterialCockpitRow implements IViewRow
{
/** Please keep its prefix in sync with {@link MaterialCockpitViewFactory#SYSCFG_DisplayIncludedRows} */
/**
* Please keep its prefix in sync with {@link MaterialCockpitViewFactory#SYSCFG_DisplayIncludedRows}
*/
public static final String SYSCFG_PREFIX = "de.metas.ui.web.material.cockpit.field";

public static MaterialCockpitRow cast(final IViewRow row)
Expand Down Expand Up @@ -119,13 +123,16 @@ public static MaterialCockpitRow cast(final IViewRow row)
private final DimensionSpecGroup dimensionGroupOrNull;

public static final String FIELDNAME_Manufacturer_ID = I_M_Product.COLUMNNAME_Manufacturer_ID;

/**
* Use supplier in order to make this work with unit tests; getting the LookupValue uses LookupDAO.retrieveLookupDisplayInfo which goes directly to the DB.
*/
@ViewColumn(fieldName = FIELDNAME_Manufacturer_ID, //
captionKey = FIELDNAME_Manufacturer_ID, //
widgetType = DocumentFieldWidgetType.Lookup, //
layouts = { @ViewColumnLayout(when = JSONViewDataType.grid, seqNo = 32, //
displayed = Displayed.SYSCONFIG, displayedSysConfigPrefix = SYSCFG_PREFIX)
})
/** Use supplier in order to make this work with unit tests; getting the LookupValue uses LookupDAO.retrieveLookupDisplayInfo which goes directly to the DB. */
private final Supplier<LookupValue> manufacturer;

public static final String FIELDNAME_PackageSize = I_M_Product.COLUMNNAME_PackageSize;
Expand All @@ -138,13 +145,16 @@ public static MaterialCockpitRow cast(final IViewRow row)
private final String packageSize;

public static final String FIELDNAME_C_UOM_ID = I_M_Product.COLUMNNAME_C_UOM_ID;

/**
* Use supplier in order to make this work with unit tests; getting the LookupValue uses LookupDAO.retrieveLookupDisplayInfo which goes directly to the DB.
*/
@ViewColumn(fieldName = FIELDNAME_C_UOM_ID, //
captionKey = FIELDNAME_C_UOM_ID, //
widgetType = DocumentFieldWidgetType.Lookup, //
layouts = { @ViewColumnLayout(when = JSONViewDataType.grid, seqNo = 32, //
displayed = Displayed.SYSCONFIG, displayedSysConfigPrefix = SYSCFG_PREFIX)
})
/** Use supplier in order to make this work with unit tests; getting the LookupValue uses LookupDAO.retrieveLookupDisplayInfo which goes directly to the DB. */
private final Supplier<LookupValue> uom;

// Zusage Lieferant
Expand Down Expand Up @@ -239,15 +249,15 @@ private MaterialCockpitRow(
@NonNull final Set<Integer> allIncludedCockpitRecordIds,
@NonNull final Set<Integer> allIncludedStockRecordIds)
{
// Check.errorIf(includedRows.isEmpty(), "The given includedRows may not be empty");
Check.errorIf(includedRows.isEmpty(), "The given includedRows may not be empty");

this.rowType = DefaultRowType.Row;

this.date = extractDateOrNull(includedRows);
this.date = extractDate(includedRows);

this.dimensionGroupOrNull = null;

this.productId = extractProductIdOrMinusOne(includedRows);
this.productId = extractProductId(includedRows);

this.documentId = DocumentId.of(DOCUMENT_ID_JOINER.join(
"main",
Expand Down Expand Up @@ -279,7 +289,7 @@ private MaterialCockpitRow(
.findById(productRecord.getManufacturer_ID());

this.packageSize = productRecord.getPackageSize();

this.includedRows = includedRows;

this.pmmQtyPromised = Quantity.toBigDecimal(pmmQtyPromised);
Expand Down Expand Up @@ -315,14 +325,14 @@ private void assertNullOrCommonUomId(@NonNull final List<Quantity> quantitiesToV
Check.errorIf(notOK, "Some of the given quantities have different UOMs; quantities={}", quantitiesToVerify);
}

private static LocalDate extractDateOrNull(@NonNull final List<MaterialCockpitRow> includedRows)
private static LocalDate extractDate(@NonNull final List<MaterialCockpitRow> includedRows)
{
return CollectionUtils.extractSingleElementOrDefault(includedRows, row -> row.date, null);
return CollectionUtils.extractSingleElement(includedRows, row -> row.date);
}

private static int extractProductIdOrMinusOne(@NonNull final List<MaterialCockpitRow> includedRows)
private static int extractProductId(@NonNull final List<MaterialCockpitRow> includedRows)
{
return CollectionUtils.extractSingleElementOrDefault(includedRows, MaterialCockpitRow::getProductId, -1);
return CollectionUtils.extractSingleElement(includedRows, MaterialCockpitRow::getProductId);
}

@lombok.Builder(builderClassName = "AttributeSubRowBuilder", builderMethodName = "attributeSubRowBuilder")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void addEmptyAttributesSubrowBucket(@NonNull final DimensionSpecGroup dim
dimensionGroupSubRows.computeIfAbsent(dimensionSpecGroup, DimensionGroupSubRowBucket::create);
}

public void addEmptyCountingSubrowBucket(int plantId)
public void addEmptyCountingSubrowBucket(final int plantId)
{
countingSubRows.computeIfAbsent(plantId, CountingSubRowBucket::create);
}
Expand All @@ -77,16 +77,25 @@ public void addCockpitRecord(
@NonNull final DimensionSpec dimensionSpec,
final boolean includePerPlantDetailRows)
{
boolean addedToAtLeastOneBucket = false;
if (cockpitRecord.getQtyOnHandCount().signum() != 0 || cockpitRecord.getPP_Plant_ID() > 0)
{
if (includePerPlantDetailRows)
{
addCockpitRecordToCounting(cockpitRecord);
addedToAtLeastOneBucket = true;
}
}
else
{
addCockpitRecordToDimensionGroups(cockpitRecord, dimensionSpec);
addedToAtLeastOneBucket = addCockpitRecordToDimensionGroups(cockpitRecord, dimensionSpec);
}

if (!addedToAtLeastOneBucket)
{
// we need at least one subRow-bucket, even if there is no qtyOnHandCount, no plant and our dimensionSpec is empty!
final DimensionGroupSubRowBucket fallbackBucket = dimensionGroupSubRows.computeIfAbsent(DimensionSpecGroup.OTHER_GROUP, DimensionGroupSubRowBucket::create);
fallbackBucket.addCockpitRecord(cockpitRecord);
}
mainRow.addDataRecord(cockpitRecord);
}
Expand All @@ -97,7 +106,10 @@ private void addCockpitRecordToCounting(@NonNull final I_MD_Cockpit stockEstimat
countingSubRow.addCockpitRecord(stockEstimate);
}

private void addCockpitRecordToDimensionGroups(
/**
* @return true if there was at least one {@link DimensionGroupSubRowBucket} to which the given dataRecord could be added.
*/
private boolean addCockpitRecordToDimensionGroups(
@NonNull final I_MD_Cockpit dataRecord,
@NonNull final DimensionSpec dimensionSpec)
{
Expand All @@ -106,6 +118,7 @@ private void addCockpitRecordToDimensionGroups(
final AttributesKey attributesKey = AttributesKey.ofString(dataRecord.getAttributesKey());
final List<DimensionGroupSubRowBucket> subRowBuckets = findOrCreateSubRowBucket(attributesKey, dimensionSpec);
subRowBuckets.forEach(bucket -> bucket.addCockpitRecord(dataRecord));
return !subRowBuckets.isEmpty();
}

private void assertProductIdAndDateOfDataRecord(@NonNull final I_MD_Cockpit dataRecord)
Expand Down Expand Up @@ -163,11 +176,20 @@ public void addStockRecord(
@NonNull final DimensionSpec dimensionSpec,
final boolean includePerPlantDetailRows)
{
boolean addedToAtLeastOneBucket = false;
if (includePerPlantDetailRows)
{
addStockRecordToCounting(stockRecord);
addedToAtLeastOneBucket = true;
}
addedToAtLeastOneBucket = addedToAtLeastOneBucket || addStockRecordToDimensionGroups(stockRecord, dimensionSpec);

if (!addedToAtLeastOneBucket)
{
// we need at least one subRow-bucket, even if there is no qtyOnHandCount, no plant and our dimensionSpec is empty!
final DimensionGroupSubRowBucket fallbackBucket = dimensionGroupSubRows.computeIfAbsent(DimensionSpecGroup.OTHER_GROUP, DimensionGroupSubRowBucket::create);
fallbackBucket.addStockRecord(stockRecord);
}
addStockRecordToDimensionGroups(stockRecord, dimensionSpec);

mainRow.addStockRecord(stockRecord);
}
Expand All @@ -179,13 +201,18 @@ private void addStockRecordToCounting(@NonNull final I_MD_Stock stockRecord)
countingSubRow.addStockRecord(stockRecord);
}

private void addStockRecordToDimensionGroups(
/**
* @return true if there was at least one {@link DimensionGroupSubRowBucket} to which the given dataRecord could be added.
*/
private boolean addStockRecordToDimensionGroups(
@NonNull final I_MD_Stock dataRecord,
@NonNull final DimensionSpec dimensionSpec)
{
final AttributesKey attributesKey = AttributesKey.ofString(dataRecord.getAttributesKey());
final List<DimensionGroupSubRowBucket> subRowBuckets = findOrCreateSubRowBucket(attributesKey, dimensionSpec);
subRowBuckets.forEach(bucket -> bucket.addStockRecord(dataRecord));

return !subRowBuckets.isEmpty();
}

public MaterialCockpitRow createMainRowWithSubRows()
Expand Down

0 comments on commit a11f822

Please sign in to comment.