KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > addrbook > DayPlan


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.addrbook;
34
35 import java.util.Date JavaDoc;
36
37 import java.sql.SQLException JavaDoc;
38
39 import com.knowgate.debug.DebugFile;
40 import com.knowgate.jdc.JDCConnection;
41 import com.knowgate.dataobjs.DB;
42 import com.knowgate.dataobjs.DBBind;
43 import com.knowgate.dataobjs.DBSubset;
44
45 /**
46  * <p>A bidimensional array with daily scheduled meeting in quarters of an hour.</p>
47  * @author Sergio Montoro Ten
48  * @version 1.0
49  */

50
51 /*
52    This class stores internally a bidimensional matrix that associates daily
53    time slices with meetings having place at each slice. The association is
54    stablished by first loading meetings for a given date and then building a
55    list for each slice witch entries point to meetings having place at that
56    slice.
57    Lets say that there are four meetings in a day, one from 9:30 to 10:30
58    [nš 0], another from 10:00 to 10:30 [nš 1], another from 11:00 to 12:00
59    [nš 2] and a last one from 11:45 to 12:30 [nš 3].
60    The internal array will then have this form:
61
62    ... [slots from 00:00 up to 9:30]
63    09:30 -----------------------------------------------------------------------
64         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
65            0 (empty) (empty) (empty) (empty) (empty) (empty) (empty)
66    09:45 -----------------------------------------------------------------------
67         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
68            0 (empty) (empty) (empty) (empty) (empty) (empty) (empty)
69    10:00 -----------------------------------------------------------------------
70         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
71            0 1 (empty) (empty) (empty) (empty) (empty) (empty)
72    10:15 -----------------------------------------------------------------------
73         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
74            0 1 (empty) (empty) (empty) (empty) (empty) (empty)
75    10:30 -----------------------------------------------------------------------
76         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
77         (empty) (empty) (empty) (empty) (empty) (empty) (empty) (empty)
78    11:00 -----------------------------------------------------------------------
79         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
80            2 (empty) (empty) (empty) (empty) (empty) (empty) (empty)
81    11:15 -----------------------------------------------------------------------
82         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
83            2 (empty) (empty) (empty) (empty) (empty) (empty) (empty)
84    11:30 -----------------------------------------------------------------------
85         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
86            2 (empty) (empty) (empty) (empty) (empty) (empty) (empty)
87    11:45 -----------------------------------------------------------------------
88         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
89            2 3 (empty) (empty) (empty) (empty) (empty) (empty)
90    12:00 -----------------------------------------------------------------------
91         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
92            3 (empty) (empty) (empty) (empty) (empty) (empty) (empty)
93    12:15 -----------------------------------------------------------------------
94         slot 0 | slot 1 | slot 2 | slot 3 | slot 4 | slot 5 | slot 6 | slot 7
95            3 (empty) (empty) (empty) (empty) (empty) (empty) (empty)
96
97    ... [slots from 12:30:00 up to 23:45]
98
99    This implies that there is a maximum of 8 concurrent meetings per slice.
100    If there were more concurrent meeting the class shall not raise any error
101    but will simply not show all of them.
102 */

