/** {@inheritDoc} */
  public List<ProductTypeDTO> findBy(final String name, final int page, final int pageSize)
      throws UnmappedInterfaceException, UnableToCreateInstanceException {

    final List<ProductType> entities;

    if (StringUtils.isNotBlank(name)) {

      final Pair<String, String> exactOrCode =
          ComplexSearchUtils.checkSpecialSearch(name, EXACT_OR_CODE);

      if (exactOrCode != null) {

        if ("!".equals(exactOrCode.getFirst())) {

          entities =
              service
                  .getGenericDao()
                  .findByCriteria(
                      page * pageSize,
                      pageSize,
                      Restrictions.or(
                          Restrictions.ilike("guid", exactOrCode.getSecond(), MatchMode.EXACT),
                          Restrictions.ilike("name", exactOrCode.getSecond(), MatchMode.EXACT)));

        } else {

          return findByAttributeCode(exactOrCode.getSecond());
        }

      } else {

        entities =
            service
                .getGenericDao()
                .findByCriteria(
                    page * pageSize,
                    pageSize,
                    Restrictions.or(
                        Restrictions.ilike("guid", name, MatchMode.ANYWHERE),
                        Restrictions.ilike("name", name, MatchMode.ANYWHERE),
                        Restrictions.ilike("description", name, MatchMode.ANYWHERE)));
      }
    } else {
      entities = service.getGenericDao().findByCriteria(page * pageSize, pageSize);
    }
    final List<ProductTypeDTO> dtos = new ArrayList<ProductTypeDTO>(entities.size());
    fillDTOs(entities, dtos);
    return dtos;
  }
Esempio n. 2
0
 /**
  * Create price view.
  *
  * @param id component id.
  * @param pricePair regular / sale price pair.
  * @param currencySymbol currency symbol
  * @param appliedPromos applied promotions
  * @param showCurrencySymbol currency symbol.
  * @param showSavings show user friendly percentage savings
  */
 public PriceView(
     final String id,
     final Pair<BigDecimal, BigDecimal> pricePair,
     final String currencySymbol,
     final String appliedPromos,
     final boolean showCurrencySymbol,
     final boolean showSavings) {
   this(
       id,
       new ProductPriceModelImpl(
           "",
           currencySymbol,
           BigDecimal.ONE,
           pricePair != null ? pricePair.getFirst() : null,
           pricePair != null ? pricePair.getSecond() : null),
       appliedPromos,
       showCurrencySymbol,
       showSavings,
       false,
       false,
       false);
 }
