Example usage for org.springframework.dao RecoverableDataAccessException getCause

List of usage examples for org.springframework.dao RecoverableDataAccessException getCause

Introduction

In this page you can find the example usage for org.springframework.dao RecoverableDataAccessException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:cz.fi.muni.pa165.calorycounter.frontend.restserver.ActivityRestResource.java

/**
 *
 * @return all activities/*from  w w w.j  a  v  a 2  s  .  c o m*/
 */
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getAll() {
    log.debug("Server: getAll()");
    List<ActivityDto> activities = null;
    try {
        activities = activityService.getActive();
    } catch (RecoverableDataAccessException ex) {
        if (ex.getCause().getClass().equals(NoResultException.class)) {
            activities = new LinkedList<>();
        } else {
            throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
    GenericEntity<List<ActivityDto>> entity = new GenericEntity<List<ActivityDto>>(activities) {
    };
    return Response.status(Response.Status.OK).entity(entity).build();
}

From source file:cz.fi.muni.pa165.calorycounter.frontend.restserver.ActivityRestResource.java

/**
 *
 * @param activityName name of the activity
 * @return information about activity with given name for all weight
 * categories//from  www .j  av a  2  s.  c  o m
 */
@GET
@Path("/name/{activityName}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getByName(@PathParam("activityName") String activityName) {
    log.debug("Server: getByName(activityName) with name: " + activityName);
    if (activityName == null) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    ActivityDto activity = null;
    try {
        activity = activityService.get(activityName);
    } catch (RecoverableDataAccessException ex) {
        if (ex.getCause().getClass().equals(NoResultException.class)) {
            return Response.status(Response.Status.BAD_REQUEST).build();
        } else {
            throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
    return Response.status(Response.Status.OK).entity(activity).build();
}

From source file:cz.fi.muni.pa165.calorycounter.frontend.restserver.ActivityRestResource.java

/**
 *
 * @param weightCategory name of the WeightCategory of which activities
 * shall be returned (in format _130_)/*from   w ww  .ja  va 2s.  com*/
 * @return activities containing information only about given weight
 * category
 */
@GET
@Path("/weightcategory/{weightcategory}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getAll(@PathParam("weightcategory") String weightCategory) {
    log.debug("Server: getAll() with weight category: " + weightCategory);
    WeightCategory wc = null;
    try {
        wc = WeightCategory.valueOf(weightCategory);
    } catch (IllegalArgumentException e) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    List<ActivityDto> activities = null;
    try {
        activities = activityService.getActive(wc);
    } catch (RecoverableDataAccessException ex) {
        if (ex.getCause().getClass().equals(NoResultException.class)) {
            activities = new LinkedList<>();
        } else {
            throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
    GenericEntity<List<ActivityDto>> entity = new GenericEntity<List<ActivityDto>>(activities) {
    };
    return Response.status(Response.Status.OK).entity(entity).build();
}

From source file:cz.fi.muni.pa165.calorycounter.frontend.restserver.RecordRestResource.java

@PUT
@Path("/update")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response update(ActivityRecordDto recordToUpdate) {
    log.debug("Server: update with record: " + recordToUpdate);
    if (recordToUpdate == null || recordToUpdate.getActivityRecordId() == null) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }/*from  w  ww .ja v  a 2  s .co m*/
    //TODO > count again caloriesBurnt and fill original if params are null
    try {
        recordService.update(recordToUpdate);
    } catch (RecoverableDataAccessException ex) {
        if (ex.getCause().getClass().equals(IllegalArgumentException.class)) {
            return Response.status(Response.Status.BAD_REQUEST).build();
        }
        throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
    }
    ActivityRecordDto updatedRecord;
    try {
        updatedRecord = recordService.get(recordToUpdate.getActivityRecordId());
    } catch (RecoverableDataAccessException ex) {
        throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
    }
    Response.ResponseBuilder builder = Response.status(Response.Status.OK);
    builder.entity(updatedRecord);
    return makeCORS(builder);
}

From source file:cz.fi.muni.pa165.calorycounter.frontend.restserver.RecordRestResource.java

@GET
@Path("/user/{username}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getRecordsOfUser(@PathParam("username") String username) {
    log.debug("Server: getRecordsOfUser(username) with username: " + username);
    if (username == null || username.isEmpty()) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }/*  w  w w.  j  a va 2 s .co  m*/
    AuthUserDto user = null;
    try {
        user = userService.getByUsername(username);
    } catch (RecoverableDataAccessException ex) {
        if (ex.getCause().getClass().equals(NoResultException.class)) {
            return Response.status(Response.Status.NOT_FOUND).build();
        } else {
            throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
    UserActivityRecordsDto records = null;
    try {
        records = recordsService.getAllActivityRecords(user);
    } catch (RecoverableDataAccessException ex) {
        if (ex.getCause().getClass().equals(NoResultException.class)) {
            records = new UserActivityRecordsDto();
            records.setNameOfUser(user.getName());
        } else {
            throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
    return Response.status(Response.Status.OK).entity(records).build();
}

From source file:cz.fi.muni.pa165.calorycounter.frontend.restserver.RecordRestResource.java

@POST
@Path("/create")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response create(ActivityRecordDto record) {
    log.debug("Server: create(record): " + record);
    if (record == null || record.getActivityName() == null || record.getActivityDate() == null
            || record.getUserId() == null) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }/*from w  w  w .  jav  a  2 s .com*/
    if (record.getWeightCategory() == null) {
        AuthUserDto user = null;
        try {
            user = userService.getById(record.getUserId());
        } catch (RecoverableDataAccessException ex) {
            if (ex.getCause().getClass().equals(NoResultException.class)) {
                return Response.status(Response.Status.NOT_FOUND).build();
            } else {
                throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
            }
        }
        record.setWeightCategory(user.getWeightCategory());
    }
    if (record.getCaloriesBurnt() == 0) {
        List<ActivityDto> activities = activityService.getActive(record.getWeightCategory());
        ActivityDto activity = null;
        for (ActivityDto act : activities) {
            if (act.getActivityName().equals(record.getActivityName())) {
                activity = act;
                break;
            }
        }
        record.setCaloriesBurnt(
                record.getDuration() * (activity.getCaloriesAmount(record.getWeightCategory())) / 60);
    }
    Long id;
    try {
        id = recordService.create(record);
    } catch (IllegalArgumentException ex) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    ActivityRecordDto newRecord;
    try {
        newRecord = recordService.get(id);
    } catch (RecoverableDataAccessException ex) {
        throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
    }
    Response.ResponseBuilder builder = Response.status(Response.Status.OK);
    builder.entity(newRecord);
    return makeCORS(builder);
}