Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/main/java/uk/ac/cam/cl/dtg/isaac/dos/ILocationHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import uk.ac.cam.cl.dtg.segue.dao.SegueDatabaseException;
import uk.ac.cam.cl.dtg.util.locations.Location;
import uk.ac.cam.cl.dtg.util.locations.PostCode;

import java.util.List;

/**
*
Expand Down Expand Up @@ -67,21 +64,4 @@ LocationHistoryEvent storeLocationEvent(final String ipAddress, final Location l
* - if there is a db error.
*/
void updateLocationEventDate(final Long id, boolean isCurrent) throws SegueDatabaseException;

/**
* @param postCode
* - a given postcode
* @return - a postcode object
* @throws SegueDatabaseException
* - if something goes wrong with the database.
*/
PostCode getPostCode(final String postCode) throws SegueDatabaseException;

/**
* @param postCodes
* - a list of given postcodes
* @throws SegueDatabaseException
* - if something goes wrong with the database.
*/
void storePostCodes(List<PostCode> postCodes) throws SegueDatabaseException;
}
65 changes: 0 additions & 65 deletions src/main/java/uk/ac/cam/cl/dtg/isaac/dos/PgLocationHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import uk.ac.cam.cl.dtg.segue.dao.SegueDatabaseException;
import uk.ac.cam.cl.dtg.segue.database.PostgresSqlDb;
import uk.ac.cam.cl.dtg.util.locations.Location;
import uk.ac.cam.cl.dtg.util.locations.PostCode;

import java.io.IOException;
import java.sql.Connection;
Expand All @@ -35,7 +34,6 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
Expand Down Expand Up @@ -91,33 +89,6 @@ public LocationHistoryEvent getLatestByIPAddress(final String ipAddress) throws
}
}

@Override
public PostCode getPostCode(final String postCode) throws SegueDatabaseException {
if (null == postCode || postCode.isEmpty()) {
return null;
}

String query = "SELECT postcode, lat, lon FROM uk_post_codes WHERE postcode = ?";
try (Connection conn = database.getDatabaseConnection();
PreparedStatement pst = conn.prepareStatement(query);
) {
pst.setString(1, postCode);

try (ResultSet results = pst.executeQuery()) {

while (results.next()) {
return new PostCode(results.getString("postcode"), results.getDouble("lat"),
results.getDouble("lon"));
}

// we must not have found anything.
return null;
}
} catch (SQLException e) {
throw new SegueDatabaseException("Postgres exception", e);
}
}

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -227,40 +198,4 @@ private PgLocationEvent buildPgLocationEntry(final ResultSet results) throws SQL
return new PgLocationEvent(results.getLong("id"), results.getString("ip_address"), location,
results.getTimestamp("created"), results.getTimestamp("last_lookup"));
}

/*
* (non-Javadoc)
*
* @see uk.ac.cam.cl.dtg.isaac.dos.LocationHistory#storePostCodes(java.util.List)
*/
@Override
public void storePostCodes(List<PostCode> foundPostCodes) throws SegueDatabaseException {
String query = "INSERT INTO uk_post_codes(postcode, lat, lon) VALUES (?, ?, ?)";
try (Connection conn = database.getDatabaseConnection()) {
conn.setAutoCommit(false);

for (PostCode postCode : foundPostCodes) {

// Ignore post codes with invalid lat/lon
if (postCode.lat() == null || postCode.lon() == null) {
continue;
}

try (PreparedStatement pst = conn.prepareStatement(query)) {
pst.setString(1, postCode.postCode());
pst.setDouble(2, postCode.lat());
pst.setDouble(3, postCode.lon());

if (pst.executeUpdate() == 0) {
throw new SegueDatabaseException("Unable to save location event.");
}
}
}
conn.commit();
conn.setAutoCommit(true);

} catch (SQLException e) {
throw new SegueDatabaseException("Postgres exception", e);
}
}
}
76 changes: 1 addition & 75 deletions src/main/java/uk/ac/cam/cl/dtg/segue/api/AdminFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package uk.ac.cam.cl.dtg.segue.api;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.api.client.util.Maps;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
Expand All @@ -36,7 +34,6 @@
import uk.ac.cam.cl.dtg.isaac.dos.content.Content;
import uk.ac.cam.cl.dtg.isaac.dos.users.EmailVerificationStatus;
import uk.ac.cam.cl.dtg.isaac.dos.users.Role;
import uk.ac.cam.cl.dtg.isaac.dos.users.School;
import uk.ac.cam.cl.dtg.isaac.dto.SegueErrorResponse;
import uk.ac.cam.cl.dtg.isaac.dto.users.RegisteredUserDTO;
import uk.ac.cam.cl.dtg.isaac.dto.users.UserIdMergeDTO;
Expand All @@ -59,14 +56,10 @@
import uk.ac.cam.cl.dtg.segue.dao.content.ContentManagerException;
import uk.ac.cam.cl.dtg.segue.dao.content.GitContentManager;
import uk.ac.cam.cl.dtg.segue.dao.schools.SchoolListReader;
import uk.ac.cam.cl.dtg.segue.dao.schools.UnableToIndexSchoolsException;
import uk.ac.cam.cl.dtg.segue.etl.GithubPushEventPayload;
import uk.ac.cam.cl.dtg.segue.scheduler.SegueJobService;
import uk.ac.cam.cl.dtg.segue.search.SegueSearchException;
import uk.ac.cam.cl.dtg.util.AbstractConfigLoader;
import uk.ac.cam.cl.dtg.util.RequestIPExtractor;
import uk.ac.cam.cl.dtg.util.locations.LocationServerException;
import uk.ac.cam.cl.dtg.util.locations.PostCodeRadius;

