1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.catalog.jpa.service;
18
19 import java.time.LocalDateTime;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Optional;
23
24 import org.apache.logging.log4j.catalog.api.Attribute;
25 import org.apache.logging.log4j.catalog.api.CatalogData;
26 import org.apache.logging.log4j.catalog.api.Category;
27 import org.apache.logging.log4j.catalog.api.Event;
28 import org.apache.logging.log4j.catalog.api.Product;
29 import org.apache.logging.log4j.catalog.api.plugins.ConstraintPlugins;
30 import org.apache.logging.log4j.catalog.jpa.converter.AttributeModelConverter;
31 import org.apache.logging.log4j.catalog.jpa.converter.CategoryModelConverter;
32 import org.apache.logging.log4j.catalog.jpa.converter.EventModelConverter;
33 import org.apache.logging.log4j.catalog.jpa.converter.ProductModelConverter;
34 import org.apache.logging.log4j.catalog.jpa.dao.AttributeRepository;
35 import org.apache.logging.log4j.catalog.jpa.dao.CatalogRepository;
36 import org.apache.logging.log4j.catalog.jpa.dao.CategoryRepository;
37 import org.apache.logging.log4j.catalog.jpa.dao.EventRepository;
38 import org.apache.logging.log4j.catalog.jpa.dao.ProductRepository;
39 import org.apache.logging.log4j.catalog.jpa.model.AttributeModel;
40 import org.apache.logging.log4j.catalog.jpa.model.CatalogModel;
41 import org.apache.logging.log4j.catalog.jpa.model.CategoryModel;
42 import org.apache.logging.log4j.catalog.jpa.model.EventModel;
43 import org.apache.logging.log4j.catalog.jpa.model.ProductModel;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Repository;
46 import org.springframework.stereotype.Service;
47 import org.springframework.transaction.annotation.Transactional;
48
49 @Service
50 @Repository
51 @Transactional(readOnly = false)
52 public class CatalogServiceImpl implements CatalogService {
53
54 private static final ConstraintPlugins constraintPlugins = ConstraintPlugins.getInstance();
55
56 @Autowired
57 private AttributeRepository attributeRepository;
58 @Autowired
59 private EventRepository eventRepository;
60 @Autowired
61 private CategoryRepository categoryRepository;
62 @Autowired
63 private ProductRepository productRepository;
64 @Autowired
65 private AttributeModelConverter attributeModelConverter;
66 @Autowired
67 private EventModelConverter eventModelConverter;
68 @Autowired
69 private CategoryModelConverter categoryModelConverter;
70 @Autowired
71 private ProductModelConverter productModelConverter;
72 @Autowired
73 private CatalogRepository catalogRepository;
74
75
76 public CatalogData getCatalogData() {
77 CatalogData data = new CatalogData();
78
79 List<AttributeModel> modelAttributes = attributeRepository.findAll();
80 List<org.apache.logging.log4j.catalog.api.Attribute> attributes = new ArrayList<>(modelAttributes.size());
81 for (AttributeModel modelAttribute : modelAttributes) {
82 Attribute attribute = attributeModelConverter.convert(modelAttribute);
83 attributes.add(attribute);
84 }
85 data.setAttributes(attributes);
86
87 List<EventModel> modelEvents = eventRepository.findAll();
88 List<org.apache.logging.log4j.catalog.api.Event> events = new ArrayList<>(modelEvents.size());
89 for (EventModel modelEvent : modelEvents) {
90 Event event = eventModelConverter.convert(modelEvent);
91 events.add(event);
92 }
93 data.setEvents(events);
94
95 List<CategoryModel> modelCategories = categoryRepository.findAll();
96 List<org.apache.logging.log4j.catalog.api.Category> categories = new ArrayList<>(modelCategories.size());
97 for (CategoryModel modelCategory : modelCategories) {
98 Category category = categoryModelConverter.convert(modelCategory);
99 categories.add(category);
100 }
101 data.setCategories(categories);
102
103 List<ProductModel> modelProducts = productRepository.findAll();
104 List<Product> products = new ArrayList<>(modelProducts.size());
105 for (ProductModel modelProduct : modelProducts) {
106 Product product = productModelConverter.convert(modelProduct);
107 products.add(product);
108 }
109 data.setProducts(products);
110
111 return data;
112 }
113
114 public List<CategoryModel> getCategories() {
115 return categoryRepository.findAll();
116 }
117
118 public Optional<CategoryModel> getCategory(String name) {
119 return categoryRepository.findByName(name);
120 }
121
122 public Optional<CategoryModel> getCategory(long id) {
123 return categoryRepository.findOne(id);
124 }
125
126 @Override
127 public CatalogModel getCatalogModel() {
128 List<CatalogModel> catalogModels = catalogRepository.findAll();
129 if (catalogModels != null && catalogModels.size() > 0) {
130 return catalogModels.get(0);
131 }
132 return null;
133 }
134
135 @Override
136 public void saveCatalog(CatalogModel catalogModel) {
137 catalogRepository.save(catalogModel);
138 }
139 }