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