import jakarta.annotation.Nullable;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -93,14 +86,11 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

Expand Down Expand Up @@ -804,8 +794,6 @@ public Response getContentProblems(@Context final HttpServletRequest request,
* - if searching by role
* @param schoolOther
* - if searching by school other field.
* @param postcode
* - if searching by postcode.
* @param schoolURN
* - if searching by school by the URN.
* @param emailVerificationStatus
Expand All @@ -820,8 +808,6 @@ public Response findUsers(@Context final HttpServletRequest httpServletRequest,
@QueryParam("id") final Long userId, @QueryParam("email") @Nullable final String email,
@QueryParam("familyName") @Nullable final String familyName, @QueryParam("role") @Nullable final Role role,
@QueryParam("schoolOther") @Nullable final String schoolOther,
@QueryParam("postcode") @Nullable final String postcode,
@QueryParam("postcodeRadius") @Nullable final String postcodeRadius,
@QueryParam("schoolURN") @Nullable final String schoolURN,
@QueryParam("emailVerificationStatus") @Nullable final EmailVerificationStatus emailVerificationStatus) {

Expand All @@ -839,8 +825,7 @@ public Response findUsers(@Context final HttpServletRequest httpServletRequest,
&& (null == familyName || familyName.isEmpty())
&& (null == schoolOther || schoolOther.isEmpty())
&& (null == email || email.isEmpty())
&& (null == schoolURN || schoolURN.isEmpty())
&& (null == postcode || postcode.isEmpty())) {
&& (null == schoolURN || schoolURN.isEmpty())) {
return new SegueErrorResponse(Status.FORBIDDEN, "You do not have permission to do wildcard searches.")
.toResponse();

Expand Down Expand Up @@ -905,65 +890,6 @@ public Response findUsers(@Context final HttpServletRequest httpServletRequest,
} else {
foundUsers = this.userManager.findUsers(userPrototype);
}
Map<Long, RegisteredUserDTO> userMapById = foundUsers.parallelStream().collect(Collectors.toMap(RegisteredUserDTO::getId, Function.identity()));

// if postcode is set, filter found users
if (null != postcode) {
try {
Map<String, List<Long>> postCodeAndUserIds = Maps.newHashMap();
for (RegisteredUserDTO userDTO : foundUsers) {
if (userDTO.getSchoolId() != null) {
School school = this.schoolReader.findSchoolById(userDTO.getSchoolId());
if (school != null) {
String schoolPostCode = school.getPostcode();
if (null == schoolPostCode || schoolPostCode.isEmpty()) {
continue;
}
List<Long> ids;
if (postCodeAndUserIds.containsKey(schoolPostCode)) {
ids = postCodeAndUserIds.get(schoolPostCode);
} else {
ids = Lists.newArrayList();
}
ids.add(userDTO.getId());
postCodeAndUserIds.put(schoolPostCode, ids);
}
}
}

PostCodeRadius radius = PostCodeRadius.valueOf(postcodeRadius);

List<Long> userIdsWithinRadius = locationManager.getUsersWithinPostCodeDistanceOf(
postCodeAndUserIds, postcode, radius);

// Make sure the list returned is users who have schools in our postcode radius
List<RegisteredUserDTO> nearbyUsers = new ArrayList<>();
for (Long id : userIdsWithinRadius) {
RegisteredUserDTO user = userMapById.get(id); //this.userManager.getUserDTOById(id);
if (user != null) {
nearbyUsers.add(user);
}
}
foundUsers = nearbyUsers;

} catch (LocationServerException e) {
log.error("Location service unavailable. ", e);
return new SegueErrorResponse(Status.SERVICE_UNAVAILABLE,
"Unable to process request using 3rd party location provider").toResponse();
} catch (UnableToIndexSchoolsException | SegueSearchException e) {
log.error("Unable to get school statistics", e);
return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR,
"Unable to process schools information").toResponse();
} catch (JsonParseException | JsonMappingException e) {
log.error("Problem parsing school", e);
return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR, "Unable to read school")
.toResponse();
} catch (IOException e) {
log.error("Problem parsing school", e);
return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR,
"IOException while trying to communicate with the school service.").toResponse();
}
}

