View Javadoc
1   /*
2    *    Copyright 2010-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.jpetstore.service;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.mybatis.jpetstore.domain.Category;
22  import org.mybatis.jpetstore.domain.Item;
23  import org.mybatis.jpetstore.domain.Product;
24  import org.mybatis.jpetstore.mapper.CategoryMapper;
25  import org.mybatis.jpetstore.mapper.ItemMapper;
26  import org.mybatis.jpetstore.mapper.ProductMapper;
27  import org.springframework.stereotype.Service;
28  
29  /**
30   * The Class CatalogService.
31   *
32   * @author Eduardo Macarron
33   */
34  @Service
35  public class CatalogService {
36  
37    /** The category mapper. */
38    private final CategoryMapper categoryMapper;
39    /** The item mapper. */
40    private final ItemMapper itemMapper;
41    /** The product mapper. */
42    private final ProductMapper productMapper;
43  
44    /**
45     * Instantiates a new catalog service.
46     *
47     * @param categoryMapper
48     *          the category mapper
49     * @param itemMapper
50     *          the item mapper
51     * @param productMapper
52     *          the product mapper
53     */
54    public CatalogService(CategoryMapper categoryMapper, ItemMapper itemMapper, ProductMapper productMapper) {
55      this.categoryMapper = categoryMapper;
56      this.itemMapper = itemMapper;
57      this.productMapper = productMapper;
58    }
59  
60    /**
61     * Gets the category list.
62     *
63     * @return the category list
64     */
65    public List<Category> getCategoryList() {
66      return categoryMapper.getCategoryList();
67    }
68  
69    /**
70     * Get category.
71     *
72     * @param categoryId
73     *          the category id
74     *
75     * @return the category
76     */
77    public Category getCategory(String categoryId) {
78      return categoryMapper.getCategory(categoryId);
79    }
80  
81    /**
82     * Get product.
83     *
84     * @param productId
85     *          the product id
86     *
87     * @return the product
88     */
89    public Product getProduct(String productId) {
90      return productMapper.getProduct(productId);
91    }
92  
93    /**
94     * Get product list by category.
95     *
96     * @param categoryId
97     *          the category id
98     *
99     * @return the list
100    */
101   public List<Product> getProductListByCategory(String categoryId) {
102     return productMapper.getProductListByCategory(categoryId);
103   }
104 
105   /**
106    * Search product list.
107    *
108    * @param keywords
109    *          the keywords
110    *
111    * @return the list
112    */
113   public List<Product> searchProductList(String keywords) {
114     List<Product> products = new ArrayList<>();
115     for (String keyword : keywords.split("\\s+")) {
116       products.addAll(productMapper.searchProductList("%" + keyword.toLowerCase() + "%"));
117     }
118     return products;
119   }
120 
121   /**
122    * Get item list by product.
123    *
124    * @param productId
125    *          the product id
126    *
127    * @return the list
128    */
129   public List<Item> getItemListByProduct(String productId) {
130     return itemMapper.getItemListByProduct(productId);
131   }
132 
133   /**
134    * Get item.
135    *
136    * @param itemId
137    *          the item id
138    *
139    * @return the item
140    */
141   public Item getItem(String itemId) {
142     return itemMapper.getItem(itemId);
143   }
144 
145   /**
146    * Is item in stock.
147    *
148    * @param itemId
149    *          the item id
150    *
151    * @return true, if successful
152    */
153   public boolean isItemInStock(String itemId) {
154     return itemMapper.getInventoryQuantity(itemId) > 0;
155   }
156 }