View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.catalog.controller;
18  
19  import javax.annotation.PostConstruct;
20  
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  import org.apache.logging.log4j.catalog.api.Product;
24  import org.apache.logging.log4j.catalog.jpa.model.ProductModel;
25  import org.apache.logging.log4j.catalog.jpa.service.ProductService;
26  import org.apache.logging.log4j.catalog.jpa.converter.ProductConverter;
27  import org.apache.logging.log4j.catalog.jpa.converter.ProductModelConverter;
28  import org.modelmapper.ModelMapper;
29  import org.modelmapper.TypeToken;
30  import org.springframework.beans.factory.annotation.Autowired;
31  import org.springframework.http.HttpStatus;
32  import org.springframework.http.ResponseEntity;
33  import org.springframework.web.bind.annotation.PostMapping;
34  import org.springframework.web.bind.annotation.RequestBody;
35  import org.springframework.web.bind.annotation.RequestMapping;
36  import org.springframework.web.bind.annotation.RequestParam;
37  import org.springframework.web.bind.annotation.RestController;
38  
39  import java.lang.reflect.Type;
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * Catalog Product controller
47   */
48  
49  @RequestMapping(value = "/api/products")
50  @RestController
51  public class ProductController {
52      private static final Logger LOGGER = LogManager.getLogger();
53  
54      private ModelMapper modelMapper = new ModelMapper();
55  
56      @Autowired
57      private ProductService productService;
58  
59      @Autowired
60      private ProductModelConverter productModelConverter;
61  
62      @Autowired
63      private ProductConverter productConverter;
64  
65      @PostConstruct
66      public void init() {
67          modelMapper.addConverter(productModelConverter);
68      }
69  
70      @PostMapping(value = "/list")
71      public ResponseEntity<Map<String, Object>> productList(@RequestParam(value="jtStartIndex", required=false) Integer startIndex,
72                                                             @RequestParam(value="jtPageSize", required=false) Integer pageSize,
73                                                             @RequestParam(value="jtSorting", required=false) String sorting) {
74          Type listType = new TypeToken<List<Product>>() {}.getType();
75          Map<String, Object> response = new HashMap<>();
76          try {
77              List<Product> products = null;
78              if (startIndex == null || pageSize == null) {
79                  products = modelMapper.map(productService.getProducts(), listType);
80              } else {
81                  int startPage = 0;
82                  if (startIndex > 0) {
83                      startPage = startIndex / pageSize;
84                  }
85                  String sortColumn = "name";
86                  String sortDirection = "ASC";
87                  if (sorting != null) {
88                      String[] sortInfo = sorting.split(" ");
89                      sortColumn = sortInfo[0];
90                      if (sortInfo.length > 0) {
91                          sortDirection = sortInfo[1];
92                      }
93                  }
94                  products = modelMapper.map(productService.getProducts(startPage, pageSize, sortColumn, sortDirection), listType);
95              }
96              if (products == null) {
97                  products = new ArrayList<>();
98              }
99              response.put("Result", "OK");
100             response.put("Records", products);
101         } catch (Exception ex) {
102             response.put("Result", "FAILURE");
103         }
104         return new ResponseEntity<>(response, HttpStatus.OK);
105     }
106 
107     @PostMapping(value = "/create")
108     public ResponseEntity<Map<String, Object>> createProduct(@RequestBody Product product) {
109         Map<String, Object> response = new HashMap<>();
110         try {
111             ProductModel model = productConverter.convert(product);
112             model = productService.saveProduct(model);
113             response.put("Result", "OK");
114             response.put("Records", productModelConverter.convert(model));
115         } catch (Exception ex) {
116             response.put("Result", "FAILURE");
117         }
118         return new ResponseEntity<>(response, HttpStatus.OK);
119     }
120 
121     @PostMapping(value = "/update")
122     public ResponseEntity<Map<String, Object>> updateProduct(@RequestBody Product product) {
123         Map<String, Object> response = new HashMap<>();
124         try {
125             ProductModel model = productConverter.convert(product);
126             model = productService.saveProduct(model);
127             response.put("Result", "OK");
128             response.put("Records", productModelConverter.convert(model));
129         } catch (Exception ex) {
130             response.put("Result", "FAILURE");
131         }
132         return new ResponseEntity<>(response, HttpStatus.OK);
133     }
134 
135     @PostMapping(value = "/delete")
136     public ResponseEntity<Map<String, Object>> deleteProduct(@RequestBody Product product) {
137         Map<String, Object> response = new HashMap<>();
138         try {
139             productService.deleteProduct(product.getId());
140             response.put("Result", "OK");
141         } catch (Exception ex) {
142             response.put("Result", "FAILURE");
143         }
144         return new ResponseEntity<>(response, HttpStatus.OK);
145     }
146 }