add notfounderror

main
Julius Sula 2023-05-22 13:21:22 +02:00
parent ae6c497f36
commit 4a06622e62
3 changed files with 59 additions and 31 deletions

View File

@ -18,6 +18,7 @@ import com.bmc.arsys.api.Value;
import com.nttdata.calender.api.Query;
import com.nttdata.calender.api.RemedyJavaAPI;
import com.nttdata.calender.changes.query.Filter;
import com.nttdata.calender.errorhandling.NotFoundError;
/**
* Class representing the change with all of the change specific attributes.
@ -76,26 +77,7 @@ public class Change {
public ChangeResponse get(ChangeRequest request) throws ARException {
api.impersonateUser("ext_StanzPa");
// Queries for SupportGroup of impersonated User
var queryPerson = new Query.QueryBuilder("CTM:Support Group Association")
.addFieldId("FullName", 1000000017)
.addFieldId("SupportGroupId", 1000000079)
.build();
var peopleInfos = api.queryFieldsById("\'Login ID\' = \"" + api.getUser() + "\"",
queryPerson.getFieldIds(),
queryPerson.getFormName(), null, 0, 0);
if (peopleInfos.isEmpty()) {
throw new RuntimeException("No user found with this loginId");
}
// constructs set of supportGroups and full name of impersonated User
String[] peopleSupportGroup = peopleInfos.stream()
.map(entry -> entry.get(queryPerson.getFieldId("SupportGroupId")).toString())
.toArray(String[]::new);
var peopleFullName = peopleInfos.get(0).get(queryPerson.getFieldId("FullName")).toString();
request.addFilter(
new Filter("SupportGroupId", "equals", peopleSupportGroup));
var peopleFullName = processPeopleInfo(request);
var qualifier = "";
if (request.getFilter() != null) {
@ -145,6 +127,32 @@ public class Change {
return new ChangeResponse(entriesSize, changes);
}
private String processPeopleInfo(ChangeRequest request) throws ARException {
// Queries for SupportGroup of impersonated User
var queryPerson = new Query.QueryBuilder("CTM:Support Group Association")
.addFieldId("FullName", 1000000017)
.addFieldId("SupportGroupId", 1000000079)
.build();
var peopleInfos = api.queryFieldsById("\'Login ID\' = \"" + api.getUser() + "\"",
queryPerson.getFieldIds(),
queryPerson.getFormName(), null, 0, 0);
if (peopleInfos.isEmpty()) {
throw new NotFoundError("No user found with this loginId");
}
// constructs set of supportGroups and full name of impersonated User
String[] peopleSupportGroup = peopleInfos.stream()
.map(entry -> entry.get(queryPerson.getFieldId("SupportGroupId")).toString())
.toArray(String[]::new);
if (peopleSupportGroup.length <= 0) {
throw new NotFoundError("No supportGroups associated to the loginId ");
}
request.addFilter(
new Filter("SupportGroupId", "equals", peopleSupportGroup));
return peopleInfos.get(0).get(queryPerson.getFieldId("FullName")).toString();
}
/**
* Returns the {@link Value} of an entry based on the provided description.
*

View File

@ -17,22 +17,18 @@ public class GlobalExceptionHandler {
@ExceptionHandler(ARException.class)
public ResponseEntity<ErrorResponse> handleARException(ARException e, HttpServletRequest request) {
var errorResponse = new ErrorResponse(e.getMessage(), e.getClass().getSimpleName());
errorResponse.setMessage(e.getMessage());
errorResponse.setError(e.getClass().getSimpleName());
var errorMessage = "Remedy server error: \n" + e.getMessage();
var errorResponse = new ErrorResponse(errorMessage, e.getClass().getSimpleName());
errorLogger.error(e.getMessage(), e);
errorLogger.error(errorMessage, e);
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleGenericException(Exception e, HttpServletRequest request) {
var errorResponse = new ErrorResponse(e.getMessage(), e.getClass().getSimpleName());
errorLogger.error(e.getMessage(), e);
var response = new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
System.out.println(response.toString());
System.out.println(HttpStatus.INTERNAL_SERVER_ERROR);
System.out.println(errorResponse.toString());
return response;
var errorMessage = "Backend internal server error: \n" + e.getMessage();
var errorResponse = new ErrorResponse(errorMessage, e.getClass().getSimpleName());
errorLogger.error(errorMessage, e);
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

View File

@ -0,0 +1,24 @@
package com.nttdata.calender.errorhandling;
import org.springframework.http.HttpStatus;
public class NotFoundError extends RuntimeException {
private int errorCode;
private HttpStatus httpStatus;
public NotFoundError(String message) {
super(message);
this.errorCode = 404;
this.httpStatus = HttpStatus.NOT_FOUND;
}
// Include getters for errorCode and httpStatus
public int getErrorCode() {
return errorCode;
}
public HttpStatus getHttpStatus() {
return httpStatus;
}
}