Search in sources :

Example 1 with RequestEventHandler

use of cern.modesti.request.spi.RequestEventHandler in project modesti by jlsalmon.

the class RequestServiceImpl method save.

/**
 * Save an existing request.
 *
 * @param updated the request to save
 * @return the newly saved request
 */
@Override
public Request save(Request updated) {
    Request original = repository.findOne(updated.getId());
    if (original == null) {
        throw new RuntimeException(format("Request #%s was not found", updated.getId()));
    }
    // The request id may not be modified manually.
    if (!Objects.equals(updated.getRequestId(), original.getRequestId())) {
        throw new IllegalArgumentException("Request ID cannot not be updated manually!");
    }
    // The request status may not be modified manually.
    if (!Objects.equals(updated.getStatus(), original.getStatus())) {
        throw new IllegalArgumentException("Request status cannot not be updated manually!");
    }
    // TODO: this shouldn't be necessary, and could cause side effects. Why do we lose properties when saving?
    Map<String, Object> properties = original.getProperties();
    properties.putAll(updated.getProperties());
    updated.setProperties(properties);
    for (Point point : updated.getPoints()) {
        if (point.getLineNo() == null) {
            point.setLineNo((long) (updated.getPoints().indexOf(point) + 1));
        }
    }
    // Invoke any callbacks
    for (RequestEventHandler requestEventHandler : requestEventHandlers) {
        requestEventHandler.onBeforeSave(updated);
    }
    // Apply formatting to the request points
    requestFormatter.format(updated);
    if (updated.getType().equals(RequestType.UPDATE) && (updated.getStatus().equals("IN_PROGRESS") || updated.getStatus().equals("FOR_ADDRESSING") || updated.getStatus().equals("IN_ERROR"))) {
        // Process and store any changes that were made to the request
        ((RequestHistoryServiceImpl) historyService).saveChangeHistory(updated);
    }
    return repository.save((RequestImpl) updated);
}
Also used : Point(cern.modesti.point.Point) RequestEventHandler(cern.modesti.request.spi.RequestEventHandler) RequestHistoryServiceImpl(cern.modesti.request.history.RequestHistoryServiceImpl)

Aggregations

Point (cern.modesti.point.Point)1 RequestHistoryServiceImpl (cern.modesti.request.history.RequestHistoryServiceImpl)1 RequestEventHandler (cern.modesti.request.spi.RequestEventHandler)1