ALF-9156 Update the new Calendar Service to match the pattern agreed yesterday

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28829 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-07-06 12:21:34 +00:00
parent ddeccadc52
commit 9ef80c899d
6 changed files with 317 additions and 162 deletions

View File

@@ -36,6 +36,7 @@ import org.alfresco.repo.blog.cannedqueries.GetBlogPostsCannedQueryFactory;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.calendar.CalendarEntry; import org.alfresco.service.cmr.calendar.CalendarEntry;
import org.alfresco.service.cmr.calendar.CalendarEntryDTO;
import org.alfresco.service.cmr.calendar.CalendarService; import org.alfresco.service.cmr.calendar.CalendarService;
import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentService; import org.alfresco.service.cmr.repository.ContentService;
@@ -55,52 +56,24 @@ import org.alfresco.util.registry.NamedObjectRegistry;
* @author Nick Burch (based on existing webscript controllers in the REST API) * @author Nick Burch (based on existing webscript controllers in the REST API)
* @since 4.0 * @since 4.0
*/ */
public class CalendarEntryImpl implements CalendarEntry public class CalendarEntryImpl extends CalendarEntryDTO
{ {
private NodeRef nodeRef; private NodeRef nodeRef;
private NodeRef parentNodeRef; private String systemName;
private Map<QName,Serializable> properties;
/**
* Creates an empty Calendar Entry, to be stored in the
* given parent
*/
public CalendarEntryImpl(NodeRef parentNodeRef)
{
this.parentNodeRef = parentNodeRef;
this.properties = new HashMap<QName, Serializable>();
}
/** /**
* Wraps an existing Calendar Entry node * Wraps an existing Calendar Entry node
*/ */
public CalendarEntryImpl(NodeRef nodeRef, NodeRef parentNodeRef, Map<QName,Serializable> properties) protected CalendarEntryImpl(NodeRef nodeRef, String systemName)
{ {
this.nodeRef = nodeRef; this.nodeRef = nodeRef;
this.parentNodeRef = parentNodeRef; this.systemName = systemName;
this.properties = properties;
} }
protected void recordStorageDetails(NodeRef nodeRef) protected void recordStorageDetails(NodeRef nodeRef, String systemName)
{ {
this.nodeRef = nodeRef; this.nodeRef = nodeRef;
} this.systemName = systemName;
/**
* Returns the NodeRef of the parent where this calendar entry
* lives, or will live
*/
public NodeRef getParentNodeRef()
{
return parentNodeRef;
}
/**
* Returns the current set of properties
*/
public Map<QName,Serializable> getProperties()
{
return properties;
} }
@Override @Override
@@ -110,72 +83,35 @@ public class CalendarEntryImpl implements CalendarEntry
} }
@Override @Override
public String getSystemName() { public String getSystemName()
// TODO Auto-generated method stub {
return null; return systemName;
} }
@Override /**
public String getTitle() { * Builds up the node properties for a given Calendar Entry
// TODO Auto-generated method stub */
return null; protected static Map<QName,Serializable> toNodeProperties(CalendarEntry entry)
{
Map<QName,Serializable> properties = new HashMap<QName, Serializable>();
// TODO
return properties;
} }
@Override /**
public void setTitle(String title) { * Populates a Calendar Entry from the given node properties
// TODO Auto-generated method stub */
protected static void populate(CalendarEntry entry, Map<QName,Serializable> properties)
} {
@Override // TODO
public String getDescription() {
// TODO Auto-generated method stub
return null;
} }
@Override /**
public void setDescription(String description) { * Populates this entry from the given node properties
// TODO Auto-generated method stub */
} protected void populate(Map<QName,Serializable> properties)
{
@Override populate(this, properties);
public Date getStart() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setStart(Date start) {
// TODO Auto-generated method stub
}
@Override
public Date getEnd() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setEnd(Date end) {
// TODO Auto-generated method stub
}
@Override
public String getLocation() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setLocation(String location) {
// TODO Auto-generated method stub
}
@Override
public List<String> getTags() {
// TODO Auto-generated method stub
return null;
} }
} }

