@RestController
public class CommentController {
private CommentService commentService;
private ResourceService resourceService;
public static final String PLAIN_RESPONSE_TYPE = "text/plain";
public static final String JSON_RESPONSE_TYPE = "application/json";
public static final String COLLECTION_NAME_COMMENTS = "commentsCollection";
public static final String COLLECTION_NAME_RESOURCE = "resourceCollection";
boolean USE_CF = false;
String MONGO_LOCAL_HOST_URL = "mongodb://localhost:27017";
@Autowired
public void setCommentService() {
this.commentService = new CommentServiceImpl(COLLECTION_NAME_COMMENTS, USE_CF, MONGO_LOCAL_HOST_URL);
this.resourceService = new ResourceServiceImpl(COLLECTION_NAME_RESOURCE, USE_CF, MONGO_LOCAL_HOST_URL);
}
@ApiOperation(value = "Ping Comment Service")
@RequestMapping(value = "/comment/ping", method = RequestMethod.GET, produces = PLAIN_RESPONSE_TYPE)
public ResponseEntity<?> pingCommentService() {
return new ResponseEntity<>("Ping Comment Successful", HttpStatus.OK);
}
// Can be used for bulk create
@ApiOperation(value = "Create comments")
@RequestMapping(value = "/comment/create", method = RequestMethod.POST, produces = JSON_RESPONSE_TYPE)
public ResponseEntity<?> addComments(@RequestBody CreateAndReadResourceTO resourceTO) {
ArrayList<CreateAndReadCommentsTO> allIncomingComments = resourceTO.getListOfComments();
ResourceModel resourceModelObj = new ResourceModel();
resourceModelObj.setResourceId(resourceTO.getResourceId());
ArrayList<String> totalRootNodes = new ArrayList<String>();
return new ResponseEntity<>(resourceTO, HttpStatus.OK);
}
@ApiOperation(value = "Update Comments")
@RequestMapping(value = "/comment/update", method = RequestMethod.PUT, produces = JSON_RESPONSE_TYPE)
public ResponseEntity<?> updateComments(@RequestBody UpdateResourceTO updatedResourceToObj) {
ArrayList<UpdateCommentsTO> listOfUpdatedComments = updatedResourceToObj.getListOfCommentsToBeUpdated();
ArrayList<UpdateCommentsTO> listOfUpdatedCommentsFromDb = new ArrayList<UpdateCommentsTO>();
for (int i = 0; i < listOfUpdatedComments.size(); i++) {
CommentModel updatedValues = commentService
.updateComment(parseToCommentModel(listOfUpdatedComments.get(i)));
if (updatedValues != null) {
listOfUpdatedCommentsFromDb.add(parseCommentModelToUpdate(updatedValues));
}
}
updatedResourceToObj.setListOfCommentsToBeUpdated(listOfUpdatedCommentsFromDb);
return new ResponseEntity<>(updatedResourceToObj, HttpStatus.OK);
}
@ApiOperation(value = "List all comments available for Resource", response = Iterable.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfull in finding the comments from the "),
@ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
@RequestMapping(value = "/comment/listAll/resource/{resourceId}", method = RequestMethod.GET, produces = "application/json")
public CreateAndReadResourceTO listComments(@PathVariable String resourceId) {
// get thelist of all the comment-ids associated with the resource
ArrayList<String> listOfTopIds = this.resourceService.getAllCommentsOfTheResource(resourceId);
Map<String, CommentModel> commentModelObj = commentService.findAllComments(listOfTopIds);
CreateAndReadResourceTO resourceToObj = new CreateAndReadResourceTO();
resourceToObj.setResourceId(resourceId);
ArrayList<CreateAndReadCommentsTO> totalCommentsList = new ArrayList<CreateAndReadCommentsTO>();
for (String key : commentModelObj.keySet()) {
if (listOfTopIds.contains(key)) {
CommentModel eachComment = commentModelObj.get(key);
String childCommentId = eachComment.getChildCommentId();
ArrayList<CommentModel> listOfCommentsStack = new ArrayList<CommentModel>();
listOfCommentsStack.add(eachComment);
CommentModel childComment;
while (childCommentId != null) {
boolean doesContainChildInMap = commentModelObj.containsKey(childCommentId);
if (doesContainChildInMap == true) {
childComment = commentModelObj.get(childCommentId);
listOfCommentsStack.add(childComment);
childCommentId = childComment.getChildCommentId();
}
}
CreateAndReadCommentsTO previouscommentsTO = null;
for (int i = listOfCommentsStack.size() - 1; i >= 0; i--) {
CreateAndReadCommentsTO currentcommentsTO = parseCommentModel(listOfCommentsStack.get(i));
if (previouscommentsTO != null) {
currentcommentsTO.setChildComment(previouscommentsTO);
}
previouscommentsTO = currentcommentsTO;
}
totalCommentsList.add(previouscommentsTO);
}
}
resourceToObj.setListOfComments(totalCommentsList);
return resourceToObj;
}
@ApiOperation(value = "Delete a comment")
@RequestMapping(value = "/comment/resource/{resourceId}/delete/{commentId}", method = RequestMethod.DELETE, produces = PLAIN_RESPONSE_TYPE)
public ResponseEntity<?> deleteComment(@PathVariable String resourceId, @PathVariable String commentId) {
ArrayList<String> listOfTopIds = this.resourceService.getAllCommentsOfTheResource(resourceId);
commentService.deleteComment(commentId);
if (listOfTopIds.contains(commentId)) {
// remove the comment from the resource as well
resourceService.deleteRootComment(resourceId, commentId);
}
return new ResponseEntity<>("Comment deleted recursively-successfully", HttpStatus.OK);
}
public class CommentCrudOperations {
String collectionName;
public final String DB_NAME = "commentsDB";
public final String SEARCH_PARAM = "commentId";
public final String UPDATE_PARAM = "commentString";
boolean useCF;
private MongoCollection<CommentModel> collection;
public CommentCrudOperations(String colName, boolean usecf, String mongoUrl) {
collectionName = colName;
useCF = usecf;
if (useCF == false) {
MongoOperations.getInstance(false).bindToDb(mongoUrl, DB_NAME);
}
MongoOperations.getInstance(useCF).createCollection(collectionName);
this.collection = MongoOperations.getInstance(useCF).getDb().getCollection(collectionName, CommentModel.class);
}
public Map<String, CommentModel> findAll(ArrayList<String> topLevelCommentIds) {
Map<String, CommentModel> listOfCommentsFound = new HashMap<String, CommentModel>();
for (int i = 0; i < topLevelCommentIds.size(); i++) {
String eachId = topLevelCommentIds.get(i);
CommentModel foundComment = collection.find(eq(SEARCH_PARAM, eachId)).first();
if (foundComment != null) {
String searchId = foundComment.getChildCommentId();
listOfCommentsFound.put(foundComment.getCommentId(), foundComment);
}
}
return listOfCommentsFound;
}
public CommentModel findOne(String commentId) {
CommentModel foundUser = collection.find(eq(SEARCH_PARAM, commentId)).first();
return foundUser;
}
public CommentModel save(CommentModel commentObj) {
collection.insertOne(commentObj);
return commentObj;
}
public ArrayList<CommentModel> saveMany(ArrayList<CommentModel> commentCollection) {
collection.insertMany(commentCollection);
return commentCollection;
}
public ArrayList<CommentModel> delete(String commentId) {
ArrayList<CommentModel> listOfDeletedComments = new ArrayList<CommentModel>();
while (commentId != null) {
CommentModel deletedComment = collection.findOneAndDelete(eq(SEARCH_PARAM, commentId));
listOfDeletedComments.add(deletedComment);
if (deletedComment != null) {
commentId = deletedComment.getChildCommentId();
} else {
commentId = null;
}
}
return listOfDeletedComments;
}
public ArrayList<CommentModel> updateComments(ArrayList<CommentModel> commentObjList) {
ArrayList<CommentModel> updatedComments = new ArrayList<CommentModel>();
for (int i = 0; i < commentObjList.size(); i++) {
CommentModel eachComment = commentObjList.get(i);
collection.findOneAndReplace(eq(SEARCH_PARAM, eachComment.getUserId()), eachComment);
updatedComments.add(eachComment);
}
return commentObjList;
}
public CommentModel updateComment(CommentModel eachComment) {
Bson filter = Filters.eq(SEARCH_PARAM, eachComment.getCommentId());
Bson updates = Updates.set(UPDATE_PARAM, eachComment.getCommentString());
CommentModel updatedValue = collection.findOneAndUpdate(filter, updates);
return updatedValue;
}
}
cf api https://api.cf.sap.hana.ondemand.com
cf login
Once the app is deployed then
//create the mongo service
cf create-service mongodb v3.0-dev mongoserv
//bind the service to the app
cf bind-service commentAsService mongoserv
//restage the app
cf restage commentAsService
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
29 | |
21 | |
10 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
5 |