protected void configure() { variantProductSource = new VariantProductSource(); given(model.getBaseProduct()).willReturn(baseProduct); given(baseProduct.getVariants()).willReturn(Sets.newHashSet(variant3)); given(model.getVariants()).willReturn(Sets.newHashSet(variant1, variant2)); }
/** {@inheritDoc} */ @Override public Map<String, Collection<Object>> getAssignedVariantAttributes( final ProductModel baseProduct) { validateParameterNotNullStandardMessage("baseProduct", baseProduct); final Map<String, Collection<Object>> result = new HashMap<String, Collection<Object>>(); final Collection<VariantProductModel> variantModelList = baseProduct.getVariants(); final List<VariantAttributeDescriptorModel> vadList = getVariantAttributesForVariantType(baseProduct.getVariantType()); // iterate trough all variants to get the qualifier with appropriate values for (final VariantProductModel variant : variantModelList) { for (final VariantAttributeDescriptorModel item : vadList) { Collection values = result.get(item.getQualifier()); if (values == null) { values = new LinkedHashSet(); result.put(item.getQualifier(), values); } // there is no chance to read the attribute value in SL, because it"s not a part of the // model. // final Object value = variant.getAttributeProvider().getAttribute(item.getQualifier()); // Currently the jalo-solution has to be used values.add(getVariantAttributeValue(variant, item.getQualifier())); } } if (LOG.isDebugEnabled()) { LOG.debug(result.size() + " variant attributes with assigned values found"); } return result; }
/** {@inheritDoc} */ @Override public Collection<VariantProductModel> getVariantProductForAttributeValues( final ProductModel baseProduct, final Map<String, Object> filterValues) { validateParameterNotNullStandardMessage("baseProduct", baseProduct); final Collection<VariantProductModel> result = new ArrayList<VariantProductModel>(); final List<VariantAttributeDescriptorModel> vadList = getVariantAttributesForVariantType(baseProduct.getVariantType()); final Collection<VariantProductModel> allBaseProductVariants = baseProduct.getVariants(); if (filterValues == null || filterValues.isEmpty()) { // no filter defined - no results. if (LOG.isDebugEnabled()) { LOG.debug("The filter values haven't been set, no matching variants in cases like this."); } return result; } // iterate through all variants and filter those with matching qualifier and values for (final VariantProductModel variant : allBaseProductVariants) { boolean add = true; for (final Iterator<Map.Entry<String, Object>> iterator = filterValues.entrySet().iterator(); iterator.hasNext(); ) { if (!add) { break; // the filter element, which has been checked doesn't match, skip checking of // addtional filter elements, go to next variant. } final Map.Entry entry = iterator.next(); final String filterKey = (String) entry.getKey(); final Object filterValue = entry.getValue(); // compare the filterKey and filterValue with all attributes (qualifier-value) of the // variantType. for (final VariantAttributeDescriptorModel attrDesc : vadList) { final String qualifier = attrDesc.getQualifier(); final Object variantAttributeValue = getVariantAttributeValue(variant, qualifier); if (!(filterKey.equals(qualifier) && (filterValue == variantAttributeValue || (filterValue != null && filterValue.equals(variantAttributeValue))))) { add = false; } else { add = true; // the current filter key and values matches for the current variant, go to // next filter-element to check it. break; } } } if (add) { result.add(variant); } } if (LOG.isDebugEnabled()) { LOG.debug(result.size() + " matching variants have been found."); } return result; }