FileDocCategorySizeDatePackage
SchoolCalendarServiceImpl.javaAPI DocExample5429Mon Aug 27 20:11:36 BST 2007com.google.gwt.sample.dynatable.server

SchoolCalendarServiceImpl

public class SchoolCalendarServiceImpl extends com.google.gwt.user.server.rpc.RemoteServiceServlet implements com.google.gwt.sample.dynatable.client.SchoolCalendarService
The implemenation of the RPC service which runs on the server.

Fields Summary
private static final String[]
FIRST_NAMES
private static final String[]
LAST_NAMES
private static final String[]
SUBJECTS
private static final com.google.gwt.sample.dynatable.client.Person[]
NO_PEOPLE
private static final int
CLASS_LENGTH_MINS
private static final int
MAX_SCHED_ENTRIES
private static final int
MIN_SCHED_ENTRIES
private static final int
MAX_PEOPLE
private static final int
STUDENTS_PER_PROF
private final List
people
private final Random
rnd
Constructors Summary
public SchoolCalendarServiceImpl()


    
    generateRandomPeople();
  
Methods Summary
private voidgenerateRandomPeople()

    for (int i = 0; i < MAX_PEOPLE; ++i) {
      Person person = generateRandomPerson();
      people.add(person);
    }
  
private com.google.gwt.sample.dynatable.client.PersongenerateRandomPerson()

    // 1 out of every so many people is a prof.
    //
    if (rnd.nextInt(STUDENTS_PER_PROF) == 1) {
      return generateRandomProfessor();
    } else {
      return generateRandomStudent();
    }
  
private com.google.gwt.sample.dynatable.client.PersongenerateRandomProfessor()

    Professor prof = new Professor();

    String firstName = pickRandomString(FIRST_NAMES);
    String lastName = pickRandomString(LAST_NAMES);
    prof.setName("Dr. " + firstName + " " + lastName);

    String subject = pickRandomString(SUBJECTS);
    prof.setDescription("Professor of " + subject);

    generateRandomSchedule(prof.getTeachingSchedule());

    return prof;
  
private voidgenerateRandomSchedule(com.google.gwt.sample.dynatable.client.Schedule sched)

    int range = MAX_SCHED_ENTRIES - MIN_SCHED_ENTRIES + 1;
    int howMany = MIN_SCHED_ENTRIES + rnd.nextInt(range);

    TimeSlot[] timeSlots = new TimeSlot[howMany];

    for (int i = 0; i < howMany; ++i) {
      int startHrs = 8 + rnd.nextInt(9); // 8 am - 5 pm
      int startMins = 15 * rnd.nextInt(4); // on the hour or some quarter
      int dayOfWeek = 1 + rnd.nextInt(5); // Mon - Fri

      int absStartMins = 60 * startHrs + startMins; // convert to minutes
      int absStopMins = absStartMins + CLASS_LENGTH_MINS;

      timeSlots[i] = new TimeSlot(dayOfWeek, absStartMins, absStopMins);
    }

    Arrays.sort(timeSlots);

    for (int i = 0; i < howMany; ++i) {
      sched.addTimeSlot(timeSlots[i]);
    }
  
private com.google.gwt.sample.dynatable.client.PersongenerateRandomStudent()

    Student student = new Student();

    String firstName = pickRandomString(FIRST_NAMES);
    String lastName = pickRandomString(LAST_NAMES);
    student.setName(firstName + " " + lastName);

    String subject = pickRandomString(SUBJECTS);
    student.setDescription("Majoring in " + subject);

    generateRandomSchedule(student.getClassSchedule());

    return student;
  
public com.google.gwt.sample.dynatable.client.Person[]getPeople(int startIndex, int maxCount)

    int peopleCount = people.size();

    int start = startIndex;
    if (start >= peopleCount) {
      return NO_PEOPLE;
    }

    int end = Math.min(startIndex + maxCount, peopleCount);
    if (start == end) {
      return NO_PEOPLE;
    }

    int resultCount = end - start;
    Person[] results = new Person[resultCount];
    for (int from = start, to = 0; to < resultCount; ++from, ++to) {
      results[to] = (Person) people.get(from);
    }

    return results;
  
protected voidonAfterResponseSerialized(java.lang.String serializedResponse)
Write the serialized response out to stdout. This is a very unusual thing to do, but it allows us to create a static file version of the response without deploying a servlet.

    System.out.println(serializedResponse);
  
private java.lang.StringpickRandomString(java.lang.String[] a)

    int i = rnd.nextInt(a.length);
    return a[i];