de.tsl2.nano.h5.ICSCalendarExport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.h5 Show documentation
Show all versions of tsl2.nano.h5 Show documentation
TSL2 Framework Html5 Extensions (WebServer, Html5Presentation, RuleCover, BeanConfigurator, LogicTable-Sheet, Expression-Descriptors for Actions, Rules, URLs, Queries)
package de.tsl2.nano.h5;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import de.tsl2.nano.bean.def.Bean;
import de.tsl2.nano.core.ENV;
import de.tsl2.nano.core.ManagedException;
/**
* exports any objects to ics format. example: new ICSCalendarExport("myCal").addAll(myObjects, MapUtil.asMap(FIELD.SUMMARY, "myTitle", ...)).write();
* @author ts
*/
public class ICSCalendarExport {
StringBuilder buf;
String name;
enum REPEAT {DAILY, WEEKLY, MONTHLY, YEARLY}
enum FIELD {BEGIN, VERSION, PROID, METHOD, UID, LOCATION, SUMMARY, DESCRIPTION, CATEGORIES, CLASS, TZID, RRULE, DTSTART, DTEND, DTSTAMP, END}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
private REPEAT repeat;
public ICSCalendarExport(String name, REPEAT repeat) {
this.name = name;
this.repeat = repeat;
buf = new StringBuilder(getStartBlock());
}
protected String getStartBlock() {
return FIELD.BEGIN + ":VCALENDAR\n" + FIELD.VERSION + ":2.0\n" + FIELD.PROID + ":https://sourceforge.net/projects/tsl2nano/\n" + FIELD.METHOD + ":PUBLISH\n";
}
protected String getEndBlock() {
return FIELD.END + ":VCALENDAR\n";
}
public ICSCalendarExport addAll(Collection> objects, Map attributeMapping) {
assert objects != null && attributeMapping != null && attributeMapping.size() > 2 : "at least SUMMARY, DTSTART, DTEND must be mapped!";
for (Object obj: objects) {
buf.append(toICS(obj, attributeMapping));
}
return this;
}
protected String toICS(Object obj, Map attributeMapping) {
StringBuilder ics = new StringBuilder();
Bean