1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package inc.che.icalgrabber;
19
20 import java.util.Date;
21 import java.util.GregorianCalendar;
22 import org.apache.commons.lang.StringUtils;
23
24 /***
25 * Base interface for string convertions.
26 */
27 interface StringConverter {
28 /***
29 * converts a string.
30 *@param string input string
31 *@return converted string
32 */
33 String convert(String string);
34 }
35 /***
36 * Converter for ical Format.
37 */
38 class ICalConverter implements StringConverter {
39 /***
40 * converts a string.
41 *@param string input string
42 *@return converted string
43 */
44 public String convert(String string) {
45 string = StringUtils.replace(string, ",", "//,");
46 return string;
47 }
48 }
49
50 /***
51 * Class represents an ical Event.
52 *
53 * @author <a href="steve.mcmee@shadoplway-online.de">Steve McMee</a>
54 *
55 */
56 public class Event {
57 /***
58 * id of the event.
59 */
60 private String id;
61 /***
62 * summary of the event.
63 */
64 private String summary = "";
65 /***
66 * description of the event.
67 */
68 private String description = "";
69 /***
70 * location of the event.
71 */
72 private String location = "";
73 /***
74 * startDate of the event.
75 */
76 private Date startDate;
77 /***
78 * endDate of the event.
79 */
80 private Date endDate;
81 /***
82 * startTime of the event.
83 */
84 private Date startTime;
85 /***
86 * endTime of the event.
87 */
88 private Date endTime;
89 /***
90 * fixed year of the event.
91 */
92 private String year;
93 /***
94 * converter.
95 */
96 private StringConverter converter;
97 /***
98 * Constructor for Event.
99 */
100 public Event() {
101 }
102 /***
103 * get the Description.
104 * @return description
105 */
106 public String getDescription() {
107 return converter != null ? converter.convert(description) : description;
108 }
109 /***
110 ** get the End date.
111 * @return end
112 */
113 public Date getEndDate() {
114 return endDate;
115 }
116 /***
117 * get the end Time.
118 * @return end
119 */
120 public Date getEndTime() {
121 return endTime;
122 }
123 /***
124 ** get the End date in ical Format.
125 * @return end
126 */
127 public String getIcalEnd() {
128 Date date = endDate;
129 if (endTime != null || year != null) {
130 GregorianCalendar calDate = new GregorianCalendar();
131 calDate.setTime(endDate);
132 if (endTime != null) {
133 GregorianCalendar calTime = new GregorianCalendar();
134 calTime.setTime(endTime);
135 calDate.set(GregorianCalendar.HOUR_OF_DAY,
136 calTime.get(GregorianCalendar.HOUR_OF_DAY));
137 calDate.set(GregorianCalendar.MINUTE,
138 calTime.get(GregorianCalendar.MINUTE));
139 }
140 if (year != null) {
141 calDate.set(GregorianCalendar.YEAR, Integer.parseInt(year));
142 }
143 date = calDate.getTime();
144 }
145 return this.getLastAllDay()
146 ? Main.ICAL_FORMATDATE.format(date)
147 : Main.ICAL_FORMAT.format(date);
148 }
149 /***
150 ** get the start date in ical Format.
151 * @return start
152 */
153 public String getIcalStart() {
154 Date date = startDate;
155 if (endTime != null || year != null) {
156 GregorianCalendar calDate = new GregorianCalendar();
157 calDate.setTime(startDate);
158 if (endTime != null) {
159 GregorianCalendar calTime = new GregorianCalendar();
160 calTime.setTime(startTime);
161 calDate.set(GregorianCalendar.HOUR_OF_DAY,
162 calTime.get(GregorianCalendar.HOUR_OF_DAY));
163 calDate.set(GregorianCalendar.MINUTE,
164 calTime.get(GregorianCalendar.MINUTE));
165 }
166 if (year != null) {
167 calDate.set(GregorianCalendar.YEAR, Integer.parseInt(year));
168 }
169 date = calDate.getTime();
170 }
171 return this.getLastAllDay()
172 ? Main.ICAL_FORMATDATE.format(date)
173 : Main.ICAL_FORMAT.format(date);
174 }
175 /***
176 * get the event fixed year.
177 * @return year
178 */
179 public String getYear() {
180 return year;
181 }
182 /***
183 * set the event fixed year.
184 * @param year the new year
185 */
186 public void setYear(String year) {
187 this.year = year;
188 }
189 /***
190 * get the event Id.
191 * @return id
192 */
193 public String getId() {
194 return converter != null ? converter.convert(id) : id;
195 }
196 /***
197 * get the start Date.
198 * @return start
199 */
200 public Date getStartDate() {
201 return startDate;
202 }
203 /***
204 * get the start Time.
205 * @return start
206 */
207 public Date getStartTime() {
208 return startTime;
209 }
210 /***
211 * get the Summary.
212 * @return summary
213 */
214 public String getSummary() {
215 return converter != null ? converter.convert(summary) : summary;
216 }
217 /***
218 * set the converter.
219 * @param converter new converter
220 */
221 public void setConverter(StringConverter converter) {
222 this.converter = converter;
223 }
224 /***
225 * set the description.
226 * @param description new description
227 */
228 public void setDescription(String description) {
229 this.description = description;
230 }
231 /***
232 * set the endDate.
233 * @param endDate new endDate
234 */
235 public void setEndDate(Date endDate) {
236 this.endDate = endDate;
237 }
238 /***
239 * set the endTime.
240 * @param endTime new endTime
241 */
242 public void setEndTime(Date endTime) {
243 this.endTime = endTime;
244 }
245 /***
246 * set the id.
247 * @param id new id
248 */
249 public void setId(String id) {
250 this.id = id;
251 }
252 /***
253 * set the startDate.
254 * @param startDate new startDate
255 */
256 public void setStartDate(Date startDate) {
257 this.startDate = startDate;
258 }
259 /***
260 * set the startTime.
261 * @param startTime new startTime
262 */
263 public void setStartTime(Date startTime) {
264 this.startTime = startTime;
265 }
266 /***
267 * set the summary.
268 * @param summary new summary
269 */
270 public void setSummary(String summary) {
271 this.summary = summary;
272 }
273
274
275
276
277 public String toString() {
278 StringBuffer buffer = new StringBuffer();
279 buffer.append("ID:").append(id).append("\n");
280 buffer.append("SUMMARY:").append(summary).append("\n");
281 if (startDate != null) {
282 buffer.append("STARTDATE:").append(startDate).append("\n");
283 buffer.append("ICALSTART:").append(getIcalStart()).append("\n");
284 }
285 if (endDate != null) {
286 buffer.append("ENDDATE:").append(endDate).append("\n");
287 buffer.append("ICALEND:").append(getIcalEnd()).append("\n");
288 }
289 buffer.append("STARTTIME:").append(startTime).append("\n");
290 buffer.append("ENDTIME:").append(endTime).append("\n");
291 buffer.append("YEAR:").append(year).append("\n");
292 buffer.append("LOCATION:").append(location).append("\n");
293 buffer.append("DESCRIPTION:").append(description).append("\n");
294 return buffer.toString();
295 }
296 /***
297 * get the location.
298 * @return location
299 */
300 public String getLocation() {
301 return converter != null ? converter.convert(location) : location;
302 }
303 /***
304 * set the location.
305 * @param location new location
306 */
307 public void setLocation(String location) {
308 this.location = location;
309 }
310 /***
311 * checks wheter the event lasts all day.
312 * @return true if the event lasts all day
313 */
314 public boolean getLastAllDay() {
315 return startTime == null;
316 }
317 /***
318 * Checks wheter the event has an end date.
319 * @return true if the event has an end date
320 */
321 public boolean getHasEnd() {
322 return this.endDate != null;
323 }
324 /***
325 * Checks the attribute values.
326 * @return true if the values are correct
327 * @throws Exception if the check fails
328 */
329 public boolean check() throws Exception {
330 if (this.id == null) {
331 throw new Exception("id == null");
332 }
333 if (this.startDate == null) {
334 throw new Exception("startDate == null");
335 }
336
337
338
339
340
341
342 return true;
343 }
344 }
345