/*******************************************************************************
* 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.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import fr.gotorennes.domain.Equipement;
import fr.gotorennes.domain.EquipementStatus;
public class EquipementStatusService extends RemoteService<EquipementStatus> {
private static EquipementStatusService instance;
private EquipementStatusService(Context context) {
super();
}
public static synchronized EquipementStatusService getInstance(Context context) {
if (instance == null) {
instance = new EquipementStatusService(context);
}
return instance;
}
private long lastUpdate = 0;
private Map<String, Boolean> cache = null;
public Map<String, Boolean> getEquipementStatus() {
if (cache == null || System.currentTimeMillis() - lastUpdate > 300000) {
List<EquipementStatus> result = loadList();
if (result != null && !result.isEmpty()) {
cache = new HashMap<String, Boolean>();
for(EquipementStatus status : result) {
cache.put(status.id, status.status);
}
lastUpdate = System.currentTimeMillis();
}
}
return cache;
}
public boolean getEquipementStatus(Equipement equipement) {
Map<String, Boolean> status = getEquipementStatus();
if (status == null) {
return true;
}
return status.get(equipement.id);
}
@Override
protected EquipementStatus populate(JSONObject jsonObject) throws JSONException {
EquipementStatus equipementStatus = new EquipementStatus();
equipementStatus.id = jsonObject.getString("idequipement");
equipementStatus.status = "En fonctionnement".equals(jsonObject.getString("etat"));
return equipementStatus;
}
@Override
protected String getDataSet() {
return "tco-metro-equipements-etat-tr";
}
}