KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > dynatable > server > SchoolCalendarServiceImpl


1 /*
2  * Copyright 2006 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 JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Random JavaDoc;
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 JavaDoc[] FIRST_NAMES = new String JavaDoc[] {
38       "Inman", "Sally", "Omar", "Teddy", "Jimmy", "Cathy", "Barney", "Fred",
39       "Eddie", "Carlos"};
40
41   private static final String JavaDoc[] LAST_NAMES = new String JavaDoc[] {
42       "Smith", "Jones", "Epps", "Gibbs", "Webber", "Blum", "Mendez",
43       "Crutcher", "Needler", "Wilson", "Chase", "Edelstein"};
44
45   private static final String JavaDoc[] SUBJECTS = new String JavaDoc[] {
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 JavaDoc people = new ArrayList JavaDoc();
63
64   private final Random JavaDoc rnd = new Random JavaDoc(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   /**
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   protected void onAfterResponseSerialized(String JavaDoc 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     // 1 out of every so many people is a prof.
110
//
111
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 JavaDoc firstName = pickRandomString(FIRST_NAMES);
122     String JavaDoc lastName = pickRandomString(LAST_NAMES);
123     prof.setName("Dr. " + firstName + " " + lastName);
124
125     String JavaDoc 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); // 8 am - 5 pm
141
int startMins = 15 * rnd.nextInt(4); // on the hour or some quarter
142
int dayOfWeek = 1 + rnd.nextInt(5); // Mon - Fri
143

144       int absStartMins = 60 * startHrs + startMins; // convert to minutes
145
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 JavaDoc firstName = pickRandomString(FIRST_NAMES);
161     String JavaDoc lastName = pickRandomString(LAST_NAMES);
162     student.setName(firstName + " " + lastName);
163
164     String JavaDoc subject = pickRandomString(SUBJECTS);
165     student.setDescription("Majoring in " + subject);
166
167     generateRandomSchedule(student.getClassSchedule());
168
169     return student;
170   }
171
172   private String JavaDoc pickRandomString(String JavaDoc[] a) {
173     int i = rnd.nextInt(a.length);
174     return a[i];
175   }
176 }
177
Popular Tags