@Override
  public void populate(
      final VariantValueCategoryModel variantValueCategory,
      final VariantMatrixElementData variantMatrixElementData)
      throws ConversionException {
    final VariantCategoryData parent = new VariantCategoryData();

    final VariantCategoryModel variantCategoryModel =
        (VariantCategoryModel) variantValueCategory.getSupercategories().get(0);
    parent.setName(variantCategoryModel.getName());
    parent.setHasImage(variantCategoryModel.getHasImage());

    final VariantValueCategoryData data = new VariantValueCategoryData();
    data.setName(variantValueCategory.getName());
    data.setSequence(variantValueCategory.getSequence());

    variantMatrixElementData.setVariantValueCategory(data);
    variantMatrixElementData.setParentVariantCategory(parent);

    final VariantOptionData variantOptionData = new VariantOptionData();
    final VariantOptionQualifierData variantOptionQualifierData = new VariantOptionQualifierData();
    variantOptionQualifierData.setImage(new ImageData());
    variantOptionData.setVariantOptionQualifiers(
        Arrays.asList(new VariantOptionQualifierData[] {variantOptionQualifierData}));

    variantMatrixElementData.setVariantOption(variantOptionData);
  }
  /**
   * 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);
   }
 }
  /**
   * 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());
    }
  }
示例#5
0
文件: Functions.java 项目: raliev2/tn
  /**
   * 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;
  }