/** Test COUNT rules. */ public void testGetConsumeTimeByCount() { Recur recur = new Recur(Recur.WEEKLY, 3); recur.setInterval(1); recur.getDayList().add(WeekDay.SU); log.info(recur); Calendar cal = getCalendarInstance(); cal.set(Calendar.DAY_OF_MONTH, 8); Date start = new DateTime(cal.getTime()); // cal.add(Calendar.DAY_OF_WEEK_IN_MONTH, 10); cal.add(Calendar.HOUR_OF_DAY, 1); Date end = new DateTime(cal.getTime()); // log.info(recur.getDates(start, end, Value.DATE_TIME)); RRule rrule = new RRule(recur); VEvent event = new VEvent(start, end, "Test recurrence COUNT"); event.getProperties().add(rrule); log.info(event); Calendar rangeCal = getCalendarInstance(); Date rangeStart = new DateTime(rangeCal.getTime()); rangeCal.add(Calendar.WEEK_OF_YEAR, 4); Date rangeEnd = new DateTime(rangeCal.getTime()); log.info(event.getConsumedTime(rangeStart, rangeEnd)); }
/** * Build recurring properties from ical recurrence. * * @param r * @return RecurTyp filled in * @throws Throwable */ public static RecurType doRecur(final Recur r) throws Throwable { RecurType rt = new RecurType(); rt.setFreq(FreqRecurType.fromValue(r.getFrequency())); if (r.getCount() > 0) { rt.setCount(BigInteger.valueOf(r.getCount())); } Date until = r.getUntil(); if (until != null) { UntilRecurType u = new UntilRecurType(); XcalUtil.initUntilRecur(u, until.toString()); } if (r.getInterval() > 0) { rt.setInterval(String.valueOf(r.getInterval())); } listFromNumberList(rt.getBysecond(), r.getSecondList()); listFromNumberList(rt.getByminute(), r.getMinuteList()); listFromNumberList(rt.getByhour(), r.getHourList()); if (r.getDayList() != null) { List<String> l = rt.getByday(); for (Object o : r.getDayList()) { l.add(((WeekDay) o).getDay()); } } listFromNumberList(rt.getByyearday(), r.getYearDayList()); intlistFromNumberList(rt.getBymonthday(), r.getMonthDayList()); listFromNumberList(rt.getByweekno(), r.getWeekNoList()); intlistFromNumberList(rt.getBymonth(), r.getMonthList()); bigintlistFromNumberList(rt.getBysetpos(), r.getSetPosList()); return rt; }
public static String getRecurrence(final String rule) { if (rule != null) { try { final Recur recur = new Recur(rule); final String frequency = recur.getFrequency(); final WeekDayList wdl = recur.getDayList(); final Integer interval = recur.getInterval(); if ((wdl != null && wdl.size() > 0)) { // we only support one rule with daylist return CalendarEntry.WORKDAILY; } else if (interval != null && interval == 2) { // we only support one rule with interval return CalendarEntry.BIWEEKLY; } else { // native supportet rule return frequency; } } catch (final ParseException e) { log.error("cannot restore recurrence rule", e); } } return null; }
/** * Fügt die Wiederholungen des übergebenen Appointment-Objekts dem übergebenen * Event-Objekt hinzu. * * @param appointment Ein Rapla Appointment. */ private void addRepeatings(Appointment appointment, PropertyList properties) { Repeating repeating = appointment.getRepeating(); if (repeating == null) { return; } // This returns the strings DAYLY, WEEKLY, MONTHLY, YEARLY String type = repeating.getType().toString().toUpperCase(); Recur recur; // here is evaluated, if a COUNT is set in Rapla, or an enddate is // specified if (repeating.getNumber() == -1) { recur = new Recur(type, -1); } else if (repeating.isFixedNumber()) { recur = new Recur(type, repeating.getNumber()); } else { net.fortuna.ical4j.model.Date endDate = new net.fortuna.ical4j.model.Date(repeating.getEnd()); // TODO do we need to translate the enddate in utc? recur = new Recur(type, endDate); } if (repeating.isDaily()) { // DAYLY -> settings : intervall recur.setInterval(repeating.getInterval()); } else if (repeating.isWeekly()) { // WEEKLY -> settings : every nTh Weekday recur.setInterval(repeating.getInterval()); calendar.setTime(appointment.getStart()); recur.getDayList().add(WeekDay.getWeekDay(calendar)); } else if (repeating.isMonthly()) { // MONTHLY -> settings : every nTh Weekday recur.setInterval(repeating.getInterval()); calendar.setTime(appointment.getStart()); int weekofmonth = Math.round(calendar.get(java.util.Calendar.DAY_OF_MONTH) / DateTools.DAYS_PER_WEEK) + 1; recur.getDayList().add(new WeekDay(WeekDay.getWeekDay(calendar), weekofmonth)); } else if (repeating.isYearly()) { // YEARLY -> settings : every nTh day mTh Monthname calendar.setTime(appointment.getStart()); calendar.get(java.util.Calendar.DAY_OF_YEAR); } else { getLogger().warn("Invalid data in recurrency rule!"); } properties.add(new RRule(recur)); // bugfix - if rapla has no exceptions, an empty EXDATE: element is // produced. This may bother some iCal tools if (repeating.getExceptions().length == 0) { return; } // Add exception dates // DateList dl = new DateList(Value.DATE); ExDate exDate = new ExDate(); TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry(); net.fortuna.ical4j.model.TimeZone tz = registry.getTimeZone(timeZone.getID()); // rku: use seperate EXDATE for each exception for (Iterator<Date> itExceptions = Arrays.asList(repeating.getExceptions()).iterator(); itExceptions.hasNext(); ) { // DateList dl = new DateList(Value.DATE); Date date = itExceptions.next(); // dl.add(new net.fortuna.ical4j.model.Date( date)); java.util.Calendar cal = raplaLocale.createCalendar(); cal.setTime(date); int year = cal.get(java.util.Calendar.YEAR); int day_of_year = cal.get(java.util.Calendar.DAY_OF_YEAR); cal.setTime(appointment.getStart()); cal.set(java.util.Calendar.YEAR, year); cal.set(java.util.Calendar.DAY_OF_YEAR, day_of_year); int offset = (int) (tz.getOffset(DateTools.cutDate(date).getTime()) / DateTools.MILLISECONDS_PER_HOUR); cal.add(java.util.Calendar.HOUR, -offset); Date dateToSave = cal.getTime(); net.fortuna.ical4j.model.DateTime dateTime = new net.fortuna.ical4j.model.DateTime(); dateTime.setTime(dateToSave.getTime()); exDate.getDates().add(dateTime); } exDate.setTimeZone(tz); properties.add(exDate); // properties.add(new ExDate(dl)); }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] courseids = request.getParameterValues("courses"); // Saving the course ids as a session attribute (not necessary, might be better for saving) HttpSession session = request.getSession(true); session.setAttribute("courses", courseids); // Get course and time info for each id if (courseids == null) { } else { try { for (String id : courseids) { Integer courseid = Integer.parseInt(id); PreparedStatement getCourse = con.prepareStatement( "SELECT title, department, number, section " + "FROM courses WHERE id=?;"); getCourse.setInt(1, courseid); PreparedStatement getTime = con.prepareStatement( "SELECT day, starttime, endtime FROM times " + "WHERE course_id=?;"); getTime.setInt(1, courseid); ResultSet courseResult = getCourse.executeQuery(); ResultSet timesResult = getTime.executeQuery(); while (courseResult.next()) { // Generating the course from the DB, lets us utilize timeblocks Course course = new Course(); course.setTitle(courseResult.getString("title")); course.setDepartment(courseResult.getString("department")); course.setNumber(courseResult.getString("number")); course.setSection(courseResult.getString("section")); String courseTitle = course.getDepartment() + course.getNumber() + "-" + course.getSection() + ": " + course.getTitle(); while (timesResult.next()) { course.addTime( timesResult.getString("day").charAt(0), timesResult.getInt("starttime"), timesResult.getInt("endtime")); } // Now parse the timeshttp://ical4j.sourceforge.net/introduction.html for the timeblocks Map<String, Set<Character>> timeblocks = course.getTimeBlocks(); for (String startend : timeblocks.keySet()) { String[] StartEnd = startend.split("-"); Integer startInt = Integer.parseInt(StartEnd[0]); Integer endInt = Integer.parseInt(StartEnd[1]); java.util.Calendar startDate = this.getTimeCalendar(startInt); java.util.Calendar endDate = this.getTimeCalendar(endInt); DateTime startTime = new DateTime(startDate.getTime()); DateTime endTime = new DateTime(endDate.getTime()); VEvent courseEvent = new VEvent(startTime, endTime, courseTitle); Recur recur = new Recur(Recur.WEEKLY, null); // add the proper days to the recurrence for (char day : timeblocks.get(startend)) { recur.getDayList().add(dayMap.get(day)); } courseEvent.getProperties().add(new RRule(recur)); // Get unique identifier UidGenerator ug = new UidGenerator("uidGen"); Uid uid = ug.generateUid(); courseEvent.getProperties().add(uid); // Finally, add the time block to the calendar this.ical.getComponents().add(courseEvent); } } } } catch (SQLException e) { e.printStackTrace(); } } // Time to handle file download try { // making the file and outputting the calendar info File file = new File("mycalendar.ics"); int length = 0; FileOutputStream fout = new FileOutputStream(file); CalendarOutputter outputter = new CalendarOutputter(); outputter.output(ical, fout); ServletOutputStream outStream = response.getOutputStream(); response.setContentType("text/calendar"); response.setContentLength((int) file.length()); // sets HTTP header response.setHeader("Content-Disposition", "attachment; filename=calendar.ics"); byte[] byteBuffer = new byte[BUFSIZE]; DataInputStream in = new DataInputStream(new FileInputStream(file)); // reads the file's bytes and writes them to the response stream while ((in != null) && ((length = in.read(byteBuffer)) != -1)) { outStream.write(byteBuffer, 0, length); } in.close(); outStream.close(); } catch (ValidationException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void setUp() throws Exception { super.setUp(); registry = TimeZoneRegistryFactory.getInstance().createRegistry(); // create timezone property.. tz = registry.getTimeZone("Australia/Melbourne").getVTimeZone(); // create tzid parameter.. tzParam = new TzId(tz.getProperties().getProperty(Property.TZID).getValue()); Calendar weekday9AM = getCalendarInstance(); weekday9AM.set(2005, Calendar.MARCH, 7, 9, 0, 0); weekday9AM.set(Calendar.MILLISECOND, 0); Calendar weekday5PM = getCalendarInstance(); weekday5PM.set(2005, Calendar.MARCH, 7, 17, 0, 0); weekday5PM.set(Calendar.MILLISECOND, 0); // Do the recurrence until December 31st. Calendar untilCal = getCalendarInstance(); untilCal.set(2005, Calendar.DECEMBER, 31); untilCal.set(Calendar.MILLISECOND, 0); Date until = new Date(untilCal.getTime().getTime()); // 9:00AM to 5:00PM Rule using weekly Recur recurWeekly = new Recur(Recur.WEEKLY, until); recurWeekly.getDayList().add(WeekDay.MO); recurWeekly.getDayList().add(WeekDay.TU); recurWeekly.getDayList().add(WeekDay.WE); recurWeekly.getDayList().add(WeekDay.TH); recurWeekly.getDayList().add(WeekDay.FR); recurWeekly.setInterval(1); recurWeekly.setWeekStartDay(WeekDay.MO.getDay()); RRule rruleWeekly = new RRule(recurWeekly); // 9:00AM to 5:00PM Rule using daily frequency Recur recurDaily = new Recur(Recur.DAILY, until); recurDaily.getDayList().add(WeekDay.MO); recurDaily.getDayList().add(WeekDay.TU); recurDaily.getDayList().add(WeekDay.WE); recurDaily.getDayList().add(WeekDay.TH); recurDaily.getDayList().add(WeekDay.FR); recurDaily.setInterval(1); recurDaily.setWeekStartDay(WeekDay.MO.getDay()); RRule rruleDaily = new RRule(recurDaily); // 9:00AM to 5:00PM Rule using monthly frequency Recur recurMonthly = new Recur(Recur.MONTHLY, until); recurMonthly.getDayList().add(WeekDay.MO); recurMonthly.getDayList().add(WeekDay.TU); recurMonthly.getDayList().add(WeekDay.WE); recurMonthly.getDayList().add(WeekDay.TH); recurMonthly.getDayList().add(WeekDay.FR); recurMonthly.setInterval(1); recurMonthly.setWeekStartDay(WeekDay.MO.getDay()); RRule rruleMonthly = new RRule(recurMonthly); Summary summary = new Summary("TEST EVENTS THAT HAPPEN 9-5 MON-FRI DEFINED WEEKLY"); weekdayNineToFiveEvents = new VEvent(); weekdayNineToFiveEvents.getProperties().add(rruleWeekly); weekdayNineToFiveEvents.getProperties().add(summary); DtStart dtStart = new DtStart(new DateTime(weekday9AM.getTime().getTime())); // dtStart.getParameters().add(Value.DATE); weekdayNineToFiveEvents.getProperties().add(dtStart); DtEnd dtEnd = new DtEnd(new DateTime(weekday5PM.getTime().getTime())); // dtEnd.getParameters().add(Value.DATE); weekdayNineToFiveEvents.getProperties().add(dtEnd); // ensure event is valid.. weekdayNineToFiveEvents.validate(); summary = new Summary("TEST EVENTS THAT HAPPEN 9-5 MON-FRI DEFINED DAILY"); dailyWeekdayEvents = new VEvent(); dailyWeekdayEvents.getProperties().add(rruleDaily); dailyWeekdayEvents.getProperties().add(summary); DtStart dtStart2 = new DtStart(new DateTime(weekday9AM.getTime().getTime())); // dtStart2.getParameters().add(Value.DATE); dailyWeekdayEvents.getProperties().add(dtStart2); DtEnd dtEnd2 = new DtEnd(new DateTime(weekday5PM.getTime().getTime())); // dtEnd2.getParameters().add(Value.DATE); dailyWeekdayEvents.getProperties().add(dtEnd2); // ensure event is valid.. dailyWeekdayEvents.validate(); summary = new Summary("TEST EVENTS THAT HAPPEN 9-5 MON-FRI DEFINED MONTHLY"); monthlyWeekdayEvents = new VEvent(); monthlyWeekdayEvents.getProperties().add(rruleMonthly); monthlyWeekdayEvents.getProperties().add(summary); DtStart dtStart3 = new DtStart(new DateTime(weekday9AM.getTime().getTime())); // dtStart3.getParameters().add(Value.DATE); monthlyWeekdayEvents.getProperties().add(dtStart3); DtEnd dtEnd3 = new DtEnd(new DateTime(weekday5PM.getTime().getTime())); // dtEnd3.getParameters().add(Value.DATE); monthlyWeekdayEvents.getProperties().add(dtEnd3); // ensure event is valid.. monthlyWeekdayEvents.validate(); }