Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.warsaw.data.service; import com.warsaw.data.model.Place; import com.warsaw.data.serviceLocal.PlaceServiceLocal; import com.warsaw.data.utils.JsonReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.ejb.Stateless; import org.json.JSONArray; import org.json.JSONObject; /** * * @author Pawel */ @Stateless public class PlaceService implements PlaceServiceLocal { private List<Place> culturePlaces; private List<Place> sportPlaces; private List<Place> sleepingPlaces; public PlaceService() { culturePlaces = this.getAllCulturePlaces(); sportPlaces = this.getAllFootballPlaces(); sleepingPlaces = this.getAllSleepingPlaces(); } private List<Place> getAllSleepingPlaces() { sleepingPlaces = new ArrayList<>(); JSONObject json = this.getJsonFromURL( "https://api.um.warszawa.pl/api/action/wfsstore_get/?id=f019448f-951c-439e-bf37-c3268682752e&&apikey=6259fbb5-468a-476c-be8c-10da5c20ad7a"); try { sleepingPlaces = this.getAnyPlaces(json, 4, 8); } catch (Exception e) { return Collections.emptyList(); } return sleepingPlaces; } private List<Place> getAllCulturePlaces() { culturePlaces = new ArrayList<>(); JSONObject json = this.getJsonFromURL( "https://api.um.warszawa.pl/api/action/wfsstore_get/?id=e26218cb-61ec-4ccb-81cc-fd19a6fee0f8&apikey=6259fbb5-468a-476c-be8c-10da5c20ad7a"); try { culturePlaces = this.getAnyPlaces(json, 4, 5); } catch (Exception e) { return Collections.emptyList(); } return culturePlaces; } /** * Getting JSON from URL * @param url * @return JSON object */ private JSONObject getJsonFromURL(String url) { JSONObject json = null; while (json == null) { try { json = JsonReader.readJSON(url); } catch (Exception ex) { } } return json; } private List<Place> getAllFootballPlaces() { sportPlaces = new ArrayList<>(); JSONObject json = this.getJsonFromURL( "https://api.um.warszawa.pl/api/action/wfsstore_get/?id=9317a2dc-d08a-41bc-874d-01cc2dc88006&apikey=6259fbb5-468a-476c-be8c-10da5c20ad7a"); try { sportPlaces = this.getAnyPlaces(json, 1, 2); } catch (Exception e) { return Collections.emptyList(); } return sportPlaces; } /** * Getting any places list from JSON file * @param json JSON file * @param descIdx Index of description field * @param areaIdx Index of area field * @return List of places * @throws Exception */ private List<Place> getAnyPlaces(JSONObject json, int descIdx, int areaIdx) throws Exception { List<Place> places = new ArrayList<>(); JSONObject obj = (JSONObject) json.get("result"); JSONArray obj1 = (JSONArray) obj.get("featureMemberList"); for (int i = 0; i < obj1.length(); i++) { JSONObject obj2 = (JSONObject) obj1.getJSONObject(i); JSONArray obj3 = (JSONArray) obj2.get("properties"); String desc = obj3.getJSONObject(descIdx).getString("value"); String area = obj3.getJSONObject(areaIdx).getString("value"); JSONObject obj4 = (JSONObject) obj2.get("geometry"); JSONArray coordinates = (JSONArray) obj4.getJSONArray("coordinates"); JSONObject obj5 = (JSONObject) coordinates.getJSONObject(0); String lon = obj5.getString("longitude"); String lat = obj5.getString("latitude"); Place place = new Place(); place.setLat(lat); place.setLon(lon); place.setDesc(desc); place.setArea(area); places.add(place); } return places; } /** * Getting theatres from specific area * @param area Area (f.e. Bielany, oliborz) * @return List of places from specific area */ @Override public List<Place> getCulturePlacesByArea(String area) { List<Place> placesFromArea = new ArrayList<>(); for (Place p : culturePlaces) { if (p.getArea().equals(area)) { placesFromArea.add(p); } } return placesFromArea; } /** * Getting football fields from specific area * @param area Area (f.e. Bielany, oliborz) * @return List of places from speicifc area */ @Override public List<Place> getFootballFieldsByArea(String area) { List<Place> placesFromArea = new ArrayList<>(); for (Place p : sportPlaces) { if (p.getArea().equals(area)) { placesFromArea.add(p); } } return placesFromArea; } }