KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > addrbook > jical > ICalendarFactory


1 /*
2   Copyright (C) 2003-2005 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.jical;
34
35 import java.io.IOException JavaDoc;
36 import java.io.File JavaDoc;
37
38 import java.util.Date JavaDoc;
39 import java.util.Locale JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.sql.SQLException JavaDoc;
42
43 import com.knowgate.jdc.JDCConnection;
44 import com.knowgate.acl.ACL;
45 import com.knowgate.acl.ACLUser;
46 import com.knowgate.debug.DebugFile;
47 import com.knowgate.misc.Gadgets;
48 import com.knowgate.dataobjs.DB;
49 import com.knowgate.dataobjs.DBSubset;
50 import com.knowgate.hipergate.DBLanguages;
51 import com.knowgate.addrbook.*;
52
53 import org.jical.ICalendar;
54 import org.jical.ICalendarParser;
55 import org.jical.ICalendarVEvent;
56
57 /**
58  * <p>Create an ICalendar object for a given hipergate Fellow meetings</p>
59  * @author Sergio Montoro Ten
60  * @version 1.0
61  */

62 public class ICalendarFactory {
63   public ICalendarFactory() {
64   }
65
66   // ---------------------------------------------------------------------------
67

68   private static String JavaDoc duration(Date JavaDoc dtStart, Date JavaDoc dtEnd) {
69     final long lSecond = 1000;
70     final long lMinute = lSecond*60l;
71     final long lHour = lMinute*60l;
72     final long lDay = lHour*24l;
73     long lDuration = dtEnd.getTime()-dtStart.getTime();
74     return (lDuration>=0 ? "" : "-")+"P"+String.valueOf(lDuration/lDay)+"DT"+String.valueOf(lDuration/lHour)+"H"+String.valueOf(lDuration/lMinute)+"M"+String.valueOf(lDuration/lSecond)+"S";
75   }
76
77   // ---------------------------------------------------------------------------
78

79   /**
80    * Get an <a HREF="http://www.ietf.org/rfc/rfc2445.txt">RFC 2445</a> calendar for a Fellow
81    * @param oConn JDCConnection
82    * @param sGuFellow String Fellow GUID from k_fellows table
83    * @param dtFrom Date from which to start exporting meetings
84    * @param dtTo Date when stop exporting meetings
85    * @param sLanguage String Two letter identifier of language to be used if this parameter is <b>null</b> then the default Locale is used.
86    * @return ICalendar
87    * @throws SQLException If not fellow with specified GUID is found at k_fellows table
88    * @throws IllegalArgumentException if dtFrom>dtTo
89    * @throws NullPointerException if sGuFellow or dtFrom or dtTo is <b>null</b>
90    */

91   public static ICalendar createICalendar(JDCConnection oConn, String JavaDoc sGuFellow,
92                                           Date JavaDoc dtFrom, Date JavaDoc dtTo, String JavaDoc sLanguage)
93     throws SQLException JavaDoc,IllegalArgumentException JavaDoc,NullPointerException JavaDoc {
94     ICalendarVEvent oEvt;
95     String JavaDoc sTpRoom, sOrganizerName, sOrganizerMail;
96     Date JavaDoc dtNow = new Date JavaDoc();
97
98     if (DebugFile.trace) {
99       DebugFile.writeln("Begin ICalendarFactory([JDCConnection],"+sGuFellow+","+dtFrom.toString()+","+dtTo.toString()+","+sLanguage+")");
100       DebugFile.incIdent();
101     }
102
103     if (sGuFellow==null) {
104       if (DebugFile.trace) DebugFile.decIdent();
105       throw new NullPointerException JavaDoc("ICalendarFactory.createICalendar() Fellow GUID parameter is required");
106     }
107
108     if (dtFrom==null || dtTo==null) {
109       if (DebugFile.trace) DebugFile.decIdent();
110       throw new NullPointerException JavaDoc("ICalendarFactory.createICalendar() Both start and end date are required");
111     }
112
113     if (dtFrom.compareTo(dtTo)>0) {
114       if (DebugFile.trace) DebugFile.decIdent();
115       throw new IllegalArgumentException JavaDoc("ICalendarFactory.createICalendar() End date cannot be prior to start date");
116     }
117
118     if (sLanguage==null) sLanguage=Locale.getDefault().getLanguage();
119
120     Fellow oFlw = new Fellow();
121     if (!oFlw.load(oConn, new Object JavaDoc[]{sGuFellow})) {
122       if (DebugFile.trace) DebugFile.decIdent();
123       throw new SQLException JavaDoc("Fellow "+sGuFellow+" not found at "+DB.k_fellows+" table");
124     }
125     DBSubset oMeetings = new DBSubset (DB.k_meetings+" m,"+DB.k_x_meeting_fellow+" f",
126                                        "m."+DB.gu_meeting+",m."+DB.dt_start+",m."+DB.dt_end+","+
127                                        "m."+DB.bo_private+",m."+DB.gu_writer+",m."+DB.df_before+","+
128                                        "m."+DB.tp_meeting+",m."+DB.tx_meeting+",m."+DB.de_meeting+","+
129                                        "m."+DB.tx_status,
130                                        "f."+DB.gu_fellow+"=? AND m."+DB.gu_fellow+"=f."+DB.gu_fellow, 100);
131     int iMeetings = oMeetings.load(oConn, new Object JavaDoc[]{sGuFellow});
132
133     if (DebugFile.trace) DebugFile.writeln(String.valueOf(iMeetings)+" meetings found");
134
135     DBSubset oRooms = new DBSubset(DB.k_x_meeting_room+" x,"+DB.k_rooms+" r",
136                                    "r."+DB.gu_workarea+",r."+DB.tp_room+",r."+DB.nm_room+","+"r."+DB.tx_location,
137                                    "x."+DB.gu_meeting+"=? AND "+"x."+DB.nm_room+"=r."+DB.nm_room,2);
138     int iRooms;
139
140     ICalendar oCal = new ICalendar();
141     oCal.setProdId("JICAL");
142     oCal.setVersion("2.0");
143     for (int m=0; m<iMeetings; m++) {
144       if (DebugFile.trace) DebugFile.writeln("Loading meeting "+oMeetings.getStringNull(7,m,""));
145       oEvt = new ICalendarVEvent(oMeetings.getDate(1,m),oMeetings.getDate(2,m),
146                                  oMeetings.getStringNull(8,m,""),
147                                  duration(oMeetings.getDate(1,m),oMeetings.getDate(2,m)),
148                                  oMeetings.getStringNull(7,m,""), null, null);
149       oEvt.setSequence(0);
150       oEvt.setDateStamp(dtNow);
151       oEvt.setCreated(dtNow);
152       oEvt.setLastModified(dtNow);
153       oEvt.setEventClass(oMeetings.getShort(3,m)==0 ? "PUBLIC" : "PRIVATE");
154       oEvt.setTransparency("OPAQUE");
155       if (!oMeetings.isNull(6,m)) oEvt.setCategories(oMeetings.getString(6,m));
156       if (oMeetings.isNull(4,m) || sGuFellow.equals(oMeetings.get(4,m))) {
157         sOrganizerName = (oFlw.getStringNull(DB.tx_name,"")+" "+oFlw.getStringNull(DB.tx_surname,"")).trim();
158         sOrganizerMail = oFlw.getStringNull(DB.tx_email,"");
159       } else {
160         ACLUser oWrt = new ACLUser();
161         if (oWrt.load(oConn, new Object JavaDoc[]{oMeetings.get(4,m)})) {
162           sOrganizerName = (oFlw.getStringNull(DB.nm_user,"")+" "+oFlw.getStringNull(DB.tx_surname1,"")+" "+oFlw.getStringNull(DB.tx_surname2,"")).trim();
163           sOrganizerMail = oFlw.getStringNull(DB.tx_main_email,"");
164         } else {
165           sOrganizerName = (oFlw.getStringNull(DB.tx_name,"")+" "+oFlw.getStringNull(DB.tx_surname,"")).trim();
166           sOrganizerMail = oFlw.getStringNull(DB.tx_email,"");
167         }
168       }
169       if (DebugFile.trace) DebugFile.writeln("Organizer is \""+sOrganizerName+"\" <"+sOrganizerMail+">");
170       oEvt.setOrganizer("CN=:\""+sOrganizerName.replace((char)34,(char)32)+"\":MAILTO:"+sOrganizerMail);
171       oEvt.setUid("hipergate-"+oMeetings.getString(0,m));
172       oEvt.setPriority(3);
173       if (!oMeetings.isNull(9,m)) oEvt.setStatus(oMeetings.getString(9,m));
174       iRooms = oRooms.load(oConn, new Object JavaDoc[]{oMeetings.getString(0,m)});
175       if (iRooms>0) {
176         if (DebugFile.trace) DebugFile.writeln(String.valueOf(iRooms)+" rooms found");
177         if (sLanguage!=null)
178           sTpRoom = DBLanguages.getLookUpTranslation(oConn, DB.k_rooms_lookup, oRooms.getString(1,0), DB.tp_room, sLanguage, oRooms.getString(2,0));
179         else
180           sTpRoom = null;
181         if (null==sTpRoom) sTpRoom=""; else sTpRoom+=" ";
182         oEvt.setLocation(sTpRoom+oRooms.getString(2,0)+(oRooms.isNull(3,0) ? "" : " "+oRooms.getStringNull(3,0,"")));
183       }
184       oCal.icalEventCollection.add(oEvt);
185     } // next
186
if (DebugFile.trace) {
187       DebugFile.decIdent();
188       DebugFile.writeln("End ICalendarFactory()");
189     }
190     return oCal;
191   }
192
193   // ---------------------------------------------------------------------------
194

195   /**
196    * Get an <a HREF="http://www.ietf.org/rfc/rfc2445.txt">RFC 2445</a> calendar for a Fellow
197    * @param oConn JDCConnection
198    * @param sTxEmail String Fellow e-mail. Must be that of tx_main-email of the corresponding User at k_users table.
199    * @param sTxPwd String User password in clear text.
200    * @param dtFrom Date from which to start exporting meetings
201    * @param dtTo Date when stop exporting meetings
202    * @param sLanguage String Two letter identifier of language to be used if this parameter is <b>null</b> then the default Locale is used.
203    * @return ICalendar
204    * @throws SQLException If not fellow with specified e-amil is found at k_users table
205    * @throws IllegalArgumentException if dtFrom>dtTo
206    * @throws NullPointerException if sGuFellow or dtFrom or dtTo is <b>null</b>
207    * @throws SecurityException e-mail/password pair does not match the one set at the database
208    */

209   public static ICalendar createICalendar(JDCConnection oConn, String JavaDoc sTxEmail, String JavaDoc sTxPwd,
210                                           Date JavaDoc dtFrom, Date JavaDoc dtTo, String JavaDoc sLanguage)
211     throws SQLException JavaDoc,IllegalArgumentException JavaDoc,NullPointerException JavaDoc,SecurityException JavaDoc {
212
213     String JavaDoc sGuFellow = ACLUser.getIdFromEmail(oConn, sTxEmail);
214
215     if (null==sGuFellow)
216       throw new SQLException JavaDoc(ACL.getErrorMessage(ACL.USER_NOT_FOUND));
217
218     short iAuth = ACL.autenticate(oConn, sGuFellow, sTxPwd, ACL.PWD_CLEAR_TEXT);
219     if (iAuth<0) throw new SecurityException JavaDoc(ACL.getErrorMessage(iAuth));
220
221     return createICalendar(oConn, sGuFellow, dtFrom, dtTo, sLanguage);
222   }
223
224   // ---------------------------------------------------------------------------
225

226   /**
227    * <p>Load a calendar file into k_meetings table</p>
228    * @param oConn JDCConnection
229    * @param sGuFellow String
230    * @param oCalFile File
231    * @param sEncoding String
232    * @throws SQLException
233    */

234   public static void loadCalendar(JDCConnection oConn, String JavaDoc sGuFellow,
235                                   File JavaDoc oCalFile, String JavaDoc sEncoding)
236     throws SQLException JavaDoc {
237
238     Meeting oMeet;
239     String JavaDoc sGuid;
240     String JavaDoc sCats;
241     Fellow oFllw = new Fellow();
242     if (!oFllw.load(oConn, new Object JavaDoc[]{sGuFellow})) {
243       throw new SQLException JavaDoc(ACL.getErrorMessage(ACL.USER_NOT_FOUND));
244     }
245     Room oRoom = new Room();
246     oRoom.put(DB.gu_workarea, oFllw.getString(DB.gu_workarea));
247
248     ICalendarVEvent oEvnt;
249     ICalendarParser oPrsr = new ICalendarParser();
250     ICalendar oCal = oPrsr.parse(oCalFile, sEncoding);
251     Iterator JavaDoc oIter = oCal.icalEventCollection.iterator();
252     while (oIter.hasNext()) {
253       oEvnt = (ICalendarVEvent) oIter.next();
254       // remove - from Mozilla UIDs
255
sGuid = Gadgets.removeChar(oEvnt.getUid(),'-').trim();
256       sCats = oEvnt.getCategories();
257       if (sCats!=null) sCats = (Gadgets.split(sCats,','))[0];
258       oMeet = new Meeting();
259       if (oMeet.load(oConn, new Object JavaDoc[]{sGuid})) {
260         // Current meeting is only modified if the one comming in the iCalendar file is newer
261
if (oMeet.lastModified().compareTo(oEvnt.getLastModified()==null ? new Date JavaDoc() : oEvnt.getLastModified())<0) {
262           oMeet.replace(DB.dt_start, oEvnt.getDateStart());
263           oMeet.replace(DB.dt_end, oEvnt.getDateEnd());
264           if (null==oEvnt.getEventClass())
265             oMeet.replace(DB.bo_private, (short)1);
266           else
267             oMeet.replace(DB.bo_private, (short)(oEvnt.getEventClass().equals("PRIVATE") ? 1 : 0));
268           oMeet.replace(DB.tx_status, oEvnt.getStatus());
269           oMeet.replace(DB.tp_meeting, sCats);
270           oMeet.replace(DB.tx_meeting, oEvnt.getSummary());
271           oMeet.replace(DB.tx_meeting, oEvnt.getDescription());
272           oMeet.store(oConn);
273         }
274       } else {
275         oMeet.put(DB.gu_meeting, sGuid);
276         oMeet.put(DB.gu_workarea, oFllw.getString(DB.gu_workarea));
277         oMeet.put(DB.id_domain, oFllw.getInt(DB.id_domain));
278         oMeet.put(DB.gu_fellow, sGuFellow);
279         oMeet.put(DB.gu_writer, sGuFellow);
280         oMeet.put(DB.dt_start, oEvnt.getDateStart());
281         oMeet.put(DB.dt_end, oEvnt.getDateEnd());
282         if (null==oEvnt.getEventClass())
283           oMeet.put(DB.bo_private, (short)1);
284         else
285           oMeet.put(DB.bo_private, (short)(oEvnt.getEventClass().equals("PRIVATE") ? 1 : 0));
286         oMeet.put(DB.tx_status, oEvnt.getStatus());
287         oMeet.replace(DB.tp_meeting, sCats);
288         oMeet.put(DB.tx_meeting, oEvnt.getSummary());
289         oMeet.put(DB.tx_meeting, oEvnt.getDescription());
290         oMeet.store(oConn);
291         oMeet.setAttendant(oConn, sGuFellow);
292       }
293       if (oEvnt.getLocation()!=null) {
294         oRoom.replace(DB.nm_room, oEvnt.getLocation());
295         if (oRoom.exists(oConn))
296           oMeet.setRoom(oConn, oEvnt.getLocation());
297       } // fi (getLocation())
298
} // wend
299
} // loadCalendar
300

301   // ---------------------------------------------------------------------------
302

303 }
304
Popular Tags