Java Code Examples for net.fortuna.ical4j.model.component.VEvent#getStartDate()

The following examples show how to use net.fortuna.ical4j.model.component.VEvent#getStartDate() . 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: ICalendarUtils.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Get the duration for an event.  If the DURATION property
 * exist, use that.  Else, calculate duration from DTSTART and
 * DTEND.
 * @param event The event.
 * @return duration for event
 */
public static Dur getDuration(VEvent event) {
    Duration duration = (Duration)
        event.getProperties().getProperty(Property.DURATION);
    if (duration != null) {
        return duration.getDuration();
    }
    DtStart dtstart = event.getStartDate();
    if (dtstart == null) {
        return null;
    }
    DtEnd dtend = (DtEnd) event.getProperties().getProperty(Property.DTEND);
    if (dtend == null) {
        return null;
    }
    return new Duration(dtstart.getDate(), dtend.getDate()).getDuration();
}
 
Example 2
Source File: EventValidator.java    From cosmo with Apache License 2.0 6 votes vote down vote up
private static boolean isEventValid(VEvent event, ValidationConfig config) {
    if (config == null) {
        LOG.error("ValidationConfig cannot be null");
        return false;
    }
    DtStart startDate = event.getStartDate();
    DtEnd endDate = event.getEndDate(true);
    if (startDate == null || startDate.getDate() == null
            || endDate != null && startDate.getDate().after(endDate.getDate())) {

        return false;
    }

    for (PropertyValidator validator : values()) {
        if (!validator.isValid(event, config)) {
            return false;
        }
    }

    return areTimeZoneIdsValid(event);
}
 
Example 3
Source File: RecurrenceExpander.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Determine if date is a valid occurence in recurring calendar component
 * @param calendar recurring calendar component
 * @param occurrence occurrence date
 * @return true if the occurrence date is a valid occurrence, otherwise false
 */
public boolean isOccurrence(Calendar calendar, Date occurrence) {
    java.util.Calendar cal = Dates.getCalendarInstance(occurrence);
    cal.setTime(occurrence);
   
    // Add a second or day (one unit forward) so we can set a range for
    // finding instances.  This is required because ical4j's Recur apis
    // only calculate recurring dates up until but not including the 
    // end date of the range.
    if(occurrence instanceof DateTime) {
        cal.add(java.util.Calendar.SECOND, 1);
    }
    else {
        cal.add(java.util.Calendar.DAY_OF_WEEK, 1);
    }
    
    Date rangeEnd = 
        org.unitedinternet.cosmo.calendar.util.Dates.getInstance(cal.getTime(), occurrence);
    
    TimeZone tz = null;
    
    for(Object obj : calendar.getComponents(Component.VEVENT)){
    	VEvent evt = (VEvent)obj;
    	if(evt.getRecurrenceId() == null && evt.getStartDate() != null){
    		tz = evt.getStartDate().getTimeZone();
    	}
    }
    InstanceList instances = getOcurrences(calendar, occurrence, rangeEnd, tz);
    
    for(Iterator<Instance> it = instances.values().iterator(); it.hasNext();) {
        Instance instance = it.next();
        if(instance.getRid().getTime()==occurrence.getTime()) {
            return true;
        }
    }
    
    return false;
}
 
Example 4
Source File: HibBaseEventStamp.java    From cosmo with Apache License 2.0 5 votes vote down vote up
public Date getStartDate() {
    VEvent event = getEvent();
    if(event==null) {
        return null;
    }
    
    DtStart dtStart = event.getStartDate();
    if (dtStart == null) {
        return null;
    }
    return dtStart.getDate();
}
 
Example 5
Source File: MockBaseEventStamp.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Gets start date.
 * @return date.
 */
public Date getStartDate() {
    VEvent event = getEvent();
    if (event==null) {
        return null;
    }
    
    DtStart dtStart = event.getStartDate();
    if (dtStart == null) {
        return null;
    }
    return dtStart.getDate();
}
 
Example 6
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 7
Source File: EntityConverter.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Sets calendar attributes.
 * @param note The note item.
 * @param event The event.
 */
private void setCalendarAttributes(NoteItem note, VEvent event) {
    
    // UID (only set if master)
    if(event.getUid()!=null && note.getModifies()==null) {
        note.setIcalUid(event.getUid().getValue());
    }
    
    // for now displayName is limited to 1024 chars
    if (event.getSummary() != null) {
        note.setDisplayName(StringUtils.substring(event.getSummary()
                .getValue(), 0, 1024));
    }

    if (event.getDescription() != null) {
        note.setBody(event.getDescription().getValue());
    }

    // look for DTSTAMP
    if(event.getDateStamp()!=null) {
        note.setClientModifiedDate(event.getDateStamp().getDate());
    }
    
    // look for absolute VALARM
    VAlarm va = ICalendarUtils.getDisplayAlarm(event);
    if (va != null && va.getTrigger()!=null) {
        Trigger trigger = va.getTrigger();
        Date reminderTime = trigger.getDateTime();
        if (reminderTime != null) {
            note.setReminderTime(reminderTime);
        }
    }

    // calculate triage status based on start date
    java.util.Date now =java.util.Calendar.getInstance().getTime();
    Date eventStartDate = event.getStartDate() != null && event.getStartDate().getDate() != null 
    		? event.getStartDate().getDate()
    		:new Date();
    boolean later = eventStartDate.after(now);
    int code = later ? TriageStatus.CODE_LATER : TriageStatus.CODE_DONE;
    
    TriageStatus triageStatus = note.getTriageStatus();
    
    // initialize TriageStatus if not present
    if (triageStatus == null) {
        triageStatus = TriageStatusUtil.initialize(entityFactory
                .createTriageStatus());
        note.setTriageStatus(triageStatus);
    }

    triageStatus.setCode(code);
    
    // check for X-OSAF-STARRED
    if ("TRUE".equals(ICalendarUtils.getXProperty(X_OSAF_STARRED, event))) {
        TaskStamp ts = StampUtils.getTaskStamp(note);
        if (ts == null) {
            note.addStamp(entityFactory.createTaskStamp());
        }
    }
}
 
