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.Set;
20  
21  import org.apache.logging.log4j.catalog.api.Attribute;
22  import org.apache.logging.log4j.catalog.api.Constraint;
23  import org.apache.logging.log4j.catalog.api.plugins.ConstraintPlugins;
24  import org.apache.logging.log4j.catalog.jpa.model.AttributeModel;
25  import org.apache.logging.log4j.catalog.jpa.model.ConstraintModel;
26  import org.modelmapper.AbstractConverter;
27  import org.springframework.stereotype.Component;
28  
29  /**
30   *
31   */
32  @Component
33  public class AttributeModelConverter extends AbstractConverter<AttributeModel, Attribute> {
34  
35      public  Attribute convert(AttributeModel model) {
36          Attribute attribute = new Attribute();
37          attribute.setName(model.getName());
38          attribute.setDisplayName(model.getDisplayName());
39          attribute.setDescription(model.getDescription());
40          attribute.setAliases(model.getAliases());
41          attribute.setId(model.getId());
42          attribute.setCatalogId(model.getCatalogId());
43          attribute.setIndexed(model.isIndexed());
44          attribute.setSortable(model.isSortable());
45          attribute.setRequestContext(model.isRequestContext());
46          attribute.setRequired(model.isRequired());
47          attribute.setDataType(model.getDataType());
48          Set<ConstraintModel> constraintModels = model.getConstraints();
49          Set<Constraint> constraints = new HashSet<>();
50          if (constraintModels != null) {
51              for (ConstraintModel constraintModel : constraintModels) {
52                  Constraint constraint = new Constraint();
53                  constraint.setId(constraintModel.getId());
54                  constraint.setConstraintType(ConstraintPlugins.getInstance().findByName(constraintModel.getConstraintType()));
55                  constraint.setValue(constraintModel.getValue());
56                  constraints.add(constraint);
57              }
58          }
59          attribute.setConstraints(constraints);
60          return attribute;
61      }
62  }