package models.transit;


import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Query;

import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.annotations.Type;

import play.db.jpa.Model;

import models.gtfs.GtfsSnapshot;

@JsonIgnoreProperties({"entityId", "persistent"})
@Entity
public class ServiceCalendar extends Model {
	
	@ManyToOne
    public Agency agency;
	
	public String description;
	
	public String gtfsServiceId;
    public Boolean monday;
    public Boolean tuesday;
    public Boolean wednesday;
    public Boolean thursday;
    public Boolean friday;
    public Boolean saturday;
    public Boolean sunday;
    public Date startDate;
    public Date endDate;
    
	@JsonCreator
    public static ServiceCalendar factory(long id) {
      return ServiceCalendar.findById(id);
    }

    @JsonCreator
    public static ServiceCalendar factory(String id) {
      return ServiceCalendar.findById(Long.parseLong(id));
    }

    public String toString() {

    	String str = "";

    	if(this.monday)
    		str += "Mo";

    	if(this.tuesday)
    		str += "Tu";

    	if(this.wednesday)
    		str += "We";

    	if(this.thursday)
    		str += "Th";

    	if(this.friday)
    		str += "Fr";

    	if(this.saturday)
    		str += "Sa";

    	if(this.sunday)
    		str += "Su";

    	return str;
    }

    public static BigInteger nativeInsert(EntityManager em, org.onebusaway.gtfs.model.ServiceCalendar gtfsServiceCalendar, BigInteger agencyId)
    {
    	Query idQuery = em.createNativeQuery("SELECT NEXTVAL('hibernate_sequence');");
		BigInteger nextId = (BigInteger)idQuery.getSingleResult();
		
		em.createNativeQuery("INSERT INTO servicecalendar (id, gtfsserviceid, monday, tuesday, wednesday, thursday, friday, saturday, sunday, startdate, enddate, agency_id)" +
	    	"  VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")
	      .setParameter(1,  nextId)
	      .setParameter(2, gtfsServiceCalendar.getId())
	      .setParameter(3, gtfsServiceCalendar.getMonday() == 1 ? true : false)
	      .setParameter(4, gtfsServiceCalendar.getTuesday() == 1 ? true : false)
	      .setParameter(5, gtfsServiceCalendar.getWednesday() == 1 ? true : false)
	      .setParameter(6, gtfsServiceCalendar.getThursday() == 1 ? true : false)
	      .setParameter(7, gtfsServiceCalendar.getFriday() == 1 ? true : false)
	      .setParameter(8, gtfsServiceCalendar.getSaturday() == 1 ? true : false)
	      .setParameter(9, gtfsServiceCalendar.getSunday() == 1 ? true : false)
	      .setParameter(10, gtfsServiceCalendar.getStartDate().getAsDate())
	      .setParameter(11, gtfsServiceCalendar.getEndDate().getAsDate())
	      .setParameter(12, agencyId)
	      .executeUpdate();
		
		return nextId;
    }
    
}

@@ -1212,32 +1212,823 @@
 ng(id));%0A    %7D%0A%0A
+    /**%0A     * Get a description for an OBA GTFS service calendar %0A     */%0A    public static String getNameForGtfsServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar cal) %7B%0A        StringBuilder sb = new StringBuilder(14);%0A        %0A        if (cal.getMonday() == 1)%0A            sb.append(%22Mo%22);%0A        %0A        if (cal.getTuesday() == 1)%0A            sb.append(%22Tu%22);%0A        %0A        if (cal.getWednesday() == 1)%0A            sb.append(%22We%22);%0A        %0A        if (cal.getThursday() == 1)%0A            sb.append(%22Th%22);%0A        %0A        if (cal.getFriday() == 1)%0A            sb.append(%22Fr%22);%0A        %0A        if (cal.getSaturday() == 1)%0A            sb.append(%22Sa%22);%0A        %0A        if (cal.getSunday() == 1)%0A            sb.append(%22Su%22);%0A        %0A        return sb.toString();%0A    %7D%0A    %0A
     public Strin
@@ -2042,16 +2042,16 @@
 ing() %7B%0A
-
 %0A    %09St
@@ -2848,16 +2848,29 @@
 gency_id
+, description
 )%22 +%0A%09  
@@ -2914,16 +2914,19 @@
 ?, ?, ?,
+ ?,
  ?);%22)%0A%09
@@ -3693,32 +3693,32 @@
 ().getAsDate())%0A
-
 %09      .setParam
@@ -3732,24 +3732,101 @@
 , agencyId)%0A
+%09      .setParameter(13, getNameForGtfsServiceCalendar(gtfsServiceCalendar))%0A
 %09      .exec

fix #97 (calendar naming on import).