Java Code Examples for net.fortuna.ical4j.model.Calendar#validate()

The following examples show how to use net.fortuna.ical4j.model.Calendar#validate() . 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: TimezoneValidator.java    From cosmo with Apache License 2.0 6 votes vote down vote up
public boolean isValid(Calendar value, ConstraintValidatorContext context) {
    if(value==null) {
        return true;
    }
    
    try {
        Calendar calendar = (Calendar) value;
        
        // validate entire icalendar object
        calendar.validate(true);
        
        // make sure we have a VTIMEZONE
        VTimeZone timezone = (VTimeZone) calendar.getComponents()
                .getComponents(Component.VTIMEZONE).get(0);
        return timezone != null;
    } catch(ValidationException ve) {
        return false;
    } catch (RuntimeException e) {
        return false;
    }
}
 
Example 2
Source File: EntityConverterTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Tests get calendar from collection.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testGetCalendarFromCollection() throws Exception {
    
    Calendar c1 = getCalendar("eventwithtimezone1.ics");
    Calendar c2 = getCalendar("vtodo.ics");
    NoteItem note1 = converter.convertEventCalendar(c1).iterator().next();
    NoteItem note2 = converter.convertTaskCalendar(c2);
   
    MockCollectionItem collection = new MockCollectionItem();
    collection.addStamp(new MockCalendarCollectionStamp(collection));
    collection.addChild(note1);
    collection.addChild(note2);
    
    Calendar fullCal = converter.convertCollection(collection);
    fullCal.validate();
    Assert.assertNotNull(fullCal);
    
    // VTIMEZONE, VTODO, VEVENT
    Assert.assertEquals(3,fullCal.getComponents().size());
    Assert.assertEquals(1, fullCal.getComponents(Component.VTIMEZONE).size());
    Assert.assertEquals(1, fullCal.getComponents(Component.VEVENT).size());
    Assert.assertEquals(1, fullCal.getComponents(Component.VTODO).size());
}
 
Example 3
Source File: ICalConverter.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/** Returns a calendar derived from a Work Effort calendar publish point.
 * @param workEffortId ID of a work effort with <code>workEffortTypeId</code> equal to
 * <code>PUBLISH_PROPS</code>.
 * @param context The conversion context
 * @return An iCalendar as a <code>String</code>, or <code>null</code>
 * if <code>workEffortId</code> is invalid.
 * @throws GenericEntityException
 */
public static ResponseProperties getICalendar(String workEffortId, Map<String, Object> context) throws GenericEntityException {
    Delegator delegator = (Delegator) context.get("delegator");
    GenericValue publishProperties = EntityQuery.use(delegator).from("WorkEffort").where("workEffortId", workEffortId).queryOne();
    if (!isCalendarPublished(publishProperties)) {
        Debug.logInfo("WorkEffort calendar is not published: " + workEffortId, module);
        return ICalWorker.createNotFoundResponse(null);
    }
    if (!"WES_PUBLIC".equals(publishProperties.get("scopeEnumId"))) {
        if (context.get("userLogin") == null) {
            return ICalWorker.createNotAuthorizedResponse(null);
        }
        if (!hasPermission(workEffortId, "VIEW", context)) {
            return ICalWorker.createForbiddenResponse(null);
        }
    }
    Calendar calendar = makeCalendar(publishProperties, context);
    ComponentList<CalendarComponent> components = calendar.getComponents();
    List<GenericValue> workEfforts = getRelatedWorkEfforts(publishProperties, context);
    if (workEfforts != null) {
        for (GenericValue workEffort : workEfforts) {
            ResponseProperties responseProps = toCalendarComponent(components, workEffort, context);
            if (responseProps != null) {
                return responseProps;
            }
        }
    }
    if (Debug.verboseOn()) {
        try {
            calendar.validate(true);
            Debug.logVerbose("iCalendar passes validation", module);
        } catch (ValidationException e) {
            if (Debug.verboseOn()) Debug.logVerbose("iCalendar fails validation: " + e, module);
        }
    }
    return ICalWorker.createOkResponse(calendar.toString());
}
 
Example 4
Source File: ExternalCalendaringServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Calendar createCalendar(List<VEvent> events, String method, boolean timeIsLocal) {
	
	if(!isIcsEnabled()) {
		log.debug("ExternalCalendaringService is disabled. Enable via calendar.ics.generation.enabled=true in sakai.properties");
		return null;
	}
	
	//setup calendar
	Calendar calendar = setupCalendar(method);
	
	//null check
	if(CollectionUtils.isEmpty(events)) {
		log.error("List of VEvents was null or empty, no calendar will be created.");
		return null;
	}
	
	//add vevents to calendar
	calendar.getComponents().addAll(events);
	
	//add vtimezone
	VTimeZone tz = getTimeZone(timeIsLocal);

	calendar.getComponents().add(tz);
	
	//validate
	try {
		calendar.validate(true);
	} catch (ValidationException e) {
		log.error("createCalendar failed validation", e);
		return null;
	}
	
	if(log.isDebugEnabled()){
		log.debug("Calendar:" + calendar);
	}
	
	return calendar;
	
}
 
