Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/GTTStopsFetcher.java
Show All 27 Lines | |||||
import java.net.URLEncoder; | import java.net.URLEncoder; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Collections; | import java.util.Collections; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.concurrent.atomic.AtomicReference; | import java.util.concurrent.atomic.AtomicReference; | ||||
public class GTTStopsFetcher implements StopsFinderByName { | public class GTTStopsFetcher implements StopsFinderByName { | ||||
@Override @NonNull | @Override @NonNull | ||||
public List<Stop> FindByName(String name, AtomicReference<result> res) { | public List<Stop> FindByName(String name, AtomicReference<Result> res) { | ||||
URL url; | URL url; | ||||
// sorting an ArrayList should be faster than a LinkedList and the API is limited to 15 results | // sorting an ArrayList should be faster than a LinkedList and the API is limited to 15 results | ||||
List<Stop> s = new ArrayList<>(15); | List<Stop> s = new ArrayList<>(15); | ||||
List<Stop> s2 = new ArrayList<>(15); | List<Stop> s2 = new ArrayList<>(15); | ||||
String fullname; | String fullname; | ||||
String content; | String content; | ||||
String bacino; | String bacino; | ||||
String localita; | String localita; | ||||
Route.Type type; | Route.Type type; | ||||
JSONArray json; | JSONArray json; | ||||
int howManyStops, i; | int howManyStops, i; | ||||
JSONObject thisstop; | JSONObject thisstop; | ||||
if(name.length() < 3) { | if(name.length() < 3) { | ||||
res.set(result.QUERY_TOO_SHORT); | res.set(Result.QUERY_TOO_SHORT); | ||||
return s; | return s; | ||||
} | } | ||||
try { | try { | ||||
url = new URL("http://www.gtt.to.it/cms/components/com_gtt/views/palinejson/view.html.php?term=" + URLEncoder.encode(name, "utf-8")); | url = new URL("http://www.gtt.to.it/cms/components/com_gtt/views/palinejson/view.html.php?term=" + URLEncoder.encode(name, "utf-8")); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return s; | return s; | ||||
} | } | ||||
content = networkTools.queryURL(url, res); | content = networkTools.queryURL(url, res); | ||||
if(content == null) { | if(content == null) { | ||||
return s; | return s; | ||||
} | } | ||||
try { | try { | ||||
json = new JSONArray(content); | json = new JSONArray(content); | ||||
} catch(JSONException e) { | } catch(JSONException e) { | ||||
if(content.contains("[]")) { | if(content.contains("[]")) { | ||||
// when no results are found, server returns a PHP Warning and an empty array. In case they fix the warning, we're looking for the array. | // when no results are found, server returns a PHP Warning and an empty array. In case they fix the warning, we're looking for the array. | ||||
res.set(result.EMPTY_RESULT_SET); | res.set(Result.EMPTY_RESULT_SET); | ||||
} else { | } else { | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
} | } | ||||
return s; | return s; | ||||
} | } | ||||
howManyStops = json.length(); | howManyStops = json.length(); | ||||
if(howManyStops == 0) { | if(howManyStops == 0) { | ||||
res.set(result.EMPTY_RESULT_SET); | res.set(Result.EMPTY_RESULT_SET); | ||||
return s; | return s; | ||||
} | } | ||||
try { | try { | ||||
for(i = 0; i < howManyStops; i++) { | for(i = 0; i < howManyStops; i++) { | ||||
thisstop = json.getJSONObject(i); | thisstop = json.getJSONObject(i); | ||||
fullname = thisstop.getString("data"); | fullname = thisstop.getString("data"); | ||||
String ID = thisstop.getString("value"); | String ID = thisstop.getString("value"); | ||||
Show All 28 Lines | public List<Stop> FindByName(String name, AtomicReference<Result> res) { | ||||
} else { | } else { | ||||
type = FiveTNormalizer.decodeType("", bacino); | type = FiveTNormalizer.decodeType("", bacino); | ||||
} | } | ||||
//TODO: refactor using content provider | //TODO: refactor using content provider | ||||
s.add(new Stop(fullname, ID, localita, type,null)); | s.add(new Stop(fullname, ID, localita, type,null)); | ||||
} | } | ||||
} catch (JSONException e) { | } catch (JSONException e) { | ||||
res.set(result.PARSER_ERROR); | res.set(Result.PARSER_ERROR); | ||||
return s; | return s; | ||||
} | } | ||||
if(s.size() < 1) { | if(s.size() < 1) { | ||||
// shouldn't happen but prevents the next part from catching fire | // shouldn't happen but prevents the next part from catching fire | ||||
res.set(result.EMPTY_RESULT_SET); | res.set(Result.EMPTY_RESULT_SET); | ||||
return s; | return s; | ||||
} | } | ||||
Collections.sort(s); | Collections.sort(s); | ||||
// the next loop won't work with less than 2 items | // the next loop won't work with less than 2 items | ||||
if(s.size() < 2) { | if(s.size() < 2) { | ||||
res.set(result.OK); | res.set(Result.OK); | ||||
return s; | return s; | ||||
} | } | ||||
/* There are some duplicate stops returned by this API. | /* There are some duplicate stops returned by this API. | ||||
* Long distance buses have stop IDs with 5 digits. Always. They are zero-padded if there | * Long distance buses have stop IDs with 5 digits. Always. They are zero-padded if there | ||||
* aren't enough. E.g. stop 631 becomes 00631. | * aren't enough. E.g. stop 631 becomes 00631. | ||||
* | * | ||||
* Unfortunately you can't use padded stops to query any API. | * Unfortunately you can't use padded stops to query any API. | ||||
Show All 30 Lines | public List<Stop> FindByName(String name, AtomicReference<Result> res) { | ||||
} | } | ||||
} | } | ||||
// unless the last one was garbage (i would be howManyStops+1 in that case), add it | // unless the last one was garbage (i would be howManyStops+1 in that case), add it | ||||
if(i == howManyStops) { | if(i == howManyStops) { | ||||
s2.add(s.get(i-1)); | s2.add(s.get(i-1)); | ||||
} | } | ||||
res.set(result.OK); | res.set(Result.OK); | ||||
return s2; | return s2; | ||||
} | } | ||||
} | } |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register