net.fortuna.ical4j.model.property.RRule Java Examples

The following examples show how to use net.fortuna.ical4j.model.property.RRule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TeamEventUtils.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
public static String calculateRRule(final TeamEventRecurrenceData recurData)
{
  if (recurData == null || recurData.getFrequency() == null || recurData.getFrequency() == RecurrenceFrequency.NONE) {
    return null;
  }
  if (recurData.isCustomized() == false) {
    recurData.setInterval(1);
  }
  final Recur recur = new Recur();
  final net.fortuna.ical4j.model.Date untilDate = ICal4JUtils.getICal4jDate(recurData.getUntil(), recurData.getTimeZone());
  if (untilDate != null) {
    recur.setUntil(untilDate);
  }
  recur.setInterval(recurData.getInterval());
  recur.setFrequency(ICal4JUtils.getCal4JFrequencyString(recurData.getFrequency()));
  final RRule rrule = new RRule(recur);
  return rrule.getValue();
}
 
Example #2
Source File: ICalRecurConverter.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(Intersection expr) {
    this.stateStack.push(this.state);
    VisitorState newState = new VisitorState();
    newState.isExcluded = this.state.isExcluded;
    newState.isIntersection = true;
    this.state = newState;
    for (TemporalExpression childExpr : expr.getExpressionSet()) {
        childExpr.accept(this);
    }
    this.state = this.stateStack.pop();
    if (newState.inclRecurList.size() > 0) {
        this.incRuleList.add(new RRule(this.consolidateRecurs(newState.inclRecurList)));
    }
    if (newState.exRecurList.size() > 0) {
        this.exRuleList.add(new ExRule(this.consolidateRecurs(newState.exRecurList)));
    }
}
 
Example #3
Source File: ICalRecurrence.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public ICalRecurrence(String rrule_text) throws ImportException {
	
	if (rrule_text == null) {
		return;	
	}
	
	try {
		this.rrule_text = rrule_text;
		this.rrule = new RRule(rrule_text);
	} catch (ParseException e) {
		log.warn("Parse exception for iCal recurrence rule: "+rrule_text);
		throw new ImportException(e);
	}
	
	// The RRule has been successfully created, now use it.
	recur = rrule.getRecur();

	// Make sure the rule makes sense.
	isValidateRRule();
}
 
Example #4
Source File: ICal4JUtils.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @param rruleString
 * @return null if rruleString is empty, otherwise new RRule object.
 */
public static RRule calculateRecurrenceRule(final String rruleString)
{
  if (StringUtils.isBlank(rruleString) == true) {
    return null;
  }
  try {
    final RRule rule = new RRule(rruleString);
    // set the recurrence end date to the last minute of the day
    final Recur recur = rule.getRecur();
    final net.fortuna.ical4j.model.Date until = recur != null ? recur.getUntil() : null;
    if (until != null) {
      final Date untilEndOfDay = CalendarUtils.getEndOfDay(until, PFUserContext.getTimeZone());
      recur.setUntil(new net.fortuna.ical4j.model.Date(untilEndOfDay));
    }
    return rule;
  } catch (final ParseException ex) {
    log.error("Exception encountered while parsing rrule '" + rruleString + "': " + ex.getMessage(), ex);
    return null;
  }
}
 
Example #5
Source File: EventValidator.java    From cosmo with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isValid(VEvent event, ValidationConfig config) {

    List<? extends Property> rrules = event.getProperties(prop);
    if (rrules == null) {
        return true;
    }
    for (Property p : rrules) {
        RRule rrule = (RRule) p;
        if (!isRRuleValid(rrule, config)) {
            return false;
        }
    }

    return true;
}
 
Example #6
Source File: EventValidator.java    From cosmo with Apache License 2.0 6 votes vote down vote up
private boolean isRRuleValid(RRule rrule, ValidationConfig config) {
    if (rrule == null) {
        return true;
    }

    if (rrule.getRecur() == null || rrule.getRecur().getFrequency() == null) {
        return false;
    }

    String recurFrequency = rrule.getRecur().getFrequency();
    if (!config.getAllowedRecurrenceFrequencies().contains(recurFrequency)) {
        return false;
    }

    return true;
}
 
