Skip to content

Commit b4f05a0

Browse files
Freestyle query is performed only on aggregated_objects collection (#101)
1 parent 5aa278c commit b4f05a0

File tree

6 files changed

+32
-126
lines changed

6 files changed

+32
-126
lines changed

src/main/java/com/ericsson/ei/controller/QueryAggregatedObjectControllerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.springframework.web.bind.annotation.CrossOrigin;
2525
import org.springframework.web.bind.annotation.RequestParam;
2626

27-
import java.util.ArrayList;
27+
import java.util.List;
2828

2929
/**
3030
* This class represents the REST GET mechanism to extract the aggregated data
@@ -48,9 +48,9 @@ public class QueryAggregatedObjectControllerImpl implements QueryAggregatedObjec
4848
*/
4949
public ResponseEntity<QueryResponse> getQueryAggregatedObject(@RequestParam("ID") final String id) {
5050
QueryResponse queryResponse= new QueryResponse();
51-
ArrayList<String> response = processAggregatedObject.processQueryAggregatedObject(id);
51+
List<String> response = processAggregatedObject.processQueryAggregatedObject(id);
5252
queryResponse.setResponseEntity(response.toString());
5353
LOGGER.debug("The response is : " + response.toString());
54-
return new ResponseEntity(queryResponse, HttpStatus.OK);
54+
return new ResponseEntity<>(queryResponse, HttpStatus.OK);
5555
}
5656
}

src/main/java/com/ericsson/ei/controller/QueryControllerImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,38 @@
3737
*/
3838
@Component
3939
@CrossOrigin
40-
@Api(value = "query", description = "REST end-points for the freestyle query service")
40+
@Api(value = "query", description = "REST endpoints for the freestyle query service")
4141
public class QueryControllerImpl implements QueryController {
4242

4343
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(QueryControllerImpl.class);
4444

4545
@Autowired
4646
private ProcessQueryParams processQueryParams;
4747

48-
private JSONArray result = null;
49-
5048
@Override
5149
@CrossOrigin
5250
@ApiOperation(value = "")
5351
public ResponseEntity<?> updateQuery(@RequestParam(value = "request") String request) {
5452
try {
55-
result = processQueryParams.filterFormParam(new ObjectMapper().readTree(request));
53+
JSONArray result = processQueryParams.filterFormParam(new ObjectMapper().readTree(request));
54+
return new ResponseEntity<>(result.toString(), HttpStatus.OK);
5655
} catch (Exception e) {
5756
LOGGER.error(e.getMessage());
57+
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
5858
}
59-
return new ResponseEntity(result.toString(), HttpStatus.OK);
6059
}
6160

6261
@Override
6362
@CrossOrigin
6463
@ApiOperation(value = "")
6564
public ResponseEntity<?> getQuery(@RequestParam(value = "request") final String request) {
6665
try {
67-
result = processQueryParams.filterQueryParam(request);
66+
JSONArray result = processQueryParams.filterQueryParam(request);
67+
LOGGER.debug("Final Output : " + result.toString());
68+
return new ResponseEntity<>(result.toString(), HttpStatus.OK);
6869
} catch (Exception e) {
6970
LOGGER.error(e.getMessage());
71+
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
7072
}
71-
LOGGER.debug("Final Output : " + result.toString());
72-
return new ResponseEntity(result.toString(), HttpStatus.OK);
7373
}
7474
}

src/main/java/com/ericsson/ei/controller/QueryMissedNotificationControllerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ResponseEntity<QueryResponse> getQueryMissedNotifications(@RequestParam("
5151
List<String> response = processMissedNotification.processQueryMissedNotification(subscriptionName);
5252
queryResponse.setResponseEntity(response.toString());
5353
LOGGER.debug("The response is : " + response.toString());
54-
return new ResponseEntity(queryResponse, HttpStatus.OK);
54+
return new ResponseEntity<>(queryResponse, HttpStatus.OK);
5555
}
5656

5757
}

