Skip to content
Snippets Groups Projects
LocationRepositoryJDBC.java 4.09 KiB
Newer Older
//Implements the locations repository using JDBC
package Team5.SmartTowns.data;
Rhys Nute's avatar
Rhys Nute committed

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
Rhys Nute's avatar
Rhys Nute committed
import java.util.List;

@Repository
Gabriel Copat's avatar
Gabriel Copat committed
public class LocationRepositoryJDBC implements LocationRepository {
   private JdbcTemplate jdbc;
    private RowMapper<Location> locationMapper;
Rhys Nute's avatar
Rhys Nute committed

Gabriel Copat's avatar
Gabriel Copat committed
    public LocationRepositoryJDBC(JdbcTemplate aJdbc) {
Rhys Nute's avatar
Rhys Nute committed
        this.jdbc = aJdbc;
        setlocationMapper();
    }
//    public LocationRepositoryJDBC() {
//        JdbcTemplate ajdbc = new JdbcTemplate();
//        this.jdbc =ajdbc;
//        setlocationMapper();
//
//    }
Rhys Nute's avatar
Rhys Nute committed
    private void setlocationMapper(){
                rs.getString("locationName"),
                rs.getString("locationEmail"),
                rs.getString("locationDescription"),
                rs.getString("locationPlace"),
                rs.getBoolean("locationApproved")
Rhys Nute's avatar
Rhys Nute committed
        );
    }
    public List<Location> getAllLocation(){
        String sql= "SELECT * FROM locations";
Rhys Nute's avatar
Rhys Nute committed
        return jdbc.query(sql, locationMapper);
    }
//    public LocationRepositoryJDBC() {
//        JdbcTemplate ajdbc = new JdbcTemplate();
//        this.jdbc =ajdbc;
//        setlocationMapper();
//
//    }
//    public LocationRepositoryJDBC(JdbcTemplate jdbc, RowMapper<Location> locationMapper) {
//        this.jdbc = jdbc;
//        this.locationMapper = locationMapper;
//    }
Rhys Evans's avatar
Rhys Evans committed

    public List<Location> getAllApprovedLocation(){
        String sql= "SELECT * FROM locations";
        List<Location> lis = jdbc.query(sql, locationMapper);
        List<Location> lisFiltered = new ArrayList<>();
        for (Location li : lis){
            if (li.isLocationApproved()){
                lisFiltered.add(li);
            }
        }
        return lisFiltered;
    }


    public List<Location> getAllUnapprovedLocation(){
        String sql= "SELECT * FROM locations";
        List<Location> lis = jdbc.query(sql, locationMapper);
        List<Location> lisFiltered = new ArrayList<>();
        for (Location li : lis){
            if (!li.isLocationApproved()){
                lisFiltered.add(li);
            }
        }
        return lisFiltered;
    }

    @Override // intended implementation at current: user data from templates/Landmarks/LandmarkFormTh.html is added to the Location table
    public void addLocation(Location loc) {
        String sql = "insert into locations( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) values (?,?,?,?,?,?)";
        jdbc.update(sql,loc.getLocationName(),loc.getLocationEmail(),loc.getLocationDescription(),loc.getLocationPlace(),loc.getLocationTrailID(), false);
    public List<Location> getApprovedLocations(){
        JdbcTemplate jdbc = new JdbcTemplate();
        List<Location> locations = new LocationRepositoryJDBC(jdbc).getAllLocation();
        List<Location> locationApprovalList= new ArrayList<Location>();
        for (Location loc :locations){
            if (loc.isLocationApproved()) {
                locationApprovalList.add(loc);
            }
        } return locationApprovalList;
//
//    @Override
//    public List<Location> getApprovedLocations2(List<Location> list){
//
//        List<Location> locationApprovalList= new ArrayList<Location>();
//        for (Location loc :list){
//            if (loc.isLocationApproved()) {
//                locationApprovalList.add(loc);
//            }
//        } return locationApprovalList;
//    }
//
    public List<Location> getUnapprovedLocations(){
        List<Location> locations = getAllLocation();
        List<Location> locationUnapprovedList= new ArrayList<Location>();
        for (Location loc :locations){
            if (!loc.isLocationApproved()) {
                locationUnapprovedList.add(loc);
            }
        } return locationUnapprovedList;
    }


//    public JdbcTemplate getJdbc() {
//        return jdbc;
//    }

Rhys Nute's avatar
Rhys Nute committed
}