/******************************************************************************* * 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.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import fr.gotorennes.domain.NextDeparture; public class NextDepartureService extends RemoteService<NextDeparture> { private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"); private static NextDepartureService instance; private Date remoteDate; private NextDepartureService(Context context) { super(); } public static synchronized NextDepartureService getInstance(Context context) { if (instance == null) { instance = new NextDepartureService(context); } return instance; } public NextDeparture getNextDeparture(String stopId, String routeId, int direction) { return loadObject("refine.idarret=" + stopId + "&refine.idligne=" + routeId + "&refine.sens=" + direction); } @Override protected NextDeparture populate(JSONObject jsonObject) throws JSONException { NextDeparture nextDeparture = new NextDeparture(); nextDeparture.stopId = jsonObject.getString("idarret"); nextDeparture.routeId = jsonObject.getString("idcourse"); nextDeparture.direction = Integer.parseInt(jsonObject.getString("sens")); try { nextDeparture.departure = dateFormat.parse(jsonObject.getString("depart")); } catch (ParseException ex) {} nextDeparture.remoteDate = remoteDate; return nextDeparture; } protected void onDataLoad(JSONObject data) throws JSONException { try { remoteDate = dateFormat.parse(data.getString("record_timestamp")); } catch (ParseException ex) {} } @Override protected String getDataSet() { return "tco-bus-circulation-passages-tr"; } }