Skip to content
This repository has been archived by the owner on Dec 20, 2020. It is now read-only.

Commit

Permalink
Merge pull request #17 from ApplETS/dev
Browse files Browse the repository at this point in the history
ETS Calendar
  • Loading branch information
ttauveron authored Feb 9, 2017
2 parents 6c2fce6 + ca33b88 commit 21de26d
Show file tree
Hide file tree
Showing 33 changed files with 545 additions and 331 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Build Status](https://travis-ci.org/ApplETS/applets-java-api.svg?branch=master)](https://travis-ci.org/ApplETS/applets-java-api) [![Coverage Status](https://coveralls.io/repos/github/ApplETS/applets-java-api/badge.svg?branch=master)](https://coveralls.io/github/ApplETS/applets-java-api?branch=master)

[![Run in Postman](https://run.pstmn.io/button.svg)](https://app.getpostman.com/run-collection/ba698d57365064fe6007)


Vue d'ensemble
========
Expand All @@ -16,13 +16,9 @@ Liens rapides :

Requêtes actuelles dans l'API
========
- Afficher les partenaires : https://api3.clubapplets.ca/partners
- Cooptel : https://api3.clubapplets.ca/cooptel?phase=[1 à 4]&appt=[num]
- Event sources : https://api3.clubapplets.ca/events/sources
- Events : https://api3.clubapplets.ca/events/list/[nomsource]
- Nouvelle sources : https://api3.clubapplets.ca/news/sources
- Nouvelles : https://api3.clubapplets.ca/news/list/[nomsource]
- Version des apps : https://api3.clubapplets.ca/app_version/[ios ou android]
Disponibles dans la collection Postman suivante :
[![Run in Postman](https://run.pstmn.io/button.svg)](https://app.getpostman.com/run-collection/8bb16d836b6212da7c4f)


Outils nécessaires
==========
Expand Down
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ dependencies {

compile 'log4j:log4j:1.2.17'

compile 'com.restfb:restfb:1.35.0'

compile 'org.postgresql:postgresql:9.4-1206-jdbc42'
compile 'org.mnode.ical4j:ical4j:2.0.0'

testCompile 'junit:junit:4.12'

testCompile 'junit:junit:4.12'
testCompile 'org.glassfish.jersey.test-framework:project:2.23.2'
testCompile 'org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:2.23.2'

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/applets/etsmtl/ca/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ public MyApplication() {
.withIdentity("triggersources", "group1")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5000).repeatForever())
.withIntervalInSeconds(60*60*10).repeatForever())
.build();

Trigger trigger_events = TriggerBuilder
.newTrigger()
.withIdentity("triggerevents", "group2")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(2500).repeatForever())
.withIntervalInSeconds(60*60*2).repeatForever())
.build();

Trigger trigger_news = TriggerBuilder
.newTrigger()
.withIdentity("triggernews", "group3")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(2500).repeatForever())
.withIntervalInSeconds(60*60).repeatForever())
.build();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import javax.ws.rs.PathParam;
import java.io.IOException;

/**
* Created by gnut3ll4 on 07/12/15.
*/
@Path("app_version")
public class AppVersionResource {

Expand Down
128 changes: 128 additions & 0 deletions src/main/java/applets/etsmtl/ca/calendar/CalendarResource.java
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 src/main/java/applets/etsmtl/ca/calendar/model/CalendarEvent.java
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;
}

}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

/**
* Created by gnut3ll4 on 08/03/16.
*/

@XmlRootElement
public class ConsommationDate implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import java.io.Serializable;
import java.util.List;

/**
* Created by gnut3ll4 on 08/03/16.
*/

@XmlRootElement
public class ConsommationGlobal implements Serializable {
float restant;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/applets/etsmtl/ca/news/EventsResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import java.util.ArrayList;
import java.util.List;

/**
* Created by Marcantvez on 09/03/16.
*/
@Path("events")
public class EventsResources {

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/applets/etsmtl/ca/news/NewsResources.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package applets.etsmtl.ca.news;

/**
* Created by gnut3ll4 on 22/01/16.
*/

import applets.etsmtl.ca.news.db.NouvellesDAO;
import applets.etsmtl.ca.news.db.SourceDAO;
import applets.etsmtl.ca.news.model.Nouvelle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import java.sql.DriverManager;
import java.sql.SQLException;

/**
* Created by gnut3ll4 on 24/01/16.
*/
public class ConnectionSingleton {

//Environment variables
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/applets/etsmtl/ca/news/db/DAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import java.sql.SQLException;
import java.util.List;

/**
* Created by gnut3ll4 on 24/01/16.
*/
public abstract class DAO<T> {

public Connection connection = ConnectionSingleton.getInstance();
Expand Down
Loading

0 comments on commit 21de26d

Please sign in to comment.