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  import java.lang.reflect.Type;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  import org.apache.logging.log4j.catalog.api.Category;
29  import org.apache.logging.log4j.catalog.jpa.model.CategoryModel;
30  import org.apache.logging.log4j.catalog.jpa.service.CategoryService;
31  import org.apache.logging.log4j.catalog.jpa.converter.CategoryConverter;
32  import org.apache.logging.log4j.catalog.jpa.converter.CategoryModelConverter;
33  import org.modelmapper.ModelMapper;
34  import org.modelmapper.TypeToken;
35  import org.springframework.beans.factory.annotation.Autowired;
36  import org.springframework.http.HttpStatus;
37  import org.springframework.http.ResponseEntity;
38  import org.springframework.web.bind.annotation.PostMapping;
39  import org.springframework.web.bind.annotation.RequestBody;
40  import org.springframework.web.bind.annotation.RequestMapping;
41  import org.springframework.web.bind.annotation.RequestParam;
42  import org.springframework.web.bind.annotation.RestController;
43  
44  /**
45   * Catalog Category controller
46   */
47  
48  @RequestMapping(value = "/api/categories")
49  @RestController
50  public class CategoryController {
51      private static final Logger LOGGER = LogManager.getLogger();
52  
53      private ModelMapper modelMapper = new ModelMapper();
54  
55      @Autowired
56      private CategoryService categoryService;
57  
58  
59      @Autowired
60      private CategoryModelConverter categoryModelConverter;
61  
62      @Autowired
63      private CategoryConverter categoryConverter;
64  
65      @PostConstruct
66      public void init() {
67          modelMapper.addConverter(categoryModelConverter);
68      }
69  
70      @PostMapping(value = "/list")
71      public ResponseEntity<Map<String, Object>> categoryList(@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<Category>>() {}.getType();
75          Map<String, Object> response = new HashMap<>();
76          try {
77              List<Category> categories = null;
78              if (startIndex == null || pageSize == null) {
79                  categories = modelMapper.map(categoryService.getCategories(), 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                  categories = modelMapper.map(categoryService.getCategories(startPage, pageSize, sortColumn, sortDirection), listType);
95              }
96              if (categories == null) {
97                  categories = new ArrayList<>();
98              }
99              response.put("Result", "OK");
100             response.put("Records", categories);
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>> createCategory(@RequestBody Category category) {
109         Map<String, Object> response = new HashMap<>();
110         try {
111             CategoryModel model = categoryConverter.convert(category);
112             model = categoryService.saveCategory(model);
113             response.put("Result", "OK");
114             response.put("Records", categoryModelConverter.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>> updateCategory(@RequestBody Category category) {
123         Map<String, Object> response = new HashMap<>();
124         try {
125             CategoryModel model = categoryConverter.convert(category);
126             model = categoryService.saveCategory(model);
127             response.put("Result", "OK");
128             response.put("Records", categoryModelConverter.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>> deleteCategory(@RequestBody Category category) {
137         Map<String, Object> response = new HashMap<>();
138         try {
139             categoryService.deleteCategory(category.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 }