Example #7
Source File: ICalRecurrence.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public ICalRecurrence(String rrule_text) throws ImportException {
	
	if (rrule_text == null) {
		return;	
	}
	
	try {
		this.rrule_text = rrule_text;
		this.rrule = new RRule(rrule_text);
	} catch (ParseException e) {
		log.warn("Parse exception for iCal recurrence rule: "+rrule_text);
		throw new ImportException(e);
	}
	
	// The RRule has been successfully created, now use it.
	recur = rrule.getRecur();

	// Make sure the rule makes sense.
	isValidateRRule();
}
 
Example #8
Source File: MockBaseEventStamp.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Gets recurrence rules.
 * @return The list.
 */
public List<Recur> getRecurrenceRules() {
    ArrayList<Recur> l = new ArrayList<Recur>();
    VEvent event = getEvent();
    if(event!=null) {
        for (Object rrule : getEvent().getProperties().getProperties(Property.RRULE)) {
            l.add(((RRule)rrule).getRecur());
        }
    }
    return l;
}
 
Example #9
Source File: TeamEventUtils.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
public static VEvent createVEvent(final TeamEventDO eventDO, final TimeZone timezone)
{
  final VEvent vEvent = ICal4JUtils.createVEvent(eventDO.getStartDate(), eventDO.getEndDate(), eventDO.getUid(), eventDO.getSubject(),
      eventDO.isAllDay(), timezone);
  if (eventDO.hasRecurrence() == true) {
    final RRule rrule = eventDO.getRecurrenceRuleObject();
    vEvent.getProperties().add(rrule);
  }
  return vEvent;
}
 
