Introduce tosca saving
[clamp.git] / src / main / java / org / onap / clamp / clds / model / CldsDictionary.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.model;
25
26 import com.fasterxml.jackson.annotation.JsonInclude;
27 import com.fasterxml.jackson.annotation.JsonInclude.Include;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.onap.clamp.clds.dao.CldsDao;
33
34 /**
35  * Represents a CLDS Dictionary.
36  */
37 @JsonInclude(Include.NON_NULL)
38 public class CldsDictionary {
39
40     private String dictionaryId;
41     private String dictionaryName;
42     private String createdBy;
43     private String updatedBy;
44     private String lastUpdatedDate;
45     private List<CldsDictionaryItem> cldsDictionaryItems = new ArrayList<>();
46
47     /**
48      * Creates or updates dictionary item for a dictionary in DB
49      *
50      * @param dictionaryName
51      * @param cldsDao
52      * @param userId
53      */
54     public void save(String dictionaryName, CldsDao cldsDao, String userId) {
55         List<CldsDictionary> list = cldsDao.getDictionary(this.getDictionaryId(), dictionaryName);
56         if (list != null && !list.isEmpty()) {
57             CldsDictionary cldsDictionary = list.stream().findFirst().get();
58             if (!cldsDictionary.getDictionaryName().equalsIgnoreCase(this.getDictionaryName())) {
59                 cldsDao.updateDictionary(cldsDictionary.getDictionaryId(), this, userId);
60                 this.setCreatedBy(cldsDictionary.getCreatedBy());
61             } else {
62                 this.setDictionaryId(cldsDictionary.getDictionaryId());
63                 this.setCreatedBy(cldsDictionary.getCreatedBy());
64                 this.setUpdatedBy(cldsDictionary.getUpdatedBy());
65                 this.setLastUpdatedDate(cldsDictionary.getLastUpdatedDate());
66             }
67         } else {
68             this.setCreatedBy(userId);
69             this.setUpdatedBy(userId);
70             cldsDao.insDictionary(this);
71         }
72     }
73
74     /**
75      * @return the dictionaryId
76      */
77     public String getDictionaryId() {
78         return dictionaryId;
79     }
80
81     /**
82      * @param dictionaryId
83      *        the dictionaryId to set
84      */
85     public void setDictionaryId(String dictionaryId) {
86         this.dictionaryId = dictionaryId;
87     }
88
89     /**
90      * @return the dictionaryName
91      */
92     public String getDictionaryName() {
93         return dictionaryName;
94     }
95
96     /**
97      * @param dictionaryName
98      *        the dictionaryName to set
99      */
100     public void setDictionaryName(String dictionaryName) {
101         this.dictionaryName = dictionaryName;
102     }
103
104     /**
105      * @return the createdBy
106      */
107     public String getCreatedBy() {
108         return createdBy;
109     }
110
111     /**
112      * @param createdBy
113      *        the createdBy to set
114      */
115     public void setCreatedBy(String createdBy) {
116         this.createdBy = createdBy;
117     }
118
119     /**
120      * @return the updatedBy
121      */
122     public String getUpdatedBy() {
123         return updatedBy;
124     }
125
126     /**
127      * @param updatedby
128      *        the updatedBy to set
129      */
130     public void setUpdatedBy(String updatedby) {
131         updatedBy = updatedby;
132     }
133
134     /**
135      * @return the lastUpdatedDate
136      */
137     public String getLastUpdatedDate() {
138         return lastUpdatedDate;
139     }
140
141     /**
142      * @param lastUpdatedDate
143      *        the lastUpdatedDate to set
144      */
145     public void setLastUpdatedDate(String lastUpdatedDate) {
146         this.lastUpdatedDate = lastUpdatedDate;
147     }
148
149     /**
150      * @return the cldsDictionaryItems
151      */
152     public List<CldsDictionaryItem> getCldsDictionaryItems() {
153         return cldsDictionaryItems;
154     }
155
156     /**
157      * @param cldsDictionaryItems
158      *        the cldsDictionaryItems to set
159      */
160     public void setCldsDictionaryItems(List<CldsDictionaryItem> cldsDictionaryItems) {
161         this.cldsDictionaryItems = cldsDictionaryItems;
162     }
163
164 }