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  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   * Catalog Product controller
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 }