Example #10
Source File: TeamEventDO.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Will be renewed if {@link #setRecurrenceRule(String)} is called.
 * @return the recurrenceRuleObject
 */
@Transient
public Recur getRecurrenceObject()
{
  final RRule rrule = getRecurrenceRuleObject();
  return rrule != null ? rrule.getRecur() : null;
}
 
Example #11
Source File: TeamEventDO.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Will be renewed if {@link #setRecurrenceRule(String)} is called.
 * @return the recurrenceRuleObject
 */
@Transient
public RRule getRecurrenceRuleObject()
{
  if (recurrenceRuleObject == null) {
    recalculate();
  }
  return recurrenceRuleObject;
}
 
Example #12
Source File: MockBaseEventStamp.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Sets recurrence rules.
 * @param recurs List with recurrence rules.
 */
public void setRecurrenceRules(List<Recur> recurs) {
    if (recurs == null) {
        return;
    }
    PropertyList<Property> pl = getEvent().getProperties();
    for (Property rrule : pl.getProperties(Property.RRULE)) {
        pl.remove(rrule);
    }
    for (Recur recur : recurs) {
        pl.add(new RRule(recur));
    }
  
}
 
Example #13
Source File: HibBaseEventStamp.java    From cosmo with Apache License 2.0 5 votes vote down vote up
public void setRecurrenceRules(List<Recur> recurs) {
    if (recurs == null) {
        return;
    }
    PropertyList<Property> properties = getEvent().getProperties();
    for (Property rrule : properties.getProperties(Property.RRULE)) {
        properties.remove(rrule);
    }
    for (Recur recur : recurs) {
        properties.add(new RRule(recur));
    }      
}
 
Example #14
Source File: HibBaseEventStamp.java    From cosmo with Apache License 2.0 5 votes vote down vote up
public List<Recur> getRecurrenceRules() {
    List<Recur> toReturn = new ArrayList<>();
    VEvent event = getEvent();
    if(event != null) {
        PropertyList<RRule> rruleProperties = event.getProperties().getProperties(Property.RRULE);
        for (RRule rrule : rruleProperties) {
            toReturn.add(rrule.getRecur());
        }
    }
    return toReturn;
}
 
Example #15
Source File: ICalRecurConverter.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public void addRecur(Recur recur) {
    if (this.isIntersection) {
        if (this.isExcluded) {
            this.exRecurList.add(recur);
        } else {
            this.inclRecurList.add(recur);
        }
    } else {
        if (this.isExcluded) {
            exRuleList.add(new ExRule(recur));
        } else {
            incRuleList.add(new RRule(recur));
        }
    }
}
 
Example #16
Source File: CalendarUtils.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Build iCalendar-compliant recurrence rule
 * 
 * @param recurrence
 * @param recurrenceEnd
 * @return rrule
 */
public static String getRecurrenceRule(final String recurrence, final Date recurrenceEnd) {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrence != null) { // recurrence available
        // create recurrence rule
        final StringBuilder sb = new StringBuilder();
        sb.append("FREQ=");
        if (recurrence.equals(CalendarEntry.WORKDAILY)) {
            // build rule for monday to friday
            sb.append(CalendarEntry.DAILY);
            sb.append(";");
            sb.append("BYDAY=MO,TU,WE,TH,FR");
        } else if (recurrence.equals(CalendarEntry.BIWEEKLY)) {
            // build rule for biweekly
            sb.append(CalendarEntry.WEEKLY);
            sb.append(";");
            sb.append("INTERVAL=2");
        } else {
            // normal supported recurrence
            sb.append(recurrence);
        }
        if (recurrenceEnd != null) {
            final DateTime recurEndDT = new DateTime(recurrenceEnd.getTime());
            recurEndDT.setTimeZone(tz);
            sb.append(";");
            sb.append(CalendarEntry.UNTIL);
            sb.append("=");
            sb.append(recurEndDT.toString());
        }
        try {
            final Recur recur = new Recur(sb.toString());
            final RRule rrule = new RRule(recur);
            return rrule.getValue();
        } catch (final ParseException e) {
            log.error("cannot create recurrence rule: " + recurrence.toString(), e);
        }
    }

    return null;
}
 
Example #17
Source File: CalendarUtils.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Build iCalendar-compliant recurrence rule
 * 
 * @param recurrence
 * @param recurrenceEnd
 * @return rrule
 */
public static String getRecurrenceRule(final String recurrence, final Date recurrenceEnd) {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrence != null) { // recurrence available
        // create recurrence rule
        final StringBuilder sb = new StringBuilder();
        sb.append("FREQ=");
        if (recurrence.equals(CalendarEntry.WORKDAILY)) {
            // build rule for monday to friday
            sb.append(CalendarEntry.DAILY);
            sb.append(";");
            sb.append("BYDAY=MO,TU,WE,TH,FR");
        } else if (recurrence.equals(CalendarEntry.BIWEEKLY)) {
            // build rule for biweekly
            sb.append(CalendarEntry.WEEKLY);
            sb.append(";");
            sb.append("INTERVAL=2");
        } else {
            // normal supported recurrence
            sb.append(recurrence);
        }
        if (recurrenceEnd != null) {
            final DateTime recurEndDT = new DateTime(recurrenceEnd.getTime());
            recurEndDT.setTimeZone(tz);
            sb.append(";");
            sb.append(CalendarEntry.UNTIL);
            sb.append("=");
            sb.append(recurEndDT.toString());
        }
        try {
            final Recur recur = new Recur(sb.toString());
            final RRule rrule = new RRule(recur);
            return rrule.getValue();
        } catch (final ParseException e) {
            log.error("cannot create recurrence rule: " + recurrence.toString(), e);
        }
    }

    return null;
}
 
Example #18
Source File: Calendar_EventServiceAdv.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 *  删除重复日程事件中某一事件开始后续所有的日程事件信息(一定是重复日程)
 *  
 * 1、删除当前事件以及该重复事件主体信息已经生成的该ID事件后续时间内所有已经生成的日程事件信息
 * 2、更新日程事件重复信息主体,将截止日期改为指定ID所在的日期的前一天的23:59:59
 * 
 * @param repeatMasterId
 * @param calendar_event
 * @return
 * @throws Exception
 */
