use of net.fortuna.ical4j.data.CalendarOutputter in project LAMSADE-tools by LAntoine.
the class Conference method generateCalendarFile.
/**
* Generates a vcal file with the conference details with this object's data
*
* @param filename:
* the path including the name of the file to be generated
* @throws IOException
* @throws ValidationException
* @throws ParserException
*
* @author Javier MartÃnez
*/
public void generateCalendarFile(String filename) throws IOException, ValidationException, ParserException {
String calFile = filename;
// start time
java.util.Calendar startCal = java.util.Calendar.getInstance();
startCal.setTime(java.sql.Date.valueOf(getStart_date()));
// end time
java.util.Calendar endCal = java.util.Calendar.getInstance();
endCal.setTime(java.sql.Date.valueOf(getEnd_date()));
String subject = getTitle();
String description = "A conference with a fee of " + getEntry_fee();
String hostEmail = "";
// Creating a new calendar
net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
calendar.getProperties().add(Version.VERSION_2_0);
calendar.getProperties().add(CalScale.GREGORIAN);
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMdd'T'hhmmss'Z'");
String strDate = sdFormat.format(startCal.getTime());
net.fortuna.ical4j.model.Date startDt = null;
try {
startDt = new net.fortuna.ical4j.model.Date(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
long diff = endCal.getTimeInMillis() - startCal.getTimeInMillis();
int min = (int) (diff / (1000 * 60));
Dur dur = new Dur(0, 0, min, 0);
// Creating a meeting event
VEvent meeting = new VEvent(startDt, dur, subject);
// This is where you would add a location if there was one
// meeting.getProperties().add(new Location(location));
meeting.getProperties().add(new Description());
try {
meeting.getProperties().getProperty(Property.DESCRIPTION).setValue(description);
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
calendar.getComponents().add(meeting);
FileOutputStream fout = null;
try {
fout = new FileOutputStream(calFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
CalendarOutputter outputter = new CalendarOutputter();
outputter.setValidating(false);
try {
outputter.output(calendar, fout);
} catch (IOException e) {
e.printStackTrace();
} catch (ValidationException e) {
e.printStackTrace();
}
System.out.println(meeting);
}
use of net.fortuna.ical4j.data.CalendarOutputter in project android by nextcloud.
the class SaveCalendar method start.
public void start() throws Exception {
mInsertedTimeZones.clear();
mFailedOrganisers.clear();
mAllCols = false;
String file = selectedCal.mDisplayName + "_" + DateFormat.format("yyyy-MM-dd_HH-mm-ss", java.util.Calendar.getInstance()).toString() + ".ics";
File fileName = new File(activity.getCacheDir(), file);
Log_OC.i(TAG, "Save id " + selectedCal.mIdStr + " to file " + fileName.getAbsolutePath());
String name = activity.getPackageName();
String ver;
try {
ver = activity.getPackageManager().getPackageInfo(name, 0).versionName;
} catch (NameNotFoundException e) {
ver = "Unknown Build";
}
String prodId = "-//" + selectedCal.mOwner + "//iCal Import/Export " + ver + "//EN";
Calendar cal = new Calendar();
cal.getProperties().add(new ProdId(prodId));
cal.getProperties().add(Version.VERSION_2_0);
cal.getProperties().add(Method.PUBLISH);
cal.getProperties().add(CalScale.GREGORIAN);
if (selectedCal.mTimezone != null) {
// We don't write any events with floating times, but export this
// anyway so the default timezone for new events is correct when
// the file is imported into a system that supports it.
cal.getProperties().add(new XProperty("X-WR-TIMEZONE", selectedCal.mTimezone));
}
// query events
ContentResolver resolver = activity.getContentResolver();
int numberOfCreatedUids = 0;
if (Events.UID_2445 != null) {
numberOfCreatedUids = ensureUids(activity, resolver, selectedCal);
}
// settings.getIcal4jValidationRelaxed(); // TODO is this option needed? default true
boolean relaxed = true;
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION, relaxed);
List<VEvent> events = getEvents(resolver, selectedCal, cal);
for (VEvent v : events) {
cal.getComponents().add(v);
}
new CalendarOutputter().output(cal, new FileOutputStream(fileName));
Resources res = activity.getResources();
String msg = res.getQuantityString(R.plurals.wrote_n_events_to, events.size(), events.size(), file);
if (numberOfCreatedUids > 0) {
msg += "\n" + res.getQuantityString(R.plurals.created_n_uids_to, numberOfCreatedUids, numberOfCreatedUids);
}
// TODO replace DisplayUtils.showSnackMessage(activity, msg);
upload(fileName);
}
use of net.fortuna.ical4j.data.CalendarOutputter in project lavagna by digitalfondue.
the class CalendarController method userCalDavCalendar.
@RequestMapping(value = "/api/calendar/{token}/calendar.ics", method = RequestMethod.GET, produces = "text/calendar")
public void userCalDavCalendar(@PathVariable("token") String userToken, HttpServletResponse response) throws IOException, URISyntaxException, ParseException {
Calendar calendar = calendarService.getCalDavCalendar(userToken);
response.setContentType("text/calendar");
// <- no validation on the output
CalendarOutputter output = new CalendarOutputter(false);
output.output(calendar, response.getOutputStream());
}
use of net.fortuna.ical4j.data.CalendarOutputter in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManagerTest method synchronizeCalendarFrom.
@Test
public void synchronizeCalendarFrom() throws ValidationException, IOException {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("s1-");
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
String eventId1 = "id-not-managed-event";
// 1. Add a standard event, not managed
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
Date start = calendar.getTime();
calendar.add(Calendar.HOUR_OF_DAY, 1);
Date end = calendar.getTime();
KalendarEvent notManagedEvent = new KalendarEvent(eventId1, null, "testEvent", start, end);
calendarManager.addEventTo(cal, notManagedEvent);
// 2. Synchronize with a first calendar
Kalendar baseCalendar = calendarManager.createCalendar("user", "first-sync");
String eventIdManaged1 = "managed-event-1";
KalendarEvent managedEvent1 = new KalendarEvent(eventIdManaged1, null, "managedEvent", start, end);
baseCalendar.addEvent(managedEvent1);
String eventIdManaged2 = "managed-event-2";
KalendarEvent managedEvent2 = new KalendarEvent(eventIdManaged2, null, "managedEvent", start, end);
baseCalendar.addEvent(managedEvent2);
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
new CalendarOutputter(false).output(calendarManager.buildCalendar(baseCalendar), os1);
InputStream in1 = new ByteArrayInputStream(os1.toByteArray());
calendarManager.synchronizeCalendarFrom(in1, "http://localhost:8080/unittest", cal);
in1.close();
// 3. Synchronize with a second calendar
Kalendar resyncCalendar = calendarManager.createCalendar("user", "first-sync");
KalendarEvent managedEvent1Alt = new KalendarEvent(eventIdManaged1, null, "managedEvent resync", start, end);
resyncCalendar.addEvent(managedEvent1Alt);
String eventIdManaged3 = "managed-event-3";
KalendarEvent managedEvent3 = new KalendarEvent(eventIdManaged3, null, "managedEvent 3", start, end);
resyncCalendar.addEvent(managedEvent3);
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
new CalendarOutputter(false).output(calendarManager.buildCalendar(resyncCalendar), os2);
InputStream in2 = new ByteArrayInputStream(os2.toByteArray());
calendarManager.synchronizeCalendarFrom(in2, "http://localhost:8080/unittest", cal);
in2.close();
emptyCalendarCache();
// check
Kalendar synchedCal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent notManagedEvent1 = synchedCal.getEvent(eventId1, null);
Assert.assertNotNull(notManagedEvent1);
Assert.assertEquals("testEvent", notManagedEvent1.getSubject());
KalendarEvent event1 = synchedCal.getEvent(eventIdManaged1, null);
Assert.assertNotNull(event1);
Assert.assertEquals("managedEvent resync", event1.getSubject());
KalendarEvent event2 = synchedCal.getEvent(eventIdManaged2, null);
Assert.assertNull(event2);
KalendarEvent event3 = synchedCal.getEvent(eventIdManaged3, null);
Assert.assertNotNull(event3);
Assert.assertEquals("managedEvent 3", event3.getSubject());
}
use of net.fortuna.ical4j.data.CalendarOutputter in project OpenOLAT by OpenOLAT.
the class ICalFileCalendarManager method writeCalendarFile.
private boolean writeCalendarFile(Calendar calendar, String calType, String calId) {
File fKalendarFile = getCalendarFile(calType, calId);
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(fKalendarFile, false));
CalendarOutputter calOut = new CalendarOutputter(false);
calOut.output(calendar, os);
} catch (Exception e) {
return false;
} finally {
FileUtils.closeSafely(os);
}
return true;
}
Aggregations