/*******************************************************************************
* Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package fr.gotorennes.remote;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import fr.gotorennes.domain.MetroStation;
public class MetroStationService extends RemoteService<MetroStation> {
private static MetroStationService instance;
private MetroStationTopoService metroTopoService;
private MetroStationOrderService metroOrderService;
private MetroStationService(Context context) {
super();
metroTopoService = MetroStationTopoService.getInstance(context);
metroOrderService = MetroStationOrderService.getInstance(context);
}
public static synchronized MetroStationService getInstance(Context context) {
if (instance == null) {
instance = new MetroStationService(context);
}
return instance;
}
private List<MetroStation> cache = null;
public List<MetroStation> getMetroStations() {
if (cache == null) {
cache = loadList();
}
return cache != null ? new ArrayList<MetroStation>(cache) : cache;
}
public List<MetroStation> getMetroStations(double latitudeMin, double latitudeMax, double longitudeMax, double longitudeMin) {
List<MetroStation> metroStations = getMetroStations();
if (metroStations == null) {
return null;
}
List<MetroStation> result = new ArrayList<MetroStation>();
for (MetroStation metro : metroStations) {
if (metro.latitude >= latitudeMin && metro.latitude <= latitudeMax && metro.longitude >= longitudeMin && metro.longitude <= longitudeMax) {
result.add(metro);
}
}
return result;
}
public List<MetroStation> getProximityMetroStations(double latitude, double longitude) {
return loadProximityList(latitude, longitude, 3);
}
public MetroStation getMetroStation(String id) {
List<MetroStation> metroStations = getMetroStations();
if (metroStations == null) {
return null;
}
for (MetroStation metroStation : metroStations) {
if (metroStation.id.equals(id)) {
return metroStation;
}
}
return null;
}
@Override
protected MetroStation populate(JSONObject jsonObject) throws JSONException {
MetroStation metroStation = metroTopoService.getMetroStation(jsonObject.getString("idstation"));
if (metroStation == null) {
metroStation = new MetroStation();
metroStation.id = jsonObject.getString("idstation");
metroStation.name = jsonObject.getString("nom");
metroStation.latitude = jsonObject.getJSONArray("coordonnees").getDouble(0);
metroStation.longitude = jsonObject.getJSONArray("coordonnees").getDouble(1);
}
metroStation.sequence = metroOrderService.getMetroStationOrder(metroStation.name);
metroStation.status = jsonObject.getString("etat");
return metroStation;
}
@Override
protected String getDataSet() {
return "tco-metro-stations-etat-tr";
}
}