/******************************************************************************* * 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.Equipement; import fr.gotorennes.domain.MetroStation; public class EquipementService extends RemoteService<Equipement> { private static EquipementService instance; private EquipementService(Context context) { super(); } public static synchronized EquipementService getInstance(Context context) { if (instance == null) { instance = new EquipementService(context); } return instance; } private List<Equipement> cache = null; public List<Equipement> getEquipements() { if (cache == null) { cache = loadList(); } return cache != null ? new ArrayList<Equipement>(cache) : cache; } public List<Equipement> getEquipements(MetroStation metroStation) { List<Equipement> equipements = getEquipements(); if (equipements == null) { return null; } List<Equipement> result = new ArrayList<Equipement>(); for (Equipement equipement : equipements) { if (equipement.station.equals(metroStation.id)) { result.add(equipement); } } return result; } @Override protected Equipement populate(JSONObject jsonObject) throws JSONException { Equipement equipement = new Equipement(); equipement.id = jsonObject.getString("id"); equipement.station = jsonObject.getString("idstation"); equipement.name = jsonObject.getString("nom"); equipement.type = Equipement.Type.fromString(jsonObject.getString("type")); equipement.fromFloor = jsonObject.optInt("niveau", 0); equipement.toFloor = jsonObject.optInt("niveaudestination", 0); return equipement; } @Override protected String getDataSet() { return "tco-metro-topologie-equipements-td"; } }