All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.fortuna.ical4j.model.rfc5545.VEventRule Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package net.fortuna.ical4j.model.rfc5545;

import java.util.Calendar;
import java.util.List;

import net.fortuna.ical4j.model.Date;
import net.fortuna.ical4j.model.Parameter;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.Rfc5545ComponentRule;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.parameter.Value;
import net.fortuna.ical4j.model.property.DateProperty;
import net.fortuna.ical4j.model.property.DtStamp;

/**
 * 
 * @author daniel grigore
 * @author corneliu dobrota
 * @author stefan popescu
 *
 */
public class VEventRule implements Rfc5545ComponentRule {

    @Override
    public void applyTo(VEvent element) {

        Property start = element.getProperty(Property.DTSTART);
        Property end = element.getProperty(Property.DTEND);
        Property duration = element.getProperty(Property.DURATION);
        
        /*
         *     ; Either 'dtend' or 'duration' MAY appear in
         *     ; a 'eventprop', but 'dtend' and 'duration'
         *     ; MUST NOT occur in the same 'eventprop'.
         */
        if (end != null && duration != null && end.getValue() != null && duration != null) {
            element.getProperties().remove(duration);
        }
        
        /*
         *      If the event is allDay, start and end must not be equal,
         *      so we add 1 day to the end date
         */  
        if (start!=null && end!=null){
            Parameter startType = start.getParameter(Parameter.VALUE);
            Parameter endType = end.getParameter(Parameter.VALUE);
            if (startType!=null && endType!=null &&
                    startType.getValue().equals(Value.DATE.getValue()) &&
                    endType.getValue().equals(Value.DATE.getValue()) &&
                    start.getValue().equals(end.getValue())){
                if (end instanceof DateProperty) {
                    DateProperty endDate = (DateProperty) end;
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(endDate.getDate());
                    cal.add(Calendar.DATE, 1);
                    endDate.setDate(new Date(cal.getTime()));
                }
            }
        }
        
        List dtStamps = element.getProperties(Property.DTSTAMP);
        if (dtStamps == null || dtStamps.isEmpty()) {
            element.getProperties().add(new DtStamp());
        }     
        
    }

    @Override
    public Class getSupportedType() {
        return VEvent.class;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy