Refactor PUT endpoint json parsing to make use of objectmapper

This commit is contained in:
Jelle Pelgrims
2019-08-01 15:26:04 +02:00
parent c40dabf9c6
commit 0377e0a605
4 changed files with 26 additions and 37 deletions

View File

@@ -184,7 +184,7 @@ public class Relationship implements ReloadableEntity<Integer> {
/** /**
* Standard getter for the leftwardLabel String in this Relationship * Standard getter for the leftwardLabel String in this Relationship
* @return the leftWardlabel String for this relationship * @return the leftwardlabel String for this relationship
*/ */
public String getLeftwardLabel() { public String getLeftwardLabel() {
return leftwardLabel; return leftwardLabel;

View File

@@ -64,7 +64,7 @@ public class RelationshipServiceImpl implements RelationshipService {
@Override @Override
public Relationship create(Context c, Item leftItem, Item rightItem, RelationshipType relationshipType, public Relationship create(Context c, Item leftItem, Item rightItem, RelationshipType relationshipType,
int leftPlace, int rightPlace, String leftWardLabel, String rightWardLabel) int leftPlace, int rightPlace, String leftwardLabel, String rightwardLabel)
throws AuthorizeException, SQLException { throws AuthorizeException, SQLException {
Relationship relationship = new Relationship(); Relationship relationship = new Relationship();
relationship.setLeftItem(leftItem); relationship.setLeftItem(leftItem);
@@ -72,8 +72,8 @@ public class RelationshipServiceImpl implements RelationshipService {
relationship.setRelationshipType(relationshipType); relationship.setRelationshipType(relationshipType);
relationship.setLeftPlace(leftPlace); relationship.setLeftPlace(leftPlace);
relationship.setRightPlace(rightPlace); relationship.setRightPlace(rightPlace);
relationship.setLeftwardLabel(leftWardLabel); relationship.setLeftwardLabel(leftwardLabel);
relationship.setRightwardLabel(rightWardLabel); relationship.setRightwardLabel(rightwardLabel);
return create(c, relationship); return create(c, relationship);
} }

View File

@@ -134,14 +134,14 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
* @param relationshipType The RelationshipType object for the relationship * @param relationshipType The RelationshipType object for the relationship
* @param leftPlace The leftPlace integer for the relationship * @param leftPlace The leftPlace integer for the relationship
* @param rightPlace The rightPlace integer for the relationship * @param rightPlace The rightPlace integer for the relationship
* @param leftWardLabel The leftWardLabel integer for the relationship * @param leftwardLabel The leftwardLabel integer for the relationship
* @param rightWardLabel The rightWardLabel integer for the relationship * @param rightwardLabel The rightwardLabel integer for the relationship
* @return The created Relationship object with the given properties * @return The created Relationship object with the given properties
* @throws AuthorizeException If something goes wrong * @throws AuthorizeException If something goes wrong
* @throws SQLException If something goes wrong * @throws SQLException If something goes wrong
*/ */
Relationship create(Context c, Item leftItem, Item rightItem, RelationshipType relationshipType, Relationship create(Context c, Item leftItem, Item rightItem, RelationshipType relationshipType,
int leftPlace, int rightPlace, String leftWardLabel, String rightWardLabel) int leftPlace, int rightPlace, String leftwardLabel, String rightwardLabel)
throws AuthorizeException, SQLException; throws AuthorizeException, SQLException;

View File

@@ -7,6 +7,7 @@
*/ */
package org.dspace.app.rest.repository; package org.dspace.app.rest.repository;
import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@@ -14,6 +15,7 @@ import java.util.UUID;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dspace.app.rest.Parameter; import org.dspace.app.rest.Parameter;
import org.dspace.app.rest.SearchRestMethod; import org.dspace.app.rest.SearchRestMethod;
@@ -110,14 +112,14 @@ public class RelationshipRestRepository extends DSpaceRestRepository<Relationshi
RelationshipType relationshipType = relationshipTypeService RelationshipType relationshipType = relationshipTypeService
.find(context, Integer.parseInt(req.getParameter("relationshipType"))); .find(context, Integer.parseInt(req.getParameter("relationshipType")));
String leftWardLabel = req.getParameter("leftwardLabel"); String leftwardLabel = req.getParameter("leftwardLabel");
String rightWardLabel = req.getParameter("rightwardLabel"); String rightwardLabel = req.getParameter("rightwardLabel");
EPerson ePerson = context.getCurrentUser(); EPerson ePerson = context.getCurrentUser();
if (authorizeService.authorizeActionBoolean(context, leftItem, Constants.WRITE) || if (authorizeService.authorizeActionBoolean(context, leftItem, Constants.WRITE) ||
authorizeService.authorizeActionBoolean(context, rightItem, Constants.WRITE)) { authorizeService.authorizeActionBoolean(context, rightItem, Constants.WRITE)) {
Relationship relationship = relationshipService.create(context, leftItem, rightItem, Relationship relationship = relationshipService.create(context, leftItem, rightItem,
relationshipType, 0, 0, leftWardLabel, rightWardLabel); relationshipType, 0, 0, leftwardLabel, rightwardLabel);
// The above if check deals with the case that a Relationship can be created if the user has write // The above if check deals with the case that a Relationship can be created if the user has write
// rights on one of the two items. The following updateItem calls can however call the // rights on one of the two items. The following updateItem calls can however call the
// ItemService.update() functions which would fail if the user doesn't have permission on both items. // ItemService.update() functions which would fail if the user doesn't have permission on both items.
@@ -231,40 +233,27 @@ public class RelationshipRestRepository extends DSpaceRestRepository<Relationshi
throw new ResourceNotFoundException("Relationship" + " with id: " + id + " not found"); throw new ResourceNotFoundException("Relationship" + " with id: " + id + " not found");
} }
Integer leftPlace = null; try {
Integer rightPlace = null;
String leftwardLabel = null;
String rightwardLabel = null;
if (jsonNode.hasNonNull("leftwardLabel")) { RelationshipRest relationshipRest;
leftwardLabel = jsonNode.get("leftwardLabel").asText();
try {
relationshipRest = new ObjectMapper().readValue(jsonNode.toString(), RelationshipRest.class);
} catch (IOException e) {
throw new UnprocessableEntityException("Error parsing request body: " + e.toString());
} }
if (jsonNode.hasNonNull("rightwardLabel")) { relationship.setLeftwardLabel(relationshipRest.getLeftwardLabel());
rightwardLabel = jsonNode.get("rightwardLabel").asText(); relationship.setRightwardLabel(relationshipRest.getRightwardLabel());
if (jsonNode.hasNonNull("rightPlace")) {
relationship.setRightPlace(relationshipRest.getRightPlace());
} }
if (jsonNode.hasNonNull("leftPlace")) { if (jsonNode.hasNonNull("leftPlace")) {
leftPlace = jsonNode.get("leftPlace").asInt(); relationship.setRightPlace(relationshipRest.getLeftPlace());
} }
if (jsonNode.hasNonNull("rightPlace")) {
rightPlace = jsonNode.get("rightPlace").asInt();
}
try {
if (leftPlace != null) {
relationship.setLeftPlace(leftPlace);
}
if (rightPlace != null) {
relationship.setRightPlace(rightPlace);
}
relationship.setLeftwardLabel(leftwardLabel);
relationship.setRightwardLabel(rightwardLabel);
relationshipService.update(context, relationship); relationshipService.update(context, relationship);
context.commit(); context.commit();
context.reloadEntity(relationship); context.reloadEntity(relationship);