package org.unitedinternet.cosmo.ext;

import java.util.HashSet;
import java.util.Set;

import org.unitedinternet.cosmo.model.ICalendarItem;
import org.unitedinternet.cosmo.model.NoteItem;
import org.unitedinternet.cosmo.model.hibernate.EntityConverter;

import net.fortuna.ical4j.model.Calendar;

/**
 * Helper class that allows translating <code>Calendar</code> objects to <code>NoteItem</code>-s.
 * 
 * @author daniel grigore
 *
 */
public class ContentConverter {

    private final EntityConverter entityConverter;

    public ContentConverter(EntityConverter entityConverter) {
        super();
        this.entityConverter = entityConverter;
    }

    public Set<NoteItem> asItems(Calendar calendar) {
        Set<NoteItem> items = new HashSet<>();
        if (calendar != null) {
            Set<ICalendarItem> calendarItems = this.entityConverter.convertCalendar(calendar);

            for (ICalendarItem item : calendarItems) {
                /**
                 * Only VEVENT are supported currently. VTODO or VJOURNAL are not yet supported.
                 */
                if (item instanceof NoteItem) {
                    items.add((NoteItem) item);
                    item.setName(item.getIcalUid() + ".ics");
                }
            }
        }
        return items;
    }
}

@@ -28,24 +28,47 @@
 cosmo.ext;%0A%0A
+import java.util.Date;%0A
 import java.
@@ -269,16 +269,77 @@
 nverter;
+%0Aimport org.unitedinternet.cosmo.model.hibernate.HibNoteItem;
 %0A%0Aimport
@@ -1191,16 +1191,19 @@
 tanceof 
+Hib
 NoteItem
@@ -1230,57 +1230,262 @@
     
-items.add((NoteItem) item);%0A                    i
+HibNoteItem noteItem = (HibNoteItem) item;%0A                    Date now = new Date();%0A                    noteItem.setCreationDate(now);%0A                    noteItem.setModifiedDate(now);%0A                    items.add(noteItem);%0A                    noteI
 tem.
@@ -1492,17 +1492,21 @@
 setName(
-i
+noteI
 tem.getI

Set creationDate and modifiedDate when reading content from external sources to be able to calculate different eTag.