1 package de.jaret.examples.timebars.calendar.model;
2
3 import de.jaret.util.date.IntervalImpl;
4 import de.jaret.util.date.JaretDate;
5
6 /***
7 * Extension of the interval class, always projecting in the same base date, thus the time is relevant.
8 *
9 * @author Peter Kliem
10 * @version $Id: Appointment.java 705 2008-01-25 22:51:54Z kliem $
11 */
12 public class Appointment extends IntervalImpl {
13 /*** enumeration for the possible states. */
14 public enum Status {
15 FREE, BOOKED, TENTATIVE, ABSENT
16 };
17
18 /*** the real begin including the date. */
19 protected JaretDate _realBegin;
20 /*** the real end including the date. */
21 protected JaretDate _realEnd;
22
23 protected String _text;
24 protected String _description;
25 protected String _id;
26 protected Status _status = Status.BOOKED;
27 /*** true if the event is part of a series. */
28 protected boolean _recurring = false;
29 protected boolean _editable = true;
30
31 /*** extension object to be used by the synchronizers. */
32 protected Object _extension;
33
34 /*** true whenever the appointment (realBegin/end) spans a whole day or more than one day. */
35 protected boolean _spansMultipleDays = false;
36
37 /*** the synchronizer id caring for this appointment. */
38 protected String _synchronizerId;
39
40 /*** date the appointment has been checked against its source the last time. */
41 protected JaretDate _lastChecked;
42 /***
43 * id of the last changing logic: either a synchronizer or the calendar plugin id. changer always defaults to the
44 * plugin, synchronizers have to set it after doing an update.
45 */
46 protected String _lastChangerId;
47 /*** timestamp of the lst change. */
48 protected JaretDate _lastChangeDate;
49
50 /***
51 * Construct a basic appointment. Begin and end have to be set seperately!
52 *
53 * @param id id to use
54 */
55 public Appointment(String id) {
56 _id = id;
57 }
58
59 public Appointment(JaretDate date, int h, int m, double durH, String text) {
60 JaretDate begin = date.copy().setTime(h, m, 0);
61 setRealBegin(begin);
62
63 JaretDate end = begin.copy().advanceHours(durH);
64 setRealEnd(end);
65
66 _text = text;
67 checkSpanMultiple();
68 changed();
69 }
70
71 public Appointment(JaretDate begin, JaretDate end, String text) {
72 setRealBegin(begin.copy());
73 setRealEnd(end.copy());
74 _text = text;
75 checkSpanMultiple();
76 changed();
77 }
78
79 /***
80 * Create a copy of the appointment. This method should be overriden by extending classes.
81 *
82 * @return a copy of the interval that does not include the meta information (synchronizer, changer, timestamps)
83 */
84 public Appointment copy() {
85 Appointment a = new Appointment(_realBegin, 0, 0, 0, _text);
86 a.setRealBegin(_realBegin.copy());
87 a.setRealEnd(_realEnd.copy());
88 a.setStatus(_status);
89 a.setEditable(_editable);
90 a.setRecurring(_recurring);
91 a.setDescription(_description);
92 a.setSynchronizerId(_synchronizerId);
93 return a;
94 }
95
96 protected void changed() {
97
98
99
100 JaretDate d = new JaretDate();
101 if (_lastChangeDate == null || d.compareTo(_lastChangeDate) > 0) {
102 _lastChangeDate = d;
103 } else {
104 _lastChangeDate.advanceMinutes(1);
105 }
106
107 }
108
109 public JaretDate getRealBegin() {
110 return _realBegin;
111 }
112
113 public void setRealBegin(JaretDate realBegin) {
114 JaretDate oldVal = _realBegin;
115 _realBegin = realBegin;
116 setBegin(correct(realBegin));
117 checkSpanMultiple();
118
119 firePropertyChange(PROP_BEGIN, oldVal, _realBegin);
120 changed();
121 }
122
123 public JaretDate getRealEnd() {
124 return _realEnd;
125 }
126
127 public void setRealEnd(JaretDate realEnd) {
128 JaretDate oldVal = _realEnd;
129 _realEnd = realEnd;
130 setEnd(correct(realEnd));
131 checkSpanMultiple();
132 changed();
133
134 firePropertyChange(PROP_END, oldVal, _realEnd);
135 }
136
137 public void setBegin(JaretDate begin) {
138 _realBegin = setTime(_realBegin, begin);
139 super.setBegin(begin);
140 changed();
141 }
142
143 public void setEnd(JaretDate end) {
144 _realEnd = setTime(_realEnd, end);
145 super.setEnd(end);
146 changed();
147 }
148
149 private JaretDate setTime(JaretDate real, JaretDate date) {
150 JaretDate d = real.copy();
151 d.setTime(date.getHours(), date.getMinutes(), date.getSeconds());
152 return d;
153 }
154
155 private void checkSpanMultiple() {
156 if (_realBegin != null && _realEnd != null) {
157 if (_realEnd.diffMinutes(_realBegin) >= 60 * 24) {
158 setSpansMultipleDays(true);
159 } else {
160 setSpansMultipleDays(false);
161 }
162 }
163
164 }
165
166 private JaretDate correct(JaretDate realBegin) {
167 return CalendarModel.BASEDATE.copy().setTime(realBegin.getHours(), realBegin.getMinutes(),
168 realBegin.getSeconds());
169 }
170
171 public String getText() {
172 return _text;
173 }
174
175 public void setText(String text) {
176 String oldVal = _text;
177 _text = text;
178 changed();
179 firePropertyChange("Text", oldVal, text);
180 }
181
182
183
184
185
186
187 public String getDescription() {
188 return _description;
189 }
190
191 public void setDescription(String description) {
192 String oldVal = _description;
193 _description = description;
194 changed();
195 firePropertyChange("Description", oldVal, description);
196 }
197
198 public String getId() {
199 return _id;
200 }
201
202 public void setId(String id) {
203 _id = id;
204 changed();
205 }
206
207 public String getSynchronizerId() {
208 return _synchronizerId;
209 }
210
211 public void setSynchronizerId(String synchronizerId) {
212 _synchronizerId = synchronizerId;
213 }
214
215 public JaretDate getLastChecked() {
216 return _lastChecked;
217 }
218
219 public void setLastChecked(JaretDate lastChecked) {
220 _lastChecked = lastChecked;
221 }
222
223 public String getLastChangerId() {
224 return _lastChangerId;
225 }
226
227 public void setLastChangerId(String lastChangerId) {
228 _lastChangerId = lastChangerId;
229 }
230
231 public JaretDate getLastChangeDate() {
232 return _lastChangeDate;
233 }
234
235 public void setLastChangeDate(JaretDate lastChangeDate) {
236 _lastChangeDate = lastChangeDate;
237 }
238
239 public Status getStatus() {
240 return _status;
241 }
242
243 public void setStatus(Status status) {
244 _status = status;
245 }
246
247 public boolean isRecurring() {
248 return _recurring;
249 }
250
251 public void setRecurring(boolean recurring) {
252 _recurring = recurring;
253 }
254
255 public boolean isEditable() {
256 return _editable;
257 }
258
259 public void setEditable(boolean editable) {
260 _editable = editable;
261 }
262
263 public boolean isSpansMultipleDays() {
264 return _spansMultipleDays;
265 }
266
267 protected void setSpansMultipleDays(boolean spansMultipleDays) {
268 if (_spansMultipleDays != spansMultipleDays) {
269 _spansMultipleDays = spansMultipleDays;
270 firePropertyChange("SpansMultipleDays", !spansMultipleDays, spansMultipleDays);
271 }
272 }
273
274 /***
275 * Check whether the appointment is a whole day event.
276 *
277 * @return true if begin and end time are 00:00
278 */
279 public boolean isWholeDayAppointment() {
280 JaretDate d = new JaretDate();
281 d.setTime(0, 0, 0);
282 return getRealBegin().compareTimeTo(d) == 0 && getRealEnd().compareTimeTo(d) == 0;
283 }
284
285 public Object getExtension() {
286 return _extension;
287 }
288
289 public void setExtension(Object extension) {
290 _extension = extension;
291 }
292
293 /***
294 * Create a string representation suitable for display.
295 *
296 * @return string representation
297 */
298 public String toDisplayString() {
299 StringBuilder buf = new StringBuilder();
300 buf.append(getText());
301 buf.append("|");
302 buf.append(getRealBegin().toDisplayString());
303 buf.append("--");
304 buf.append(getRealEnd().toDisplayString());
305
306 return buf.toString();
307 }
308
309 }