diff options
Diffstat (limited to 'src/main/java/at/ac/tuwien/sepm')
3 files changed, 59 insertions, 30 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java index fe12952..2f0df44 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDAO.java @@ -34,6 +34,16 @@ public interface VehicleDAO {      List<Vehicle> list() throws PersistenceException;      /** +     * Returns the vehicle with the given id. +     * +     * @param vehicleId id of the vehicle that should be returned +     * @return vehicle with the given id +     * @throws ElementNotFoundException if no vehicle with the given id exists +     * @throws PersistenceException if the vehicle could not be loaded +     */ +    Vehicle get(long vehicleId) throws ElementNotFoundException, PersistenceException; + +    /**       * Remove vehicle with the given id from the store.       *       * @param id of the vehicle that should be removed diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDao.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDao.java index ca1d45c..8f0d28b 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDao.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDao.java @@ -7,6 +7,7 @@ import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.Veh  import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;  import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;  import at.ac.tuwien.sepm.assignment.groupphase.util.JDBCConnectionManager; +import java.sql.Connection;  import java.sql.PreparedStatement;  import java.sql.ResultSet;  import java.sql.SQLException; @@ -114,19 +115,8 @@ public class VehicleDatabaseDao implements VehicleDAO {                                              + "Vehicle where VehicleVersion.id=Vehicle.version");              pstmt.executeQuery();              ResultSet rs = pstmt.getResultSet(); -            while (rs.next()) { -                Vehicle vehicle = -                        Vehicle.builder() -                                .name(rs.getString("name")) -                                .constructionType( -                                        ConstructionType.valueOf(rs.getString("constructionType"))) -                                .status(Status.valueOf(rs.getString("status"))) -                                .id(rs.getInt("id")) -                                .hasNef(rs.getBoolean("hasNef")) -                                .type(VehicleType.valueOf(rs.getString("type").replace("-", "_"))) -                                .build(); -                result.add(vehicle); -            } +            while (rs.next()) result.add(vehicleFromRS(rs)); +          } catch (SQLException e) {              throw new PersistenceException("Die Werte konnten nicht geladen werden.", e);          } finally { @@ -143,5 +133,39 @@ public class VehicleDatabaseDao implements VehicleDAO {      }      @Override +    public Vehicle get(long id) throws ElementNotFoundException, PersistenceException { +        String sql = +                "SELECT a.id, b.name, b.constructionType, b.type, a.status, b.hasNef" +                        + " FROM Vehicle a" +                        + " INNER JOIN VehicleVersion b" +                        + " ON version = b.id" +                        + " WHERE a.id = ?"; + +        try { +            Connection con = jdbcConnectionManager.getConnection(); +            try (PreparedStatement pstmt = con.prepareStatement(sql); +                    ResultSet rs = pstmt.executeQuery()) { + +                if (!rs.first()) throw new ElementNotFoundException("No such vehicle exists"); + +                return vehicleFromRS(rs); +            } +        } catch (SQLException e) { +            throw new PersistenceException(e); +        } +    } + +    @Override      public void remove(long id) throws ElementNotFoundException, PersistenceException {} + +    private Vehicle vehicleFromRS(ResultSet rs) throws SQLException { +        return Vehicle.builder() +                .id(rs.getLong("id")) +                .name(rs.getString("name")) +                .constructionType(ConstructionType.valueOf(rs.getString("constructionType"))) +                .type(VehicleType.valueOf(rs.getString("type"))) +                .status(Status.valueOf(rs.getString("constructionType"))) +                .hasNef(rs.getBoolean("hasNef")) +                .build(); +    }  } diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java index a267b6f..8203ef3 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/service/RegistrationServiceImpl.java @@ -1,15 +1,15 @@  package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.service;  import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.RegistrationDAO; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao.VehicleDAO;  import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration;  import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.RegistrationValidator;  import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle; -import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Vehicle.Status; +import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;  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.PersistenceException;  import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException; -import java.util.EnumSet;  import java.util.List;  import org.slf4j.Logger;  import org.slf4j.LoggerFactory; @@ -22,36 +22,31 @@ public class RegistrationServiceImpl implements RegistrationService {      private static final Logger LOG = LoggerFactory.getLogger(RegistrationServiceImpl.class);      private final RegistrationDAO registrationDAO; -    private final VehicleService vehicleService; +    private final VehicleDAO vehicleDAO;      @Autowired -    public RegistrationServiceImpl(RegistrationDAO registrationDAO, VehicleService vehicleService) { +    public RegistrationServiceImpl(RegistrationDAO registrationDAO, VehicleDAO vehicleDAO) {          this.registrationDAO = registrationDAO; -        this.vehicleService = vehicleService; +        this.vehicleDAO = vehicleDAO;      }      @Override      public List<Long> add(long vehicleId, List<Registration> registrations)              throws InvalidVehicleException, InvalidRegistrationException, ServiceException { -        Vehicle vehicle = -                vehicleService -                        .list(EnumSet.of(Status.ABGEMELDET)) -                        .stream() -                        .filter(v -> v.id() == vehicleId) -                        .findFirst() -                        .orElse(null); +        if (vehicleId <= 0) throw new InvalidVehicleException("VehicleId invalid"); -        if (vehicle == null) { -            throw new ServiceException("no vehicle with this id"); -        } - -        RegistrationValidator.validate(vehicle, registrations);          try { +            Vehicle vehicle = vehicleDAO.get(vehicleId); + +            RegistrationValidator.validate(vehicle, registrations); +              return registrationDAO.add(vehicle.id(), registrations);          } catch (PersistenceException e) {              LOG.warn("PersistenceException caught, throwing matching ServiceException");              throw new ServiceException(e); +        } catch (ElementNotFoundException e) { +            throw new InvalidVehicleException(e);          }      }  | 