View File

@@ -19,9 +19,9 @@
package org.alfresco.repo.calendar; package org.alfresco.repo.calendar;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date;
import java.util.Map; import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
@@ -97,18 +97,23 @@ public class CalendarServiceImpl implements CalendarService
{ {
if(create) if(create)
{ {
if(transactionService.isReadOnly())
{
throw new AlfrescoRuntimeException(
"Unable to create the calendar container from a read only transaction"
);
}
// Have the site container created // Have the site container created
if(logger.isDebugEnabled()) if(logger.isDebugEnabled())
{ {
logger.debug("Creating " + CALENDAR_COMPONENT + " container in site " + siteShortName); logger.debug("Creating " + CALENDAR_COMPONENT + " container in site " + siteShortName);
} }
NodeRef container = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<NodeRef>() { NodeRef container = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<NodeRef>()
{
public NodeRef doWork() throws Exception public NodeRef doWork() throws Exception
{ {
return transactionService.getRetryingTransactionHelper().doInTransaction(
new RetryingTransactionCallback<NodeRef>() {
public NodeRef execute() throws Throwable {
// Create the site container // Create the site container
NodeRef container = siteService.createContainer( NodeRef container = siteService.createContainer(
siteShortName, CALENDAR_COMPONENT, null, null siteShortName, CALENDAR_COMPONENT, null, null
@@ -117,10 +122,8 @@ public class CalendarServiceImpl implements CalendarService
// Done // Done
return container; return container;
} }
}, false, true }, AuthenticationUtil.getSystemUserName()
); );
}
}, AuthenticationUtil.getSystemUserName());
if(logger.isDebugEnabled()) if(logger.isDebugEnabled())
{ {
@@ -176,7 +179,8 @@ public class CalendarServiceImpl implements CalendarService
} }
@Override @Override
public CalendarEntry getCalendarEntry(String siteShortName, String entryName) { public CalendarEntry getCalendarEntry(String siteShortName, String entryName)
{
NodeRef container = getSiteCalendarContainer(siteShortName, false); NodeRef container = getSiteCalendarContainer(siteShortName, false);
if(container == null) if(container == null)
{ {
@@ -187,55 +191,78 @@ public class CalendarServiceImpl implements CalendarService
NodeRef event = nodeService.getChildByName(container, ContentModel.ASSOC_CONTAINS, entryName); NodeRef event = nodeService.getChildByName(container, ContentModel.ASSOC_CONTAINS, entryName);
if(event != null) if(event != null)
{ {
return new CalendarEntryImpl(event, container, nodeService.getProperties(event)); CalendarEntryImpl entry = new CalendarEntryImpl(event, entryName);
entry.populate(nodeService.getProperties(event));
return entry;
} }
return null; return null;
} }
@Override @Override
public CalendarEntry createCalendarEntry(String siteShortName, public CalendarEntry createCalendarEntry(String siteShortName, CalendarEntry entry)
String eventTitle, String eventDescription, Date eventStart, Date eventEnd) { {
NodeRef container = getSiteCalendarContainer(siteShortName, true); if(entry.getNodeRef() != null)
{
CalendarEntry entry = new CalendarEntryImpl(container); throw new IllegalArgumentException("Can't call create for a calendar entry that was previously persisted");
entry.setTitle(eventTitle);
entry.setDescription(eventDescription);
entry.setStart(eventStart);
entry.setEnd(eventEnd);
return entry;
} }
@Override // Grab the location to store in
public void saveCalendarEntry(CalendarEntry entry) { NodeRef container = getSiteCalendarContainer(siteShortName, true);
CalendarEntryImpl entryImpl = (CalendarEntryImpl)entry;
Map<QName,Serializable> properties = entryImpl.getProperties(); // Turn the entry into properties
Map<QName,Serializable> properties = CalendarEntryImpl.toNodeProperties(entry);
if(entry.getNodeRef() == null)
{
// Generate a name // Generate a name
String name = "123.ics"; // TODO String name = "123.ics"; // TODO
properties.put(ContentModel.PROP_NAME, name); properties.put(ContentModel.PROP_NAME, name);
// Add the entry // Add the entry
NodeRef nodeRef = nodeService.createNode( NodeRef nodeRef = nodeService.createNode(
entryImpl.getParentNodeRef(), container,
ContentModel.ASSOC_CONTAINS, ContentModel.ASSOC_CONTAINS,
QName.createQName(name), QName.createQName(name),
CalendarModel.TYPE_EVENT, CalendarModel.TYPE_EVENT,
properties properties
).getChildRef(); ).getChildRef();
entryImpl.recordStorageDetails(nodeRef);
// Record it's details
CalendarEntryImpl entryImpl;
if(entry instanceof CalendarEntryImpl)
{
entryImpl = (CalendarEntryImpl)entry;
entryImpl.recordStorageDetails(nodeRef, name);
} }
else else
{ {
entryImpl = new CalendarEntryImpl(nodeRef, name);
entryImpl.populate(properties);
}
return entryImpl;
}
@Override
public CalendarEntry updateCalendarEntry(CalendarEntry entry) {
Map<QName,Serializable> properties = CalendarEntryImpl.toNodeProperties(entry);
if(entry.getNodeRef() == null)
{
throw new IllegalArgumentException("Can't update a calendar entry that was never persisted, call create instead");
}
// Update the existing one // Update the existing one
nodeService.setProperties(entry.getNodeRef(), properties); nodeService.setProperties(entry.getNodeRef(), properties);
}
// Nothing changed
return entry;
} }
@Override @Override
public void deleteCalendarEntry(CalendarEntry entry) { public void deleteCalendarEntry(CalendarEntry entry) {
// TODO Auto-generated method stub if(entry.getNodeRef() == null)
{
throw new IllegalArgumentException("Can't delete a calendar entry that was never persisted");
}
nodeService.deleteNode(entry.getNodeRef());
} }
} }

View File

@@ -21,6 +21,7 @@ package org.alfresco.repo.calendar;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@@ -37,6 +38,7 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.site.SiteModel; import org.alfresco.repo.site.SiteModel;
import org.alfresco.repo.transaction.RetryingTransactionHelper; import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.service.cmr.calendar.CalendarEntry; import org.alfresco.service.cmr.calendar.CalendarEntry;
import org.alfresco.service.cmr.calendar.CalendarEntryDTO;
import org.alfresco.service.cmr.calendar.CalendarService; import org.alfresco.service.cmr.calendar.CalendarService;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition; import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.dictionary.DictionaryService;
@@ -133,20 +135,31 @@ public class CalendarServiceImplTest
// Create one // Create one
entry = CALENDAR_SERVICE.createCalendarEntry( entry = new CalendarEntryDTO(
CALENDAR_SITE.getShortName(), "Title", "Title", "Description", "Location", new Date(1), new Date(1234)
"Description", new Date(1), new Date(1234)
); );
// Can't be got until saved // Can't be got until saved
assertEquals(null, entry.getSystemName()); assertEquals(null, entry.getSystemName());
assertEquals(null, entry.getNodeRef()); assertEquals(null, entry.getNodeRef());
// But we do know where it'll go
assertNotNull( ((CalendarEntryImpl)entry).getParentNodeRef() ); // Can't call update yet
try
{
CALENDAR_SERVICE.updateCalendarEntry(entry);
fail("Shouldn't be able to update a brand new entry");
}
catch(IllegalArgumentException e)
{}
// Have it saved // Have it saved
entry = CALENDAR_SERVICE.createCalendarEntry(CALENDAR_SITE.getShortName(), entry);
// Ensure it got a noderef, and the correct site
// TODO
testNodesToTidy.add(entry.getNodeRef());
// TODO // TODO
} }

View File

@@ -22,6 +22,7 @@ import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.alfresco.repo.security.permissions.PermissionCheckValue;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
/** /**
@@ -30,7 +31,7 @@ import org.alfresco.service.cmr.repository.NodeRef;
* @author Nick Burch * @author Nick Burch
* @since 4.0 * @since 4.0
*/ */
public interface CalendarEntry extends Serializable { public interface CalendarEntry extends Serializable, PermissionCheckValue {
/** /**
* @return the NodeRef of the underlying calendar entry * @return the NodeRef of the underlying calendar entry
*/ */

View File

@@ -0,0 +1,176 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.service.cmr.calendar;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.alfresco.service.cmr.repository.NodeRef;
/**
* This class represents an event in a calendar.
*
* @author Nick Burch
* @since 4.0
*/
public class CalendarEntryDTO implements CalendarEntry, Serializable {
private NodeRef nodeRef;
private String systemName;
private String title;
private String description;
private String location;
private Date start;
private Date end;
private List<String> tags;
/**
* Creates an empty {@link CalendarEntry}, which can be populated
* with set calls.
*/
public CalendarEntryDTO()
{}
/**
* Creates a {@link CalendarEntry} with common properties.
*/
public CalendarEntryDTO(String title, String description,
String location, Date start, Date end)
{
this.title = title;
this.description = description;
this.location = location;
this.start = start;
this.end = end;
}
/**
* @return the NodeRef of the underlying calendar entry
*/
public NodeRef getNodeRef()
{
return nodeRef;
}
/**
* @return the System generated name for the event
*/
public String getSystemName()
{
return systemName;
}
/**
* @return the Title ("what") of the event
*/
public String getTitle()
{
return title;
}
/**
* Sets the Title ("what") of the event
*/
public void setTitle(String title)
{
this.title = title;
}
/**
* @return the Description of the event
*/
public String getDescription()
{
return description;
}
/**
* Sets the Description of the event
*/
public void setDescription(String description)
{
this.description = description;
}
/**
* @return the Location of the event
*/
public String getLocation()
{
return location;
}
/**
* Sets the Location of the event
*/
public void setLocation(String location)
{
this.location = location;
}
/**
* @return the Start date and time
*/
public Date getStart()
{
return start;
}
/**
* Sets the event start date and time
*/
public void setStart(Date start)
{
this.start = start;
}
/**
* @return the End date and time
*/
public Date getEnd()
{
return end;
}
/**
* Sets the event end date and time
*/
public void setEnd(Date end)
{
this.end = end;
}
/**
* @return the Tags associated with the event
*/
public List<String> getTags()
{
// TODO Immutable?
return tags;
}
// TODO All Day events
// TODO Doc folder
// TODO Recurrence
// TODO Is Outlook
}

View File

@@ -33,20 +33,22 @@ import org.alfresco.service.cmr.site.SiteInfo;
*/ */
public interface CalendarService { public interface CalendarService {
/** /**
* Creates a new {@link CalendarEntry} for the given site, but * Stores a new {@link CalendarEntry} into the given site.
* doesn't save it to the repository. * The concrete class {@link CalendarEntryDTO} can be used
* to create a {@link CalendarEntry} instance for this.
* *
* @return The newly cre * @return The newly created CalendarEntry
*/ */
@NotAuditable @NotAuditable
CalendarEntry createCalendarEntry(String siteShortName, String eventTitle, CalendarEntry createCalendarEntry(String siteShortName, CalendarEntry entry);
String eventDescription, Date from, Date to);
/** /**
* Saves a {@link CalendarEntry} in the repository. * Updates an existing {@link CalendarEntry} in the repository.
*
* @return The updated CalendarEntry
*/ */
@NotAuditable @NotAuditable
void saveCalendarEntry(CalendarEntry entry); CalendarEntry updateCalendarEntry(CalendarEntry entry);
/** /**
* Deletes an existing {@link CalendarEntry} from the repository * Deletes an existing {@link CalendarEntry} from the repository