001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.sample.dynatable.server;
017:
018: import com.google.gwt.sample.dynatable.client.Person;
019: import com.google.gwt.sample.dynatable.client.Professor;
020: import com.google.gwt.sample.dynatable.client.Schedule;
021: import com.google.gwt.sample.dynatable.client.SchoolCalendarService;
022: import com.google.gwt.sample.dynatable.client.Student;
023: import com.google.gwt.sample.dynatable.client.TimeSlot;
024: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
025:
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.List;
029: import java.util.Random;
030:
031: /**
032: * The implemenation of the RPC service which runs on the server.
033: */
034: public class SchoolCalendarServiceImpl extends RemoteServiceServlet
035: implements SchoolCalendarService {
036:
037: private static final String[] FIRST_NAMES = new String[] { "Inman",
038: "Sally", "Omar", "Teddy", "Jimmy", "Cathy", "Barney",
039: "Fred", "Eddie", "Carlos" };
040:
041: private static final String[] LAST_NAMES = new String[] { "Smith",
042: "Jones", "Epps", "Gibbs", "Webber", "Blum", "Mendez",
043: "Crutcher", "Needler", "Wilson", "Chase", "Edelstein" };
044:
045: private static final String[] SUBJECTS = new String[] {
046: "Chemistry", "Phrenology", "Geometry",
047: "Underwater Basket Weaving", "Basketball",
048: "Computer Science", "Statistics", "Materials Engineering",
049: "English Literature", "Geology" };
050:
051: private static final Person[] NO_PEOPLE = new Person[0];
052:
053: private static final int CLASS_LENGTH_MINS = 50;
054:
055: private static final int MAX_SCHED_ENTRIES = 5;
056:
057: private static final int MIN_SCHED_ENTRIES = 1;
058:
059: private static final int MAX_PEOPLE = 100;
060:
061: private static final int STUDENTS_PER_PROF = 5;
062:
063: private final List<Person> people = new ArrayList<Person>();
064:
065: private final Random rnd = new Random(3);
066:
067: public SchoolCalendarServiceImpl() {
068: generateRandomPeople();
069: }
070:
071: public Person[] getPeople(int startIndex, int maxCount) {
072: int peopleCount = people.size();
073:
074: int start = startIndex;
075: if (start >= peopleCount) {
076: return NO_PEOPLE;
077: }
078:
079: int end = Math.min(startIndex + maxCount, peopleCount);
080: if (start == end) {
081: return NO_PEOPLE;
082: }
083:
084: int resultCount = end - start;
085: Person[] results = new Person[resultCount];
086: for (int from = start, to = 0; to < resultCount; ++from, ++to) {
087: results[to] = people.get(from);
088: }
089:
090: return results;
091: }
092:
093: /**
094: * Write the serialized response out to stdout. This is a very unusual thing
095: * to do, but it allows us to create a static file version of the response
096: * without deploying a servlet.
097: */
098: @Override
099: protected void onAfterResponseSerialized(String serializedResponse) {
100: System.out.println(serializedResponse);
101: }
102:
103: private void generateRandomPeople() {
104: for (int i = 0; i < MAX_PEOPLE; ++i) {
105: Person person = generateRandomPerson();
106: people.add(person);
107: }
108: }
109:
110: private Person generateRandomPerson() {
111: // 1 out of every so many people is a prof.
112: //
113: if (rnd.nextInt(STUDENTS_PER_PROF) == 1) {
114: return generateRandomProfessor();
115: } else {
116: return generateRandomStudent();
117: }
118: }
119:
120: private Person generateRandomProfessor() {
121: Professor prof = new Professor();
122:
123: String firstName = pickRandomString(FIRST_NAMES);
124: String lastName = pickRandomString(LAST_NAMES);
125: prof.setName("Dr. " + firstName + " " + lastName);
126:
127: String subject = pickRandomString(SUBJECTS);
128: prof.setDescription("Professor of " + subject);
129:
130: generateRandomSchedule(prof.getTeachingSchedule());
131:
132: return prof;
133: }
134:
135: private void generateRandomSchedule(Schedule sched) {
136: int range = MAX_SCHED_ENTRIES - MIN_SCHED_ENTRIES + 1;
137: int howMany = MIN_SCHED_ENTRIES + rnd.nextInt(range);
138:
139: TimeSlot[] timeSlots = new TimeSlot[howMany];
140:
141: for (int i = 0; i < howMany; ++i) {
142: int startHrs = 8 + rnd.nextInt(9); // 8 am - 5 pm
143: int startMins = 15 * rnd.nextInt(4); // on the hour or some quarter
144: int dayOfWeek = 1 + rnd.nextInt(5); // Mon - Fri
145:
146: int absStartMins = 60 * startHrs + startMins; // convert to minutes
147: int absStopMins = absStartMins + CLASS_LENGTH_MINS;
148:
149: timeSlots[i] = new TimeSlot(dayOfWeek, absStartMins,
150: absStopMins);
151: }
152:
153: Arrays.sort(timeSlots);
154:
155: for (int i = 0; i < howMany; ++i) {
156: sched.addTimeSlot(timeSlots[i]);
157: }
158: }
159:
160: private Person generateRandomStudent() {
161: Student student = new Student();
162:
163: String firstName = pickRandomString(FIRST_NAMES);
164: String lastName = pickRandomString(LAST_NAMES);
165: student.setName(firstName + " " + lastName);
166:
167: String subject = pickRandomString(SUBJECTS);
168: student.setDescription("Majoring in " + subject);
169:
170: generateRandomSchedule(student.getClassSchedule());
171:
172: return student;
173: }
174:
175: private String pickRandomString(String[] a) {
176: int i = rnd.nextInt(a.length);
177: return a[i];
178: }
179: }
|