@Override
  protected ShoppingItemField removeImpl(ShoppingItemField shoppingItemField)
      throws SystemException {
    shoppingItemField = toUnwrappedModel(shoppingItemField);

    Session session = null;

    try {
      session = openSession();

      if (shoppingItemField.isCachedModel()) {
        shoppingItemField =
            (ShoppingItemField)
                session.get(ShoppingItemFieldImpl.class, shoppingItemField.getPrimaryKeyObj());
      }

      session.delete(shoppingItemField);
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    clearCache(shoppingItemField);

    return shoppingItemField;
  }
  /**
   * Creates a new shopping item field with the primary key. Does not add the shopping item field to
   * the database.
   *
   * @param itemFieldId the primary key for the new shopping item field
   * @return the new shopping item field
   */
  public ShoppingItemField create(long itemFieldId) {
    ShoppingItemField shoppingItemField = new ShoppingItemFieldImpl();

    shoppingItemField.setNew(true);
    shoppingItemField.setPrimaryKey(itemFieldId);

    return shoppingItemField;
  }
  @Override
  public ShoppingItemField updateImpl(
      com.liferay.portlet.shopping.model.ShoppingItemField shoppingItemField)
      throws SystemException {
    shoppingItemField = toUnwrappedModel(shoppingItemField);

    boolean isNew = shoppingItemField.isNew();

    ShoppingItemFieldModelImpl shoppingItemFieldModelImpl =
        (ShoppingItemFieldModelImpl) shoppingItemField;

    Session session = null;

    try {
      session = openSession();

      if (shoppingItemField.isNew()) {
        session.save(shoppingItemField);

        shoppingItemField.setNew(false);
      } else {
        session.merge(shoppingItemField);
      }
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

    if (isNew || !ShoppingItemFieldModelImpl.COLUMN_BITMASK_ENABLED) {
      FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    } else {
      if ((shoppingItemFieldModelImpl.getColumnBitmask()
              & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMID.getColumnBitmask())
          != 0) {
        Object[] args = new Object[] {Long.valueOf(shoppingItemFieldModelImpl.getOriginalItemId())};

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ITEMID, args);
        FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMID, args);

        args = new Object[] {Long.valueOf(shoppingItemFieldModelImpl.getItemId())};

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ITEMID, args);
        FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMID, args);
      }
    }

    EntityCacheUtil.putResult(
        ShoppingItemFieldModelImpl.ENTITY_CACHE_ENABLED,
        ShoppingItemFieldImpl.class,
        shoppingItemField.getPrimaryKey(),
        shoppingItemField);

    return shoppingItemField;
  }
  /**
   * Caches the shopping item field in the entity cache if it is enabled.
   *
   * @param shoppingItemField the shopping item field
   */
  public void cacheResult(ShoppingItemField shoppingItemField) {
    EntityCacheUtil.putResult(
        ShoppingItemFieldModelImpl.ENTITY_CACHE_ENABLED,
        ShoppingItemFieldImpl.class,
        shoppingItemField.getPrimaryKey(),
        shoppingItemField);

    shoppingItemField.resetOriginalValues();
  }
  @Override
  public void clearCache(List<ShoppingItemField> shoppingItemFields) {
    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

    for (ShoppingItemField shoppingItemField : shoppingItemFields) {
      EntityCacheUtil.removeResult(
          ShoppingItemFieldModelImpl.ENTITY_CACHE_ENABLED,
          ShoppingItemFieldImpl.class,
          shoppingItemField.getPrimaryKey());
    }
  }
 /**
  * Caches the shopping item fields in the entity cache if it is enabled.
  *
  * @param shoppingItemFields the shopping item fields
  */
 public void cacheResult(List<ShoppingItemField> shoppingItemFields) {
   for (ShoppingItemField shoppingItemField : shoppingItemFields) {
     if (EntityCacheUtil.getResult(
             ShoppingItemFieldModelImpl.ENTITY_CACHE_ENABLED,
             ShoppingItemFieldImpl.class,
             shoppingItemField.getPrimaryKey())
         == null) {
       cacheResult(shoppingItemField);
     } else {
       shoppingItemField.resetOriginalValues();
     }
   }
 }
  protected ShoppingItemField toUnwrappedModel(ShoppingItemField shoppingItemField) {
    if (shoppingItemField instanceof ShoppingItemFieldImpl) {
      return shoppingItemField;
    }

    ShoppingItemFieldImpl shoppingItemFieldImpl = new ShoppingItemFieldImpl();

    shoppingItemFieldImpl.setNew(shoppingItemField.isNew());
    shoppingItemFieldImpl.setPrimaryKey(shoppingItemField.getPrimaryKey());

    shoppingItemFieldImpl.setItemFieldId(shoppingItemField.getItemFieldId());
    shoppingItemFieldImpl.setItemId(shoppingItemField.getItemId());
    shoppingItemFieldImpl.setName(shoppingItemField.getName());
    shoppingItemFieldImpl.setValues(shoppingItemField.getValues());
    shoppingItemFieldImpl.setDescription(shoppingItemField.getDescription());

    return shoppingItemFieldImpl;
  }
  /**
   * Returns an ordered range of all the shopping item fields where itemId = &#63;.
   *
   * <p>Useful when paginating results. Returns a maximum of <code>end - start</code> instances.
   * <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result
   * set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start
   * </code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}
   * will return the full result set.
   *
   * @param itemId the item ID
   * @param start the lower bound of the range of shopping item fields
   * @param end the upper bound of the range of shopping item fields (not inclusive)
   * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
   * @return the ordered range of matching shopping item fields
   * @throws SystemException if a system exception occurred
   */
  public List<ShoppingItemField> findByItemId(
      long itemId, int start, int end, OrderByComparator orderByComparator) throws SystemException {
    FinderPath finderPath = null;
    Object[] finderArgs = null;

    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
      finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMID;
      finderArgs = new Object[] {itemId};
    } else {
      finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ITEMID;
      finderArgs = new Object[] {itemId, start, end, orderByComparator};
    }

    List<ShoppingItemField> list =
        (List<ShoppingItemField>) FinderCacheUtil.getResult(finderPath, finderArgs, this);

    if ((list != null) && !list.isEmpty()) {
      for (ShoppingItemField shoppingItemField : list) {
        if ((itemId != shoppingItemField.getItemId())) {
          list = null;

          break;
        }
      }
    }

    if (list == null) {
      StringBundler query = null;

      if (orderByComparator != null) {
        query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 3));
      } else {
        query = new StringBundler(3);
      }

      query.append(_SQL_SELECT_SHOPPINGITEMFIELD_WHERE);

      query.append(_FINDER_COLUMN_ITEMID_ITEMID_2);

      if (orderByComparator != null) {
        appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
      } else {
        query.append(ShoppingItemFieldModelImpl.ORDER_BY_JPQL);
      }

      String sql = query.toString();

      Session session = null;

      try {
        session = openSession();

        Query q = session.createQuery(sql);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(itemId);

        list = (List<ShoppingItemField>) QueryUtil.list(q, getDialect(), start, end);
      } catch (Exception e) {
        throw processException(e);
      } finally {
        if (list == null) {
          FinderCacheUtil.removeResult(finderPath, finderArgs);
        } else {
          cacheResult(list);

          FinderCacheUtil.putResult(finderPath, finderArgs, list);
        }

        closeSession(session);
      }
    }

    return list;
  }
  @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;
  }
  public ShoppingItemField addShoppingItemField(ShoppingItemField model) throws SystemException {
    ShoppingItemField shoppingItemField = new ShoppingItemFieldImpl();

    shoppingItemField.setNew(true);

    shoppingItemField.setItemFieldId(model.getItemFieldId());
    shoppingItemField.setItemId(model.getItemId());
    shoppingItemField.setName(model.getName());
    shoppingItemField.setValues(model.getValues());
    shoppingItemField.setDescription(model.getDescription());

    return shoppingItemFieldPersistence.update(shoppingItemField);
  }