public Integer destoryAfterEventId( String repeatMasterId, Calendar_Event calendar_event ) throws Exception {		
	
	if( StringUtils.isEmpty( repeatMasterId )) {
		throw new Exception("calendar repeat master id is empty, calendar can not delete!" );
	}
	
	if( calendar_event == null ) {
		throw new Exception("calendar event is null, calendar can not delete!" );
	}
	
	Integer count = 0;
	Business business = null;
	List<String> eventIds = null;
	List<Calendar_Event> calendarEvents = null;
	Calendar_EventRepeatMaster calendar_EventRepeatMaster = null;
	try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
		business = new Business(emc);
		calendar_EventRepeatMaster = emc.find( calendar_event.getRepeatMasterId(), Calendar_EventRepeatMaster.class );
		
		//-----------------------------------------------------------------------------------------------------------------
		//1、删除当前事件以及该重复事件主体信息已经生成的该ID事件后续时间内所有已经生成的日程事件信息
		eventIds = business.calendar_EventFactory().listWithRepeatMaster(repeatMasterId, calendar_event.getStartTime(), null );
		if( ListTools.isNotEmpty( eventIds )) {
			count = eventIds.size();
			calendarEvents = business.calendar_EventFactory().list( eventIds );
		}
		if( ListTools.isNotEmpty( calendarEvents )) {
			emc.beginTransaction( Calendar_Event.class );	
			for( Calendar_Event calendar_Event : calendarEvents ) {
				emc.remove( calendar_Event, CheckRemoveType.all );
			}
		}
		
		//将截止日期改为指定ID所在的日期的前一天的23:59:59
		Date repeatEndTime = dateOperation.getEndTimeInDay( dateOperation.getDayAddDate( calendar_event.getStartTime(), -1 ) );
		RRule rule = new RRule( calendar_EventRepeatMaster.getRecurrenceRule() );
		Recur recur = rule.getRecur();
		recur.setUntil( new net.fortuna.ical4j.model.Date(repeatEndTime) );		
		calendar_EventRepeatMaster.setRecurrenceRule( new RRule(recur).getValue());
		
		emc.beginTransaction( Calendar_EventRepeatMaster.class );
		emc.check( calendar_EventRepeatMaster, CheckPersistType.all );
		emc.commit();
	} catch ( Exception e ) {
		throw e;
	}
	return count;
}
 
Example #19
Source File: ICal4JUtils.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param rruleString
 * @see ICal4JUtils#calculateRecurrenceRule(String)
 * @see RRule#getRecur()
 */
public static Recur calculateRecurrence(final String rruleString)
{
  final RRule rule = calculateRecurrenceRule(rruleString);
  return rule != null ? rule.getRecur() : null;
}
 
Example #20
Source File: IcsFileImporter.java    From ganttproject with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Reads calendar events from file
 * @return a list of events if file was parsed successfully or null otherwise
 */
