Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / sync / entity / SearchableEntity.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.sync.entity;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.onap.aai.sparky.util.NodeUtils;
31
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.annotation.JsonProperty;
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37 /**
38  * The Class SearchableEntity.
39  */
40 public class SearchableEntity extends IndexableEntity implements IndexDocument {
41   
42   @JsonIgnore
43   protected List<String> searchTagCollection = new ArrayList<String>();
44   
45   @JsonIgnore
46   protected List<String> searchTagIdCollection = new ArrayList<String>();
47   
48   @JsonIgnore
49   protected ObjectMapper mapper = new ObjectMapper();
50
51   /**
52    * Instantiates a new searchable entity.
53    */
54   public SearchableEntity() {
55     super();
56   }
57
58   /*
59    * Generated fields, leave the settings for junit overrides
60    */
61   
62   protected String searchTags; // generated based on searchTagCollection values
63   
64   protected String searchTagIDs;
65   
66   /**
67    * Generates the sha based id.
68    */
69   public void generateId() {
70     this.id = NodeUtils.generateUniqueShaDigest(link);
71   }
72
73   /* (non-Javadoc)
74    * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
75    */
76   @Override
77   public void deriveFields() {
78
79     /*
80      * We'll try and create a unique identity key that we can use for differencing the previously
81      * imported record sets as we won't have granular control of what is created/removed and when.
82      * The best we can hope for is identification of resources by generated Id until the
83      * Identity-Service UUID is tagged against all resources, then we can use that instead.
84      */
85     generateId();
86     this.searchTags = NodeUtils.concatArray(searchTagCollection, ";");
87     this.searchTagIDs = NodeUtils.concatArray(this.searchTagIdCollection, ";");
88   }
89
90   /**
91    * Adds the search tag with key.
92    *
93    * @param searchTag the search tag
94    * @param searchTagKey the key associated with the search tag (key:value)
95    */
96   public void addSearchTagWithKey(String searchTag, String searchTagKey) {
97     searchTagIdCollection.add(searchTagKey);
98     searchTagCollection.add(searchTag);
99   }
100
101   public List<String> getSearchTagCollection() {
102     return searchTagCollection;
103   }
104
105   @JsonProperty("searchTags")
106   public String getSearchTags() {
107     return searchTags;
108   }
109
110   @JsonProperty("searchTagIDs")
111   public String getSearchTagIDs() {
112     return searchTagIDs;
113   }
114
115   @JsonIgnore
116   public List<String> getSearchTagIdCollection() {
117     return searchTagIdCollection;
118   }
119
120   @Override
121   @JsonIgnore
122   public String getAsJson() throws JsonProcessingException  {
123     return NodeUtils.convertObjectToJson(this, false);
124   }
125
126   /* (non-Javadoc)
127    * @see java.lang.Object#toString()
128    */
129   @Override
130   public String toString() {
131     return "IndexDocument [" + (entityType != null ? "entityType=" + entityType + ", " : "")
132         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
133             : "")
134         + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
135         + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
136             : "")
137         + (mapper != null ? "mapper=" + mapper + ", " : "") + (id != null ? "id=" + id + ", " : "")
138         + (lastmodTimestamp != null ? "lastmodTimestamp=" + lastmodTimestamp + ", " : "")
139         + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
140         + (searchTagIDs != null ? "searchTagIDs=" + searchTagIDs : "") + "]";
141   }
142 }