1 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 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 people = new ArrayList (); 63 64 private final Random rnd = new Random (3); 65 66 public SchoolCalendarServiceImpl() { 67 generateRandomPeople(); 68 } 69 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] = (Person) people.get(from); 87 } 88 89 return results; 90 } 91 92 97 protected void onAfterResponseSerialized(String serializedResponse) { 98 System.out.println(serializedResponse); 99 } 100 101 private void generateRandomPeople() { 102 for (int i = 0; i < MAX_PEOPLE; ++i) { 103 Person person = generateRandomPerson(); 104 people.add(person); 105 } 106 } 107 108 private Person generateRandomPerson() { 109 if (rnd.nextInt(STUDENTS_PER_PROF) == 1) { 112 return generateRandomProfessor(); 113 } else { 114 return generateRandomStudent(); 115 } 116 } 117 118 private Person generateRandomProfessor() { 119 Professor prof = new Professor(); 120 121 String firstName = pickRandomString(FIRST_NAMES); 122 String lastName = pickRandomString(LAST_NAMES); 123 prof.setName("Dr. " + firstName + " " + lastName); 124 125 String subject = pickRandomString(SUBJECTS); 126 prof.setDescription("Professor of " + subject); 127 128 generateRandomSchedule(prof.getTeachingSchedule()); 129 130 return prof; 131 } 132 133 private void generateRandomSchedule(Schedule sched) { 134 int range = MAX_SCHED_ENTRIES - MIN_SCHED_ENTRIES + 1; 135 int howMany = MIN_SCHED_ENTRIES + rnd.nextInt(range); 136 137 TimeSlot[] timeSlots = new TimeSlot[howMany]; 138 139 for (int i = 0; i < howMany; ++i) { 140 int startHrs = 8 + rnd.nextInt(9); int startMins = 15 * rnd.nextInt(4); int dayOfWeek = 1 + rnd.nextInt(5); 144 int absStartMins = 60 * startHrs + startMins; int absStopMins = absStartMins + CLASS_LENGTH_MINS; 146 147 timeSlots[i] = new TimeSlot(dayOfWeek, absStartMins, absStopMins); 148 } 149 150 Arrays.sort(timeSlots); 151 152 for (int i = 0; i < howMany; ++i) { 153 sched.addTimeSlot(timeSlots[i]); 154 } 155 } 156 157 private Person generateRandomStudent() { 158 Student student = new Student(); 159 160 String firstName = pickRandomString(FIRST_NAMES); 161 String lastName = pickRandomString(LAST_NAMES); 162 student.setName(firstName + " " + lastName); 163 164 String subject = pickRandomString(SUBJECTS); 165 student.setDescription("Majoring in " + subject); 166 167 generateRandomSchedule(student.getClassSchedule()); 168 169 return student; 170 } 171 172 private String pickRandomString(String [] a) { 173 int i = rnd.nextInt(a.length); 174 return a[i]; 175 } 176 } 177 | Popular Tags |