|
| 1 | +/* |
| 2 | + Copyright 2018 Ericsson AB. |
| 3 | + For a full list of individual contributors, please see the commit history. |
| 4 | +
|
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +*/ |
| 17 | +package com.ericsson.ei.subscriptionhandler; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.beans.factory.annotation.Value; |
| 25 | +import org.springframework.stereotype.Component; |
| 26 | + |
| 27 | + |
| 28 | +import com.ericsson.ei.mongodbhandler.MongoDBHandler; |
| 29 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 30 | +import com.fasterxml.jackson.databind.JsonNode; |
| 31 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 32 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 33 | +import com.mongodb.BasicDBObject; |
| 34 | + |
| 35 | +import lombok.Getter; |
| 36 | +import lombok.Setter; |
| 37 | + |
| 38 | + |
| 39 | +@Component |
| 40 | +public class SubscriptionRepeatDbHandler { |
| 41 | + |
| 42 | + private static Logger LOGGER = (Logger) LoggerFactory.getLogger(SubscriptionRepeatDbHandler.class); |
| 43 | + |
| 44 | + |
| 45 | + @Autowired |
| 46 | + public MongoDBHandler mongoDbHandler; |
| 47 | + |
| 48 | + private ObjectMapper mapper = new ObjectMapper(); |
| 49 | + |
| 50 | + @Getter |
| 51 | + @Setter |
| 52 | + @Value("${database.name}") |
| 53 | + public String dataBaseName; |
| 54 | + @Getter |
| 55 | + @Setter |
| 56 | + @Value("${subscription.collection.repeatFlagHandlerName}") |
| 57 | + public String collectionName; |
| 58 | + |
| 59 | + |
| 60 | + /* |
| 61 | + * RepeatFlagHandling structure in MongoDb: |
| 62 | + * { |
| 63 | + "_id" : ObjectId("5ac62b4ea4f87e29e8cc5915"), |
| 64 | + "subscriptionId" : "subsA", |
| 65 | + // RequirementId corresponds to a Requirement List of matched Aggregated Objects Ids. |
| 66 | + <RequirementId> <AggrObjIds> |
| 67 | + "requirements" : {"0" : [ |
| 68 | + "11112", |
| 69 | + "72324", |
| 70 | + "72364", |
| 71 | + "72233", |
| 72 | + "71233" |
| 73 | + ], |
| 74 | + "1" : [ |
| 75 | + "11112", |
| 76 | + "72324", |
| 77 | + "72364", |
| 78 | + "72233", |
| 79 | + "71233" |
| 80 | + ] |
| 81 | + } |
| 82 | + } |
| 83 | + * |
| 84 | + */ |
| 85 | + |
| 86 | + /* |
| 87 | + * Function that stores the matched aggregatedObjectId to the database. |
| 88 | + * |
| 89 | + */ |
| 90 | + public void addMatchedAggrObjToSubscriptionId(String subscriptionId, int requirementId, String aggrObjId) throws Exception { |
| 91 | + |
| 92 | + LOGGER.debug("Adding/Updating matched AggrObjId: " + aggrObjId + " to SubscriptionsId: " + subscriptionId + " aggrId matched list" ); |
| 93 | + |
| 94 | + if (checkIfAggrObjIdExistInSubscriptionAggrIdsMatchedList(subscriptionId, requirementId, aggrObjId)) { |
| 95 | + LOGGER.info("Subscription: " + subscriptionId + " and AggrObjId, " + |
| 96 | + aggrObjId + " has already been matched." + |
| 97 | + "No need to register the subscription match."); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + if (!updateExistingMatchedSubscriptionWithAggrObjId(subscriptionId, requirementId, aggrObjId)) { |
| 103 | + LOGGER.error("Couldn't update SubscriptionMathced id."); |
| 104 | + } |
| 105 | + else { |
| 106 | + LOGGER.debug("New Subscription AggrId has not matched, inserting new SubscriptionId and AggrObjId to matched list."); |
| 107 | + BasicDBObject document = new BasicDBObject(); |
| 108 | + document.put("subscriptionId", subscriptionId); |
| 109 | + |
| 110 | + ArrayList<String> aggrObjIdsList = new ArrayList<String>(); |
| 111 | + aggrObjIdsList.add(aggrObjId); |
| 112 | + |
| 113 | + BasicDBObject reqDocument = new BasicDBObject(); |
| 114 | + reqDocument.put(String.valueOf(requirementId), aggrObjIdsList); |
| 115 | + |
| 116 | + document.put("requirements", reqDocument); |
| 117 | + |
| 118 | + LOGGER.debug("New Matched AggrIdObject update on Subscription to be inserted to Db: " + document); |
| 119 | + boolean result = mongoDbHandler.insertDocument(dataBaseName, collectionName, document.toString()); |
| 120 | + if (result == false) { |
| 121 | + throw new Exception("Failed to insert the document into database"); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private boolean updateExistingMatchedSubscriptionWithAggrObjId(String subscriptionId, int requirementId, |
| 127 | + String aggrObjId) throws Exception { |
| 128 | + String subscriptionQuery = "{\"subscriptionId\" : \"" + subscriptionId + "\"}"; |
| 129 | + |
| 130 | + JsonNode updateDocJsonNode = mapper |
| 131 | + .readValue("{\"$push\" : { \"requirements." + requirementId + "\" : \"" + aggrObjId + "\"}}", JsonNode.class); |
| 132 | + |
| 133 | + LOGGER.debug("SubscriptionId \"", subscriptionId, "\" document will be updated with following requirement update object: " + updateDocJsonNode.toString()); |
| 134 | + |
| 135 | + JsonNode queryJsonNode = mapper.readValue(subscriptionQuery, JsonNode.class); |
| 136 | + try { |
| 137 | + mongoDbHandler.findAndModify(dataBaseName, collectionName, queryJsonNode.toString(), |
| 138 | + updateDocJsonNode.toString()); |
| 139 | + } catch (Exception e) { |
| 140 | + LOGGER.debug("Failed to update existing matched SubscriptionId with new AggrId." + "SubscriptionId: " |
| 141 | + + subscriptionId + "New matched AggrObjId: " + aggrObjId + "RequirementId that have matched: " |
| 142 | + + requirementId); |
| 143 | + return false; |
| 144 | + } |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + public boolean checkIfAggrObjIdExistInSubscriptionAggrIdsMatchedList(String subscriptionId, int requirementId, String aggrObjId) { |
| 150 | + |
| 151 | + LOGGER.debug("Checking if AggrObjId: " + aggrObjId + " exist in SubscriptionId: " + subscriptionId + " AggrId matched list."); |
| 152 | + String subscriptionQuery = "{\"subscriptionId\" : \"" + subscriptionId + "\"}"; |
| 153 | + List<String> objArray = mongoDbHandler.find(dataBaseName, collectionName, subscriptionQuery); |
| 154 | + if (objArray != null && !objArray.isEmpty()) { |
| 155 | + JsonNode jNode = null; |
| 156 | + LOGGER.debug("Making AggrObjId checks on SubscriptionId document: " + objArray.get(0)); |
| 157 | + try { |
| 158 | + jNode = mapper.readTree(objArray.get(0)); |
| 159 | + } catch (Exception e) { |
| 160 | + LOGGER.error(e.getMessage()); |
| 161 | + e.printStackTrace(); |
| 162 | + } |
| 163 | + if (jNode.get("subscriptionId").asText().trim().equals(subscriptionId)) { |
| 164 | + LOGGER.debug("SubscriptionId \"" , subscriptionId , "\" , exist in document. Checking if AggrObjId has matched earlier."); |
| 165 | + List<String> listAggrObjIds = null; |
| 166 | + LOGGER.debug("Subscription requirementId: " + requirementId + " and Requirements content:\n" + jNode.get("requirements").get(new Integer(requirementId).toString())); |
| 167 | + try { |
| 168 | + ObjectReader reader = mapper.readerFor(new TypeReference<List<String>>() { |
| 169 | + }); |
| 170 | + JsonNode arrayNode = mapper.createArrayNode().add(jNode.get("requirements").get(new Integer(requirementId))); |
| 171 | + listAggrObjIds = reader.readValue(arrayNode); |
| 172 | + |
| 173 | + if (requirementId > (listAggrObjIds.size() - 1)) { |
| 174 | + LOGGER.debug("RequirementId: " + requirementId + " and SubscriptionId: " + subscriptionId + |
| 175 | + "\nhas not matched any AggregatedObject yet. No need to do anymore check."); |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + if (listAggrObjIds.get(requirementId) == aggrObjId) { |
| 180 | + LOGGER.info("Subscription has matched aggrObjId already: " + aggrObjId); |
| 181 | + return true; |
| 182 | + } |
| 183 | + } catch (Exception e) { |
| 184 | + LOGGER.error(e.getMessage()); |
| 185 | + e.printStackTrace(); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + LOGGER.info("AggrObjId not found for SubscriptionId in SubscriptionRepeatFlagHandlerDb -> Returning FALSE."); |
| 190 | + return false; |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments