/**
   * Gets the element with the same code from the children and if not found returns the first.
   *
   * @param parent the parent to look for the right child.
   */
  protected VariantMatrixElementData getRightElementToCopy(final VariantMatrixElementData parent) {

    for (final VariantMatrixElementData child : parent.getElements()) {
      if (parent.getVariantOption().getCode().equals(child.getVariantOption().getCode())) {
        return child;
      }
    }

    throw new IllegalStateException(
        "One of the child elements must have the same code as the parent");
  }
 protected void populateNodes(
     final VariantMatrixElementData parentNode, final VariantProductModel variant) {
   if (parentNode.getIsLeaf()) {
     if (variant.getCode().equals(parentNode.getVariantOption().getCode())) {
       getVariantOptionDataPopulator()
           .populate(variant, parentNode.getVariantOption(), getProductVariantOptionList());
     }
   } else {
     for (final VariantMatrixElementData childNode : parentNode.getElements()) {
       populateNodes(childNode, variant);
     }
     copyData(parentNode);
   }
 }
Exemple #3
0
  /**
   * JSP EL Function to get an image for a Product in a specific format based on the product code.
   *
   * @param product The product.
   * @param productCode The desired product code.
   * @param format The desired format.
   * @return The image.
   */
  public static ImageData getImageForProductCode(
      final ProductData product, final String productCode, final String format) {
    if (product != null && productCode != null && format != null) {

      int selectedIndex = 0;

      for (int i = 1; i <= product.getCategories().size(); i++) {
        int j = 0;
        final List<VariantMatrixElementData> theMatrix;

        if (i == 1) {
          theMatrix = product.getVariantMatrix();
        } else {
          theMatrix = product.getVariantMatrix().get(selectedIndex).getElements();
          selectedIndex = 0;
        }

        if (theMatrix.get(selectedIndex).getParentVariantCategory().getHasImage()) {
          for (final VariantMatrixElementData matrixElementData : theMatrix) {
            if (matrixElementData.getVariantOption().getVariantOptionQualifiers() != null
                && productCode.equals(matrixElementData.getVariantOption().getCode())) {
              for (final VariantOptionQualifierData variantOption :
                  matrixElementData.getVariantOption().getVariantOptionQualifiers()) {
                if (format.equals(variantOption.getImage().getFormat())) {
                  return variantOption.getImage();
                }
              }
              selectedIndex = j;
            }

            j++;
          }
        }
      }
    }
    return null;
  }
  /**
   * Copies the data from the element with the same code or from the first one on the list.
   *
   * @param parent the element to copy the data into.
   */
  protected void copyData(final VariantMatrixElementData parent) {

    if (CollectionUtils.isNotEmpty(parent.getElements()) && parent.getVariantOption() != null) {

      final VariantMatrixElementData elementToCopy = getRightElementToCopy(parent);

      parent.getVariantOption().setCode(elementToCopy.getVariantOption().getCode());
      parent.getVariantOption().setUrl(elementToCopy.getVariantOption().getUrl());
      parent
          .getVariantOption()
          .setVariantOptionQualifiers(
              elementToCopy.getVariantOption().getVariantOptionQualifiers());
    }
  }