Merge "Fix duplicate code in Jpa classes"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import java.util.List;
27 import javax.persistence.ElementCollection;
28 import javax.persistence.Entity;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.Table;
32 import lombok.Data;
33 import lombok.EqualsAndHashCode;
34 import lombok.NoArgsConstructor;
35 import lombok.NonNull;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.common.parameters.annotations.Valid;
39 import org.onap.policy.models.base.PfConcept;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
44
45 /**
46  * Class to represent the policy type in TOSCA definition.
47  *
48  * @author Chenfei Gao (cgao@research.att.com)
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51
52 @Entity
53 @Table(name = "ToscaPolicyType")
54 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
55 @Data
56 @EqualsAndHashCode(callSuper = true)
57 @NoArgsConstructor
58 public class JpaToscaPolicyType extends JpaToscaWithToscaProperties<ToscaPolicyType> {
59     private static final long serialVersionUID = -563659852901842616L;
60
61     @ElementCollection
62     private List<@NotNull @Valid PfConceptKey> targets;
63
64     @ElementCollection
65     private List<@NotNull @Valid JpaToscaTrigger> triggers;
66
67     /**
68      * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
69      *
70      * @param key the key
71      */
72     public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
73         super(key);
74     }
75
76     /**
77      * Copy constructor.
78      *
79      * @param copyConcept the concept to copy from
80      */
81     public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
82         super(copyConcept);
83         this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
84         this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
85     }
86
87     /**
88      * Authorative constructor.
89      *
90      * @param authorativeConcept the authorative concept to copy from
91      */
92     public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
93         super(authorativeConcept);
94     }
95
96     @Override
97     public ToscaPolicyType toAuthorative() {
98         ToscaPolicyType toscaPolicyType = new ToscaPolicyType();
99         super.setToscaEntity(toscaPolicyType);
100         super.toAuthorative();
101
102         // TODO need to copy targets & triggers?
103
104         return toscaPolicyType;
105     }
106
107     @Override
108     public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
109         super.fromAuthorative(toscaPolicyType);
110
111         // TODO need to copy targets & triggers?
112     }
113
114     @Override
115     public List<PfKey> getKeys() {
116         final List<PfKey> keyList = super.getKeys();
117
118         if (targets != null) {
119             keyList.addAll(targets);
120         }
121
122         if (triggers != null) {
123             for (JpaToscaTrigger trigger : triggers) {
124                 keyList.addAll(trigger.getKeys());
125             }
126         }
127
128         return keyList;
129     }
130
131     @Override
132     public void clean() {
133         super.clean();
134
135         if (targets != null) {
136             for (PfConceptKey target : targets) {
137                 target.clean();
138             }
139         }
140
141         if (triggers != null) {
142             for (JpaToscaTrigger trigger : triggers) {
143                 trigger.clean();
144             }
145         }
146     }
147
148     @Override
149     public BeanValidationResult validate(@NonNull String fieldName) {
150         return validateWithKey(fieldName);
151     }
152
153     @Override
154     public int compareTo(final PfConcept otherConcept) {
155         if (this == otherConcept) {
156             return 0;
157         }
158
159         int result = super.compareTo(otherConcept);
160         if (result != 0) {
161             return result;
162         }
163
164         final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
165
166         result = PfUtils.compareCollections(targets, other.targets);
167         if (result != 0) {
168             return result;
169         }
170
171         return PfUtils.compareCollections(triggers, other.triggers);
172     }
173 }