Example 8
Source File: ICalendarService.java    From axelor-open-suite with GNU Affero General Public License v3.0 4 votes vote down vote up
@Transactional
protected ICalendarEvent findOrCreateEvent(VEvent vEvent, ICalendar calendar) {

  String uid = vEvent.getUid().getValue();
  DtStart dtStart = vEvent.getStartDate();
  DtEnd dtEnd = vEvent.getEndDate();

  ICalendarEvent event = iEventRepo.findByUid(uid);
  if (event == null) {
    event = ICalendarEventFactory.getNewIcalEvent(calendar);
    event.setUid(uid);
    event.setCalendar(calendar);
  }

  ZoneId zoneId = OffsetDateTime.now().getOffset();
  if (dtStart.getDate() != null) {
    if (dtStart.getTimeZone() != null) {
      zoneId = dtStart.getTimeZone().toZoneId();
    }
    event.setStartDateTime(LocalDateTime.ofInstant(dtStart.getDate().toInstant(), zoneId));
  }

  if (dtEnd.getDate() != null) {
    if (dtEnd.getTimeZone() != null) {
      zoneId = dtEnd.getTimeZone().toZoneId();
    }
    event.setEndDateTime(LocalDateTime.ofInstant(dtEnd.getDate().toInstant(), zoneId));
  }

  event.setAllDay(!(dtStart.getDate() instanceof DateTime));

  event.setSubject(getValue(vEvent, Property.SUMMARY));
  event.setDescription(getValue(vEvent, Property.DESCRIPTION));
  event.setLocation(getValue(vEvent, Property.LOCATION));
  event.setGeo(getValue(vEvent, Property.GEO));
  event.setUrl(getValue(vEvent, Property.URL));
  event.setSubjectTeam(event.getSubject());
  if (Clazz.PRIVATE.getValue().equals(getValue(vEvent, Property.CLASS))) {
    event.setVisibilitySelect(ICalendarEventRepository.VISIBILITY_PRIVATE);
  } else {
    event.setVisibilitySelect(ICalendarEventRepository.VISIBILITY_PUBLIC);
  }
  if (Transp.TRANSPARENT.getValue().equals(getValue(vEvent, Property.TRANSP))) {
    event.setDisponibilitySelect(ICalendarEventRepository.DISPONIBILITY_AVAILABLE);
  } else {
    event.setDisponibilitySelect(ICalendarEventRepository.DISPONIBILITY_BUSY);
  }
  if (event.getVisibilitySelect() == ICalendarEventRepository.VISIBILITY_PRIVATE) {
    event.setSubjectTeam(I18n.get("Available"));
    if (event.getDisponibilitySelect() == ICalendarEventRepository.DISPONIBILITY_BUSY) {
      event.setSubjectTeam(I18n.get("Busy"));
    }
  }
  ICalendarUser organizer = findOrCreateUser(vEvent.getOrganizer(), event);
  if (organizer != null) {
    event.setOrganizer(organizer);
    iCalendarUserRepository.save(organizer);
  }

  for (Object item : vEvent.getProperties(Property.ATTENDEE)) {
    ICalendarUser attendee = findOrCreateUser((Property) item, event);
    if (attendee != null) {
      event.addAttendee(attendee);
      iCalendarUserRepository.save(attendee);
    }
  }
  iEventRepo.save(event);
  return event;
}
 
Example 9
Source File: MainActivity.java    From ICSImport with GNU General Public License v3.0 4 votes vote down vote up
@Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       Intent intent = getIntent();

Uri data = intent.getData();

try {
	//use ical4j to parse the event
	CalendarBuilder cb = new CalendarBuilder();
	Calendar calendar = null;
	calendar = cb.build(getStreamFromOtherSource(data));

	if (calendar != null) {

		Iterator i = calendar.getComponents(Component.VEVENT).iterator();

		while (i.hasNext()) {
			VEvent event = (VEvent) i.next();

			Intent insertIntent = new Intent(Intent.ACTION_INSERT)
				.setType("vnd.android.cursor.item/event");

			if (event.getStartDate() != null)
				insertIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, event.getStartDate().getDate().getTime());

			if (event.getEndDate() != null)
				insertIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, event.getEndDate().getDate().getTime());

			if (event.getSummary() != null)
				insertIntent.putExtra(CalendarContract.Events.TITLE, event.getSummary().getValue());

			if (event.getDescription() != null)
				insertIntent.putExtra(CalendarContract.Events.DESCRIPTION, event.getDescription().getValue());

			if (event.getLocation() != null) 
				insertIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, event.getLocation().getValue());

			insertIntent.putExtra(CalendarContract.Events.ACCESS_LEVEL, CalendarContract.Events.ACCESS_PRIVATE);
			startActivity(insertIntent);

		}
	}

} catch (Exception e) {
	e.printStackTrace();
	Toast.makeText(getBaseContext(), "Invalid ICS file", Toast.LENGTH_LONG).show();
}
finish();

   }