diff options
| author | Tharre <tharre3@gmail.com> | 2018-06-19 21:09:14 +0200 | 
|---|---|---|
| committer | Tharre <tharre3@gmail.com> | 2018-06-19 21:10:07 +0200 | 
| commit | 280196ec77ffb1ff722b947afa6b2e2439a663ec (patch) | |
| tree | 09658a6778914d0cc010ffd20e5870d20105aa4a | |
| parent | fffb6c7bbc2a68fb824309616d3d278b39f4e824 (diff) | |
| download | sepm-groupproject-280196ec77ffb1ff722b947afa6b2e2439a663ec.tar.gz sepm-groupproject-280196ec77ffb1ff722b947afa6b2e2439a663ec.tar.xz sepm-groupproject-280196ec77ffb1ff722b947afa6b2e2439a663ec.zip | |
Implement and remove registrations #25963
3 files changed, 45 insertions, 9 deletions
| diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/CreateOperationController.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/CreateOperationController.java index e5d7b72..cc09127 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/CreateOperationController.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/controller/CreateOperationController.java @@ -5,6 +5,7 @@ import static at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller.  import static at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.controller.Helper.showValidationErrorAlertAndWait;  import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidOperationException; +import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidRegistrationException;  import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException;  import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException;  import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation; @@ -12,6 +13,7 @@ import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation.Stat  import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Registration;  import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle;  import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.OperationService; +import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.RegistrationService;  import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service.VehicleService;  import java.io.IOException;  import java.time.Instant; @@ -66,11 +68,15 @@ public class CreateOperationController {      private final OperationService operationService;      private final VehicleService vehicleService; +    private final RegistrationService registrationService;      public CreateOperationController( -            OperationService operationService, VehicleService vehicleService) { +            OperationService operationService, +            VehicleService vehicleService, +            RegistrationService registrationService) {          this.operationService = operationService;          this.vehicleService = vehicleService; +        this.registrationService = registrationService;      }      @FXML @@ -117,7 +123,10 @@ public class CreateOperationController {                          .setOnMouseClicked(                                  event -> {                                      if (event.getButton().equals(MouseButton.SECONDARY)) { -                                        createContextMenu(vehicle, vehicleService) +                                        createContextMenu( +                                                        vehicle, +                                                        vehicleService, +                                                        registrationService)                                                  .show(                                                          controller.getRootElement(),                                                          event.getScreenX(), @@ -172,14 +181,11 @@ public class CreateOperationController {          }      } -    private ContextMenu createContextMenu(Vehicle data, VehicleService vehicleService) { +    private ContextMenu createContextMenu( +            Vehicle data, VehicleService vehicleService, RegistrationService registrationService) {          ContextMenu menu = new ContextMenu();          for (Vehicle.Status status : Vehicle.Status.values()) { -            if (status == Vehicle.Status.ABGEMELDET) { -                continue; -            } -              MenuItem mi = new MenuItem(status.name());              if (status == Vehicle.Status.FREI_FUNK || status == Vehicle.Status.FREI_WACHE) { @@ -191,9 +197,13 @@ public class CreateOperationController {              mi.setOnAction(                      event -> {                          try { +                            if (status == Vehicle.Status.ABGEMELDET) +                                for (Registration registration : data.registrations()) +                                    registrationService.remove(registration.id()); +                              vehicleService.update(data.toBuilder().status(status).build());                              this.updateList(); -                        } catch (InvalidVehicleException e) { +                        } catch (InvalidVehicleException | InvalidRegistrationException e) {                              LOG.debug(                                      "Validation error in createContextMenu(). (mi.setOnAction) ",                                      e); diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/dao/RegistrationDatabaseDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/dao/RegistrationDatabaseDAO.java index 9f182c2..8ae319c 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/dao/RegistrationDatabaseDAO.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/dao/RegistrationDatabaseDAO.java @@ -11,6 +11,7 @@ import java.sql.PreparedStatement;  import java.sql.ResultSet;  import java.sql.SQLException;  import java.sql.Statement; +import java.time.Instant;  import java.time.LocalDate;  import java.time.OffsetDateTime;  import java.time.ZoneId; @@ -120,6 +121,23 @@ public class RegistrationDatabaseDAO implements RegistrationDAO {      @Override      public void remove(long id) throws ElementNotFoundException, PersistenceException { +        String sql = "UPDATE Registration SET active = 0, end = ? WHERE id = ?"; + +        try { +            Connection con = jdbcConnectionManager.getConnection(); + +            try (PreparedStatement pstmt = con.prepareStatement(sql)) { +                pstmt.setObject(1, OffsetDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())); +                pstmt.setLong(2, id); + +                if (pstmt.executeUpdate() != 1) +                    throw new ElementNotFoundException("No such registrationId exists"); +            } + +        } catch (SQLException e) { +            throw new PersistenceException(e); +        } +          throw new UnsupportedOperationException();      } diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/RegistrationServiceImpl.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/RegistrationServiceImpl.java index eb2cd1d..a6a1dfe 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/RegistrationServiceImpl.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/RegistrationServiceImpl.java @@ -47,6 +47,14 @@ public class RegistrationServiceImpl implements RegistrationService {      @Override      public void remove(long registrationId) throws InvalidRegistrationException, ServiceException { -        throw new UnsupportedOperationException(); +        if (registrationId <= 0) throw new InvalidRegistrationException("RegistrationId invalid"); + +        try { +            registrationDAO.remove(registrationId); +        } catch (PersistenceException e) { +            throw new ServiceException(e); +        } catch (ElementNotFoundException e) { +            throw new InvalidRegistrationException(e); +        }      }  } | 
