Java tutorial
/* * CalDataProvider - This program provides google calendar data and visualizes them * * Copyright (C) 2012 Steve Liedtke <sliedtke57@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, or (at your option) any later version. * * 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 can find a copy of the GNU General Public License on http://www.gnu.org/licenses/gpl.html. * * Contributors: * Steve Liedtke <sliedtke57@gmail.com> */ package de.steveliedtke.calendar.domain.impl; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.logging.Logger; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import de.steveliedtke.calendar.data.api.CalendarInfoDAO; import de.steveliedtke.calendar.domain.api.EventService; /** * @author Steve Liedtke */ public final class EventServiceImpl implements EventService { /** * static attribute used for logging. */ private static final Logger logger = Logger.getLogger(EventServiceImpl.class.getName()); private final CalendarInfoDAO calDAO; public EventServiceImpl(final CalendarInfoDAO calDAO) { this.calDAO = calDAO; } /* (non-Javadoc) * @see de.liedtke.business.api.CloseService#close() */ @Override public void close() { this.calDAO.close(); } /* (non-Javadoc) * @see de.steveliedtke.calendar.domain.api.UpdateDataService#getNextEvents(java.lang.String, int) */ @Override public JSONArray getNextEvents(final String basicUrl, final int amount) { final String parameters = "full?ctz=Europe/Berlin&futureevents=true&orderby=starttime" + "&max-results=" + amount + "&singleevents=true&sortorder=a&alt=json&fields=entry(title,gd:when,content,gd:where)"; final JSONArray result = new JSONArray(); try { final JSONObject feedResult = new JSONObject(this.fetchCalendarUrl(basicUrl + parameters)); final JSONObject feed = feedResult.getJSONObject("feed"); final JSONArray entries = feed.getJSONArray("entry"); for (int i = 0; i < entries.length(); i++) { final JSONObject entry = entries.getJSONObject(i); final String title = entry.getJSONObject("title").getString("$t"); final String content = entry.getJSONObject("content").getString("$t"); final String starttime = entry.getJSONArray("gd$when").getJSONObject(0).getString("startTime"); final String endtime = entry.getJSONArray("gd$when").getJSONObject(0).getString("endTime"); final String where = entry.getJSONArray("gd$where").getJSONObject(0).getString("valueString"); final JSONObject resultEntry = new JSONObject(); resultEntry.put("title", title); resultEntry.put("content", content); resultEntry.put("starttime", starttime); resultEntry.put("endtime", endtime); resultEntry.put("where", where); result.put(resultEntry); } } catch (JSONException e) { logger.warning("JSONException: " + e.getMessage()); } return result; } private String fetchCalendarUrl(String urlToRead) { URL url; HttpURLConnection conn; BufferedReader rd; String line; String result = ""; try { url = new URL(urlToRead); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(60000); rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = rd.readLine()) != null) { result += line; } rd.close(); } catch (Exception e) { logger.warning(e.getMessage()); } return result; } /* (non-Javadoc) * @see de.steveliedtke.calendar.domain.api.UpdateDataService#updateCache(java.lang.String) */ @Override public void updateCache(String basicUrl) { // TODO Auto-generated method stub } }