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.HashSet;
19  import java.util.Map;
20  import java.util.Set;
21  import java.util.function.Function;
22  import java.util.stream.Collectors;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.apache.logging.log4j.catalog.api.Attribute;
27  import org.apache.logging.log4j.catalog.api.Constraint;
28  import org.apache.logging.log4j.catalog.jpa.model.AttributeModel;
29  import org.apache.logging.log4j.catalog.jpa.model.ConstraintModel;
30  import org.apache.logging.log4j.catalog.jpa.service.AttributeService;
31  import org.modelmapper.AbstractConverter;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.stereotype.Component;
34  
35  /**
36   *
37   */
38  @Component
39  public class AttributeConverter extends AbstractConverter<Attribute, AttributeModel> {
40      private static Logger LOGGER = LogManager.getLogger(AttributeConverter.class);
41  
42      @Autowired
43      private AttributeService attributeService;
44  
45      public  AttributeModel convert(Attribute attribute) {
46          LOGGER.traceEntry(attribute.getName());
47          AttributeModel model;
48          if (attribute.getId() != null) {
49              model = attributeService.getAttribute(attribute.getId()).orElseGet(AttributeModel::new);
50          } else {
51              model = new AttributeModel();
52          }
53          model.setName(attribute.getName());
54          model.setAliases(attribute.getAliases());
55          model.setDescription(attribute.getDescription());
56          model.setDisplayName(attribute.getDisplayName());
57          model.setDataType(attribute.getDataType());
58          model.setId(attribute.getId());
59          model.setCatalogId(attribute.getCatalogId());
60          model.setIndexed(attribute.isIndexed());
61          model.setRequestContext(attribute.isRequestContext());
62          model.setRequired(attribute.isRequired());
63          model.setSortable(attribute.isSortable());
64          model.setExamples(attribute.getExamples());
65          Set<ConstraintModel> constraintModels = model.getConstraints() != null ? model.getConstraints() :
66                  new HashSet<>();
67          Map<Long, ConstraintModel> constraintMap =
68              constraintModels.stream().collect(Collectors.toMap(ConstraintModel::getId, Function.identity()));
69          if (attribute.getConstraints() != null) {
70              constraintModels.removeIf(a -> attribute.getConstraints().stream().noneMatch(b -> b.getId().equals(a.getId())));
71              for (Constraint constraint : attribute.getConstraints()) {
72                  ConstraintModel constraintModel;
73                  if (constraint.getId() != null) {
74                      constraintModel = constraintMap.get(constraint.getId());
75                      constraintModel.setConstraintType(constraint.getConstraintType().getName());
76                      constraintModel.setValue(constraint.getValue());
77                  } else {
78                      constraintModel = new ConstraintModel();
79                      constraintModel.setConstraintType(constraint.getConstraintType().getName());
80                      constraintModel.setValue(constraint.getValue());
81                      constraintModels.add(constraintModel);
82                  }
83              }
84          }
85          model.setConstraints(constraintModels);
86          return LOGGER.traceExit(model);
87      }
88  }