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 java.text.ParseException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.apache.logging.log4j.catalog.api.dao.CatalogDao;
28  import org.apache.logging.log4j.catalog.api.Attribute;
29  import org.apache.logging.log4j.catalog.api.Category;
30  import org.apache.logging.log4j.catalog.api.Event;
31  import org.apache.logging.log4j.catalog.api.Product;
32  import org.apache.logging.log4j.catalog.jpa.converter.AttributeModelConverter;
33  import org.apache.logging.log4j.catalog.jpa.converter.CategoryModelConverter;
34  import org.apache.logging.log4j.catalog.jpa.converter.EventModelConverter;
35  import org.apache.logging.log4j.catalog.jpa.converter.ProductModelConverter;
36  import org.apache.logging.log4j.catalog.jpa.model.AttributeModel;
37  import org.apache.logging.log4j.catalog.jpa.model.CategoryModel;
38  import org.apache.logging.log4j.catalog.jpa.model.EventModel;
39  import org.apache.logging.log4j.catalog.jpa.model.ProductModel;
40  import org.apache.logging.log4j.catalog.jpa.service.AttributeService;
41  import org.apache.logging.log4j.catalog.jpa.service.CategoryService;
42  import org.apache.logging.log4j.catalog.jpa.service.EventService;
43  import org.apache.logging.log4j.catalog.jpa.service.ProductService;
44  import org.springframework.beans.factory.annotation.Autowired;
45  import org.springframework.http.HttpStatus;
46  import org.springframework.http.ResponseEntity;
47  import org.springframework.web.bind.annotation.PathVariable;
48  import org.springframework.web.bind.annotation.PostMapping;
49  import org.springframework.web.bind.annotation.RequestMapping;
50  import org.springframework.web.bind.annotation.RequestMethod;
51  import org.springframework.web.bind.annotation.RequestParam;
52  
53  import org.apache.logging.log4j.catalog.api.CatalogData;
54  import org.springframework.web.bind.annotation.RestController;
55  
56  /**
57   * The Class CatalogController.
58   */
59  @RestController
60  public class CatalogController {
61  
62  	/** The logger. */
63  	private static Logger logger = LogManager.getLogger(CatalogController.class);
64  
65  	@Autowired
66  	private EventService eventService;
67  
68  	@Autowired
69  	private AttributeService attributeService;
70  
71  	@Autowired
72  	private ProductService productService;
73  
74  	@Autowired
75  	private CategoryService categoryService;
76  
77  	@Autowired
78  	private AttributeModelConverter attributeModelConverter;
79  
80  	@Autowired
81  	private EventModelConverter eventModelConverter;
82  
83  	@Autowired
84  	private ProductModelConverter productModelConverter;
85  
86  	@Autowired
87  	private CategoryModelConverter categoryModelConverter;
88  
89  	@Autowired
90  	private CatalogDao catalogDao;
91  
92  
93  	@PostMapping(value = "catalog")
94  	public ResponseEntity<?> saveCatalog() {
95  		CatalogData catalogData = new CatalogData();
96  		List<Attribute> attributes = new ArrayList<>();
97  		for (AttributeModel attributeModel : attributeService.getAttributes()) {
98  			attributes.add(attributeModelConverter.convert(attributeModel));
99  		}
100 		catalogData.setAttributes(attributes);
101 		List<Event> events = new ArrayList<>();
102 		for (EventModel eventModel : eventService.getEvents()) {
103 			events.add(eventModelConverter.convert(eventModel));
104 		}
105 		catalogData.setEvents(events);
106 		List<Category> categories = new ArrayList<>();
107 		for (CategoryModel categoryModel : categoryService.getCategories()) {
108 			categories.add(categoryModelConverter.convert(categoryModel));
109 		}
110 		catalogData.setCategories(categories);
111 		List<Product> products = new ArrayList<>();
112 		for (ProductModel productModel : productService.getProducts()) {
113 			products.add(productModelConverter.convert(productModel));
114 		}
115 		catalogData.setProducts(products);
116 		catalogDao.write(catalogData);
117 		return new ResponseEntity<>(HttpStatus.NO_CONTENT);
118 	}
119 /*
120 	@RequestMapping(value = "/catalog", method = RequestMethod.GET)
121 	public ResponseEntity<Object> handleGetCatalog(
122 			@RequestParam(required = false) boolean attributeDetails,
123 			HttpServletRequest servletRequest) {
124 		CatalogData catalogData = null;
125 		try {
126 			//catalogData = globalLoggingCatalog.getCatalog();
127 			if (attributeDetails) {
128 				getAttributeDetailsForEvents(catalogData);
129 			}
130 			return new ResponseEntity<Object>(catalogData, HttpStatus.OK);
131 
132 		} catch (Exception e) {
133 			logger.error("Error While Retrieving Data", e);
134 
135 			Status status = new Status();
136 			ErrorInfo errorInfo = new ErrorInfo();
137 			errorInfo.setErrorCode("00000");
138 			errorInfo.setErrorMessage(e.getMessage());
139 			status.getErrorInfo().add(errorInfo);
140 			status.setStatusMessage(e.getMessage());
141 			return new ResponseEntity<Object>(status,
142 					HttpStatus.INTERNAL_SERVER_ERROR);
143 		}
144 
145 
146 	} */
147 
148 }