Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unique index creation error for schemas added via API #229

7 changes: 5 additions & 2 deletions java/pojos/src/main/java/dev/sunbirdrc/pojos/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ public enum API_ID {
HEALTH(getApiPrefix() + ".health"),
DELETE(getApiPrefix() + ".delete"),

PUT(getApiPrefix() + ".put"),

POST(getApiPrefix() + ".post"),

GET(getApiPrefix() + ".get"),

PATCH(getApiPrefix() + ".patch"),
PUT(getApiPrefix() + ".put"),
SEARCH(getApiPrefix() + ".search"),

POST(getApiPrefix() + ".post"),
SIGN(getApiName() + ".utils.sign"),
VERIFY(getApiName() + ".utils.verify"),
KEYS(getApiName() + ".utils.keys"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import dev.sunbirdrc.registry.transform.Data;
import dev.sunbirdrc.registry.transform.ITransformer;
import dev.sunbirdrc.registry.util.ViewTemplateManager;
import dev.sunbirdrc.registry.util.EntityParenter;
import dev.sunbirdrc.registry.util.ViewTemplateManager;
import dev.sunbirdrc.validators.ValidationException;
import org.agrona.Strings;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -73,6 +75,14 @@ public class RegistryEntityController extends AbstractController {
@Value("${certificate.enableExternalTemplates:false}")
boolean externalTemplatesEnabled;

@Autowired
EntityParenter entityParenter;

@Value("${authentication.enabled:true}")
boolean securityEnabled;
@Value("${certificate.enableExternalTemplates:false}")
boolean externalTemplatesEnabled;

@RequestMapping(value = "/api/v1/{entityName}/invite", method = RequestMethod.POST)
public ResponseEntity<Object> invite(
@PathVariable String entityName,
Expand Down Expand Up @@ -119,7 +129,6 @@ public ResponseEntity<Object> invite(
return internalErrorResponse(responseParams, response, e);
}
}

@NotNull
private void createSchemaNotFoundResponse(String errorMessage, ResponseParams responseParams) {
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
Expand Down Expand Up @@ -165,12 +174,12 @@ public ResponseEntity<Object> deleteEntity(
responseParams.setStatus(Response.Status.SUCCESSFUL);
watch.stop(tag);
return new ResponseEntity<>(response, HttpStatus.OK);

} catch (RecordNotFoundException e) {
} catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(), responseParams);
response = new Response(Response.API_ID.DELETE, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception e) {
}
catch (Exception e) {
logger.error("RegistryController: Exception while Deleting entity", e);
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());
Expand Down Expand Up @@ -202,11 +211,12 @@ public ResponseEntity<Object> searchEntity(@PathVariable String entityName, @Req
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(String.format("Searching on entity %s not allowed", entityName));
}
} catch (RecordNotFoundException e) {
} catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(), responseParams);
response = new Response(Response.API_ID.SEARCH, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception e) {
}
catch (Exception e) {
logger.error("Exception in controller while searching entities !", e);
response.setResult("");
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
Expand Down Expand Up @@ -256,12 +266,12 @@ public ResponseEntity<Object> putEntity(
watch.stop(tag);

return new ResponseEntity<>(response, HttpStatus.OK);

} catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(), responseParams);
createSchemaNotFoundResponse(e.getMessage(), responseParams);
response = new Response(Response.API_ID.PUT, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception e) {
}
catch (Exception e) {
logger.error("RegistryController: Exception while updating entity (without id)!", e);
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());
Expand Down Expand Up @@ -307,11 +317,12 @@ public ResponseEntity<Object> postEntity(
watch.stop("RegistryController.addToExistingEntity");

return new ResponseEntity<>(response, HttpStatus.OK);
} catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(), responseParams);
}catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(), responseParams);
response = new Response(Response.API_ID.POST, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (MiddlewareHaltException e) {
}
catch (MiddlewareHaltException e) {
logger.info("Error in validating the request");
return badRequestException(responseParams, response, e.getMessage());
} catch (Exception e) {
Expand Down Expand Up @@ -359,11 +370,12 @@ public ResponseEntity<Object> updatePropertyOfTheEntity(
registryHelper.invalidateAttestation(entityName, entityId, userId, registryHelper.getPropertyToUpdate(request, entityId));
watch.stop(tag);
return new ResponseEntity<>(response, HttpStatus.OK);
} catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(), responseParams);
} catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(), responseParams);
response = new Response(Response.API_ID.PUT, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception e) {
}
catch (Exception e) {
responseParams.setErrmsg(e.getMessage());
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
Expand All @@ -384,10 +396,11 @@ public ResponseEntity<Object> addNewPropertyToTheEntity(
checkEntityNameInDefinitionManager(entityName);
registryHelper.authorize(entityName, entityId, request);
} catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(), responseParams);
response = new Response(Response.API_ID.POST, "ERROR", responseParams);
createSchemaNotFoundResponse(e.getMessage(), responseParams);
response = new Response(Response.API_ID.POST, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception e) {
}
catch (Exception e) {
return createUnauthorizedExceptionResponse(e);
}

