Permalink
Browse files

Finished 4 tutorial on Script tables.

  • Loading branch information...
1 parent 8126876 commit 805de84e5390e7f02d15f515a5733b8b4668627e Brett L. Schuchert committed Apr 7, 2009
@@ -1,5 +1,9 @@
package com.om.example.dvr.domain;
+import java.util.Date;
+
+import com.om.example.util.DateUtil;
+
public class Program {
public final String programName;
@@ -33,4 +37,8 @@ public boolean sameEpisodeAs(Program program) {
&& programName.equals(program.programName)
&& episodeName.equals(program.episodeName);
}
+
+ public boolean isOn(Date date) {
+ return DateUtil.instance().isSameDate(timeSlot.startDateTime, date);
+ }
}
@@ -1,5 +1,6 @@
package com.om.example.dvr.domain;
+import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@@ -48,4 +49,14 @@ private boolean alreadyInToDoList(Program candidate) {
return result;
}
+
+ public List<Program> toDoListContentsOn(Date date) {
+ List<Program> result = new LinkedList<Program>();
+
+ for (Program current : toDoList)
+ if (current.isOn(date))
+ result.add(current);
+
+ return result;
+ }
}
@@ -2,11 +2,11 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
-import java.util.Date;
import com.om.example.dvr.domain.ConflictingProgramException;
import com.om.example.dvr.domain.Program;
import com.om.example.dvr.domain.Schedule;
+import com.om.example.util.DateUtil;
public class AddProgramsToSchedule {
static SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy|h:mm");
@@ -48,10 +48,10 @@ public void setMinutes(int minutes) {
this.minutes = minutes;
}
- public void execute() {
+ public void execute() throws ParseException {
try {
- Program p = schedule.addProgram(programName, episodeName, channel,
- buildStartDateTime(), minutes);
+ Program p = schedule.addProgram(programName, episodeName, channel, DateUtil
+ .instance().buildDate(date, startTime), minutes);
lastId = p.getId();
lastCreationSuccessful = true;
} catch (ConflictingProgramException e) {
@@ -68,13 +68,4 @@ public String lastId() {
return lastId;
return "n/a";
}
-
- private Date buildStartDateTime() {
- try {
- String dateTime = String.format("%s|%s", date, startTime);
- return dateFormat.parse(dateTime);
- } catch (ParseException e) {
- throw new RuntimeException("Unable to prase date/time", e);
- }
- }
}
@@ -0,0 +1,26 @@
+package com.om.example.dvr.fixtures;
+
+import java.text.ParseException;
+import java.util.List;
+
+import com.om.example.dvr.domain.Program;
+import com.om.example.util.DateUtil;
+import com.om.query.QueryResultBuilder;
+import com.om.query.domain.QueryResult;
+
+public class EpisodesInToDoListOn {
+ private final String date;
+
+ public EpisodesInToDoListOn(String date) {
+ this.date = date;
+ }
+
+ public List<Object> query() throws ParseException {
+ List<Program> programs = CreateSeasonPassFor.getSeasonPassManager()
+ .toDoListContentsOn(DateUtil.instance().formatDate(date));
+ QueryResultBuilder builder = new QueryResultBuilder(Program.class);
+ builder.register("timeSlot", new TimeSlotPropertyHandler());
+ QueryResult result = builder.build(programs);
+ return result.render();
+ }
+}
@@ -0,0 +1,56 @@
+package com.om.example.dvr.fixtures;
+
+import java.text.ParseException;
+
+import com.om.example.dvr.domain.ConflictingProgramException;
+import com.om.example.util.DateUtil;
+
+public class GeneratePrograms {
+ AddProgramsToSchedule addProgramsToSchedule = new AddProgramsToSchedule();
+ int totalEpisodesCreated;
+
+ public int TotalEpisodesCreated() {
+ return totalEpisodesCreated;
+ }
+
+ public String CreateWeeklyProgramNamedOnChannelStartingOnAtLengthEpisodes(
+ String programName, int channel, String startDate, String startTime,
+ int lengthInMinutes, int episodes) throws ParseException {
+
+ generateManyPrograms(programName, channel, startDate, startTime, lengthInMinutes,
+ episodes, 7);
+ return addProgramsToSchedule.lastId();
+ }
+
+ public String CreateDailyProgramNamedOnChannelStartingOnAtLengthEpisodes(
+ String programName, int channel, String startDate, String startTime,
+ int lengthInMinutes, int episodes) throws ParseException {
+ generateManyPrograms(programName, channel, startDate, startTime, lengthInMinutes,
+ episodes, 1);
+ return addProgramsToSchedule.lastId();
+ }
+
+ private void generateManyPrograms(String programName, int channel, String startDate,
+ String startTime, int minutes, int episodes, int daysBetween)
+ throws ParseException {
+ String nextStartDate = startDate;
+ for (int i = 0; i < episodes; ++i) {
+ createOneProgram(programName, channel, startTime, minutes, nextStartDate, i);
+ nextStartDate = DateUtil.instance().addDaysTo(daysBetween, nextStartDate);
+ }
+ }
+
+ private void createOneProgram(String programName, int channel, String startTime,
+ int minutes, String nextStartDate, int i) throws ParseException {
+ addProgramsToSchedule.setChannel(channel);
+ addProgramsToSchedule.setDate(nextStartDate);
+ addProgramsToSchedule.setEpisode(String.format("E%d", (i + 1)));
+ addProgramsToSchedule.setName(programName);
+ addProgramsToSchedule.setStartTime(startTime);
+ addProgramsToSchedule.setMinutes(minutes);
+ addProgramsToSchedule.execute();
+ if (!addProgramsToSchedule.created())
+ throw new ConflictingProgramException();
+ ++totalEpisodesCreated;
+ }
+}
@@ -0,0 +1,50 @@
+package com.om.example.dvr.fixtures;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.om.example.dvr.domain.ConflictingProgramException;
+import com.om.example.dvr.domain.Program;
+import com.om.example.util.DateUtil;
+
+public class GenerateProgramsTest {
+ private GeneratePrograms generatePrograms;
+
+ @Before
+ public void init() throws Exception {
+ generatePrograms = new GeneratePrograms();
+ }
+
+ @Test
+ public void ReviewToDoListByDay() throws Exception {
+ generatePrograms.CreateWeeklyProgramNamedOnChannelStartingOnAtLengthEpisodes("W1",
+ 7, "3/4/2008", "21:00", 60, 8);
+ generatePrograms.CreateWeeklyProgramNamedOnChannelStartingOnAtLengthEpisodes("W2",
+ 8, "3/4/2008", "21:00", 60, 8);
+ generatePrograms.CreateDailyProgramNamedOnChannelStartingOnAtLengthEpisodes("D1",
+ 7, "3/4/2008", "20:30", 30, 56);
+ generatePrograms.CreateDailyProgramNamedOnChannelStartingOnAtLengthEpisodes("D2",
+ 8, "3/4/2008", "22:00", 30, 56);
+
+ new CreateSeasonPassFor("W1", 7);
+ new CreateSeasonPassFor("W2", 8);
+ new CreateSeasonPassFor("D1", 7);
+ new CreateSeasonPassFor("D2", 8);
+
+ List<Program> results = CreateSeasonPassFor.getSeasonPassManager()
+ .toDoListContentsOn(DateUtil.instance().formatDate("3/4/2008"));
+ assertEquals(4, results.size());
+ }
+
+ @Test(expected = ConflictingProgramException.class)
+ public void GeneratingConflictingProgramsThrowsException() throws Exception {
+ generatePrograms.CreateWeeklyProgramNamedOnChannelStartingOnAtLengthEpisodes("N1",
+ 7, "3/4/2008", "21:00", 60, 1);
+ generatePrograms.CreateWeeklyProgramNamedOnChannelStartingOnAtLengthEpisodes("N2",
+ 7, "3/4/2008", "21:00", 60, 1);
+ }
+}
@@ -1,26 +1,24 @@
package com.om.example.dvr.fixtures;
-import java.text.SimpleDateFormat;
import java.util.Date;
import com.om.example.dvr.domain.TimeSlot;
+import com.om.example.util.DateUtil;
import com.om.query.domain.ObjectDescription;
import com.om.query.handler.PropertyHandler;
import com.om.reflection.PropertyGetter;
public class TimeSlotPropertyHandler extends PropertyHandler {
- static SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");
- static SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm");
-
@Override
public void handle(PropertyGetter propertyGetter, Object targetObject,
ObjectDescription objectDescription) {
TimeSlot timeSlot = propertyGetter.getValue(targetObject, TimeSlot.class);
Date startDateTime = timeSlot.startDateTime;
- objectDescription.addPropertyDescription("date", dateFormat.format(startDateTime));
- objectDescription.addPropertyDescription("startTime", timeFormat
- .format(startDateTime));
+ objectDescription.addPropertyDescription("date", DateUtil.instance().formatDate(
+ startDateTime));
+ objectDescription.addPropertyDescription("startTime", DateUtil.instance()
+ .formatTime(startDateTime));
}
-}
+}
@@ -0,0 +1,54 @@
+package com.om.example.util;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+public class DateUtil {
+ private static DateUtil INSTANCE = new DateUtil();
+
+ private DateUtil() {
+ }
+
+ public static DateUtil instance() {
+ return INSTANCE;
+ }
+
+ public static final SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");
+
+ public Date formatDate(String date) throws ParseException {
+ return dateFormat.parse(date);
+ }
+
+ public String formatDate(Date startDateTime) {
+ return dateFormat.format(startDateTime);
+ }
+
+ static SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm");
+
+ public String formatTime(Date startDateTime) {
+ return timeFormat.format(startDateTime);
+ }
+
+ public String addDaysTo(int days, String nextStartDate) throws ParseException {
+ Calendar calendar = Calendar.getInstance();
+ calendar.clear();
+ Date startingDate = dateFormat.parse(nextStartDate);
+ calendar.setTime(startingDate);
+ calendar.add(Calendar.DATE, days);
+ return dateFormat.format(calendar.getTime());
+ }
+
+ public static final SimpleDateFormat dateTimeMergedFormat = new SimpleDateFormat(
+ "M/d/yyyy|h:mm");
+
+ public Date buildDate(String date, String startTime) throws ParseException {
+ String dateTime = String.format("%s|%s", date, startTime);
+ return dateTimeMergedFormat.parse(dateTime);
+ }
+
+ public boolean isSameDate(Date startDateTime, Date date) {
+ return formatDate(startDateTime).equals(formatDate(date));
+ }
+}
@@ -0,0 +1,25 @@
+package com.om.example.util;
+
+import static org.junit.Assert.assertEquals;
+
+import java.text.ParseException;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.junit.Test;
+
+public class DateUtilTest {
+ @Test
+ public void CanCreateDateTimeFromStrings() throws ParseException {
+ Date buildDate = DateUtil.instance().buildDate("1/3/2007", "23:45");
+ Calendar calendar = Calendar.getInstance();
+ calendar.clear();
+ calendar.setTime(buildDate);
+ assertEquals(2007, calendar.get(Calendar.YEAR));
+ assertEquals(3, calendar.get(Calendar.DATE));
+ assertEquals(Calendar.JANUARY, calendar.get(Calendar.MONTH));
+ assertEquals(23, calendar.get(Calendar.HOUR_OF_DAY));
+ assertEquals(45, calendar.get(Calendar.MINUTE));
+ assertEquals(0, calendar.get(Calendar.SECOND));
+ }
+}

0 comments on commit 805de84

Please sign in to comment.