/** * @param p * @return details */ public static ProductDetails valueOf(Product p) { ProductDetails pd = new ProductDetails(); pd.setId(p.getId()); pd.setBrand(p.getBrand()); pd.setName(p.getName()); pd.setImageUrl(findProductImage(p)); pd.setUrl(p.getUrl()); setSkuLevelStuff(p, pd); return pd; }
private static void setSkuLevelStuff(Product p, ProductDetails pd) { boolean isSoldOut = true; List<String> colors = new ArrayList<String>(); List<String> sizes = new ArrayList<String>(); for (Sku sku : p.getSkus()) { try { NumberFormat format = NumberFormat.getInstance(); Number skuPrice = NumberFormat.getInstance().parse(sku.getSalePrice()); // minimum price logic if (pd.getMinPrice() != null) { Number minPrice = format.parse(pd.getMinPrice()); if (skuPrice.doubleValue() < minPrice.doubleValue()) { pd.setMinPrice(sku.getSalePrice()); } } else { pd.setMinPrice(sku.getSalePrice()); } // maximum price logic if (pd.getMaxPrice() != null) { Number maxPrice = format.parse(pd.getMaxPrice()); if (skuPrice.doubleValue() > maxPrice.doubleValue()) { pd.setMaxPrice(sku.getSalePrice()); } } else { pd.setMaxPrice(sku.getSalePrice()); } } catch (ParseException e) { e.printStackTrace(System.err); } if ("for sale".equalsIgnoreCase(sku.getInventoryStatus())) { isSoldOut = false; String color = getAttribute(sku.getAttributes(), "color"); if (color != null) { colors.add(color); } String size = getAttribute(sku.getAttributes(), "size"); if (size != null) { sizes.add(size); } } if ("reserved".equalsIgnoreCase(sku.getInventoryStatus())) { isSoldOut = false; } } pd.setSoldOut(isSoldOut); pd.setSizes(sizes); pd.setColors(colors); }
private static String findProductImage(Product p) { return p.getImageUrls().get(SMALL_IMAGE_SIZE).get(0).getUrl(); }