diff options
Diffstat (limited to 'src/main/java/at/ac/tuwien/sepm/assignment')
| -rw-r--r-- | src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java | 43 | 
1 files changed, 42 insertions, 1 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java index 5c00447..13f2c0f 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/RegistrationDatabaseDAO.java @@ -1,5 +1,6 @@  package at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dao; +import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Employee;  import at.ac.tuwien.sepm.assignment.groupphase.einsatzverwaltung.dto.Registration;  import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;  import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException; @@ -10,7 +11,9 @@ import java.sql.ResultSet;  import java.sql.SQLException;  import java.sql.Statement;  import java.sql.Timestamp; +import java.util.ArrayList;  import java.util.HashSet; +import java.util.List;  import java.util.Set;  import org.slf4j.Logger;  import org.slf4j.LoggerFactory; @@ -26,20 +29,27 @@ public class RegistrationDatabaseDAO implements RegistrationDAO {              "INSERT INTO Registration (vehicleId, employeeId, start, end, active) VALUES (?,?,?,?,?);";      private static final String UPDATE_VEHICLE =              "UPDATE Vehicle SET status = 'FREI_WACHE' WHERE id = ?;"; +    private static final String FETCH_REGISTRATIONS = +            "SELECT * FROM Registration WHERE vehicleId = ?";      private PreparedStatement addRegistration;      private PreparedStatement updateVehicle; +    private PreparedStatement fetchRegistrations;      private Connection connection; +    private EmployeeDAO employeePersistence;      @Autowired -    public RegistrationDatabaseDAO(JDBCConnectionManager connectionManager) +    public RegistrationDatabaseDAO( +            JDBCConnectionManager connectionManager, EmployeeDAO employeePersistence)              throws PersistenceException { +        this.employeePersistence = employeePersistence;          try {              connection = connectionManager.getConnection();              addRegistration =                      connection.prepareStatement(ADD_REGISTRATION, Statement.RETURN_GENERATED_KEYS);              updateVehicle = connection.prepareStatement(UPDATE_VEHICLE); +            fetchRegistrations = connection.prepareStatement(FETCH_REGISTRATIONS);          } catch (SQLException e) {              LOG.error("Could not get connection or preparation of statement failed");              throw new PersistenceException(e); @@ -103,4 +113,35 @@ public class RegistrationDatabaseDAO implements RegistrationDAO {      public void remove(long id) throws ElementNotFoundException, PersistenceException {          throw new UnsupportedOperationException();      } + +    public List<Registration> list(long vehicleId) throws PersistenceException { +        List<Registration> registrationList = new ArrayList<>(); +        try { +            fetchRegistrations.setLong(1, vehicleId); +            ResultSet rs = fetchRegistrations.executeQuery(); +            while (rs.next()) { +                long employeeId = rs.getLong("employeeId"); +                // TODO: replace the following with employeePersistence.get once implemented +                Employee emp = +                        employeePersistence +                                .list() +                                .stream() +                                .filter(employee -> employee.id() == employeeId) +                                .findAny() +                                .orElse(null); +                Registration registration = +                        Registration.builder() +                                .id(rs.getLong("id")) +                                .start(rs.getTimestamp("start").toInstant()) +                                .end(rs.getTimestamp("end").toInstant()) +                                .employee(emp) +                                .build(); +                registrationList.add(registration); +            } +        } catch (SQLException e) { +            throw new PersistenceException(e); +        } + +        return registrationList; +    }  }  | 
