/*******************************************************************************
* 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 android.content.Context;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import fr.gotorennes.domain.BikeStation;
import fr.gotorennes.domain.MetroStation;
public class BikeStationTopoService extends RemoteService<BikeStation> {
private static BikeStationTopoService instance;
private BikeStationTopoService(Context context) {
super();
}
public static synchronized BikeStationTopoService getInstance(Context context) {
if (instance == null) {
instance = new BikeStationTopoService(context);
}
return instance;
}
private Map<Integer, BikeStation> cache = null;
public BikeStation getBikeStation(int id) {
if (cache == null || cache.isEmpty()) {
List<BikeStation> stations = loadList();
cache = new HashMap<Integer, BikeStation>();
if (stations != null && !stations.isEmpty()) {
for (BikeStation station : stations) {
cache.put(station.id, station);
}
}
}
return cache.get(id);
}
@Override
protected BikeStation populate(JSONObject jsonObject) throws JSONException {
BikeStation bikeStation = new BikeStation();
bikeStation.id = jsonObject.getInt("id");
bikeStation.name = jsonObject.getString("nom");
bikeStation.address = jsonObject.optString("adressenumero") + " " + jsonObject.getString("adressevoie");
bikeStation.latitude = jsonObject.getJSONArray("coordonnees").getDouble(0);
bikeStation.longitude = jsonObject.getJSONArray("coordonnees").getDouble(1);
bikeStation.carteCredit = Boolean.parseBoolean(jsonObject.getString("possedetpe"));
return bikeStation;
}
@Override
protected String getDataSet() {
return "vls-stations-topologie-td";
}
}