1 /* 2 * Copyright 2007 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.google.gwt.sample.dynatable.server; 17 18 import com.google.gwt.sample.dynatable.client.Person; 19 import com.google.gwt.sample.dynatable.client.Professor; 20 import com.google.gwt.sample.dynatable.client.Schedule; 21 import com.google.gwt.sample.dynatable.client.SchoolCalendarService; 22 import com.google.gwt.sample.dynatable.client.Student; 23 import com.google.gwt.sample.dynatable.client.TimeSlot; 24 import com.google.gwt.user.server.rpc.RemoteServiceServlet; 25 26 import java.util.ArrayList; 27 import java.util.Arrays; 28 import java.util.List; 29 import java.util.Random; 30 31 /** 32 * The implemenation of the RPC service which runs on the server. 33 */ 34 public class SchoolCalendarServiceImpl extends RemoteServiceServlet implements 35 SchoolCalendarService { 36 37 private static final String[] FIRST_NAMES = new String[] { 38 "Inman", "Sally", "Omar", "Teddy", "Jimmy", "Cathy", "Barney", "Fred", 39 "Eddie", "Carlos"}; 40 41 private static final String[] LAST_NAMES = new String[] { 42 "Smith", "Jones", "Epps", "Gibbs", "Webber", "Blum", "Mendez", 43 "Crutcher", "Needler", "Wilson", "Chase", "Edelstein"}; 44 45 private static final String[] SUBJECTS = new String[] { 46 "Chemistry", "Phrenology", "Geometry", "Underwater Basket Weaving", 47 "Basketball", "Computer Science", "Statistics", "Materials Engineering", 48 "English Literature", "Geology"}; 49 50 private static final Person[] NO_PEOPLE = new Person[0]; 51 52 private static final int CLASS_LENGTH_MINS = 50; 53 54 private static final int MAX_SCHED_ENTRIES = 5; 55 56 private static final int MIN_SCHED_ENTRIES = 1; 57 58 private static final int MAX_PEOPLE = 100; 59 60 private static final int STUDENTS_PER_PROF = 5; 61 62 private final List<Person> people = new ArrayList<Person>(); 63 64 private final Random rnd = new Random(3); 65 SchoolCalendarServiceImpl()66 public SchoolCalendarServiceImpl() { 67 generateRandomPeople(); 68 } 69 getPeople(int startIndex, int maxCount)70 public Person[] getPeople(int startIndex, int maxCount) { 71 int peopleCount = people.size(); 72 73 int start = startIndex; 74 if (start >= peopleCount) { 75 return NO_PEOPLE; 76 } 77 78 int end = Math.min(startIndex + maxCount, peopleCount); 79 if (start == end) { 80 return NO_PEOPLE; 81 } 82 83 int resultCount = end - start; 84 Person[] results = new Person[resultCount]; 85 for (int from = start, to = 0; to < resultCount; ++from, ++to) { 86 results[to] = people.get(from); 87 } 88 89 return results; 90 } 91 92 /** 93 * Write the serialized response out to stdout. This is a very unusual thing 94 * to do, but it allows us to create a static file version of the response 95 * without deploying a servlet. 96 */ 97 @Override onAfterResponseSerialized(String serializedResponse)98 protected void onAfterResponseSerialized(String serializedResponse) { 99 System.out.println(serializedResponse); 100 } 101 generateRandomPeople()102 private void generateRandomPeople() { 103 for (int i = 0; i < MAX_PEOPLE; ++i) { 104 Person person = generateRandomPerson(); 105 people.add(person); 106 } 107 } 108 generateRandomPerson()109 private Person generateRandomPerson() { 110 // 1 out of every so many people is a prof. 111 // 112 if (rnd.nextInt(STUDENTS_PER_PROF) == 1) { 113 return generateRandomProfessor(); 114 } else { 115 return generateRandomStudent(); 116 } 117 } 118 generateRandomProfessor()119 private Person generateRandomProfessor() { 120 Professor prof = new Professor(); 121 122 String firstName = pickRandomString(FIRST_NAMES); 123 String lastName = pickRandomString(LAST_NAMES); 124 prof.setName("Dr. " + firstName + " " + lastName); 125 126 String subject = pickRandomString(SUBJECTS); 127 prof.setDescription("Professor of " + subject); 128 129 generateRandomSchedule(prof.getTeachingSchedule()); 130 131 return prof; 132 } 133 generateRandomSchedule(Schedule sched)134 private void generateRandomSchedule(Schedule sched) { 135 int range = MAX_SCHED_ENTRIES - MIN_SCHED_ENTRIES + 1; 136 int howMany = MIN_SCHED_ENTRIES + rnd.nextInt(range); 137 138 TimeSlot[] timeSlots = new TimeSlot[howMany]; 139 140 for (int i = 0; i < howMany; ++i) { 141 int startHrs = 8 + rnd.nextInt(9); // 8 am - 5 pm 142 int startMins = 15 * rnd.nextInt(4); // on the hour or some quarter 143 int dayOfWeek = 1 + rnd.nextInt(5); // Mon - Fri 144 145 int absStartMins = 60 * startHrs + startMins; // convert to minutes 146 int absStopMins = absStartMins + CLASS_LENGTH_MINS; 147 148 timeSlots[i] = new TimeSlot(dayOfWeek, absStartMins, absStopMins); 149 } 150 151 Arrays.sort(timeSlots); 152 153 for (int i = 0; i < howMany; ++i) { 154 sched.addTimeSlot(timeSlots[i]); 155 } 156 } 157 generateRandomStudent()158 private Person generateRandomStudent() { 159 Student student = new Student(); 160 161 String firstName = pickRandomString(FIRST_NAMES); 162 String lastName = pickRandomString(LAST_NAMES); 163 student.setName(firstName + " " + lastName); 164 165 String subject = pickRandomString(SUBJECTS); 166 student.setDescription("Majoring in " + subject); 167 168 generateRandomSchedule(student.getClassSchedule()); 169 170 return student; 171 } 172 pickRandomString(String[] a)173 private String pickRandomString(String[] a) { 174 int i = rnd.nextInt(a.length); 175 return a[i]; 176 } 177 } 178