diff --git a/source/java/org/alfresco/repo/calendar/CalendarEntryImpl.java b/source/java/org/alfresco/repo/calendar/CalendarEntryImpl.java index 18d002b255..f9d8bac14e 100644 --- a/source/java/org/alfresco/repo/calendar/CalendarEntryImpl.java +++ b/source/java/org/alfresco/repo/calendar/CalendarEntryImpl.java @@ -21,6 +21,7 @@ package org.alfresco.repo.calendar; import java.io.Serializable; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.alfresco.service.cmr.calendar.CalendarEntry; @@ -99,7 +100,9 @@ public class CalendarEntryImpl extends CalendarEntryDTO entry.setEnd((Date)properties.get(CalendarModel.PROP_TO_DATE)); entry.setRecurrenceRule((String)properties.get(CalendarModel.PROP_RECURRENCE_RULE)); entry.setLastRecurrence((Date)properties.get(CalendarModel.PROP_RECURRENCE_LAST_MEETING)); - entry.setOutlook((Boolean)properties.get(CalendarModel.PROP_IS_OUTLOOK)); + + Boolean isOutlook = (Boolean)properties.get(CalendarModel.PROP_IS_OUTLOOK); + entry.setOutlook(isOutlook == null ? false : isOutlook); entry.setOutlookUID((String)properties.get(CalendarModel.PROP_OUTLOOK_UID)); //entry.setColor(properties.get(CalendarModel.PROP_COLOR)); @@ -114,4 +117,13 @@ public class CalendarEntryImpl extends CalendarEntryDTO { populate(this, properties); } + + /** + * Sets the list of tags for the entry + */ + protected void setTags(List tags) + { + super.getTags().clear(); + super.getTags().addAll(tags); + } } \ No newline at end of file diff --git a/source/java/org/alfresco/repo/calendar/CalendarServiceImpl.java b/source/java/org/alfresco/repo/calendar/CalendarServiceImpl.java index 48c7c6924c..fb2770a3c4 100644 --- a/source/java/org/alfresco/repo/calendar/CalendarServiceImpl.java +++ b/source/java/org/alfresco/repo/calendar/CalendarServiceImpl.java @@ -19,7 +19,12 @@ package org.alfresco.repo.calendar; import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Set; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; @@ -177,6 +182,54 @@ public class CalendarServiceImpl implements CalendarService return container; } } + + private void handleTags(CalendarEntry entry) + { + NodeRef nodeRef = entry.getNodeRef(); + + List currentTags = taggingService.getTags(nodeRef); + List newTags = entry.getTags(); + + if(currentTags.size() == 0 && newTags.size() == 0) + { + // No tags, easy + return; + } + + // Figure out what (if anything) changed + Set toAdd = new HashSet(newTags); + Set toDel = new HashSet(currentTags); + for(String tag : currentTags) + { + if(toAdd.contains(tag)) + { + toAdd.remove(tag); + } + } + for(String tag : newTags) + { + if(toDel.contains(tag)) + { + toDel.remove(tag); + } + } + + if(toDel.size() == 0 && toAdd.size() == 0) + { + // No changes + } + + // Make the changes + taggingService.clearTags(nodeRef); + for(String tag : toDel) + { + taggingService.removeTag(nodeRef, tag); + } + for(String tag : toAdd) + { + taggingService.addTag(nodeRef, tag); + } + } @Override public CalendarEntry getCalendarEntry(String siteShortName, String entryName) @@ -193,6 +246,7 @@ public class CalendarServiceImpl implements CalendarService { CalendarEntryImpl entry = new CalendarEntryImpl(event, entryName); entry.populate(nodeService.getProperties(event)); + entry.setTags(taggingService.getTags(event)); return entry; } return null; @@ -212,8 +266,10 @@ public class CalendarServiceImpl implements CalendarService // Turn the entry into properties Map properties = CalendarEntryImpl.toNodeProperties(entry); - // Generate a name - String name = "123.ics"; // TODO + // Generate a unique name + // (Should be unique, but will retry for a new one if not) + String name = (new Date()).getTime() + "-" + + Math.round(Math.random()*10000) + ".ics"; properties.put(ContentModel.PROP_NAME, name); // Add the entry @@ -236,7 +292,13 @@ public class CalendarServiceImpl implements CalendarService { entryImpl = new CalendarEntryImpl(nodeRef, name); entryImpl.populate(properties); + entryImpl.setTags(entry.getTags()); } + + // Tag it + handleTags(entryImpl); + + // All done return entryImpl; } @@ -252,6 +314,9 @@ public class CalendarServiceImpl implements CalendarService // Update the existing one nodeService.setProperties(entry.getNodeRef(), properties); + // Update tags + handleTags(entry); + // Nothing changed return entry; } diff --git a/source/java/org/alfresco/repo/calendar/CalendarServiceImplTest.java b/source/java/org/alfresco/repo/calendar/CalendarServiceImplTest.java index 1999029200..00349a6d8d 100644 --- a/source/java/org/alfresco/repo/calendar/CalendarServiceImplTest.java +++ b/source/java/org/alfresco/repo/calendar/CalendarServiceImplTest.java @@ -321,6 +321,115 @@ public class CalendarServiceImplTest }); } + @Test public void tagging() throws Exception + { + CalendarEntry entry; + final String TAG_1 = "calendar_tag_1"; + final String TAG_2 = "calendar_tag_2"; + final String TAG_3 = "calendar_tag_3"; + + // Create one without tagging + entry = new CalendarEntryDTO( + "Title", "Description", "Location", new Date(1), new Date(1234) + ); + entry = CALENDAR_SERVICE.createCalendarEntry(CALENDAR_SITE.getShortName(), entry); + + // Check + assertEquals(0, entry.getTags().size()); + + entry = CALENDAR_SERVICE.getCalendarEntry(CALENDAR_SITE.getShortName(), entry.getSystemName()); + assertEquals(0, entry.getTags().size()); + + + // Update it to have tags + entry.getTags().add(TAG_1); + entry.getTags().add(TAG_2); + entry.getTags().add(TAG_1); + assertEquals(3, entry.getTags().size()); + CALENDAR_SERVICE.updateCalendarEntry(entry); + + // Check + entry = CALENDAR_SERVICE.getCalendarEntry(CALENDAR_SITE.getShortName(), entry.getSystemName()); + assertEquals(2, entry.getTags().size()); + assertEquals(true, entry.getTags().contains(TAG_1)); + assertEquals(true, entry.getTags().contains(TAG_2)); + assertEquals(false, entry.getTags().contains(TAG_3)); + + + // Update it to have different tags + entry.getTags().remove(TAG_2); + entry.getTags().add(TAG_3); + entry.getTags().add(TAG_1); + CALENDAR_SERVICE.updateCalendarEntry(entry); + + // Check + entry = CALENDAR_SERVICE.getCalendarEntry(CALENDAR_SITE.getShortName(), entry.getSystemName()); + assertEquals(2, entry.getTags().size()); + assertEquals(true, entry.getTags().contains(TAG_1)); + assertEquals(false, entry.getTags().contains(TAG_2)); + assertEquals(true, entry.getTags().contains(TAG_3)); + + + // Update it to have no tags + entry.getTags().clear(); + CALENDAR_SERVICE.updateCalendarEntry(entry); + + // Check + entry = CALENDAR_SERVICE.getCalendarEntry(CALENDAR_SITE.getShortName(), entry.getSystemName()); + assertEquals(0, entry.getTags().size()); + + + // Update it to have tags again + entry.getTags().add(TAG_1); + entry.getTags().add(TAG_2); + entry.getTags().add(TAG_3); + CALENDAR_SERVICE.updateCalendarEntry(entry); + + // Check + entry = CALENDAR_SERVICE.getCalendarEntry(CALENDAR_SITE.getShortName(), entry.getSystemName()); + assertEquals(3, entry.getTags().size()); + assertEquals(true, entry.getTags().contains(TAG_1)); + assertEquals(true, entry.getTags().contains(TAG_2)); + assertEquals(true, entry.getTags().contains(TAG_3)); + + // Tidy + CALENDAR_SERVICE.deleteCalendarEntry(entry); + + + // Create an event with tags + entry = new CalendarEntryDTO( + "Title", "Description", "Location", new Date(1), new Date(1234) + ); + entry.getTags().add(TAG_1); + entry.getTags().add(TAG_1); + entry.getTags().add(TAG_2); + entry = CALENDAR_SERVICE.createCalendarEntry(CALENDAR_SITE.getShortName(), entry); + + // Check + entry = CALENDAR_SERVICE.getCalendarEntry(CALENDAR_SITE.getShortName(), entry.getSystemName()); + assertEquals(2, entry.getTags().size()); + assertEquals(true, entry.getTags().contains(TAG_1)); + assertEquals(true, entry.getTags().contains(TAG_2)); + assertEquals(false, entry.getTags().contains(TAG_3)); + + + // Update it to have different tags + entry.getTags().remove(TAG_2); + entry.getTags().add(TAG_3); + entry.getTags().add(TAG_1); + CALENDAR_SERVICE.updateCalendarEntry(entry); + + // Check + entry = CALENDAR_SERVICE.getCalendarEntry(CALENDAR_SITE.getShortName(), entry.getSystemName()); + assertEquals(2, entry.getTags().size()); + assertEquals(true, entry.getTags().contains(TAG_1)); + assertEquals(false, entry.getTags().contains(TAG_2)); + assertEquals(true, entry.getTags().contains(TAG_3)); + + // Tidy + CALENDAR_SERVICE.deleteCalendarEntry(entry); + } + @Test public void calendarListing() throws Exception { // TODO diff --git a/source/java/org/alfresco/service/cmr/calendar/CalendarEntryDTO.java b/source/java/org/alfresco/service/cmr/calendar/CalendarEntryDTO.java index 480fcfb69c..0336a304d3 100644 --- a/source/java/org/alfresco/service/cmr/calendar/CalendarEntryDTO.java +++ b/source/java/org/alfresco/service/cmr/calendar/CalendarEntryDTO.java @@ -19,6 +19,7 @@ package org.alfresco.service.cmr.calendar; import java.io.Serializable; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; @@ -44,7 +45,7 @@ public class CalendarEntryDTO implements CalendarEntry, Serializable { private Date lastRecurrence; private boolean isOutlook = false; private String outlookUID; - private List tags; + private List tags = new ArrayList(); /** * Creates an empty {@link CalendarEntry}, which can be populated @@ -233,7 +234,6 @@ public class CalendarEntryDTO implements CalendarEntry, Serializable { */ public List getTags() { - // TODO Immutable? return tags; }