1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 import java.util.Optional;
26 import java.util.Set;
27
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.apache.logging.log4j.catalog.api.Attribute;
31 import org.apache.logging.log4j.catalog.api.Constraint;
32 import org.apache.logging.log4j.catalog.api.ListResponse;
33 import org.apache.logging.log4j.catalog.api.plugins.ConstraintPlugins;
34 import org.apache.logging.log4j.catalog.jpa.converter.AttributeConverter;
35 import org.apache.logging.log4j.catalog.jpa.converter.AttributeModelConverter;
36 import org.apache.logging.log4j.catalog.jpa.model.AttributeModel;
37 import org.apache.logging.log4j.catalog.jpa.model.ConstraintModel;
38 import org.apache.logging.log4j.catalog.jpa.service.AttributeService;
39 import org.modelmapper.ModelMapper;
40 import org.modelmapper.TypeToken;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PostMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import org.springframework.web.bind.annotation.RequestParam;
49 import org.springframework.web.bind.annotation.RestController;
50
51
52
53
54
55 @RequestMapping(value = "/api/attributes")
56 @RestController
57 public class AttributeController {
58 private static final Logger LOGGER = LogManager.getLogger();
59 private static ConstraintPlugins constraintPlugins = ConstraintPlugins.getInstance();
60
61 private ModelMapper modelMapper = new ModelMapper();
62
63 @Autowired
64 private AttributeService attributeService;
65
66 @Autowired
67 private AttributeModelConverter attributeModelConverter;
68
69 @Autowired
70 private AttributeConverter attributeConverter;
71
72 @PostConstruct
73 public void init() {
74 modelMapper.addConverter(attributeModelConverter);
75 }
76
77 @PostMapping(value = "/list")
78 public ResponseEntity<Map<String, Object>> attributeList(@RequestParam(value="jtStartIndex", required=false) Integer startIndex,
79 @RequestParam(value="jtPageSize", required=false) Integer pageSize,
80 @RequestParam(value="jtSorting", required=false) String sorting) {
81 Type listType = new TypeToken<List<Attribute>>() {}.getType();
82 Map<String, Object> response = new HashMap<>();
83 try {
84 List<Attribute> attributes = null;
85 if (startIndex == null || pageSize == null) {
86 attributes = modelMapper.map(attributeService.getAttributes(), listType);
87 } else {
88 int startPage = 0;
89 if (startIndex > 0) {
90 startPage = startIndex / pageSize;
91 }
92 String sortColumn = "name";
93 String sortDirection = "ASC";
94 if (sorting != null) {
95 String[] sortInfo = sorting.split(" ");
96 sortColumn = sortInfo[0];
97 if (sortInfo.length > 0) {
98 sortDirection = sortInfo[1];
99 }
100 }
101 attributes = modelMapper.map(attributeService.getAttributes(startPage, pageSize, sortColumn, sortDirection), listType);
102 }
103 if (attributes == null) {
104 attributes = new ArrayList<>();
105 }
106 response.put("Result", "OK");
107 response.put("Records", attributes);
108 } catch (Exception ex) {
109 response.put("Result", "FAILURE");
110 }
111 return new ResponseEntity<>(response, HttpStatus.OK);
112 }
113
114 @PostMapping(value = "/create")
115 public ResponseEntity<Map<String, Object>> createAttribute(@RequestBody Attribute attribute) {
116 Map<String, Object> response = new HashMap<>();
117 try {
118 AttributeModel model = attributeConverter.convert(attribute);
119 model = attributeService.saveAttribute(model);
120 Attribute result = attributeModelConverter.convert(model);
121 response.put("Result", "OK");
122 response.put("Records", result);
123 } catch (Exception ex) {
124 response.put("Result", "FAILURE");
125 }
126 return new ResponseEntity<>(response, HttpStatus.OK);
127 }
128
129 @PostMapping(value = "/update")
130 public ResponseEntity<Map<String, Object>> updateAttribute(@RequestBody Attribute attribute) {
131 Map<String, Object> response = new HashMap<>();
132 try {
133 AttributeModel model = attributeConverter.convert(attribute);
134 model = attributeService.saveAttribute(model);
135 Attribute result = attributeModelConverter.convert(model);
136 response.put("Result", "OK");
137 response.put("Records", result);
138 } catch (Exception ex) {
139 response.put("Result", "FAILURE");
140 }
141 return new ResponseEntity<>(response, HttpStatus.OK);
142 }
143
144 @PostMapping(value = "/delete")
145 public ResponseEntity<Map<String, Object>> deleteAttribute(@RequestBody Attribute attribute) {
146 Map<String, Object> response = new HashMap<>();
147 try {
148 attributeService.deleteAttribute(attribute.getId());
149 response.put("Result", "OK");
150 } catch (Exception ex) {
151 response.put("Result", "FAILURE");
152 }
153 return new ResponseEntity<>(response, HttpStatus.OK);
154 }
155
156 @GetMapping
157 public ResponseEntity<ListResponse<String>> getAttributeNames() {
158 List<AttributeModel> attributes = attributeService.getAttributes();
159 List<String> attributeNames;
160 if (attributes != null) {
161 attributeNames = new ArrayList<>(attributes.size());
162 for (AttributeModel model : attributes) {
163 attributeNames.add(model.getName());
164 }
165 } else {
166 attributeNames = new ArrayList<>();
167 }
168 ListResponse<String> response = new ListResponse<>();
169 response.setResult("OK");
170 response.setData(attributeNames);
171 return new ResponseEntity<>(response, HttpStatus.OK);
172 }
173
174 @PostMapping(value = "/constraints")
175 public ResponseEntity<Map<String, Object>> constraintList(@RequestParam("attributeId") Long attributeId) {
176 Type listType = new TypeToken<List<Constraint>>() {}.getType();
177 Map<String, Object> response = new HashMap<>();
178 try {
179 Optional<AttributeModel> optional = attributeService.getAttribute(attributeId);
180 List<Constraint> constraints = new ArrayList<>();
181 if (optional.isPresent()) {
182 Set<ConstraintModel> constraintModels = optional.get().getConstraints();
183 if (constraintModels != null) {
184 for (ConstraintModel constraintModel : constraintModels) {
185 Constraint constraint = new Constraint();
186 constraint.setConstraintType(constraintPlugins.findByName(constraintModel.getConstraintType()));
187 constraint.setValue(constraintModel.getValue());
188 constraints.add(constraint);
189 }
190 }
191 response.put("Result", "OK");
192 response.put("Records", constraints);
193 }
194 } catch (Exception ex) {
195 response.put("Result", "FAILURE");
196 }
197 return new ResponseEntity<>(response, HttpStatus.OK);
198 }
199 }