/*******************************************************************************
* 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.PointDeVente;
public class PointDeVenteService extends RemoteService<PointDeVente> {
private static PointDeVenteService instance;
private PointDeVenteService(Context context) {
super();
}
public static synchronized PointDeVenteService getInstance(Context context) {
if (instance == null) {
instance = new PointDeVenteService(context);
}
return instance;
}
private List<PointDeVente> cache = null;
public List<PointDeVente> getPointsDeVente() {
if (cache == null) {
cache = loadList();
}
return cache != null ? new ArrayList<PointDeVente>(cache) : cache;
}
public List<PointDeVente> getPointsDeVente(double latitudeMin, double latitudeMax, double longitudeMax, double longitudeMin) {
List<PointDeVente> pointsDeVente = getPointsDeVente();
if( pointsDeVente == null ) {
return null;
}
List<PointDeVente> result = new ArrayList<PointDeVente>();
for (PointDeVente point : pointsDeVente) {
if (point.latitude >= latitudeMin && point.latitude <= latitudeMax && point.longitude >= longitudeMin && point.longitude <= longitudeMax) {
result.add(point);
}
}
return result;
}
public List<PointDeVente> getProximityPointsDeVente(double latitude, double longitude) {
return loadProximityList(latitude, longitude, 10);
}
@Override
protected PointDeVente populate(JSONObject jsonObject) throws JSONException {
PointDeVente pointDeVente = new PointDeVente();
pointDeVente.nom = jsonObject.getString("nom");
pointDeVente.type = jsonObject.getString("type");
pointDeVente.address = jsonObject.optString("adressenumero") + " " + jsonObject.getString("adressevoie") + " " + jsonObject.getString("nomcommune");
//pointDeVente.telephone = jsonObject.getString("phone");
pointDeVente.horaires = "Lundi: " + jsonObject.optString("horaireslundi") + "\n"
+ "Mardi: " + jsonObject.optString("horairesmardi") + "\n"
+ "Mercredi: " + jsonObject.optString("horairesmercredi") + "\n"
+ "Jeudi: " + jsonObject.optString("horairesjeudi") + "\n"
+ "Vendredi: " + jsonObject.optString("horairesvendredi") + "\n"
+ "Samedi: " + jsonObject.optString("horairessamedi") + "\n"
+ "Dimanche: " + jsonObject.optString("horairesdimanche");
pointDeVente.latitude = jsonObject.getJSONArray("coordonnees").getDouble(0);
pointDeVente.longitude = jsonObject.getJSONArray("coordonnees").getDouble(1);
return pointDeVente;
}
@Override
protected String getDataSet() {
return "mkt-titres-pointsvente-partenaires-td";
}
}