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.service;
18  
19  import javax.annotation.PostConstruct;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
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.CatalogData;
30  import org.apache.logging.log4j.catalog.api.Category;
31  import org.apache.logging.log4j.catalog.api.Event;
32  import org.apache.logging.log4j.catalog.api.Product;
33  import org.apache.logging.log4j.catalog.jpa.converter.AttributeConverter;
34  import org.apache.logging.log4j.catalog.jpa.converter.CategoryConverter;
35  import org.apache.logging.log4j.catalog.jpa.converter.EventConverter;
36  import org.apache.logging.log4j.catalog.jpa.converter.ProductConverter;
37  import org.apache.logging.log4j.catalog.jpa.model.AttributeModel;
38  import org.apache.logging.log4j.catalog.jpa.model.CategoryModel;
39  import org.apache.logging.log4j.catalog.jpa.model.EventModel;
40  import org.apache.logging.log4j.catalog.jpa.model.ProductModel;
41  import org.apache.logging.log4j.catalog.jpa.service.AttributeService;
42  import org.apache.logging.log4j.catalog.jpa.service.CategoryService;
43  import org.apache.logging.log4j.catalog.jpa.service.EventService;
44  import org.apache.logging.log4j.catalog.jpa.service.ProductService;
45  import org.springframework.beans.factory.annotation.Autowired;
46  import org.springframework.stereotype.Component;
47  
48  /**
49   *
50   */
51  @Component
52  public class CatalogInitializer {
53      private static final Logger logger = LogManager.getLogger(CatalogInitializer.class);
54  
55      @Autowired
56      AttributeService attributeService;
57  
58      @Autowired
59      EventService eventService;
60  
61      @Autowired
62      CategoryService categoryService;
63  
64      @Autowired
65      ProductService productService;
66  
67      @Autowired
68      CatalogDao catalogDao;
69  
70      @Autowired
71      AttributeConverter attributeConverter;
72  
73      @Autowired
74      EventConverter eventConverter;
75  
76      @Autowired
77      CategoryConverter categoryConverter;
78  
79      @Autowired
80      ProductConverter productConverter;
81  
82      @PostConstruct
83      private void initialize() {
84          logger.debug("Performing initialization");
85          CatalogData catalogData = catalogDao.read();
86  
87          logger.debug("Loading attributes");
88          List<AttributeModel> attributeModels = new ArrayList<>();
89          for (Attribute attribute : catalogData.getAttributes()) {
90              AttributeModel model = attributeConverter.convert(attribute);
91              attributeService.saveAttribute(model);
92              attributeModels.add(model);
93          }
94          Map<String, EventModel> eventMap = new HashMap<>();
95          logger.debug("Loading events");
96          for (Event event : catalogData.getEvents()) {
97              logger.debug("Processing Event: {}", event);
98              EventModel model = eventConverter.convert(event);
99              logger.debug("Created EventModel: {} ", model);
100             eventMap.put(event.getName(), model);
101             eventService.saveEvent(model);
102         }
103         logger.debug("Loading categories");
104         for (Category category : catalogData.getCategories()) {
105             CategoryModel model = categoryConverter.convert(category);
106             categoryService.saveCategory(model);
107         }
108         logger.debug("loading products");
109         for (Product product : catalogData.getProducts()) {
110             ProductModel model = productConverter.convert(product);
111             productService.saveProduct(model);
112         }
113     }
114 }