This repository has been archived by the owner on Dec 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from ApplETS/dev
ETS Calendar
- Loading branch information
Showing
33 changed files
with
545 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/main/java/applets/etsmtl/ca/calendar/CalendarResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package applets.etsmtl.ca.calendar; | ||
|
||
|
||
import applets.etsmtl.ca.calendar.model.CalendarEvent; | ||
import com.squareup.okhttp.OkHttpClient; | ||
import com.squareup.okhttp.Request; | ||
import com.squareup.okhttp.Response; | ||
import net.fortuna.ical4j.data.CalendarBuilder; | ||
import net.fortuna.ical4j.data.ParserException; | ||
import net.fortuna.ical4j.model.Calendar; | ||
import net.fortuna.ical4j.model.component.CalendarComponent; | ||
import net.sf.ehcache.Cache; | ||
import net.sf.ehcache.CacheManager; | ||
import net.sf.ehcache.Element; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
@Path("calendar") | ||
public class CalendarResource { | ||
|
||
private static final String CALENDAR_URL = "http://www.google.com/calendar/ical/etsmtl.net_shfr1g6kdra1dcjdl0orb6jico%40group.calendar.google.com/public/basic.ics"; | ||
private SimpleDateFormat dateUrlParser = new SimpleDateFormat("yyyy-MM-dd"); | ||
|
||
@GET | ||
@Path("{id}/{start_date}/{end_date}") | ||
@Produces({MediaType.APPLICATION_JSON}) | ||
public List<CalendarEvent> getCalendarEvents( | ||
@PathParam("id") String id, | ||
@PathParam("start_date") String startDateParam, | ||
@PathParam("end_date") String endDateParam | ||
) { | ||
|
||
List<CalendarEvent> calendarEvents = new ArrayList<>(); | ||
|
||
//At the moment, there is only one source for the calendar | ||
if (!id.equals("ets")) | ||
return calendarEvents; | ||
|
||
try { | ||
Date startDate = dateUrlParser.parse(startDateParam); | ||
Date endDate = dateUrlParser.parse(endDateParam); | ||
|
||
calendarEvents = getCalendarEvents().stream() | ||
.filter(calendarEvent -> | ||
calendarEvent.getStartDate().after(startDate) && | ||
calendarEvent.getStartDate().before(endDate)) | ||
.collect(Collectors.toList()); | ||
|
||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return calendarEvents; | ||
} | ||
|
||
private ArrayList<CalendarEvent> getCalendarEvents() { | ||
ArrayList<CalendarEvent> calendarEvents = new ArrayList<>(); | ||
CacheManager cacheManager = CacheManager.getInstance(); | ||
Cache cache = cacheManager.getCache("calendar"); | ||
|
||
//Return data from the cache, if available | ||
Element element = cache.get("calendar"); | ||
if (element != null) { | ||
return (ArrayList<CalendarEvent>) element.getObjectValue(); | ||
} | ||
|
||
InputStream stream; | ||
|
||
//Download and parse .ics and refresh the cache | ||
try { | ||
|
||
OkHttpClient client = new OkHttpClient(); | ||
|
||
Request request = new Request.Builder() | ||
.url(CALENDAR_URL) | ||
.get() | ||
.build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
|
||
String redirect = response.header("Location"); | ||
if (redirect != null) { | ||
request = new Request.Builder() | ||
.url(redirect) | ||
.get() | ||
.build(); | ||
response = client.newCall(request).execute(); | ||
} | ||
|
||
stream = response.body().byteStream(); | ||
|
||
CalendarBuilder builder = new CalendarBuilder(); | ||
Calendar calendar = builder.build(stream); | ||
|
||
|
||
SimpleDateFormat dateParser = new SimpleDateFormat("yyyyMMdd"); | ||
|
||
for (CalendarComponent calendarComponent : calendar.getComponents()) { | ||
Date dtstart = dateParser.parse(calendarComponent.getProperty("DTSTART").getValue()); | ||
String uid = calendarComponent.getProperty("UID").getValue(); | ||
Date dtend = dateParser.parse(calendarComponent.getProperty("DTEND").getValue()); | ||
String summary = calendarComponent.getProperty("SUMMARY").getValue(); | ||
|
||
calendarEvents.add(new CalendarEvent(dtstart, uid, dtend, summary)); | ||
} | ||
|
||
cache.put(new Element("calendar", calendarEvents)); | ||
|
||
} catch (ParserException | IOException | ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return calendarEvents; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/applets/etsmtl/ca/calendar/model/CalendarEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package applets.etsmtl.ca.calendar.model; | ||
|
||
import applets.etsmtl.ca.calendar.utils.JsonDateSerializer; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import java.io.Serializable; | ||
import java.util.Date; | ||
|
||
@XmlRootElement | ||
public class CalendarEvent implements Serializable { | ||
|
||
private String id; | ||
private String summary; | ||
|
||
@JsonProperty("start_date") | ||
private Date startDate; | ||
@JsonProperty("end_date") | ||
private Date endDate; | ||
|
||
|
||
public CalendarEvent(Date startDate, String id, Date endDate, String summary) { | ||
this.startDate = startDate; | ||
this.id = id; | ||
this.endDate = endDate; | ||
this.summary = summary; | ||
} | ||
|
||
@JsonSerialize(using = JsonDateSerializer.class) | ||
public Date getStartDate() { | ||
return startDate; | ||
} | ||
|
||
public void setStartDate(Date startDate) { | ||
this.startDate = startDate; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
@JsonSerialize(using = JsonDateSerializer.class) | ||
public Date getEndDate() { | ||
return endDate; | ||
} | ||
|
||
public void setEndDate(Date endDate) { | ||
this.endDate = endDate; | ||
} | ||
|
||
public String getSummary() { | ||
return summary; | ||
} | ||
|
||
public void setSummary(String summary) { | ||
this.summary = summary; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/applets/etsmtl/ca/calendar/utils/JsonDateSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package applets.etsmtl.ca.calendar.utils; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
||
import java.io.IOException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
|
||
public class JsonDateSerializer extends JsonSerializer<Date> { | ||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | ||
|
||
@Override | ||
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) | ||
throws IOException { | ||
String formattedDate = dateFormat.format(date); | ||
gen.writeString(formattedDate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.