103
104 public class DayPlan {
105   private int aMeetings[][];
106   private DBSubset oMeetings;
107   private Meeting oMeeting;
108
109   private final int MaxSlots = 8; // Maximum allowed concurrent meetings per slot
110
private final int MaxSlices = 96; // Slices per day 96=24/4 -> slices of 15 mins.
111
private final int EmptySlot = -1;
112   private final long SliceLapsus = (24*3600*1000)/MaxSlices;
113
114   public DayPlan() {
115     oMeeting = new Meeting();
116     aMeetings = new int[MaxSlots][MaxSlices];
117     for (int slice=0; slice<MaxSlices; slice++)
118       for (int slot=0; slot<MaxSlots; slot++)
119         aMeetings[slot][slice] = EmptySlot;
120   } // DayPlan
121

122   // ----------------------------------------------------------
123

124   /**
125    * <p>Load scheduled meetings for a given Fellow.</p>
126    * @param oConn Database Connection
127    * @param sFellowId Fellow Unique Identifier
128    * @param dtToday Date for witch meeting are to be retrieved
129    * @throws SQLException
130    * @see {@link Fellow}
131    */

132   public void load(JDCConnection oConn, String JavaDoc sFellowId, java.util.Date JavaDoc dtToday) throws SQLException JavaDoc {
133     if (DebugFile.trace) {
134       DebugFile.writeln("Begin DayPlan.load([Connection], " + sFellowId + "," + dtToday.toString() + ")");
135       DebugFile.incIdent();
136     }
137
138     long lToday = dtToday.getTime();
139     java.util.Date JavaDoc zero = new java.util.Date JavaDoc(lToday);
140     java.util.Date JavaDoc four = new java.util.Date JavaDoc(lToday);
141
142     zero.setHours(0) ; zero.setMinutes(0) ; zero.setSeconds(0) ;
143     zero.setTime((zero.getTime()/1000l)*1000l); // truncar los milisegundos
144

145     four.setHours(23); four.setMinutes(59); four.setSeconds(59);
146
147     oMeetings = new DBSubset(DB.k_meetings + " m," + DB.k_x_meeting_fellow + " f",
148                               "m." + DB.gu_meeting + ",m." + DB.gu_fellow + ",m." + DB.dt_start + ",m." + DB.dt_end + ",m." + DB.bo_private + ",m." + DB.df_before + ",m." + DB.tp_meeting + ",m." + DB.tx_meeting + ",m." + DB.de_meeting,
149                               "m." + DB.gu_meeting +"=f." + DB.gu_meeting + " AND f." + DB.gu_fellow + "=? AND m." + DB.dt_start + ">=" + DBBind.escape(zero, "ts") + " AND m." + DB.dt_start + "< " + DBBind.escape(four, "ts") + " ORDER BY m."+ DB.dt_start, 8);
150
151     int mCount = oMeetings.load(oConn, new Object JavaDoc[] { sFellowId });
152
153     if (DebugFile.trace) DebugFile.writeln(String.valueOf(mCount) + " meetings found");
154
155     long dtStart, dtEnd, dtSliceBegin, dtZero = zero.getTime();
156
157     for (int meeting=0; meeting<mCount; meeting++) {
158       dtStart = oMeetings.getDate(2,meeting).getTime();
159       dtEnd = oMeetings.getDate(3,meeting).getTime();
160
161       dtSliceBegin = dtZero;
162       for (int slice=0; slice<MaxSlices; slice++) {
163         if ((dtStart>=dtSliceBegin && dtStart<dtSliceBegin+SliceLapsus) ||
164             (dtStart<dtSliceBegin && dtEnd>dtSliceBegin+SliceLapsus))
165           for (int slot=0; slot<MaxSlots; slot++)
166             if (EmptySlot==aMeetings[slot][slice]) {
167               if (DebugFile.trace) {
168                 DebugFile.writeln("set slot[" + String.valueOf(slot) + "][" + String.valueOf(slice) + "] to meeting " + String.valueOf(meeting) + " " + oMeetings.getStringNull(7,meeting,""));
169                 DebugFile.writeln("dtStart=" + new Date JavaDoc(dtStart).toString() + "(" + String.valueOf(dtStart) + "ms)");
170                 DebugFile.writeln("dtEnd=" + new Date JavaDoc(dtEnd).toString() + "(" + String.valueOf(dtEnd) + "ms)");
171                 DebugFile.writeln("dtSliceBegin=" + new Date JavaDoc(dtSliceBegin).toString() + "(" + String.valueOf(dtSliceBegin) + "ms)");
172                 DebugFile.writeln("dtSliceNext=" + new Date JavaDoc(dtSliceBegin+SliceLapsus).toString() + "(" + String.valueOf(dtSliceBegin+SliceLapsus) + "ms)");
173               }
174               aMeetings[slot][slice] = meeting;
175               break;
176             } // fi (aMeetings[slot][slice])
177
dtSliceBegin += SliceLapsus;
178       } // next (slice)
179
} // next(meeting)
180

181     if (DebugFile.trace) {
182       DebugFile.decIdent();
183       DebugFile.writeln("End DayPlan.load()");
184     }
185   } // load
186

187   // ----------------------------------------------------------
188

189   /**
190    * <p>Slice count per day.</p>
191    * <p>96 is the default and equals a day divided in 15 minutes slices</p>
192    * @return Maximum number of slices per day
193    */

194   public int sliceCount() {
195     return MaxSlices;
196   }
197
198   // ----------------------------------------------------------
199

200   /**
201    * @return Maximum number of allowed concurrent meetings per day slice
202    */

203   public int slotsPerSlice() {
204     return MaxSlots;
205   }
206
207   // ----------------------------------------------------------
208

209   /**
210    * <p>Get count of concurrent meetings at a given slice</p>
211    * @param slice [0...sliceCount()-1]
212    * @throws ArrayIndexOutOfBoundsException If slice<0 or slice>=sliceCount()
213    */

214
215   public int concurrentMeetings(int slice) throws ArrayIndexOutOfBoundsException JavaDoc {
216     int slots = 0;
217
218     do {
219       if (EmptySlot!=aMeetings[slots][slice])
220         slots++;
221       else
222         break;
223     }
224     while (slots < MaxSlots);
225
226     return slots;
227   } // concurrentMeetings
228

229   // ----------------------------------------------------------
230

231   /**
232    * <p>Get meeting information</p>
233    * <p>Each meeting is asoociated with one or more day slices by having a list
234    * at the slice that point to every meeting taking place on the slice.</p>
235    * <p>Thus for retriving a meeting both the slice number and the relative ordinal
236    * position of the meeting at the slice are needed.</p>
237    * @param slice [0...sliceCount()-1]
238    * @param slot [0...slotsPerSlice()-1]
239    * @throws ArrayIndexOutOfBoundsException If slice<0 or slice>=sliceCount() or slot<0 or slot>=slotsPerSlice()
240    * @return {@link Meeting} or <b>null</b> if no meeting was found at the given (slice,slot) pair.
241    */

242   public Meeting getMeeting(int slice, int slot) throws ArrayIndexOutOfBoundsException JavaDoc {
243     if (DebugFile.trace) {
244       DebugFile.writeln("Begin DayPlan.getMeeting(" + String.valueOf(slice) + "," + String.valueOf(slot) + ")");
245     }
246
247     if (slice>=MaxSlices || slot>=MaxSlots) return null;
248
249     int iMeeting = aMeetings[slot][slice];
250
251     if (DebugFile.trace) DebugFile.writeln("iMeeting=" + String.valueOf(iMeeting));
252
253     if (-1==iMeeting) return null;
254
255     oMeeting.clear();
256
257     if (DebugFile.trace) DebugFile.writeln("Meeting object cleared");
258
259     oMeeting.put(DB.gu_meeting, oMeetings.getString(0,iMeeting));
260     oMeeting.put(DB.gu_fellow, oMeetings.getString(1,iMeeting));
261     oMeeting.put(DB.dt_start, oMeetings.getDate (2,iMeeting));
262     oMeeting.put(DB.dt_end, oMeetings.getDate (3,iMeeting));
263
264     if (DebugFile.trace)
265       if (oMeetings.getShort (4,iMeeting)!=(short)0)
266         DebugFile.writeln("meeting is private");
267
268     oMeeting.put(DB.bo_private, oMeetings.getShort (4,iMeeting));
269
270     if (!oMeetings.isNull(5,iMeeting))
271       oMeeting.put(DB.df_before, oMeetings.getInt (5,iMeeting));
272     if (!oMeetings.isNull(6,iMeeting))
273       oMeeting.put(DB.tp_meeting, oMeetings.getString(6,iMeeting));
274     oMeeting.put(DB.tx_meeting, oMeetings.getStringNull(7,iMeeting,""));
275     oMeeting.put(DB.de_meeting, oMeetings.getStringNull(8,iMeeting,""));
276
277     if (DebugFile.trace) {
278       DebugFile.writeln("End DayPlan.getMeeting() : " + oMeetings.getString(0,iMeeting));
279     }
280
281     return oMeeting;
282   } // getMeeting()
283

284   // ----------------------------------------------------------
285

286   /**
287    * <p>Lookup a meeting given its unique identifier</p>
288    * @param sMeeting Meeting Unique Identifier
289    * @return {@link Meeting} or <b>null</b> if no meeting was found with given identifier.
290    */

291   public Meeting seekMeeting(String JavaDoc sMeeting) {
292     int iMeetings = oMeetings.getRowCount();
293
294     for (int m=0; m<iMeetings; m++) {
295       if (sMeeting.equals(oMeetings.getString(0, m))) {
296         oMeeting.clear();
297
298         oMeeting.put(DB.gu_meeting, oMeetings.getString(0,m));
299         oMeeting.put(DB.gu_fellow, oMeetings.getString(1,m));
300         oMeeting.put(DB.dt_start, oMeetings.getDate (2,m));
301         oMeeting.put(DB.dt_end, oMeetings.getDate (3,m));
302         oMeeting.put(DB.bo_private, oMeetings.getShort (4,m));
303         if (!oMeetings.isNull(5,m))
304           oMeeting.put(DB.df_before, oMeetings.getInt (5,m));
305         if (!oMeetings.isNull(6,m))
306           oMeeting.put(DB.tp_meeting, oMeetings.getString(6,m));
307         oMeeting.put(DB.tx_meeting, oMeetings.getStringNull(7,m,""));
308         oMeeting.put(DB.de_meeting, oMeetings.getStringNull(8,m,""));
309
310         return oMeeting;
311       }
312     }
313     return null;
314   } // seekMeeting
315
} // DayPlay
Popular Tags