1 32 33 package com.knowgate.addrbook.jical; 34 35 import java.io.IOException ; 36 import java.io.File ; 37 38 import java.util.Date ; 39 import java.util.Locale ; 40 import java.util.Iterator ; 41 import java.sql.SQLException ; 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 62 public class ICalendarFactory { 63 public ICalendarFactory() { 64 } 65 66 68 private static String duration(Date dtStart, Date 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 79 91 public static ICalendar createICalendar(JDCConnection oConn, String sGuFellow, 92 Date dtFrom, Date dtTo, String sLanguage) 93 throws SQLException ,IllegalArgumentException ,NullPointerException { 94 ICalendarVEvent oEvt; 95 String sTpRoom, sOrganizerName, sOrganizerMail; 96 Date dtNow = new Date (); 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 ("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 ("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 ("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 []{sGuFellow})) { 122 if (DebugFile.trace) DebugFile.decIdent(); 123 throw new SQLException ("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 []{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 []{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 []{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 } if (DebugFile.trace) { 187 DebugFile.decIdent(); 188 DebugFile.writeln("End ICalendarFactory()"); 189 } 190 return oCal; 191 } 192 193 195 209 public static ICalendar createICalendar(JDCConnection oConn, String sTxEmail, String sTxPwd, 210 Date dtFrom, Date dtTo, String sLanguage) 211 throws SQLException ,IllegalArgumentException ,NullPointerException ,SecurityException { 212 213 String sGuFellow = ACLUser.getIdFromEmail(oConn, sTxEmail); 214 215 if (null==sGuFellow) 216 throw new SQLException (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 (ACL.getErrorMessage(iAuth)); 220 221 return createICalendar(oConn, sGuFellow, dtFrom, dtTo, sLanguage); 222 } 223 224 226 234 public static void loadCalendar(JDCConnection oConn, String sGuFellow, 235 File oCalFile, String sEncoding) 236 throws SQLException { 237 238 Meeting oMeet; 239 String sGuid; 240 String sCats; 241 Fellow oFllw = new Fellow(); 242 if (!oFllw.load(oConn, new Object []{sGuFellow})) { 243 throw new SQLException (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 oIter = oCal.icalEventCollection.iterator(); 252 while (oIter.hasNext()) { 253 oEvnt = (ICalendarVEvent) oIter.next(); 254 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 []{sGuid})) { 260 if (oMeet.lastModified().compareTo(oEvnt.getLastModified()==null ? new Date () : 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 } } } 301 303 } 304 | Popular Tags |