src/main/java/com/ericsson/ei/queryservice/ProcessAggregatedObject.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
@Component
4141
public class ProcessAggregatedObject {
4242

43+
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(ProcessAggregatedObject.class);
44+
4345
@Value("${aggregated.collection.name}")
4446
private String aggregationCollectionName;
4547

4648
@Value("${spring.data.mongodb.database}")
4749
private String aggregationDataBaseName;
4850

49-
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(ProcessAggregatedObject.class);
50-
5151
@Autowired
5252
private MongoDBHandler handler;
5353

@@ -78,7 +78,7 @@ public ArrayList<String> processQueryAggregatedObject(String id) {
7878
* The method is responsible to extract the aggregated data on the basis of
7979
* the ID from the aggregatedObject.
8080
*
81-
* @param id
81+
* @param templateName
8282
* @return ArrayList
8383
*/
8484
public ArrayList<String> getAggregatedObjectByTemplateName(String templateName) {
@@ -131,7 +131,7 @@ public JSONArray processQueryAggregatedObject(JsonNode request, String Aggregati
131131

132132
@PostConstruct
133133
public void init() {
134-
LOGGER.debug("The Aggregated Database is : " + aggregationDataBaseName);
135-
LOGGER.debug("The Aggregated Collection is : " + aggregationCollectionName);
134+
LOGGER.debug("The Aggregated Database is : " + aggregationDataBaseName
135+
+ "\nThe Aggregated Collection is : " + aggregationCollectionName);
136136
}
137137
}

src/main/java/com/ericsson/ei/queryservice/ProcessMissedNotification.java

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,13 @@
1616
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
1717
import com.fasterxml.jackson.databind.JsonNode;
1818
import com.fasterxml.jackson.databind.ObjectMapper;
19-
import com.mongodb.DB;
20-
import com.mongodb.MongoClient;
21-
import org.bson.Document;
22-
import org.jongo.Jongo;
23-
import org.jongo.MongoCollection;
24-
import org.jongo.MongoCursor;
25-
import org.json.JSONArray;
26-
import org.json.JSONObject;
2719
import org.slf4j.Logger;
2820
import org.slf4j.LoggerFactory;
2921
import org.springframework.beans.factory.annotation.Autowired;
3022
import org.springframework.beans.factory.annotation.Value;
3123
import org.springframework.stereotype.Component;
3224

3325
import javax.annotation.PostConstruct;
34-
import java.util.ArrayList;
3526
import java.util.List;
3627
import java.util.stream.Collectors;
3728

@@ -42,13 +33,13 @@
4233
@Component
4334
public class ProcessMissedNotification {
4435

36+
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(ProcessMissedNotification.class);
37+
4538
@Value("${missedNotificationCollectionName}")
4639
private String missedNotificationCollectionName;
4740

4841
@Value("${missedNotificationDataBaseName}")
49-
private String missedNotificationDataBaseName;
50-
51-
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(ProcessMissedNotification.class);
42+
private String missedNotificationDatabaseName;
5243

5344
@Autowired
5445
private MongoDBHandler handler;
@@ -71,7 +62,7 @@ public List<String> processQueryMissedNotification(String subscriptionName) {
7162
LOGGER.error(e.getMessage(), e);
7263
}
7364
LOGGER.debug("The Json condition is : " + jsonCondition);
74-
ArrayList<String> output = handler.find(missedNotificationDataBaseName, missedNotificationCollectionName,
65+
List<String> output = handler.find(missedNotificationDatabaseName, missedNotificationCollectionName,
7566
jsonCondition.toString());
7667
return output.stream().map(a -> {
7768
try {
@@ -84,40 +75,10 @@ public List<String> processQueryMissedNotification(String subscriptionName) {
8475

8576
}
8677

87-
/**
88-
* This method is responsible for fetching all the missed notifications from
89-
* the missed Notification database and return it as JSONArray.
90-
*
91-
* @param request
92-
* @param MissedNotificationDataBaseName
93-
* @param MissedNotificationCollectionName
94-
* @return JSONArray
95-
*/
96-
public JSONArray processQueryMissedNotification(JsonNode request, String MissedNotificationDataBaseName, String MissedNotificationCollectionName) {
97-
DB db = new MongoClient().getDB(MissedNotificationDataBaseName);
98-
Jongo jongo = new Jongo(db);
99-
MongoCollection aggObjects = jongo.getCollection(MissedNotificationCollectionName);
100-
LOGGER.debug("Successfully connected to MissedNotification database");
101-
MongoCursor<Document> allDocuments = aggObjects.find(request.toString()).as(Document.class);
102-
LOGGER.debug("Number of document returned from Notification collection is : " + allDocuments.count());
103-
JSONArray jsonArray = new JSONArray();
104-
JSONObject doc = null;
105-
while (allDocuments.hasNext()) {
106-
Document temp = allDocuments.next();
107-
try {
108-
doc = new JSONObject(temp.toJson());
109-
} catch (Exception e) {
110-
LOGGER.error(e.getMessage(), e);
111-
}
112-
jsonArray.put(doc);
113-
}
114-
return jsonArray;
115-
}
116-
11778
@PostConstruct
11879
public void init() {
119-
LOGGER.debug("The Aggregated Database is : " + missedNotificationDataBaseName);
120-
LOGGER.debug("The Aggregated Collection is : " + missedNotificationCollectionName);
80+
LOGGER.debug("MissedNotification Database is : " + missedNotificationDatabaseName
81+
+ "\nMissedNotification Collection is : " + missedNotificationCollectionName);
12182
}
12283

12384
}

src/main/java/com/ericsson/ei/queryservice/ProcessQueryParams.java

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.fasterxml.jackson.databind.ObjectMapper;
1919
import com.fasterxml.jackson.databind.node.ObjectNode;
2020
import org.json.JSONArray;
21-
import org.json.JSONException;
2221
import org.slf4j.Logger;
2322
import org.slf4j.LoggerFactory;
2423
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,7 +26,6 @@
2726

2827
import javax.annotation.PostConstruct;
2928
import java.io.IOException;
30-
import java.util.stream.IntStream;
3129

3230
/**
3331
* This class is responsible to fetch the criterias from both the query
@@ -43,20 +41,11 @@ public class ProcessQueryParams {
4341
private String aggregationCollectionName;
4442

4543
@Value("${spring.data.mongodb.database}")
46-
private String dataBaseName;
47-
48-
@Value("${missedNotificationCollectionName}")
49-
private String missedNotificationCollectionName;
50-
51-
@Value("${missedNotificationDataBaseName}")
52-
private String missedNotificationDataBaseName;
44+
private String databaseName;
5345

5446
@Autowired
5547
private ProcessAggregatedObject processAggregatedObject;
5648

57-
@Autowired
58-
private ProcessMissedNotification processMissedNotification;
59-
6049
/**
6150
* This method takes the parameters from the REST POST request body. If the
6251
* Aggregated Object matches the condition, then it is returned.
@@ -70,32 +59,15 @@ public JSONArray filterFormParam(JsonNode request) throws IOException {
7059
JsonNode options = request.get("options");
7160
LOGGER.debug("The criteria is : " + criteria.toString());
7261
LOGGER.debug("The options is : " + options.toString());
62+
JSONArray resultAggregatedObject;
7363
if (options.toString().equals("{}") || options.isNull()) {
74-
return getProcessQuery(criteria);
64+
resultAggregatedObject = processAggregatedObject.processQueryAggregatedObject(criteria, databaseName, aggregationCollectionName);
7565
} else {
7666
String result = "{ \"$and\" : [ " + criteria.toString() + "," + options.toString() + " ] }";
77-
return getProcessQuery(new ObjectMapper().readTree(result));
78-
}
79-
}
80-
81-
/**
82-
* This method is responsible for concatenating two JSONArrays.
83-
*
84-
* @param firstArray
85-
* @param secondArray
86-
* @return JSONArray
87-
* @throws JSONException
88-
*/
89-
private static JSONArray concatArray(JSONArray firstArray, JSONArray secondArray) throws JSONException {
90-
JSONArray result = new JSONArray();
91-
JSONArray[] inputs = new JSONArray[] { firstArray, secondArray };
92-
for (JSONArray input : inputs) {
93-
for (int i = 0; i < input.length(); i++) {
94-
result.put(input.get(i));
95-
}
67+
resultAggregatedObject = processAggregatedObject.processQueryAggregatedObject(new ObjectMapper().readTree(result), databaseName, aggregationCollectionName);
9668
}
97-
98-
return result;
69+
LOGGER.debug("resultAggregatedObject : " + resultAggregatedObject.toString());
70+
return resultAggregatedObject;
9971
}
10072

10173
/**
@@ -120,39 +92,12 @@ public JSONArray filterQueryParam(String request) {
12092
criteria.put(key, value);
12193
}
12294
LOGGER.debug(criteria.toString());
123-
return getProcessQuery(criteria);
124-
}
125-
126-
/**
127-
* Process parameters to create a JsonNode request to query the Aggregated
128-
* Objects.
129-
*
130-
* @param criteria
131-
* @return
132-
*/
133-
private JSONArray getProcessQuery(JsonNode criteria) {
134-
JSONArray resultAggregatedObject = processAggregatedObject.processQueryAggregatedObject(criteria, dataBaseName,
135-
aggregationCollectionName);
136-
JSONArray resultMissedNotification = processMissedNotification.processQueryMissedNotification(criteria,
137-
missedNotificationDataBaseName, missedNotificationCollectionName);
138-
LOGGER.debug("resultAggregatedObject : " + resultAggregatedObject.toString());
139-
LOGGER.debug("resultMissedNotification : " + resultMissedNotification.toString());
140-
JSONArray result = null;
141-
try {
142-
result = ProcessQueryParams.concatArray(resultAggregatedObject, resultMissedNotification);
143-
} catch (Exception e) {
144-
LOGGER.error(e.getMessage());
145-
}
146-
LOGGER.debug("Final Result is : " + result.toString());
147-
return result;
95+
return processAggregatedObject.processQueryAggregatedObject(criteria, databaseName, aggregationCollectionName);
14896
}
14997

15098
@PostConstruct
15199
public void print() {
152-
LOGGER.debug("Values from application.properties file");
153-
LOGGER.debug("AggregationCollectionName : " + aggregationCollectionName);
154-
LOGGER.debug("AggregationDataBaseName : " + dataBaseName);
155-
LOGGER.debug("MissedNotificationCollectionName : " + missedNotificationCollectionName);
156-
LOGGER.debug("MissedNotificationDataBaseName : " + missedNotificationDataBaseName);
100+
LOGGER.debug("Aggregation Database : " + databaseName
101+
+ "\nAggregation Collection is : " + aggregationCollectionName);
157102
}
158103
}

0 commit comments

Comments
 (0)