Skip to content

Commit

Permalink
add optional parameter locationType to filter locations on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueck committed Nov 1, 2016
1 parent af6a527 commit c23ea11
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ REST Endpoint description

1. q -- Name of station you want to search
2. (optional) providerName -- Name of the provider, for example: Vagfr
3. (optional) locationType -- type of the locations, default: ANY, possible values. ANY, STATION, STREET, POI

**Example:**
/station/suggest?q=Technisches+Rathaus
Expand Down
43 changes: 41 additions & 2 deletions src/main/java/de/fewi/ptwa/controller/StationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import de.fewi.ptwa.util.ProviderUtil;
import de.schildbach.pte.NetworkProvider;
import de.schildbach.pte.VagfrProvider;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.SuggestLocationsResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -13,6 +16,9 @@
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


@Controller
Expand All @@ -21,15 +27,48 @@ public class StationController {

@RequestMapping(value = "/station/suggest", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity suggest(@RequestParam("q") final String query, @RequestParam(value = "provider", required = false) String providerName) throws IOException {
public ResponseEntity suggest(@RequestParam("q") final String query, @RequestParam(value = "provider", required = false) String providerName, @RequestParam(value = "locationType", required = false) String stationType) throws IOException {
NetworkProvider provider;
if (providerName != null) {
provider = ProviderUtil.getObjectForProvider(providerName);
} else
provider = new VagfrProvider();
if (provider == null)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Provider " + providerName + " not found or can not instantiated...");
return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(provider.suggestLocations(query));
SuggestLocationsResult suggestLocations = provider.suggestLocations(query);
if (SuggestLocationsResult.Status.OK.equals(suggestLocations.status)) {
Iterator<Location> iterator = suggestLocations.getLocations().iterator();
LocationType locationType = getLocationType(stationType);
List<Location> resultList = new ArrayList<>();
if (locationType == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("LocationType " + stationType + " not found or can not instantiated...");
} else if (!LocationType.ANY.equals(locationType)) {
while (iterator.hasNext()) {
Location loc = iterator.next();
if (locationType.equals(loc.type)) {
resultList.add(loc);
}
}
} else {
resultList.addAll(suggestLocations.getLocations());
}
return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(resultList);
} else {
return ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).body("Remote Service is down or temporarily not available");
}
}


private LocationType getLocationType(String locationType) {
if (locationType == null || "*".equals(locationType)) {
return LocationType.ANY;
} else {
try {
return LocationType.valueOf(locationType.toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
}
}

}

0 comments on commit c23ea11

Please sign in to comment.