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