View Javadoc
1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.logging.log4j.catalog.jpa.converter;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.logging.log4j.catalog.api.Product;
23  import org.apache.logging.log4j.catalog.jpa.model.EventModel;
24  import org.apache.logging.log4j.catalog.jpa.model.ProductModel;
25  import org.apache.logging.log4j.catalog.jpa.service.EventService;
26  import org.modelmapper.AbstractConverter;
27  import org.springframework.beans.factory.annotation.Autowired;
28  import org.springframework.stereotype.Component;
29  
30  @Component
31  public class ProductConverter extends AbstractConverter<Product, ProductModel> {
32  
33      @Autowired
34      private EventService eventService;
35  
36      public ProductModel convert(Product product) {
37          Map<String, EventModel> eventMap = eventService.getEventMap();
38          ProductModel model = new ProductModel();
39          model.setId(product.getId());
40          model.setName(product.getName());
41          model.setDescription(product.getDescription());
42          model.setDisplayName(product.getDisplayName());
43          model.setCatalogId(product.getCatalogId());
44          List<EventModel> events = new ArrayList<>(product.getEvents().size());
45          for (String name : product.getEvents()) {
46              EventModel event = eventMap.get(name);
47              if (event != null) {
48                  events.add(event);
49              } else {
50                  throw new IllegalArgumentException("Unknown event " + name + " for product " + product.getName());
51              }
52          }
53          model.setEvents(events);
54          return model;
55      }
56  }