Example 5
Source File: EntityConverterTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
   * Tests convert task.
   * @throws Exception - if something is wrong this exception is thrown.
   */
  @Test
  public void testConvertTask() throws Exception {
      @SuppressWarnings("unused")
TimeZoneRegistry registry =
          TimeZoneRegistryFactory.getInstance().createRegistry();
      NoteItem master = new MockNoteItem();
      master.setDisplayName("displayName");
      master.setBody("body");
      master.setIcalUid("icaluid");
      master.setClientModifiedDate(new DateTime("20070101T100000Z"));
      master.setTriageStatus(TriageStatusUtil.initialize(new MockTriageStatus()));
      
      Calendar cal = converter.convertNote(master);
      cal.validate();
      
      Assert.assertEquals(1, cal.getComponents().size());
      
      ComponentList<VToDo> comps = cal.getComponents(Component.VTODO);
      Assert.assertEquals(1, comps.size());
      VToDo task = comps.get(0);
      
      Assert.assertNull(task.getDateCompleted());
      Assert.assertNull(ICalendarUtils.getXProperty("X-OSAF-STARRED", task));
      
      DateTime completeDate = new DateTime("20080122T100000Z");
      
      master.getTriageStatus().setCode(TriageStatus.CODE_DONE);
      master.getTriageStatus().setRank(TriageStatusUtil.getRank(completeDate.getTime()));
      master.addStamp(new MockTaskStamp());
      
      cal = converter.convertNote(master);
      task = (VToDo) cal.getComponents().get(0);
      
      Completed completed = task.getDateCompleted();
      Assert.assertNotNull(completed);
      Assert.assertEquals(completeDate.getTime(), completed.getDate().getTime());
      Assert.assertEquals("TRUE", ICalendarUtils.getXProperty("X-OSAF-STARRED", task));
      
  }
 
Example 6
Source File: CalendarClientsAdapterTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * MKCALENDAR in icalIOS7 provides a timezone without prodid
 * @throws IOException
 * @throws ParserException
 * @throws ValidationException
 */
@Test
public void icalIOS7_missingTimezoneProductIdIsAdded() throws IOException, ParserException, ValidationException{
    Calendar calendar = new CalendarBuilder().build(new ByteArrayInputStream(geticalIOS7Calendar()));
    CalendarClientsAdapter.adaptTimezoneCalendarComponent(calendar);
    calendar.validate(true);//must not throw exceptions
}
 
Example 7
Source File: ExternalCalendaringServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Calendar createCalendar(List<VEvent> events, String method, boolean timeIsLocal) {
	
	if(!isIcsEnabled()) {
		log.debug("ExternalCalendaringService is disabled. Enable via calendar.ics.generation.enabled=true in sakai.properties");
		return null;
	}
	
	//setup calendar
	Calendar calendar = setupCalendar(method);
	
	//null check
	if(CollectionUtils.isEmpty(events)) {
		log.error("List of VEvents was null or empty, no calendar will be created.");
		return null;
	}
	
	//add vevents to calendar
	calendar.getComponents().addAll(events);
	
	//add vtimezone
	VTimeZone tz = getTimeZone(timeIsLocal);

	calendar.getComponents().add(tz);
	
	//validate
	try {
		calendar.validate(true);
	} catch (ValidationException e) {
		log.error("createCalendar failed validation", e);
		return null;
	}
	
	if(log.isDebugEnabled()){
		log.debug("Calendar:" + calendar);
	}
	
	return calendar;
	
}
 
Example 8
Source File: EntityConverterTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests convert event.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testConvertEvent() throws Exception {
    TimeZoneRegistry registry =
        TimeZoneRegistryFactory.getInstance().createRegistry();
    NoteItem master = new MockNoteItem();
    master.setDisplayName("displayName");
    master.setBody("body");
    master.setIcalUid("icaluid");
    master.setClientModifiedDate(new DateTime("20070101T100000Z"));
    EventStamp eventStamp = new MockEventStamp(master);
    eventStamp.createCalendar();
    eventStamp.setStartDate(new DateTime("20070212T074500"));
    master.addStamp(eventStamp);
    
    Calendar cal = converter.convertNote(master);
    cal.validate();
    
    // date has no timezone, so there should be no timezones
    Assert.assertEquals(0, cal.getComponents(Component.VTIMEZONE).size());
  
    eventStamp.setStartDate(new DateTime("20070212T074500",TIMEZONE_REGISTRY.getTimeZone("America/Chicago")));
    
    cal = converter.convertNote(master);
    cal.validate();
    
    // should be a single VEVENT
    ComponentList<VEvent> comps = cal.getComponents(Component.VEVENT);
    Assert.assertEquals(1, comps.size());
    VEvent event = (VEvent) comps.get(0);
    
    // test VALUE=DATE-TIME is not present
    Assert.assertNull(event.getStartDate().getParameter(Parameter.VALUE));
    
    // test item properties got merged into calendar
    Assert.assertEquals("displayName", event.getSummary().getValue());
    Assert.assertEquals("body", event.getDescription().getValue());
    Assert.assertEquals("icaluid", event.getUid().getValue());
    Assert.assertEquals(master.getClientModifiedDate().getTime(), event.getDateStamp().getDate().getTime());
     
    // date has timezone, so there should be a timezone
    Assert.assertEquals(1, cal.getComponents(Component.VTIMEZONE).size());
    
    eventStamp.setEndDate(new DateTime("20070212T074500",TIMEZONE_REGISTRY.getTimeZone("America/Los_Angeles")));
    
    cal = converter.convertNote(master);
    cal.validate();
    
    // dates have 2 different timezones, so there should be 2 timezones
    Assert.assertEquals(2, cal.getComponents(Component.VTIMEZONE).size());
    
    // add timezones to master event calendar
    eventStamp.getEventCalendar().getComponents().add(registry.getTimeZone("America/Chicago").getVTimeZone());
    eventStamp.getEventCalendar().getComponents().add(registry.getTimeZone("America/Los_Angeles").getVTimeZone());
    
    cal = converter.convertNote(master);
    cal.validate();
    Assert.assertEquals(2, cal.getComponents(Component.VTIMEZONE).size());
}