Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / sync / entity / SearchableEntity.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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 package org.onap.aai.sparky.sync.entity;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.onap.aai.sparky.util.NodeUtils;
27
28 import com.fasterxml.jackson.annotation.JsonIgnore;
29 import com.fasterxml.jackson.annotation.JsonProperty;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32
33 /**
34  * The Class SearchableEntity.
35  */
36 public class SearchableEntity extends IndexableEntity implements IndexDocument {
37   
38   @JsonIgnore
39   protected List<String> searchTagCollection = new ArrayList<String>();
40   
41   @JsonIgnore
42   protected List<String> searchTagIdCollection = new ArrayList<String>();
43   
44   @JsonIgnore
45   protected ObjectMapper mapper = new ObjectMapper();
46
47   /**
48    * Instantiates a new searchable entity.
49    */
50   public SearchableEntity() {
51     super();
52   }
53
54   /*
55    * Generated fields, leave the settings for junit overrides
56    */
57   
58   protected String searchTags; // generated based on searchTagCollection values
59   
60   protected String searchTagIDs;
61   
62   /**
63    * Generates the sha based id.
64    */
65   public void generateId() {
66     this.id = NodeUtils.generateUniqueShaDigest(link);
67   }
68
69   /* (non-Javadoc)
70    * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
71    */
72   @Override
73   public void deriveFields() {
74
75     /*
76      * We'll try and create a unique identity key that we can use for differencing the previously
77      * imported record sets as we won't have granular control of what is created/removed and when.
78      * The best we can hope for is identification of resources by generated Id until the
79      * Identity-Service UUID is tagged against all resources, then we can use that instead.
80      */
81     generateId();
82     this.searchTags = NodeUtils.concatArray(searchTagCollection, ";");
83     this.searchTagIDs = NodeUtils.concatArray(this.searchTagIdCollection, ";");
84   }
85
86   /**
87    * Adds the search tag with key.
88    *
89    * @param searchTag the search tag
90    * @param searchTagKey the key associated with the search tag (key:value)
91    */
92   public void addSearchTagWithKey(String searchTag, String searchTagKey) {
93     searchTagIdCollection.add(searchTagKey);
94     searchTagCollection.add(searchTag);
95   }
96
97   public List<String> getSearchTagCollection() {
98     return searchTagCollection;
99   }
100
101   @JsonProperty("searchTags")
102   public String getSearchTags() {
103     return searchTags;
104   }
105
106   @JsonProperty("searchTagIDs")
107   public String getSearchTagIDs() {
108     return searchTagIDs;
109   }
110
111   @JsonIgnore
112   public List<String> getSearchTagIdCollection() {
113     return searchTagIdCollection;
114   }
115
116   @Override
117   @JsonIgnore
118   public String getAsJson() throws JsonProcessingException  {
119     return NodeUtils.convertObjectToJson(this, false);
120   }
121
122   /* (non-Javadoc)
123    * @see java.lang.Object#toString()
124    */
125   @Override
126   public String toString() {
127     return "IndexDocument [" + (entityType != null ? "entityType=" + entityType + ", " : "")
128         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
129             : "")
130         + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
131         + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
132             : "")
133         + (mapper != null ? "mapper=" + mapper + ", " : "") + (id != null ? "id=" + id + ", " : "")
134         + (lastmodTimestamp != null ? "lastmodTimestamp=" + lastmodTimestamp + ", " : "")
135         + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
136         + (searchTagIDs != null ? "searchTagIDs=" + searchTagIDs : "") + "]";
137   }
138 }