private static List<CalendarEvent> readEvents(File f) {
  try {
    CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, true);
    CalendarBuilder builder = new CalendarBuilder();
    List<CalendarEvent> gpEvents = Lists.newArrayList();
    Calendar c = builder.build(new UnfoldingReader(new FileReader(f)));
    for (Component comp : (List<Component>)c.getComponents()) {
      if (comp instanceof VEvent) {
        VEvent event = (VEvent) comp;
        if (event.getStartDate() == null) {
          GPLogger.log("No start date found, ignoring. Event="+event);
          continue;
        }
        Date eventStartDate = event.getStartDate().getDate();
        if (event.getEndDate() == null) {
          GPLogger.log("No end date found, using start date instead. Event="+event);
        }
        Date eventEndDate = event.getEndDate() == null ? eventStartDate : event.getEndDate().getDate();
        TimeDuration oneDay = GPTimeUnitStack.createLength(GPTimeUnitStack.DAY, 1);
        if (eventEndDate != null) {
          java.util.Date startDate = GPTimeUnitStack.DAY.adjustLeft(eventStartDate);
          java.util.Date endDate = GPTimeUnitStack.DAY.adjustLeft(eventEndDate);
          RRule recurrenceRule = (RRule) event.getProperty(Property.RRULE);
          boolean recursYearly = false;
          if (recurrenceRule != null) {
            recursYearly = Recur.YEARLY.equals(recurrenceRule.getRecur().getFrequency()) && 1 == recurrenceRule.getRecur().getInterval();
          }
          while (startDate.compareTo(endDate) <= 0) {
            Summary summary = event.getSummary();
            gpEvents.add(CalendarEvent.newEvent(
                startDate, recursYearly, CalendarEvent.Type.HOLIDAY,
                summary == null ? "" : summary.getValue(),
                null));
            startDate = GPCalendarCalc.PLAIN.shiftDate(startDate, oneDay);
          }
        }
      }
    }
    return gpEvents;
  } catch (IOException | ParserException e) {
    GPLogger.log(e);
    return null;
  }
}
 
Example #21
Source File: Calendar_EventServiceAdv.java    From o2oa with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 *  删除重复日程事件中某一事件开始后续所有的日程事件信息(一定是重复日程)
 *  
 * 1、删除当前事件以及该重复事件主体信息已经生成的该ID事件后续时间内所有已经生成的日程事件信息
 * 2、更新日程事件重复信息主体,将截止日期改为指定ID所在的日期的前一天的23:59:59
 * 
 * @param repeatMasterId
 * @param calendar_event
 * @return
 * @throws Exception
 */
public Integer destoryAfterEventId( String repeatMasterId, Calendar_Event calendar_event ) throws Exception {		
	
	if( StringUtils.isEmpty( repeatMasterId )) {
		throw new Exception("calendar repeat master id is empty, calendar can not delete!" );
	}
	
	if( calendar_event == null ) {
		throw new Exception("calendar event is null, calendar can not delete!" );
	}
	
	Integer count = 0;
	Business business = null;
	List<String> eventIds = null;
	List<Calendar_Event> calendarEvents = null;
	Calendar_EventRepeatMaster calendar_EventRepeatMaster = null;
	try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
		business = new Business(emc);
		calendar_EventRepeatMaster = emc.find( calendar_event.getRepeatMasterId(), Calendar_EventRepeatMaster.class );
		
		//-----------------------------------------------------------------------------------------------------------------
		//1、删除当前事件以及该重复事件主体信息已经生成的该ID事件后续时间内所有已经生成的日程事件信息
		eventIds = business.calendar_EventFactory().listWithRepeatMaster(repeatMasterId, calendar_event.getStartTime(), null );
		if( ListTools.isNotEmpty( eventIds )) {
			count = eventIds.size();
			calendarEvents = business.calendar_EventFactory().list( eventIds );
		}
		if( ListTools.isNotEmpty( calendarEvents )) {
			emc.beginTransaction( Calendar_Event.class );	
			for( Calendar_Event calendar_Event : calendarEvents ) {
				emc.remove( calendar_Event, CheckRemoveType.all );
			}
		}
		
		//将截止日期改为指定ID所在的日期的前一天的23:59:59
		Date repeatEndTime = dateOperation.getEndTimeInDay( dateOperation.getDayAddDate( calendar_event.getStartTime(), -1 ) );
		RRule rule = new RRule( calendar_EventRepeatMaster.getRecurrenceRule() );
		Recur recur = rule.getRecur();
		recur.setUntil( new net.fortuna.ical4j.model.Date(repeatEndTime) );		
		calendar_EventRepeatMaster.setRecurrenceRule( new RRule(recur).getValue());
		
		emc.beginTransaction( Calendar_EventRepeatMaster.class );
		emc.check( calendar_EventRepeatMaster, CheckPersistType.all );
		emc.commit();
	} catch ( Exception e ) {
		throw e;
	}
	return count;
}