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.HashSet;
20  import java.util.List;
21  import java.util.Optional;
22  import java.util.Set;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.apache.logging.log4j.catalog.api.Event;
27  import org.apache.logging.log4j.catalog.api.EventAttribute;
28  import org.apache.logging.log4j.catalog.api.constant.Constants;
29  import org.apache.logging.log4j.catalog.api.exception.CatalogModificationException;
30  import org.apache.logging.log4j.catalog.jpa.model.AttributeModel;
31  import org.apache.logging.log4j.catalog.jpa.model.EventAttributeModel;
32  import org.apache.logging.log4j.catalog.jpa.model.EventModel;
33  import org.apache.logging.log4j.catalog.jpa.service.AttributeService;
34  import org.apache.logging.log4j.catalog.jpa.service.EventService;
35  import org.modelmapper.AbstractConverter;
36  import org.springframework.beans.factory.annotation.Autowired;
37  import org.springframework.stereotype.Component;
38  
39  /**
40   *
41   */
42  @Component
43  public class EventConverter extends AbstractConverter<Event, EventModel> {
44      private static Logger LOGGER = LogManager.getLogger(EventConverter.class);
45  
46      @Autowired
47      private EventService eventService;
48  
49      @Autowired
50      private AttributeService attributeService;
51  
52      public  EventModel convert(Event event) {
53          LOGGER.traceEntry(event.getName());
54          EventModel model;
55          if (event.getId() != null) {
56              model = eventService.getEvent(event.getId()).orElseGet(EventModel::new);
57          } else {
58              model = new EventModel();
59          }
60          model.setCatalogId(event.getCatalogId());
61          model.setName(event.getName());
62          model.setAliases(event.getAliases());
63          model.setDescription(event.getDescription());
64          model.setDisplayName(event.getDisplayName());
65          if (model.getAttributes() == null) {
66              model.setAttributes(new HashSet<>());
67          }
68          Set<EventAttributeModel> eventAttributeModels = model.getAttributes() != null ? model.getAttributes() :
69                  new HashSet<>();
70          List<EventAttribute> eventAttributes = event.getAttributes() != null ? event.getAttributes() : new ArrayList<>();
71          if (event.getAttributes() != null) {
72              for (EventAttribute eventAttribute : eventAttributes) {
73                  EventAttributeModel eventAttributeModel = model.getAttribute(eventAttribute.getName());
74                  if (eventAttributeModel != null) {
75                      eventAttributeModel.setRequired(eventAttribute != null ? eventAttribute.isRequired() : null);
76                  } else {
77                      Optional<AttributeModel> optional = getAttribute(event.getCatalogId(), eventAttribute.getName());
78                      if (optional.isPresent()) {
79                          eventAttributeModel = new EventAttributeModel();
80                          if (eventAttribute != null) {
81                              eventAttributeModel.setRequired(eventAttribute.isRequired());
82                          }
83                          eventAttributeModel.setEvent(model);
84                          eventAttributeModel.setAttribute(optional.get());
85                          eventAttributeModels.add(eventAttributeModel);
86                      } else {
87                          throw new CatalogModificationException("No catalog entry for " + eventAttribute.getName());
88                      }
89                  }
90              }
91          }
92          eventAttributeModels.removeIf(a -> eventAttributes.stream().noneMatch(b -> b.getName().equals(a.getAttribute().getName())));
93          model.setAttributes(eventAttributeModels);
94          return LOGGER.traceExit(model);
95      }
96  
97      private Optional<AttributeModel> getAttribute(String catalogId, String name) {
98          Optional<AttributeModel> optional = attributeService.getAttribute(catalogId, name);
99          if (!optional.isPresent()) {
100             optional = attributeService.getAttribute(Constants.DEFAULT_CATALOG, name);
101         }
102         return optional;
103     }
104 }