protected ShoppingItemPrice addShoppingItemPrice() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    ShoppingItemPrice shoppingItemPrice = _persistence.create(pk);

    shoppingItemPrice.setItemId(ServiceTestUtil.nextLong());

    shoppingItemPrice.setMinQuantity(ServiceTestUtil.nextInt());

    shoppingItemPrice.setMaxQuantity(ServiceTestUtil.nextInt());

    shoppingItemPrice.setPrice(ServiceTestUtil.nextDouble());

    shoppingItemPrice.setDiscount(ServiceTestUtil.nextDouble());

    shoppingItemPrice.setTaxable(ServiceTestUtil.randomBoolean());

    shoppingItemPrice.setShipping(ServiceTestUtil.nextDouble());

    shoppingItemPrice.setUseShippingFormula(ServiceTestUtil.randomBoolean());

    shoppingItemPrice.setStatus(ServiceTestUtil.nextInt());

    _persistence.update(shoppingItemPrice);

    return shoppingItemPrice;
  }
  @Test
  public void testFetchByPrimaryKeyExisting() throws Exception {
    ShoppingItemPrice newShoppingItemPrice = addShoppingItemPrice();

    ShoppingItemPrice existingShoppingItemPrice =
        _persistence.fetchByPrimaryKey(newShoppingItemPrice.getPrimaryKey());

    Assert.assertEquals(existingShoppingItemPrice, newShoppingItemPrice);
  }
  @Test
  public void testCreate() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    ShoppingItemPrice shoppingItemPrice = _persistence.create(pk);

    Assert.assertNotNull(shoppingItemPrice);

    Assert.assertEquals(shoppingItemPrice.getPrimaryKey(), pk);
  }
  @Test
  public void testRemove() throws Exception {
    ShoppingItemPrice newShoppingItemPrice = addShoppingItemPrice();

    _persistence.remove(newShoppingItemPrice);

    ShoppingItemPrice existingShoppingItemPrice =
        _persistence.fetchByPrimaryKey(newShoppingItemPrice.getPrimaryKey());

    Assert.assertNull(existingShoppingItemPrice);
  }
  /**
   * Adds the shopping item price to the database. Also notifies the appropriate model listeners.
   *
   * @param shoppingItemPrice the shopping item price
   * @return the shopping item price that was added
   * @throws SystemException if a system exception occurred
   */
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public ShoppingItemPrice addShoppingItemPrice(ShoppingItemPrice shoppingItemPrice)
      throws SystemException {
    shoppingItemPrice.setNew(true);

    return shoppingItemPricePersistence.update(shoppingItemPrice);
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    ShoppingItemPrice newShoppingItemPrice = addShoppingItemPrice();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(
            ShoppingItemPrice.class, ShoppingItemPrice.class.getClassLoader());

    dynamicQuery.add(
        RestrictionsFactoryUtil.eq("itemPriceId", newShoppingItemPrice.getItemPriceId()));

    List<ShoppingItemPrice> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    ShoppingItemPrice existingShoppingItemPrice = result.get(0);

    Assert.assertEquals(existingShoppingItemPrice, newShoppingItemPrice);
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    ShoppingItemPrice newShoppingItemPrice = addShoppingItemPrice();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(
            ShoppingItemPrice.class, ShoppingItemPrice.class.getClassLoader());

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("itemPriceId"));

    Object newItemPriceId = newShoppingItemPrice.getItemPriceId();

    dynamicQuery.add(RestrictionsFactoryUtil.in("itemPriceId", new Object[] {newItemPriceId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Object existingItemPriceId = result.get(0);

    Assert.assertEquals(existingItemPriceId, newItemPriceId);
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    ShoppingItemPrice newShoppingItemPrice = _persistence.create(pk);

    newShoppingItemPrice.setItemId(ServiceTestUtil.nextLong());

    newShoppingItemPrice.setMinQuantity(ServiceTestUtil.nextInt());

    newShoppingItemPrice.setMaxQuantity(ServiceTestUtil.nextInt());

    newShoppingItemPrice.setPrice(ServiceTestUtil.nextDouble());

    newShoppingItemPrice.setDiscount(ServiceTestUtil.nextDouble());

    newShoppingItemPrice.setTaxable(ServiceTestUtil.randomBoolean());

    newShoppingItemPrice.setShipping(ServiceTestUtil.nextDouble());

    newShoppingItemPrice.setUseShippingFormula(ServiceTestUtil.randomBoolean());

    newShoppingItemPrice.setStatus(ServiceTestUtil.nextInt());

    _persistence.update(newShoppingItemPrice);

    ShoppingItemPrice existingShoppingItemPrice =
        _persistence.findByPrimaryKey(newShoppingItemPrice.getPrimaryKey());

    Assert.assertEquals(
        existingShoppingItemPrice.getItemPriceId(), newShoppingItemPrice.getItemPriceId());
    Assert.assertEquals(existingShoppingItemPrice.getItemId(), newShoppingItemPrice.getItemId());
    Assert.assertEquals(
        existingShoppingItemPrice.getMinQuantity(), newShoppingItemPrice.getMinQuantity());
    Assert.assertEquals(
        existingShoppingItemPrice.getMaxQuantity(), newShoppingItemPrice.getMaxQuantity());
    AssertUtils.assertEquals(existingShoppingItemPrice.getPrice(), newShoppingItemPrice.getPrice());
    AssertUtils.assertEquals(
        existingShoppingItemPrice.getDiscount(), newShoppingItemPrice.getDiscount());
    Assert.assertEquals(existingShoppingItemPrice.getTaxable(), newShoppingItemPrice.getTaxable());
    AssertUtils.assertEquals(
        existingShoppingItemPrice.getShipping(), newShoppingItemPrice.getShipping());
    Assert.assertEquals(
        existingShoppingItemPrice.getUseShippingFormula(),
        newShoppingItemPrice.getUseShippingFormula());
    Assert.assertEquals(existingShoppingItemPrice.getStatus(), newShoppingItemPrice.getStatus());
  }
  @Override
  public ShoppingItem addItem(
      long userId,
      long groupId,
      long categoryId,
      String sku,
      String name,
      String description,
      String properties,
      String fieldsQuantities,
      boolean requiresShipping,
      int stockQuantity,
      boolean featured,
      Boolean sale,
      boolean smallImage,
      String smallImageURL,
      File smallImageFile,
      boolean mediumImage,
      String mediumImageURL,
      File mediumImageFile,
      boolean largeImage,
      String largeImageURL,
      File largeImageFile,
      List<ShoppingItemField> itemFields,
      List<ShoppingItemPrice> itemPrices,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Item

    User user = userPersistence.findByPrimaryKey(userId);
    sku = StringUtil.toUpperCase(sku.trim());

    byte[] smallImageBytes = null;
    byte[] mediumImageBytes = null;
    byte[] largeImageBytes = null;

    try {
      smallImageBytes = FileUtil.getBytes(smallImageFile);
      mediumImageBytes = FileUtil.getBytes(mediumImageFile);
      largeImageBytes = FileUtil.getBytes(largeImageFile);
    } catch (IOException ioe) {
    }

    Date now = new Date();

    validate(
        user.getCompanyId(),
        0,
        sku,
        name,
        smallImage,
        smallImageURL,
        smallImageFile,
        smallImageBytes,
        mediumImage,
        mediumImageURL,
        mediumImageFile,
        mediumImageBytes,
        largeImage,
        largeImageURL,
        largeImageFile,
        largeImageBytes);

    long itemId = counterLocalService.increment();

    ShoppingItem item = shoppingItemPersistence.create(itemId);

    item.setGroupId(groupId);
    item.setCompanyId(user.getCompanyId());
    item.setUserId(user.getUserId());
    item.setUserName(user.getFullName());
    item.setCreateDate(now);
    item.setModifiedDate(now);
    item.setCategoryId(categoryId);
    item.setSku(sku);
    item.setName(name);
    item.setDescription(description);
    item.setProperties(properties);
    item.setFields(itemFields.size() > 0);
    item.setFieldsQuantities(fieldsQuantities);

    for (ShoppingItemPrice itemPrice : itemPrices) {
      if (itemPrice.getStatus() == ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT) {

        item.setMinQuantity(itemPrice.getMinQuantity());
        item.setMaxQuantity(itemPrice.getMaxQuantity());
        item.setPrice(itemPrice.getPrice());
        item.setDiscount(itemPrice.getDiscount());
        item.setTaxable(itemPrice.getTaxable());
        item.setShipping(itemPrice.getShipping());
        item.setUseShippingFormula(itemPrice.getUseShippingFormula());
      }

      if ((sale == null)
          && (itemPrice.getDiscount() > 0)
          && ((itemPrice.getStatus() == ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT)
              || (itemPrice.getStatus() == ShoppingItemPriceConstants.STATUS_ACTIVE))) {

        sale = Boolean.TRUE;
      }
    }

    item.setRequiresShipping(requiresShipping);
    item.setStockQuantity(stockQuantity);
    item.setFeatured(featured);
    item.setSale((sale != null) ? sale.booleanValue() : false);
    item.setSmallImage(smallImage);
    item.setSmallImageId(counterLocalService.increment());
    item.setSmallImageURL(smallImageURL);
    item.setMediumImage(mediumImage);
    item.setMediumImageId(counterLocalService.increment());
    item.setMediumImageURL(mediumImageURL);
    item.setLargeImage(largeImage);
    item.setLargeImageId(counterLocalService.increment());
    item.setLargeImageURL(largeImageURL);

    shoppingItemPersistence.update(item);

    // Resources

    if (serviceContext.isAddGroupPermissions() || serviceContext.isAddGuestPermissions()) {

      addItemResources(
          item, serviceContext.isAddGroupPermissions(), serviceContext.isAddGuestPermissions());
    } else {
      addItemResources(
          item, serviceContext.getGroupPermissions(), serviceContext.getGuestPermissions());
    }

    // Images

    saveImages(
        smallImage,
        item.getSmallImageId(),
        smallImageFile,
        smallImageBytes,
        mediumImage,
        item.getMediumImageId(),
        mediumImageFile,
        mediumImageBytes,
        largeImage,
        item.getLargeImageId(),
        largeImageFile,
        largeImageBytes);

    // Item fields

    for (ShoppingItemField itemField : itemFields) {
      long itemFieldId = counterLocalService.increment();

      itemField.setItemFieldId(itemFieldId);
      itemField.setItemId(itemId);
      itemField.setName(checkItemField(itemField.getName()));
      itemField.setValues(checkItemField(itemField.getValues()));

      shoppingItemFieldPersistence.update(itemField);
    }

    // Item prices

    if (itemPrices.size() > 1) {
      for (ShoppingItemPrice itemPrice : itemPrices) {
        long itemPriceId = counterLocalService.increment();

        itemPrice.setItemPriceId(itemPriceId);
        itemPrice.setItemId(itemId);

        shoppingItemPricePersistence.update(itemPrice);
      }
    }

    return item;
  }
  protected void doAddBookItems(long userId, long groupId, long categoryId, String[] isbns)
      throws IOException, PortalException, SystemException {

    if (!AmazonRankingsUtil.isEnabled()) {
      throw new AmazonException("Amazon integration is not enabled");
    }

    String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);

    for (int i = 0; (i < isbns.length) && (i < 50); i++) {
      String isbn = isbns[i];

      AmazonRankings amazonRankings = AmazonRankingsUtil.getAmazonRankings(isbn);

      if (amazonRankings == null) {
        continue;
      }

      String name = amazonRankings.getProductName();
      String description = StringPool.BLANK;
      String properties = getBookProperties(amazonRankings);

      int minQuantity = 0;
      int maxQuantity = 0;
      double price = amazonRankings.getListPrice();
      double discount = 1 - amazonRankings.getOurPrice() / price;
      boolean taxable = true;
      double shipping = 0.0;
      boolean useShippingFormula = true;

      ShoppingItemPrice itemPrice = shoppingItemPricePersistence.create(0);

      itemPrice.setMinQuantity(minQuantity);
      itemPrice.setMaxQuantity(maxQuantity);
      itemPrice.setPrice(price);
      itemPrice.setDiscount(discount);
      itemPrice.setTaxable(taxable);
      itemPrice.setShipping(shipping);
      itemPrice.setUseShippingFormula(useShippingFormula);
      itemPrice.setStatus(ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT);

      boolean requiresShipping = true;
      int stockQuantity = 0;
      boolean featured = false;
      Boolean sale = null;

      // Small image

      boolean smallImage = true;
      String smallImageURL = StringPool.BLANK;
      File smallImageFile =
          new File(
              tmpDir
                  + File.separatorChar
                  + PwdGenerator.getPassword(8, PwdGenerator.KEY2)
                  + ".jpg");

      byte[] smallImageBytes = HttpUtil.URLtoByteArray(amazonRankings.getSmallImageURL());

      if (smallImageBytes.length < 1024) {
        smallImage = false;
      } else {
        OutputStream os = new FileOutputStream(smallImageFile);

        os.write(smallImageBytes);

        os.close();
      }

      // Medium image

      boolean mediumImage = true;
      String mediumImageURL = StringPool.BLANK;
      File mediumImageFile =
          new File(
              tmpDir
                  + File.separatorChar
                  + PwdGenerator.getPassword(8, PwdGenerator.KEY2)
                  + ".jpg");

      byte[] mediumImageBytes = HttpUtil.URLtoByteArray(amazonRankings.getMediumImageURL());

      if (mediumImageBytes.length < 1024) {
        mediumImage = false;
      } else {
        OutputStream os = new FileOutputStream(mediumImageFile);

        os.write(mediumImageBytes);

        os.close();
      }

      // Large image

      boolean largeImage = true;
      String largeImageURL = StringPool.BLANK;
      File largeImageFile =
          new File(
              tmpDir
                  + File.separatorChar
                  + PwdGenerator.getPassword(8, PwdGenerator.KEY2)
                  + ".jpg");

      byte[] largeImageBytes = HttpUtil.URLtoByteArray(amazonRankings.getLargeImageURL());

      if (largeImageBytes.length < 1024) {
        largeImage = false;
      } else {
        OutputStream os = new FileOutputStream(largeImageFile);

        os.write(largeImageBytes);

        os.close();
      }

      List<ShoppingItemField> itemFields = new ArrayList<ShoppingItemField>();

      List<ShoppingItemPrice> itemPrices = new ArrayList<ShoppingItemPrice>();

      itemPrices.add(itemPrice);

      ServiceContext serviceContext = new ServiceContext();

      serviceContext.setAddGroupPermissions(true);
      serviceContext.setAddGuestPermissions(true);

      addItem(
          userId,
          groupId,
          categoryId,
          isbn,
          name,
          description,
          properties,
          StringPool.BLANK,
          requiresShipping,
          stockQuantity,
          featured,
          sale,
          smallImage,
          smallImageURL,
          smallImageFile,
          mediumImage,
          mediumImageURL,
          mediumImageFile,
          largeImage,
          largeImageURL,
          largeImageFile,
          itemFields,
          itemPrices,
          serviceContext);

      smallImageFile.delete();
      mediumImageFile.delete();
      largeImageFile.delete();
    }
  }