Enable UI component to display property constraints
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / mapper / PropertyDefinitionDtoMapper.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.model.mapper;
23
24 import com.google.gson.Gson;
25 import java.util.ArrayList;
26 import java.util.List;
27 import lombok.AccessLevel;
28 import lombok.NoArgsConstructor;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
33 import org.openecomp.sdc.be.model.PropertyConstraint;
34 import org.openecomp.sdc.be.model.PropertyDefinition;
35 import org.openecomp.sdc.be.model.dto.PropertyDefinitionDto;
36
37 @NoArgsConstructor(access = AccessLevel.PRIVATE)
38 public class PropertyDefinitionDtoMapper {
39
40     public static PropertyDefinition mapTo(final PropertyDefinitionDto propertyDefinitionDto) {
41         final var propertyDefinition = new PropertyDefinition();
42         propertyDefinition.setUniqueId(propertyDefinitionDto.getUniqueId());
43         propertyDefinition.setType(propertyDefinitionDto.getType());
44         propertyDefinition.setRequired(propertyDefinitionDto.getRequired());
45         propertyDefinition.setName(propertyDefinitionDto.getName());
46         if (StringUtils.isNotBlank(propertyDefinitionDto.getSchemaType())) {
47             final PropertyDefinition schemaProperty = new PropertyDefinition();
48             schemaProperty.setType(propertyDefinitionDto.getSchemaType());
49             final SchemaDefinition schema = new SchemaDefinition();
50             schema.setProperty(schemaProperty);
51             propertyDefinition.setSchema(schema);
52         }
53         if (CollectionUtils.isNotEmpty(propertyDefinitionDto.getConstraints())) {
54             final List<PropertyConstraint> propertyConstraints = new ArrayList<>();
55             propertyDefinition.setConstraints(propertyConstraints);
56             propertyConstraints.addAll(propertyDefinitionDto.getConstraints());
57         }
58         propertyDefinition.setDescription(propertyDefinitionDto.getDescription());
59         propertyDefinition.setValue(new Gson().toJson(propertyDefinitionDto.getValue()));
60         propertyDefinition.setDefaultValue(new Gson().toJson(propertyDefinitionDto.getDefaultValue()));
61         return propertyDefinition;
62     }
63
64     public static PropertyDefinitionDto mapFrom(final PropertyDataDefinition propertyDataDefinition) {
65         final var propertyDefinition = new PropertyDefinition(propertyDataDefinition);
66         final var propertyDefinitionDto = new PropertyDefinitionDto();
67         propertyDefinitionDto.setUniqueId(propertyDefinition.getUniqueId());
68         propertyDefinitionDto.setName(propertyDefinition.getName());
69         propertyDefinitionDto.setType(propertyDefinition.getType());
70         propertyDefinitionDto.setDescription(propertyDefinition.getDescription());
71         propertyDefinitionDto.setRequired(propertyDefinition.getRequired());
72         propertyDefinitionDto.setSchemaType(propertyDefinition.getSchemaType());
73         if (CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) {
74             final List<PropertyConstraint> propertyConstraints = new ArrayList<>();
75             propertyDefinitionDto.setConstraints(propertyConstraints);
76             propertyConstraints.addAll(propertyDefinition.getConstraints());
77         }
78         propertyDefinitionDto.setValue(new Gson().fromJson(propertyDataDefinition.getValue(), Object.class));
79         propertyDefinitionDto.setDefaultValue(new Gson().fromJson(propertyDataDefinition.getDefaultValue(), Object.class));
80         return propertyDefinitionDto;
81     }
82 }