package no.java.incogito.web.servlet; import static fj.data.Option.fromNull; import static no.java.incogito.web.resources.XmlFunctions.sessionToXml; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Collection; import java.util.Map; import no.java.incogito.application.IncogitoApplication; import no.java.incogito.domain.IncogitoUri; import no.java.incogito.domain.Schedule; import no.java.incogito.domain.IncogitoUri.IncogitoEventsUri.IncogitoEventUri; import no.java.incogito.domain.IncogitoUri.IncogitoRestEventsUri.IncogitoRestEventUri; import no.java.incogito.dto.EventXml; import no.java.incogito.dto.ScheduleXml; import no.java.incogito.dto.SessionXml; import no.java.incogito.web.WebFunctions; import no.java.incogito.web.resources.XmlFunctions; import org.joda.time.Interval; import org.joda.time.LocalDate; import fj.F; import fj.data.List; import fj.data.TreeMap; /** * Taglib functions. * * @author <a href="mailto:trygvis@java.no">Trygve Laugstøl</a> * @version $Id$ */ public class IncogitoFunctions { public static String urlDecode(String input) { try { return URLDecoder.decode(input, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } public static Object some(fj.data.Option option) { return option.some(); } public static Object mapGet(Map map, Object key) { return map.get(key); } public static Object treeMapGet(TreeMap<Object, Object> map, Object key) { return map.get(key).some(); } public static Object mapGet(Map map, Object key, Object defaultValue) { Object value = map.get(key); return value == null ? defaultValue : value; } public static java.util.List splitAt(java.util.List list, int n) { java.util.List<java.util.List<Object>> l = new ArrayList<java.util.List<Object>>(); java.util.List<Object> current = null; for (int i = 0, listSize = list.size(); i < listSize; i++) { if (i % n == 0) { current = new ArrayList<Object>(); l.add(current); } //noinspection ConstantConditions current.add(list.get(i)); } return l; } public static SessionXml castToSession(Object o) { return SessionXml.class.cast(o); } public static SessionXml[] castToSessionList(Object o) { @SuppressWarnings({"unchecked"}) List<SessionXml> list = (List<SessionXml>) o; return list.toArray(SessionXml[].class).array(); } public static Interval[] castToIntervalList(Object o) { @SuppressWarnings({"unchecked"}) Collection<Interval> list = (Collection<Interval>) o; return list.toArray(new Interval[list.size()]); } public static WebCalendar getCalendar(IncogitoApplication app, String eventName, String userName) { IncogitoUri uri = new IncogitoUri(app.getConfiguration().baseurl); IncogitoRestEventUri restEventUri = uri.restEvents().eventUri(eventName); IncogitoEventUri eventUri = uri.events().eventUri(eventName); return app.getSchedule(urlDecode(eventName), fromNull(userName)).ok(). map(WebFunctions.webCalendar.f(restEventUri).f(eventUri)).value(); } public static WebSessionList getSessionList(IncogitoApplication app, String eventName, String userName) { IncogitoUri uri = new IncogitoUri(app.getConfiguration().baseurl); IncogitoRestEventUri restEventUri = uri.restEvents().eventUri(eventName); IncogitoEventUri eventUri = uri.events().eventUri(eventName); return app.getSchedule( urlDecode(eventName), fromNull(userName) ) .ok(). map(WebFunctions.webSessionList.f(restEventUri).f(eventUri)).value(); } public static EventXml[] getEvents(IncogitoApplication app) { IncogitoUri uri = new IncogitoUri(app.getConfiguration().baseurl); return app.getEvents().value(). map(XmlFunctions.eventToXml.f(uri)).toArray(EventXml[].class).array(); } public static SessionXml[] getSessions(IncogitoApplication app, String eventName) { IncogitoUri uri = new IncogitoUri(app.getConfiguration().baseurl); IncogitoRestEventUri restEventUri = uri.restEvents().eventUri(eventName); IncogitoEventUri eventUri = uri.events().eventUri(eventName); return app.getSessions(urlDecode(eventName)).value(). map(sessionToXml.f(restEventUri).f(eventUri)).toArray(SessionXml[].class).array(); } public static EventXml getEventByName(IncogitoApplication app, String eventName) { IncogitoUri uri = new IncogitoUri(app.getConfiguration().baseurl); return app.getEventByName(urlDecode(eventName)).ok().map(XmlFunctions.eventToXml.f(uri)).value(); } public static SessionXml getSessionByTitle(IncogitoApplication app, String eventName, String sessionTitle) { IncogitoUri uri = new IncogitoUri(app.getConfiguration().baseurl); IncogitoRestEventUri restEventUri = uri.restEvents().eventUri(eventName); IncogitoEventUri eventUri = uri.events().eventUri(eventName); return sessionToXml.f(restEventUri).f(eventUri).f(app.getSessionByTitle(urlDecode(eventName), urlDecode(sessionTitle)).value()); } public static ScheduleXml getSchedule(IncogitoApplication app, String eventName, String userName) { IncogitoUri uri = new IncogitoUri(app.getConfiguration().baseurl); IncogitoRestEventUri restEventUri = uri.restEvents().eventUri(eventName); IncogitoEventUri eventUri = uri.events().eventUri(eventName); F<Schedule, ScheduleXml> scheduleToXml = XmlFunctions.scheduleToXml.f(restEventUri).f(eventUri); return app.getSchedule(urlDecode(eventName), fromNull(userName)).ok().map(scheduleToXml).value(); } public static String formatDate(LocalDate date) { return date.toString("EEE d MMMM"); } }