Add DAO module for Models
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / ToscaEntityType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca;
22
23 import com.google.gson.annotations.SerializedName;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.TreeMap;
30
31 import javax.persistence.CascadeType;
32 import javax.persistence.Column;
33 import javax.persistence.EmbeddedId;
34 import javax.persistence.Entity;
35 import javax.persistence.Inheritance;
36 import javax.persistence.InheritanceType;
37 import javax.persistence.OneToMany;
38 import javax.persistence.Table;
39
40 import lombok.Data;
41 import lombok.EqualsAndHashCode;
42 import lombok.NonNull;
43
44 import org.onap.policy.common.utils.validation.Assertions;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfValidationResult;
49
50 /**
51  * Class to represent the EntrySchema of list/map property in TOSCA definition.
52  */
53 @Entity
54 @Table(name = "ToscaEntityType")
55 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
56 @Data
57 @EqualsAndHashCode(callSuper = false)
58 public class ToscaEntityType extends PfConcept {
59     private static final long serialVersionUID = -1330661834220739393L;
60
61     @EmbeddedId
62     @NonNull
63     private PfConceptKey key = PfConceptKey.getNullKey();
64
65     @SerializedName("derived_from")
66     @Column(name = "derivedFrom")
67     private PfConceptKey derivedFrom;
68
69     @OneToMany(cascade = CascadeType.ALL)
70     private Map<String, String> metadata;
71
72     @Column(name = "description")
73     private String description;
74
75     /**
76      * The Default Constructor creates a {@link ToscaEntityType} object with a null key.
77      */
78     public ToscaEntityType() {
79         this(new PfConceptKey());
80     }
81
82     /**
83      * The Key Constructor creates a {@link ToscaEntityType} object with the given concept key.
84      *
85      * @param key the key
86      */
87     public ToscaEntityType(@NonNull final PfConceptKey key) {
88         this.key = key;
89     }
90
91     /**
92      * Copy constructor.
93      *
94      * @param copyConcept the concept to copy from
95      */
96     public ToscaEntityType(final ToscaEntityType copyConcept) {
97         super(copyConcept);
98     }
99
100     @Override
101     public List<PfKey> getKeys() {
102         final List<PfKey> keyList = new ArrayList<>();
103         keyList.add(getKey());
104         return keyList;
105     }
106
107     @Override
108     public void clean() {
109         description = description.trim();
110
111         for (Entry<String, String> metadataEntry : metadata.entrySet()) {
112             metadataEntry.setValue(metadataEntry.getValue().trim());
113         }
114     }
115
116     @Override
117     public PfValidationResult validate(PfValidationResult result) {
118         return null;
119     }
120
121     @Override
122     public int compareTo(final PfConcept otherConcept) {
123         if (otherConcept == null) {
124             return -1;
125         }
126         if (this == otherConcept) {
127             return 0;
128         }
129         if (getClass() != otherConcept.getClass()) {
130             return this.hashCode() - otherConcept.hashCode();
131         }
132
133         final ToscaEntityType other = (ToscaEntityType) otherConcept;
134         if (!key.equals(other.key)) {
135             return key.compareTo(other.key);
136         }
137
138         if (!derivedFrom.equals(other.derivedFrom)) {
139             return derivedFrom.compareTo(other.derivedFrom);
140         }
141
142         if (!metadata.equals(other.metadata)) {
143             return (metadata.hashCode() - other.metadata.hashCode());
144         }
145
146         return description.compareTo(other.description);
147     }
148
149     @Override
150     public PfConcept copyTo(@NonNull PfConcept target) {
151         final Object copyObject = target;
152         Assertions.instanceOf(copyObject, PfConcept.class);
153
154         final ToscaEntityType copy = ((ToscaEntityType) copyObject);
155         copy.key = new PfConceptKey(key);
156         copy.derivedFrom = new PfConceptKey(derivedFrom);
157
158         final Map<String, String> newMatadata = new TreeMap<>();
159         for (final Entry<String, String> metadataEntry : metadata.entrySet()) {
160             newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue());
161         }
162         copy.metadata = newMatadata;
163
164         copy.description = description;
165
166         return copy;
167     }
168 }