mirror of
https://github.com/nicobo/dataprovence.git
synced 2026-04-10 16:06:23 +02:00
+ DataprovenceManager -> execute les requetes en tache de fond
This commit is contained in:
parent
2d4ec896fc
commit
c377cfe4dd
BIN
bin/classes/bma/groomservice/data/PoiListener.class
Normal file
BIN
bin/classes/bma/groomservice/data/PoiListener.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7
src/bma/groomservice/data/PoiListener.java
Normal file
7
src/bma/groomservice/data/PoiListener.java
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package bma.groomservice.data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface PoiListener {
|
||||||
|
void onPoiReceived(List<Poi> pois);
|
||||||
|
}
|
||||||
|
|
@ -5,15 +5,9 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
@ -28,34 +22,6 @@ public class DataprovenceHelper {
|
||||||
|
|
||||||
Logger logger = LoggerFactory.getLogger(DataprovenceHelper.class);
|
Logger logger = LoggerFactory.getLogger(DataprovenceHelper.class);
|
||||||
|
|
||||||
//
|
|
||||||
// Constantes des "thèmes"
|
|
||||||
//
|
|
||||||
|
|
||||||
public static final String THEME_PLEINAIR = "PLEIN AIR";
|
|
||||||
public static final String THEME_RESTAURATION = "RESTAURATION";
|
|
||||||
public static final String THEME_SPORT = "SPORT";
|
|
||||||
public static final String THEME_CULTURE = "CULTURE";
|
|
||||||
|
|
||||||
public static final Map<String, String[]> DATASETS = new HashMap<String, String[]>();
|
|
||||||
static {
|
|
||||||
DATASETS.put(THEME_PLEINAIR, new String[] { "ServicesDeGuides",
|
|
||||||
"LocationDeVelo", "CentresEquestres",
|
|
||||||
"AccompagnateurDeMoyenneMontagneEtMoniteursDEscalade",
|
|
||||||
"ParcsAThemesEtAnimaliers", "GolfsEtMinigolfs",
|
|
||||||
"ParcsAcrobatiquesForestiers", "ParcsEtJardins",
|
|
||||||
"SitesNaturelsIncontournables", "Plages" });
|
|
||||||
DATASETS.put(THEME_RESTAURATION, new String[] {
|
|
||||||
"RestaurantsGastronomiques", "Restaurants" });
|
|
||||||
DATASETS.put(THEME_SPORT, new String[] { "SportNautique",
|
|
||||||
"LocationDeVelo", "CentresEquestres",
|
|
||||||
"AccompagnateurDeMoyenneMontagneEtMoniteursDEscalade",
|
|
||||||
"GolfsEtMinigolfs", "ParcsAcrobatiquesForestiers" });
|
|
||||||
DATASETS.put(THEME_CULTURE, new String[] { "ServicesDeGuides",
|
|
||||||
"SitesNaturelsIncontournables", "Musees",
|
|
||||||
"MonumentsEtStesCulturels" });
|
|
||||||
}
|
|
||||||
|
|
||||||
private final String rootUrl;
|
private final String rootUrl;
|
||||||
private final String datasetName;
|
private final String datasetName;
|
||||||
private final Collection<Filter> filters;
|
private final Collection<Filter> filters;
|
||||||
|
|
@ -182,34 +148,4 @@ public class DataprovenceHelper {
|
||||||
return Arrays.asList(gl.d);
|
return Arrays.asList(gl.d);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static Collection<DataprovenceHelper> findHelpers(
|
|
||||||
Collection<String> tags) {
|
|
||||||
ArrayList<DataprovenceHelper> helpers = new ArrayList<DataprovenceHelper>();
|
|
||||||
for (String tag : tags) {
|
|
||||||
String[] datasets = DATASETS.get(tag);
|
|
||||||
for (int d = 0; d < datasets.length; d++) {
|
|
||||||
helpers.add(new DataprovenceHelper(datasets[d]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return helpers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<Poi> findAll(Collection<String> tags) throws IOException {
|
|
||||||
|
|
||||||
Set<DataprovenceHelper> helpers = new HashSet<DataprovenceHelper>();
|
|
||||||
helpers.addAll(findHelpers(tags));
|
|
||||||
|
|
||||||
TreeSet<Poi> pois = new TreeSet<Poi>();
|
|
||||||
for (DataprovenceHelper helper : findHelpers(tags)) {
|
|
||||||
pois.addAll(helper.find(null));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ArrayList<Poi>(pois);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<Poi> findAll(String[] tags) throws IOException {
|
|
||||||
return findAll(Arrays.asList(tags));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
140
src/bma/groomservice/data/dataprovence/DataprovenceManager.java
Normal file
140
src/bma/groomservice/data/dataprovence/DataprovenceManager.java
Normal file
|
|
@ -0,0 +1,140 @@
|
||||||
|
package bma.groomservice.data.dataprovence;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import bma.groomservice.data.Poi;
|
||||||
|
import bma.groomservice.data.PoiListener;
|
||||||
|
|
||||||
|
public class DataprovenceManager implements PoiListener {
|
||||||
|
|
||||||
|
Logger logger = LoggerFactory.getLogger(DataprovenceManager.class);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Constantes des "thèmes"
|
||||||
|
//
|
||||||
|
|
||||||
|
public static final String THEME_PLEINAIR = "PLEIN AIR";
|
||||||
|
public static final String THEME_RESTAURATION = "RESTAURATION";
|
||||||
|
public static final String THEME_SPORT = "SPORT";
|
||||||
|
public static final String THEME_CULTURE = "CULTURE";
|
||||||
|
|
||||||
|
//
|
||||||
|
// Associations themes <-> datasets
|
||||||
|
//
|
||||||
|
|
||||||
|
public static final Map<String, String[]> DATASETS = new HashMap<String, String[]>();
|
||||||
|
static {
|
||||||
|
DATASETS.put(THEME_PLEINAIR, new String[] { "ServicesDeGuides",
|
||||||
|
"LocationDeVelo", "CentresEquestres",
|
||||||
|
"AccompagnateurDeMoyenneMontagneEtMoniteursDEscalade",
|
||||||
|
"ParcsAThemesEtAnimaliers", "GolfsEtMinigolfs",
|
||||||
|
"ParcsAcrobatiquesForestiers", "ParcsEtJardins",
|
||||||
|
"SitesNaturelsIncontournables", "Plages" });
|
||||||
|
DATASETS.put(THEME_RESTAURATION, new String[] {
|
||||||
|
"RestaurantsGastronomiques", "Restaurants" });
|
||||||
|
DATASETS.put(THEME_SPORT, new String[] { "SportNautique",
|
||||||
|
"LocationDeVelo", "CentresEquestres",
|
||||||
|
"AccompagnateurDeMoyenneMontagneEtMoniteursDEscalade",
|
||||||
|
"GolfsEtMinigolfs", "ParcsAcrobatiquesForestiers" });
|
||||||
|
DATASETS.put(THEME_CULTURE, new String[] { "ServicesDeGuides",
|
||||||
|
"SitesNaturelsIncontournables", "Musees",
|
||||||
|
"MonumentsEtStesCulturels" });
|
||||||
|
}
|
||||||
|
|
||||||
|
private final TreeSet<Poi> pois = new TreeSet<Poi>();
|
||||||
|
|
||||||
|
private final PoiListener listener;
|
||||||
|
|
||||||
|
private int count = 0;
|
||||||
|
|
||||||
|
private class LoadDataTask extends
|
||||||
|
AsyncTask<DataprovenceHelper, Integer, Long> {
|
||||||
|
List<Poi> taskPois;
|
||||||
|
PoiListener listener;
|
||||||
|
|
||||||
|
public LoadDataTask(PoiListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Long doInBackground(DataprovenceHelper... dph) {
|
||||||
|
try {
|
||||||
|
taskPois = dph[0].find(null);
|
||||||
|
return 1L;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Erreur lors du chargement de " + dph, e);
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onProgressUpdate(Integer... progress) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Long result) {
|
||||||
|
listener.onPoiReceived(taskPois);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataprovenceManager(PoiListener listener) {
|
||||||
|
super();
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Collection<DataprovenceHelper> findHelpers(
|
||||||
|
Collection<String> tags) {
|
||||||
|
ArrayList<DataprovenceHelper> helpers = new ArrayList<DataprovenceHelper>();
|
||||||
|
for (String tag : tags) {
|
||||||
|
String[] datasets = DATASETS.get(tag);
|
||||||
|
for (int d = 0; d < datasets.length; d++) {
|
||||||
|
helpers.add(new DataprovenceHelper(datasets[d]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return helpers;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPoiReceived(List<Poi> pois) {
|
||||||
|
synchronized (this) {
|
||||||
|
this.pois.addAll(pois);
|
||||||
|
count--;
|
||||||
|
// quand on a recu toutes les reponses, on notifie le listener
|
||||||
|
// ("externe")
|
||||||
|
if (count == 0) {
|
||||||
|
listener.onPoiReceived(new ArrayList<Poi>(this.pois));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findAll(Collection<String> tags) throws IOException {
|
||||||
|
|
||||||
|
Set<DataprovenceHelper> helpers = new HashSet<DataprovenceHelper>();
|
||||||
|
helpers.addAll(findHelpers(tags));
|
||||||
|
|
||||||
|
// lance une AsyncTask pour chaque helper
|
||||||
|
for (DataprovenceHelper helper : findHelpers(tags)) {
|
||||||
|
synchronized (this) {
|
||||||
|
count++;
|
||||||
|
new LoadDataTask(this).execute(helper);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findAll(String[] tags) throws IOException {
|
||||||
|
findAll(Arrays.asList(tags));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -56,32 +56,32 @@ public class DataprovenceParserTest extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testfindHelpersCulture() {
|
// public void testfindHelpersCulture() {
|
||||||
try {
|
// try {
|
||||||
List<Poi> pois = DataprovenceHelper
|
// DataprovenceManager
|
||||||
.findAll(new String[] { "CULTURE" });
|
// .findAll(new String[] { "CULTURE" });
|
||||||
assertEquals(4, pois.size());
|
// assertEquals(4, pois.size());
|
||||||
assertTrue(pois
|
// assertTrue(pois
|
||||||
.contains(new DataprovenceHelper("ServicesDeGuides")));
|
// .contains(new DataprovenceHelper("ServicesDeGuides")));
|
||||||
assertTrue(pois.contains(new DataprovenceHelper(
|
// assertTrue(pois.contains(new DataprovenceHelper(
|
||||||
"SitesNaturelsIncontournables")));
|
// "SitesNaturelsIncontournables")));
|
||||||
assertTrue(pois.contains(new DataprovenceHelper("Musees")));
|
// assertTrue(pois.contains(new DataprovenceHelper("Musees")));
|
||||||
assertTrue(pois.contains(new DataprovenceHelper(
|
// assertTrue(pois.contains(new DataprovenceHelper(
|
||||||
"MonumentsEtStesCulturels")));
|
// "MonumentsEtStesCulturels")));
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
e.printStackTrace(System.err);
|
// e.printStackTrace(System.err);
|
||||||
fail(e.getMessage());
|
// fail(e.getMessage());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public void testfindHelpersCultureSport() {
|
// public void testfindHelpersCultureSport() {
|
||||||
try {
|
// try {
|
||||||
List<Poi> pois = DataprovenceHelper.findAll(new String[] {
|
// List<Poi> pois = DataprovenceManager.findAll(new String[] {
|
||||||
"CULTURE", "PLEIN AIR" });
|
// "CULTURE", "PLEIN AIR" });
|
||||||
assertEquals(12, pois.size());
|
// assertEquals(12, pois.size());
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
e.printStackTrace(System.err);
|
// e.printStackTrace(System.err);
|
||||||
fail(e.getMessage());
|
// fail(e.getMessage());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue