package com.screaminggreen.formprocessing;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
import com.screaminggreen.beans.ProfessorBean;
import com.screaminggreen.beans.SessionBean;
import com.screaminggreen.datastore.CourseTab;
import com.screaminggreen.datastore.DatastoreAPI;

public class ScheduleFormParser implements FormParser {

	public final static String TYPE_OF_COURSETAB = "Schedule";
	
	private String strArrayToString(String [] arr) {
		if(arr == null || arr.length == 0) { return ""; }		
		
		if(arr.length == 1) {
			return arr[0];
		}
		
		String bigString = "";
		
		for(String s : arr) {
			bigString += s + ",";
		}
		
		//No last comma
		bigString = bigString.substring(0, bigString.length() - 1);
		
		return bigString;
		
	}
	
	@Override
	public void parse(HttpServletRequest req) {
		
		String [] dates = req.getParameterValues("dateOfEvent");
		String [] events = req.getParameterValues("eventDesc");
		
		if(dates.length != events.length) {
			//Mismatch!
			return;
		}
		
		//Get the webId
		SessionBean sBean = (SessionBean) req.getSession().getAttribute("sessionBean");
		if(sBean == null){
			return;
		}

		ProfessorBean pBean = (ProfessorBean) sBean.getProfBean();		
		if(pBean == null){
			return;
		}
		
		String webId = pBean.getWebId();
		
		//Update this data in Datastore...
		
		//Get or make new entity
		Entity tabEntity = CourseTab.createOrGetCourseTab(webId, TYPE_OF_COURSETAB);
		
		tabEntity.setProperty("dates", strArrayToString(dates));
		tabEntity.setProperty("events", strArrayToString(events));				
		
		DatastoreAPI.persistEntity(tabEntity);
	}

}

@@ -1243,24 +1243,125 @@
 tDesc%22);%0A%09%09%0A
+%09%09if(dates == null %7C%7C dates.length %3C= 0 %7C%7C events == null %7C%7C events.length %3C= 0) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09%0A
 %09%09if(dates.l

Fix for saving schedule events with 0