KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > items > task > model > Task


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.exchange.items.task.model;
19
20 import java.text.MessageFormat JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23
24 import java.util.Date JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import sync4j.exchange.items.task.TaskParseException;
28 import sync4j.exchange.xml.XmlParser;
29 import sync4j.exchange.xml.XmlParseException;
30 import sync4j.exchange.util.StringTools;
31 import java.util.TimeZone JavaDoc;
32
33 /**
34  * Represent an Exchange Task
35  * @version $Id: Task.java,v 1.22 2005/07/20 16:07:30 nichele Exp $
36  **/

37 public class Task {
38
39     //-------------------------------------------------------------------- Constants
40

41     private static final String JavaDoc TAG_PP_ACTUAL_WORK = "ActualWork" ;
42     private static final String JavaDoc TAG_PP_BILLING_INFORMATION = "BillingInformation" ;
43     private static final String JavaDoc TAG_PP_CATEGORIES = "Categories" ;
44     private static final String JavaDoc TAG_PP_COMPANIES = "Companies" ;
45     private static final String JavaDoc TAG_PP_COMPLETE = "Complete" ;
46     private static final String JavaDoc TAG_PP_DATE_COMPLETED = "DateCompleted" ;
47     private static final String JavaDoc TAG_PP_DUE_DATE = "DueDate" ;
48     private static final String JavaDoc TAG_PP_IMPORTANCE = "Importance" ;
49     private static final String JavaDoc TAG_PP_MILEAGE = "Mileage" ;
50     private static final String JavaDoc TAG_PP_NOTE = "Body" ;
51     private static final String JavaDoc TAG_PP_OWNER = "Owner" ;
52     private static final String JavaDoc TAG_PP_PERCENT_COMPLETE = "PercentComplete" ;
53     private static final String JavaDoc TAG_PP_REMINDER_SET = "ReminderSet" ;
54     private static final String JavaDoc TAG_PP_REMINDER_TIME = "ReminderTime" ;
55     private static final String JavaDoc TAG_PP_SENSITIVITY = "Sensitivity" ;
56     private static final String JavaDoc TAG_PP_DATE = "StartDate" ;
57     private static final String JavaDoc TAG_PP_STATUS = "Status" ;
58     private static final String JavaDoc TAG_PP_SUBJECT = "Subject" ;
59     private static final String JavaDoc TAG_PP_TEAM_TASK = "TeamTask" ;
60     private static final String JavaDoc TAG_PP_TOTAL_WORK = "TotalWork" ;
61
62     //------------------------------------------------------------- Private data
63
private int actualWork = 0 ;
64     private String JavaDoc billingInformation = null ;
65     private String JavaDoc categories = null ;
66     private String JavaDoc companies = null ;
67     private boolean complete = false ;
68     private Date JavaDoc date = null ;
69     private Date JavaDoc dateCompleted = null ;
70     private Date JavaDoc dueDate = null ;
71     private String JavaDoc importance = null ;
72     private String JavaDoc mileage = null ;
73     private String JavaDoc owner = null ;
74     private String JavaDoc percentComplete = null ;
75     private boolean reminderSet = false ;
76     private Date JavaDoc reminderTime = null ;
77     private String JavaDoc sensitivity = null ;
78     private String JavaDoc status = null ;
79     private String JavaDoc subject = null ;
80     private boolean teamTask = false ;
81     private String JavaDoc textDescription = null ;
82     private int totalWork = 0 ;
83
84     private String JavaDoc href = null ;
85
86     private String JavaDoc username = null ;
87     private String JavaDoc id = null ;
88
89     private Date JavaDoc createDate = null ;
90     private Date JavaDoc lastModified = null ;
91     private char state = ' ' ;
92
93     //------------------------------------------------------------- Constructors
94

95     /**
96      * Creates a Task
97      *
98      */

99     public Task() {
100     }
101
102     /**
103      * Creates a Task
104      *
105      * @param id client item id
106      */

107     public Task (String JavaDoc id) {
108         this.id = id;
109     }
110
111     /**
112      * Creates a Task
113      *
114      * @param id client item id
115      * @param t client item timestamp
116      */

117     public Task(String JavaDoc id, Date JavaDoc t) throws TaskParseException {
118         this.id = id;
119         this.lastModified = t ;
120     }
121
122     /**
123      * Creates a Task
124      *
125      * @param id client item id
126      * @param state client item state
127      */

128     public Task(String JavaDoc id, char state) {
129         this.id = id ;
130         this.state = state;
131     }
132
133     /**
134      * Creates a Task
135      *
136      * @param id
137      * @param href
138      * @param state
139      * @param lastModified
140      */

141     public Task(String JavaDoc id ,
142                 String JavaDoc href ,
143                 char state ,
144                 Date JavaDoc lastModified) {
145
146         this.id = id ;
147         this.href = href ;
148         this.state = state ;
149         this.lastModified = lastModified;
150     }
151
152     /**
153      * Creates a Task
154      *
155      * @param id client item id
156      * @param content client item content
157      * @param t client item timestamp
158      */

159     public Task(String JavaDoc id, String JavaDoc content, Date JavaDoc t) throws TaskParseException {
160         getPPTask(id, content, t);
161     }
162
163     //----------------------------------------------------------- Public methods
164
public String JavaDoc getId() {
165         return this.id;
166     }
167
168     public String JavaDoc getUsername() {
169         return this.username;
170     }
171
172     public Date JavaDoc getDate() {
173         return this.date;
174     }
175
176     public String JavaDoc getHref() {
177         return this.href;
178     }
179
180     public Date JavaDoc getLastModified() {
181         return this.lastModified;
182     }
183
184     public char getState() {
185         return this.state;
186     }
187
188     public int getActualWork() {
189         return this.actualWork;
190     }
191
192     public String JavaDoc getBillingInformation() {
193         return this.billingInformation;
194     }
195
196     public String JavaDoc getCompanies() {
197         return this.companies;
198     }
199
200     public String JavaDoc getCategories() {
201         return categories;
202     }
203
204     public Date JavaDoc getDateCompleted() {
205         return this.dateCompleted;
206     }
207
208     public Date JavaDoc getDueDate() {
209         return dueDate;
210     }
211
212     public String JavaDoc getImportance() {
213         return importance;
214     }
215
216     public String JavaDoc getMileage() {
217         return mileage;
218     }
219
220     public String JavaDoc getOwner() {
221         return this.owner;
222     }
223
224     public String JavaDoc getPercentComplete() {
225         return percentComplete;
226     }
227
228     public boolean getReminderSet() {
229         return reminderSet;
230     }
231
232     public Date JavaDoc getReminderTime() {
233         return reminderTime;
234     }
235
236     public String JavaDoc getSensitivity() {
237         return sensitivity;
238     }
239
240     public String JavaDoc getStatus() {
241         return status;
242     }
243
244     public String JavaDoc getSubject() {
245         return this.subject;
246     }
247
248     public boolean getTeamTask() {
249         return this.teamTask;
250     }
251
252     public boolean isTeamTask() {
253         return this.teamTask;
254     }
255
256     public String JavaDoc getTextDescription() {
257         return this.textDescription;
258     }
259
260     public int getTotalWork() {
261         return this.totalWork;
262     }
263
264     public boolean isComplete() {
265         return this.complete;
266     }
267
268     public void setId(String JavaDoc id) {
269         this.id = id;
270     }
271
272     public void setUsername(String JavaDoc username) {
273         this.username = username;
274     }
275
276     public void setState(char state) {
277         this.state = state;
278     }
279
280     public void setDate(Date JavaDoc date) {
281         this.date = date;
282     }
283
284     public void setHref(String JavaDoc href) {
285         this.href = href;
286     }
287
288     public void setLastModified(Date JavaDoc lastModified) {
289         this.lastModified = lastModified;
290     }
291
292     public void setActualWork(int actualWork) {
293         this.actualWork = actualWork;
294     }
295
296     public void setBillingInformation (String JavaDoc billingInformation) {
297         this.billingInformation = billingInformation;
298     }
299
300     public void setCompanies(String JavaDoc companies) {
301         this.companies = companies;
302     }
303
304     public void setComplete(boolean complete) {
305         this.complete = complete;
306     }
307
308     public void setCategories(String JavaDoc categories) {
309         this.categories = categories;
310     }
311
312     public void setDateCompleted(Date JavaDoc dateCompleted) {
313         this.dateCompleted = dateCompleted;
314     }
315
316     public void setDueDate(Date JavaDoc dueDate) {
317         this.dueDate = dueDate;
318     }
319
320     public void setImportance(String JavaDoc importance) {
321         this.importance = importance;
322     }
323
324     public void setMileage(String JavaDoc mileage) {
325         this.mileage = mileage ;
326     }
327
328     public void setOwner(String JavaDoc owner) {
329         this.owner = owner;
330     }
331
332     public void setPercentComplete(String JavaDoc percentComplete) {
333         this.percentComplete = percentComplete ;
334     }
335
336     public void setReminderSet(boolean reminderSet) {
337         this.reminderSet = reminderSet ;
338     }
339
340     public void setReminderTime(Date JavaDoc reminderTime) {
341         this.reminderTime = reminderTime;
342     }
343
344     public void setSensitivity(String JavaDoc sensitivity) {
345         this.sensitivity = sensitivity;
346     }
347
348     public void setStatus(String JavaDoc status) {
349         this.status = status;
350     }
351
352     public void setSubject(String JavaDoc subject) {
353         this.subject = subject;
354     }
355
356     public void setTeamTask(boolean teamTask) {
357         this.teamTask = teamTask;
358     }
359
360     public void setTextDescription(String JavaDoc textDescription) {
361         this.textDescription = textDescription;
362     }
363
364     public void setTotalWork(int totalWork) {
365         this.totalWork = totalWork;
366     }
367     /**
368      * Set a Task properties from Client format content
369      *
370      * @param id client item id
371      * @param content client item content
372      * @param t client item timestamp
373      */

374     public void getPPTask(String JavaDoc id, String JavaDoc content, Date JavaDoc t)
375     throws TaskParseException {
376
377         String JavaDoc dt = null;
378         String JavaDoc tmp = null;
379
380         this.id = id;
381         this.lastModified = t ;
382
383         try {
384
385             XmlParser p = new XmlParser();
386
387             dt = p.getXMLInitTagValue(content, TAG_PP_DATE );
388             this.date = getDate(dt);
389
390             tmp = p.getXMLInitTagValue(content, TAG_PP_ACTUAL_WORK);
391             if (tmp != null && tmp.length() > 0) {
392                 this.actualWork = Integer.parseInt(tmp);
393             }
394
395             this.billingInformation = p.getXMLInitTagValue(content, TAG_PP_BILLING_INFORMATION);
396             this.categories = p.getXMLInitTagValue(content, TAG_PP_CATEGORIES);
397             this.companies = p.getXMLInitTagValue(content, TAG_PP_COMPANIES);
398             this.dateCompleted = getDate(p.getXMLInitTagValue(content, TAG_PP_DATE_COMPLETED ));
399             this.dueDate = getDate(p.getXMLInitTagValue(content, TAG_PP_DUE_DATE));
400             this.importance = p.getXMLInitTagValue(content, TAG_PP_IMPORTANCE);
401             this.mileage = p.getXMLInitTagValue(content, TAG_PP_MILEAGE);
402             this.owner = p.getXMLInitTagValue(content, TAG_PP_OWNER);
403             this.percentComplete = p.getXMLInitTagValue(content, TAG_PP_PERCENT_COMPLETE);
404
405             this.complete = sifValueToBoolean(p.getXMLInitTagValue(content, TAG_PP_COMPLETE));
406             this.reminderSet = sifValueToBoolean(p.getXMLInitTagValue(content, TAG_PP_REMINDER_SET));
407
408             this.teamTask = sifValueToBoolean(p.getXMLInitTagValue(content, TAG_PP_TEAM_TASK));;
409
410
411             this.subject = p.getXMLInitTagValue(content, TAG_PP_SUBJECT);
412             this.textDescription = p.getXMLInitTagValue(content, TAG_PP_NOTE);
413
414
415             tmp = p.getXMLInitTagValue(content, TAG_PP_REMINDER_TIME);
416             if (tmp != null && !tmp.equals("")) {
417                 this.reminderTime = p.clientDateToDate(tmp);
418             }
419             this.sensitivity = p.getXMLInitTagValue(content, TAG_PP_SENSITIVITY);
420             this.status = p.getXMLInitTagValue(content, TAG_PP_STATUS);
421
422             tmp = p.getXMLInitTagValue(content, TAG_PP_TOTAL_WORK);
423
424             if (tmp != null && tmp.length() > 0) {
425                 this.totalWork = Integer.parseInt(tmp);
426             }
427
428         } catch (NumberFormatException JavaDoc e) {
429             throw new TaskParseException(e.getMessage());
430         } catch (XmlParseException e) {
431             throw new TaskParseException(e.getMessage());
432         } catch (Exception JavaDoc e) {
433             throw new TaskParseException(e.getMessage());
434         }
435
436     }
437
438     /**
439      * Set a Task properties to Client format content
440      *
441      * @return content in client format
442      */

443     public String JavaDoc toXml() throws TaskParseException {
444
445         StringBuffer JavaDoc xmlMsg = new StringBuffer JavaDoc();
446         String JavaDoc date = null;
447         XmlParser p = new XmlParser();
448
449         try {
450
451
452             xmlMsg.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><task>\n");
453
454             xmlMsg.append("<" + TAG_PP_ACTUAL_WORK + ">").
455                         append(String.valueOf(actualWork)).
456                             append("</" +TAG_PP_ACTUAL_WORK + ">");
457
458             if (categories != null) {
459                 xmlMsg.append("<" + TAG_PP_CATEGORIES + ">");
460                 xmlMsg.append(categories);
461                 xmlMsg.append("</" + TAG_PP_CATEGORIES + ">");
462             }
463
464             if (companies != null) {
465                 xmlMsg.append("<" + TAG_PP_COMPANIES + ">");
466                 xmlMsg.append(companies);
467                 xmlMsg.append("</" + TAG_PP_COMPANIES + ">");
468             }
469
470             xmlMsg.append("<" + TAG_PP_COMPLETE + ">").
471                    append(booleanToSIFValue(complete)).
472                    append("</" + TAG_PP_COMPLETE + ">");
473
474             date = p.toClientDate(this.date);
475
476             if (date != null && date.length() > 0) {
477                 xmlMsg.append("<" + TAG_PP_DATE + ">");
478                 xmlMsg.append(date);
479                 xmlMsg.append("</" + TAG_PP_DATE + ">");
480             } else {
481                 xmlMsg.append("<" + TAG_PP_DATE + ">" +
482                               "</" + TAG_PP_DATE + ">");
483             }
484
485             if (dateCompleted != null) {
486                 xmlMsg.append("<" + TAG_PP_DATE_COMPLETED + ">");
487                 xmlMsg.append(XmlParser.toClientDate(dateCompleted));
488                 xmlMsg.append("</" + TAG_PP_DATE_COMPLETED + ">");
489             }
490
491             if (dueDate != null) {
492                 xmlMsg.append("<" + TAG_PP_DUE_DATE + ">");
493                 xmlMsg.append(XmlParser.toClientDate(dueDate));
494                 xmlMsg.append("</" + TAG_PP_DUE_DATE + ">");
495             } else {
496                 xmlMsg.append("<" + TAG_PP_DUE_DATE + ">" +
497                               "</" + TAG_PP_DUE_DATE + ">");
498             }
499
500             if (importance != null) {
501                 xmlMsg.append("<" + TAG_PP_IMPORTANCE + ">");
502                 xmlMsg.append(importance);
503                 xmlMsg.append("</" + TAG_PP_IMPORTANCE + ">");
504             }
505
506             if (mileage != null) {
507                 xmlMsg.append("<" + TAG_PP_MILEAGE + ">");
508                 xmlMsg.append(mileage);
509                 xmlMsg.append("</" + TAG_PP_MILEAGE + ">");
510             }
511
512             if (owner != null) {
513                 xmlMsg.append("<" + TAG_PP_OWNER + ">");
514                 xmlMsg.append(owner);
515                 xmlMsg.append("</" + TAG_PP_OWNER + ">");
516             }
517
518             if (percentComplete != null) {
519                 xmlMsg.append("<" + TAG_PP_PERCENT_COMPLETE + ">");
520                 xmlMsg.append(percentComplete);
521                 xmlMsg.append("</" + TAG_PP_PERCENT_COMPLETE + ">");
522             }
523
524             xmlMsg.append("<" + TAG_PP_REMINDER_SET + ">");
525             xmlMsg.append(booleanToSIFValue(reminderSet));
526             xmlMsg.append("</" + TAG_PP_REMINDER_SET + ">");
527
528             if (reminderTime != null) {
529                 xmlMsg.append("<" + TAG_PP_REMINDER_TIME + ">");
530                 xmlMsg.append(XmlParser.toClientDate(reminderTime));
531                 xmlMsg.append("</" + TAG_PP_REMINDER_TIME + ">");
532             } else {
533                 xmlMsg.append("<" + TAG_PP_REMINDER_TIME + ">" +
534                               "</" + TAG_PP_REMINDER_TIME + ">");
535             }
536
537             if (sensitivity != null) {
538                 xmlMsg.append("<" + TAG_PP_SENSITIVITY + ">");
539                 xmlMsg.append(sensitivity);
540                 xmlMsg.append("</" + TAG_PP_SENSITIVITY + ">");
541             }
542
543             if (status != null) {
544                 xmlMsg.append("<" + TAG_PP_STATUS + ">");
545                 xmlMsg.append(status);
546                 xmlMsg.append("</" + TAG_PP_STATUS + ">");
547             }
548
549             if (subject != null) {
550                 xmlMsg.append("<" + TAG_PP_SUBJECT + ">");
551                 xmlMsg.append(subject);
552                 xmlMsg.append("</" + TAG_PP_SUBJECT + ">");
553             }
554
555
556             xmlMsg.append("<" + TAG_PP_TEAM_TASK + ">");
557             xmlMsg.append(booleanToSIFValue(this.teamTask));
558             xmlMsg.append("</" + TAG_PP_TEAM_TASK + ">");
559
560             if (textDescription != null) {
561                 xmlMsg.append("<" + TAG_PP_NOTE + ">");
562                 xmlMsg.append(textDescription);
563                 xmlMsg.append("</" + TAG_PP_NOTE + ">");
564             }
565
566             xmlMsg.append("<" + TAG_PP_TOTAL_WORK + ">").
567                         append(String.valueOf(totalWork)).
568                             append("</" +TAG_PP_TOTAL_WORK + ">");
569
570             xmlMsg.append("</task>");
571
572          } catch (Exception JavaDoc e) {
573              throw new TaskParseException("Error composing task Device: " +
574                                            e.getMessage());
575          }
576
577          return xmlMsg.toString();
578      }
579
580      private Date JavaDoc getDate(String JavaDoc sDate) throws Exception JavaDoc {
581          if (sDate == null || sDate.equals("")) {
582              return null;
583          }
584          SimpleDateFormat JavaDoc formatter = null;
585          String JavaDoc formatUtc = "yyyyMMdd'T'HHmmss'Z'";
586          formatter = new SimpleDateFormat JavaDoc(formatUtc);
587          formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
588
589          return formatter.parse(sDate);
590      }
591
592      /**
593       * Returns the SIF representation of a boolean value.
594       *
595       * @param value boolean
596       * @return "0" if the given boolean is false, "1" otherwise
597       */

598      private String JavaDoc booleanToSIFValue(boolean value) {
599          if (value) {
600              return "1";
601          } else {
602              return "0";
603          }
604      }
605
606      /**
607       * If the given string is "1" returns true, otherwise returns false
608       * @param value String
609       * @return boolean
610       */

611      private boolean sifValueToBoolean(String JavaDoc value) {
612          if (value != null && value.equals("1")) {
613              return true;
614          }
615          return false;
616     }
617 }
618
Popular Tags