package models;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.OneToMany;

import play.data.validation.Required;
import play.db.jpa.Model;

@SuppressWarnings("serial")
@Entity
public class Slot extends Model {
	
	@Field("startDate")
	@Required
	public Date startDate;
	
	@Field("endDate")
	@Required
	public Date endDate;
	
	@OneToMany(mappedBy = "slot")
	public List<Talk> talks = new ArrayList<Talk>();
	
	@Override
	public String toString() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
		
		return dateFormat.format(startDate)+" "+timeFormat.format(startDate)+" - "+timeFormat.format(endDate);
	}
	
	public static List<Slot> findPerDay(Date day){
		return find("date_trunc('day', startDate) = ? ORDER BY startDate", day).fetch();
	}
	
	public Talk getTalkPerTrack(Track track){
		for(Talk talk : talks)
			if(talk.track == track)
				return talk;
		return null;
	}
	
	public Talk getAllTracksEvent(){
		return talks.size() == 1 && talks.get(0).track == null ? talks.get(0) : null;
	}
}

@@ -697,23 +697,77 @@
 );%0A%09%09%0A%09%09
-return 
+StringBuffer strbuf = new StringBuffer();%0A%09%09%0A%09%09strbuf.append(
 dateForm
@@ -790,13 +790,32 @@
 ate)
-+%22 %22+
+).append(%22 %22)%0A%09%09.append(
 time
@@ -842,15 +842,31 @@
 ate)
-+%22 - %22+
+).append(%22 - %22).append(
 time
@@ -887,16 +887,231 @@
 endDate)
+);%0A%09%09strbuf.append(%22 (%22);%0A%09%09boolean first = true;%0A%09%09for(Talk talk : talks)%7B%0A%09%09%09if(!first)%0A%09%09%09%09strbuf.append(%22, %22);%0A%09%09%09first = false;%0A%09%09%09strbuf.append(talk.track);%0A%09%09%7D%0A%09%09strbuf.append(%22)%22);%0A%09%09return strbuf.toString()
 ;%0A%09%7D%0A%09%0A%09

include the taken tracks in the given slot's toString