// Calculate the ETag
EntityTag etag = new EntityTag(foundUsers.size() + foundUsers.toString().hashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@
import uk.ac.cam.cl.dtg.util.email.MailJetApiClientWrapper;
import uk.ac.cam.cl.dtg.util.locations.IPLocationResolver;
import uk.ac.cam.cl.dtg.util.locations.MaxMindIPLocationResolver;
import uk.ac.cam.cl.dtg.util.locations.PostCodeIOLocationResolver;
import uk.ac.cam.cl.dtg.util.locations.PostCodeLocationResolver;
import uk.ac.cam.cl.dtg.util.mappers.AssignmentMapper;
import uk.ac.cam.cl.dtg.util.mappers.ContentMapper;
import uk.ac.cam.cl.dtg.util.mappers.EventBookingMapper;
Expand Down Expand Up @@ -422,8 +420,6 @@ private void configureAuthenticationProviders() {
private void configureApplicationManagers() {
bind(ILocationHistory.class).to(PgLocationHistory.class);

bind(PostCodeLocationResolver.class).to(PostCodeIOLocationResolver.class);

bind(IUserDataManager.class).to(PgUsers.class);

bind(IAnonymousUserDataManager.class).to(PgAnonymousUsers.class);
Expand Down
32 changes: 1 addition & 31 deletions src/main/java/uk/ac/cam/cl/dtg/segue/dao/LocationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@
import uk.ac.cam.cl.dtg.util.locations.IPLocationResolver;
import uk.ac.cam.cl.dtg.util.locations.Location;
import uk.ac.cam.cl.dtg.util.locations.LocationServerException;
import uk.ac.cam.cl.dtg.util.locations.PostCodeLocationResolver;
import uk.ac.cam.cl.dtg.util.locations.PostCodeRadius;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -46,23 +42,18 @@ public class LocationManager {

private final ILocationHistory dao;
private final IPLocationResolver ipLocationResolver;
private final PostCodeLocationResolver postCodeLocationResolver;
private final Cache<String, Boolean> locationUpdatedRecentlyCache;

/**
* @param dao
* - the location history data access object.
* @param ipLocationResolver
* - the external ip location resolver.
* @param postCodeLocationResolver
* - the external postCode location resolver.
*/
@Inject
public LocationManager(final ILocationHistory dao, final IPLocationResolver ipLocationResolver,
final PostCodeLocationResolver postCodeLocationResolver) {
public LocationManager(final ILocationHistory dao, final IPLocationResolver ipLocationResolver) {
this.dao = dao;
this.ipLocationResolver = ipLocationResolver;
this.postCodeLocationResolver = postCodeLocationResolver;

// This cache is here to prevent lots of needless look-ups to the database.
locationUpdatedRecentlyCache = CacheBuilder.newBuilder().expireAfterWrite(NON_PERSISTENT_CACHE_TIME_IN_HOURS, TimeUnit.HOURS).build();
Expand Down Expand Up @@ -135,25 +126,4 @@ public void refreshLocation(final String ipAddress) throws SegueDatabaseExceptio
this.locationUpdatedRecentlyCache.put(ipAddress, false);
}
}

/**
* @param postCodeAndUserIds
* - A map of postcodes to userids
* @param targetPostCode
* - The post code we want to find users near to
* @param radius
* - radius to search
* @return - a list of userids who have schools in that radius
* @throws LocationServerException
* - anm exception when the location service fails
* @throws SegueDatabaseException
* - anm exception when the database service fails
*/
public List<Long> getUsersWithinPostCodeDistanceOf(final Map<String, List<Long>> postCodeAndUserIds,
final String targetPostCode, final PostCodeRadius radius) throws LocationServerException,
SegueDatabaseException {
return postCodeLocationResolver.filterPostcodesWithinProximityOfPostcode(postCodeAndUserIds,
targetPostCode, radius);
}

}
47 changes: 0 additions & 47 deletions src/main/java/uk/ac/cam/cl/dtg/util/locations/PostCode.java

This file was deleted.

Loading
Loading