/**
   * 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());
    }
  }