Expand Down Expand Up @@ -461,8 +474,8 @@ public ResponseEntity<Object> getEntityWithConsent(
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(), responseParams);
Response response = new Response(Response.API_ID.GET, "ERROR", responseParams);
createSchemaNotFoundResponse(e.getMessage(), responseParams);
Response response = new Response(Response.API_ID.GET, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception e) {
logger.error("Error in partner api access", e);
Expand Down Expand Up @@ -499,10 +512,8 @@ private ArrayList<String> getConsentFields(HttpServletRequest request) {
{MediaType.APPLICATION_PDF_VALUE, MediaType.TEXT_HTML_VALUE, Constants.SVG_MEDIA_TYPE})
public ResponseEntity<Object> getEntityType(@PathVariable String entityName,
@PathVariable String entityId,
HttpServletRequest request,
@RequestHeader(required = false) String viewTemplateId) {
HttpServletRequest request) {
ResponseParams responseParams = new ResponseParams();
Response response ;
if (registryHelper.doesEntityOperationRequireAuthorization(entityName) && securityEnabled) {
try {

Expand All @@ -511,11 +522,12 @@ public ResponseEntity<Object> getEntityType(@PathVariable String entityName,
try {
checkEntityNameInDefinitionManager(entityName);
registryHelper.authorizeAttestor(entityName, request);
} catch (RecordNotFoundException re) {
createSchemaNotFoundResponse(re.getMessage(), responseParams);
response = new Response(Response.API_ID.GET, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception exceptionFromAuthorizeAttestor) {
} catch (RecordNotFoundException re) {
createSchemaNotFoundResponse(re.getMessage(), responseParams);
Response response = new Response(Response.API_ID.GET, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
catch (Exception exceptionFromAuthorizeAttestor) {
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
}
Expand Down Expand Up @@ -595,8 +607,8 @@ public ResponseEntity<Object> getEntity(
try {
checkEntityNameInDefinitionManager(entityName);
String readerUserId = getUserId(entityName, request);
JsonNode node = getEntityJsonNode(entityName, entityId, requireLDResponse, readerUserId, viewTemplateId);
if (requireLDResponse) {
JsonNode node = getEntityJsonNode(entityName, entityId, requireLDResponse, readerUserId);
if(requireLDResponse) {
addJsonLDSpec(node);
} else if (requireVCResponse) {
String vcString = node.get(OSSystemFields._osSignedData.name()).textValue();
Expand All @@ -605,7 +617,7 @@ public ResponseEntity<Object> getEntity(
return new ResponseEntity<>(node, HttpStatus.OK);

} catch (RecordNotFoundException re) {
createSchemaNotFoundResponse(re.getMessage(), responseParams);
createSchemaNotFoundResponse(re.getMessage(), responseParams);
response = new Response(Response.API_ID.GET, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception e) {
Expand Down Expand Up @@ -665,10 +677,11 @@ public ResponseEntity<Object> getEntityByToken(@PathVariable String entityName,
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
} catch (RecordNotFoundException e) {
createSchemaNotFoundResponse(e.getMessage(),responseParams);
createSchemaNotFoundResponse(e.getMessage(),responseParams);
response = new Response(Response.API_ID.GET, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception e) {
}
catch (Exception e) {
logger.error("Exception in controller while searching entities !", e);
response.setResult("");
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
Expand All @@ -682,8 +695,7 @@ public ResponseEntity<Object> getEntityByToken(@PathVariable String entityName,
public ResponseEntity<Object> getEntityForAttestation(
@PathVariable String entity,
@PathVariable String entityId
) {
ResponseParams responseParams = new ResponseParams();
) { ResponseParams responseParams= new ResponseParams();
try {
JsonNode resultNode = registryHelper.readEntity("", entity, entityId, false, null, false);
ObjectNode objectNode = objectMapper.createObjectNode();
Expand All @@ -694,9 +706,8 @@ public ResponseEntity<Object> getEntityForAttestation(
.getAttestationPolicies();
objectNode.set("attestationPolicies", objectMapper.convertValue(attestationPolicies, JsonNode.class));
return new ResponseEntity<>(objectNode, HttpStatus.OK);

} catch (RecordNotFoundException re) {
createSchemaNotFoundResponse(re.getMessage(), responseParams);
createSchemaNotFoundResponse(re.getMessage(), responseParams);
Response response = new Response(Response.API_ID.GET, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
Expand All @@ -715,16 +726,15 @@ public ResponseEntity<Object> attestEntity(
@RequestHeader HttpHeaders header,
@RequestBody JsonNode rootNode
) throws Exception {
ResponseParams responseParams = new ResponseParams();
ResponseParams responseParams=new ResponseParams();
try {
checkEntityNameInDefinitionManager(entityName);
} catch (RecordNotFoundException re) {
createSchemaNotFoundResponse(re.getMessage(),responseParams);
createSchemaNotFoundResponse(re.getMessage(),responseParams);
Response response = new Response(Response.API_ID.PATCH, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}

logger.info("Attestation request for {}", rootNode.get("fieldPaths"));
logger.info("Attestation request for {}", rootNode.get("fieldPaths"));
JsonNode nodePath = rootNode.get("jsonPaths");
if (nodePath instanceof ArrayNode) {
Iterator<JsonNode> elements = ((ArrayNode) nodePath).elements();
Expand Down Expand Up @@ -811,10 +821,11 @@ public ResponseEntity<Object> getSignedEntityByToken(@PathVariable String entity
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
} catch (RecordNotFoundException re) {
createSchemaNotFoundResponse(re.getMessage(), responseParams);
createSchemaNotFoundResponse(re.getMessage(), responseParams);
response = new Response(Response.API_ID.GET, "ERROR", responseParams);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
} catch (Exception e) {
}
catch (Exception e) {
logger.error("Exception in controller while searching entities !", e);
response.setResult("");
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
Expand All @@ -841,16 +852,16 @@ public ResponseEntity<Object> getAttestationCertificate(HttpServletRequest reque
getTemplateUrlFromRequest(request, entityName),
getAttestationNode(attestationId, node)
), HttpStatus.OK);

} catch (RecordNotFoundException re) {
}catch (RecordNotFoundException re) {
createSchemaNotFoundResponse(re.getMessage(), responseParams);
Response response = new Response(Response.API_ID.GET, "ERROR", responseParams);
try {
return new ResponseEntity<>(objectMapper.writeValueAsString(response), HttpStatus.NOT_FOUND);
} catch (JsonProcessingException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (AttestationNotFoundException e) {
}
catch (AttestationNotFoundException e) {
logger.error(e.getMessage());
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ public String addEntity(Shard shard, String userId, JsonNode rootNode, boolean s
entityId = registryDao.addEntity(graph, rootNode);
if (commitEnabled) {
dbProvider.commitTransaction(graph, tx);
entityParenter.ensureKnownParentNodesForSchema(rootNode);
entityParenter.loadDefinitionIndexForSchema(rootNode);
entityParenter.ensureIndexExists();
}
} finally {
if (tx != null) {
Expand Down
Loading