Esempio n. 3
0
  @Override
  protected void onBeforeRender() {

    boolean showSave = false;
    String savePercent = "";
    final String lang = getLocale().getLanguage();

    BigDecimal priceToFormat = productPriceModel.getRegularPrice();
    String cssModificator = "regular";
    if (productPriceModel.getSalePrice() != null
        && MoneyUtils.isFirstBiggerThanSecond(
            productPriceModel.getRegularPrice(), productPriceModel.getSalePrice())) {
      priceToFormat = productPriceModel.getSalePrice();
      cssModificator = "sale";
      showSave = this.showSavings;
      if (showSave) {
        final BigDecimal save =
            MoneyUtils.getDiscountDisplayValue(
                productPriceModel.getRegularPrice(), productPriceModel.getSalePrice());
        savePercent = save.toString();
      }
    }
    final String[] formatted = getFormattedPrice(priceToFormat);

    addOrReplace(
        new Label(WHOLE_LABEL, formatted[0])
            .add(new AttributeModifier(HTML_CLASS, cssModificator + CSS_SUFFIX_WHOLE)));
    addOrReplace(
        new Label(DOT_LABEL, ".")
            .setVisible(
                StringUtils.isNotBlank(formatted[0]) || StringUtils.isNotBlank(formatted[1]))
            .add(new AttributeModifier(HTML_CLASS, cssModificator + CSS_SUFFIX_DOT)));
    addOrReplace(
        new Label(DECIMAL_LABEL, formatted[1])
            .setVisible(
                StringUtils.isNotBlank(formatted[0]) || StringUtils.isNotBlank(formatted[1]))
            .add(new AttributeModifier(HTML_CLASS, cssModificator + CSS_SUFFIX_DECIMAL)));

    final Pair<String, Boolean> symbol =
        currencySymbolService.getCurrencySymbol(productPriceModel.getCurrency());

    addOrReplace(
        new Label(CURRENCY_LABEL, symbol.getFirst())
            .setVisible(showCurrencySymbol && !symbol.getSecond())
            .setEscapeModelStrings(false)
            .add(new AttributeModifier(HTML_CLASS, cssModificator + CSS_SUFFIX_CURRENCY)));

    addOrReplace(
        new Label(CURRENCY2_LABEL, symbol.getFirst())
            .setVisible(showCurrencySymbol && symbol.getSecond())
            .setEscapeModelStrings(false)
            .add(new AttributeModifier(HTML_CLASS, cssModificator + CSS_SUFFIX_CURRENCY)));

    final Map<String, Object> tax = new HashMap<String, Object>();
    if (this.showTaxAmount) {
      tax.put(
          "tax",
          this.productPriceModel.getPriceTax() != null
              ? this.productPriceModel.getPriceTax().toPlainString()
              : Total.ZERO.toPlainString());
    } else {
      tax.put(
          "tax",
          this.productPriceModel.getPriceTaxRate() != null
              ? this.productPriceModel.getPriceTaxRate().stripTrailingZeros().toPlainString() + "%"
              : "0%");
    }
    tax.put("code", this.productPriceModel.getPriceTaxCode());

    final String taxNote = this.showTaxNet ? "taxNoteNet" : "taxNoteGross";

    addOrReplace(
        new Label(TAX_LABEL, WicketUtil.createStringResourceModel(this, taxNote, tax))
            .setVisible(this.showTax)
            .add(new AttributeModifier(HTML_CLASS, cssModificator + CSS_SUFFIX_TAX)));

    final Label discount =
        new Label(
            SAVE_LABEL,
            WicketUtil.createStringResourceModel(
                this,
                "savePercent",
                Collections.<String, Object>singletonMap("discount", savePercent)));

    discount.setVisible(showSave);
    discount.add(new AttributeModifier(HTML_CLASS, "sale-price-save"));

    if (showSave && StringUtils.isNotBlank(promos)) {

      final Map<String, ProductPromotionModel> promoModels =
          productServiceFacade.getPromotionModel(promos);

      final StringBuilder details = new StringBuilder();
      for (final ProductPromotionModel model : promoModels.values()) {

        final String name = model.getName().getValue(lang);
        final String desc = model.getDescription().getValue(lang);

        details.append(name);
        if (model.getCouponCode() != null) {
          details.append(" (").append(model.getCouponCode()).append(")");
        }

        if (StringUtils.isNotBlank(desc)) {
          details.append(": ").append(desc);
        }
        details.append("\n");
      }

      discount.add(new AttributeModifier("title", details.toString()));
    } else {
      discount.add(
          new AttributeModifier(
              "title", WicketUtil.createStringResourceModel(this, "savePercentTitle")));
    }

    addOrReplace(discount);

    addOrReplace(
        new Label(PROMO_LABEL, promos)
            .setVisible(StringUtils.isNotBlank(promos))
            .add(new AttributeModifier(HTML_CLASS, "sale-price-save sale-price-save-details")));

    super.onBeforeRender();
  }
  @Test
  public void testRun() throws Exception {

    final WarehouseService warehouseService =
        ctx().getBean("warehouseService", WarehouseService.class);
    final ProductService productService = ctx().getBean("productService", ProductService.class);
    final SkuWarehouseService skuWarehouseService =
        ctx().getBean("skuWarehouseService", SkuWarehouseService.class);
    final LuceneQueryFactory luceneQueryFactory =
        ctx().getBean("luceneQueryFactory", LuceneQueryFactory.class);

    final List<Warehouse> warehouses = warehouseService.getByShopId(10L);

    Product product = productService.findById(9998L);
    assertEquals(Product.AVAILABILITY_STANDARD, product.getAvailability());

    final String skuCode = product.getDefaultSku().getCode();
    Pair<BigDecimal, BigDecimal> quantity =
        skuWarehouseService.findQuantity(warehouses, product.getDefaultSku().getCode());
    assertTrue(quantity.getFirst().compareTo(BigDecimal.ZERO) > 0);
    assertTrue(quantity.getFirst().compareTo(quantity.getSecond()) > 0);

    final BigDecimal oldQuantity = quantity.getFirst();

    productService.reindexProduct(product.getId());

    final NavigationContext context =
        luceneQueryFactory.getFilteredNavigationQueryChain(
            10L,
            null,
            Collections.singletonMap(
                ProductSearchQueryBuilder.PRODUCT_ID_FIELD, (List) Arrays.asList("9998")));

    List<ProductSearchResultDTO> rez =
        productService
            .getProductSearchResultDTOByQuery(context.getProductQuery(), 0, 1, null, false)
            .getResults();
    assertNotNull(rez);
    assertEquals(1, rez.size());

    getTx()
        .execute(
            new TransactionCallbackWithoutResult() {
              @Override
              protected void doInTransactionWithoutResult(
                  final TransactionStatus transactionStatus) {
                // native update to bypass indexing on save!!
                for (final Warehouse warehouse : warehouses) {
                  productService
                      .getGenericDao()
                      .executeNativeUpdate(
                          "update TSKUWAREHOUSE set QUANTITY = 0"
                              + ", UPDATED_TIMESTAMP = '2099-01-01 00:00:00' where WAREHOUSE_ID = "
                              + warehouse.getWarehouseId()
                              + " and SKU_CODE = '"
                              + skuCode
                              + "'");
                }
              }
            });

    product = productService.findById(9998L);

    quantity =
        skuWarehouseService.findQuantity(
            warehouseService.getByShopId(10L), product.getDefaultSku().getCode());
    assertTrue(quantity.getFirst().compareTo(BigDecimal.ZERO) == 0);
    assertTrue(quantity.getFirst().compareTo(quantity.getSecond()) <= 0);

    getTx()
        .execute(
            new TransactionCallbackWithoutResult() {
              @Override
              protected void doInTransactionWithoutResult(
                  final TransactionStatus transactionStatus) {
                new ProductInventoryChangedProcessorImpl(
                    skuWarehouseService, productService, null, null, null) {
                  @Override
                  protected String getNodeId() {
                    return "TEST";
                  }

                  @Override
                  protected Boolean isLuceneIndexDisabled() {
                    return false;
                  }
                }.doRun(
                    new Date()); // this should reindex product and it will be removed as there is
                                 // no inventory
              }
            });

    final CacheManager mgr = ctx().getBean("cacheManager", CacheManager.class);

    mgr.getCache("productService-productSearchResultDTOByQuery").clear();
    mgr.getCache("productSkuService-productSkuSearchResultDTOByQuery").clear();

    rez =
        productService
            .getProductSearchResultDTOByQuery(context.getProductQuery(), 0, 1, null, false)
            .getResults();
    assertNotNull(rez);
    assertEquals(0, rez.size());

    getTx()
        .execute(
            new TransactionCallbackWithoutResult() {
              @Override
              protected void doInTransactionWithoutResult(
                  final TransactionStatus transactionStatus) {
                // native update to bypass indexing on save!!
                for (final Warehouse warehouse : warehouses) {
                  productService
                      .getGenericDao()
                      .executeNativeUpdate(
                          "update TSKUWAREHOUSE set QUANTITY = "
                              + oldQuantity.toPlainString()
                              + ", UPDATED_TIMESTAMP = '2099-01-01 00:00:00' where WAREHOUSE_ID = "
                              + warehouse.getWarehouseId()
                              + " and SKU_CODE = '"
                              + skuCode
                              + "'");
                }
              }
            });

    product = productService.findById(9998L);

    quantity =
        skuWarehouseService.findQuantity(
            warehouseService.getByShopId(10L), product.getDefaultSku().getCode());
    assertTrue(quantity.getFirst().compareTo(BigDecimal.ZERO) > 0);
    assertTrue(quantity.getFirst().compareTo(quantity.getSecond()) > 0);

    getTx()
        .execute(
            new TransactionCallbackWithoutResult() {
              @Override
              protected void doInTransactionWithoutResult(
                  final TransactionStatus transactionStatus) {
                new ProductInventoryChangedProcessorImpl(
                    skuWarehouseService, productService, null, null, null) {
                  @Override
                  protected String getNodeId() {
                    return "TEST";
                  }

                  @Override
                  protected Boolean isLuceneIndexDisabled() {
                    return false;
                  }
                }.doRun(
                    new Date()); // this should reindex product and it will be removed as there is
                                 // no inventory
              }
            });

    mgr.getCache("productService-productSearchResultDTOByQuery").clear();
    mgr.getCache("productSkuService-productSkuSearchResultDTOByQuery").clear();

    rez =
        productService
            .getProductSearchResultDTOByQuery(context.getProductQuery(), 0, 1, null, false)
            .getResults();
    assertNotNull(rez);
    assertEquals(1, rez.size());
  }