Implemented truncation of status update messages if they exceed the max status length for that channel.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29681 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
N Smith
2011-08-11 09:57:56 +00:00
parent ce5a0480e3
commit 0b171e0f40
6 changed files with 154 additions and 17 deletions

View File

@@ -113,9 +113,20 @@ public class ChannelImpl implements Channel
/**
* {@inheritDoc}
*/
public void updateStatus(String status)
public void updateStatus(String status, String nodeUrl)
{
channelType.updateStatus(this, status, getProperties());
if(channelType.canPublishStatusUpdates())
{
int urlLength = nodeUrl == null ? 0 : nodeUrl.length();
int maxLength = channelType.getMaximumStatusLength() - urlLength;
if (maxLength > 0)
{
int endpoint = Math.min(maxLength, status.length());
status = status.substring(0, endpoint );
}
String msg = nodeUrl==null ? status : status + nodeUrl;
channelType.updateStatus(this, msg, getProperties());
}
}
/**

View File

@@ -0,0 +1,113 @@
/*
* 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.repo.publishing;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import junit.framework.TestCase;
import org.alfresco.service.cmr.publishing.channels.ChannelType;
import org.alfresco.service.cmr.repository.NodeRef;
/**
* @author Nick Smith
* @since 4.0
*
*/
public class ChannelImplTest extends TestCase
{
public void testUpdateStatus() throws Exception
{
int maxLength = 30;
ChannelType channelType = mockChannelType(maxLength);
ChannelHelper helper = mock(ChannelHelper.class);
when(helper.getChannelProperties(any(NodeRef.class))).thenReturn(null);
NodeRef node = new NodeRef("test://channel/node");
ChannelImpl channel = new ChannelImpl(channelType, node, "Name", helper);
String msg = "Here is a message";
channel.updateStatus(msg, null);
verify(channelType).updateStatus(channel, msg, null);
}
public void testUpdateStatusTruncates() throws Exception
{
int maxLength = 30;
ChannelType channelType = mockChannelType(maxLength);
ChannelHelper helper = mock(ChannelHelper.class);
when(helper.getChannelProperties(any(NodeRef.class))).thenReturn(null);
NodeRef node = new NodeRef("test://channel/node");
ChannelImpl channel = new ChannelImpl(channelType, node, "Name", helper);
String msg = "Here is a much longer message to truncate.";
String expMsg = msg.substring(0, maxLength);
channel.updateStatus(msg, null);
verify(channelType).updateStatus(channel, expMsg, null);
}
public void testUpdateStatusTruncatesWithUrl() throws Exception
{
int maxLength = 30;
ChannelType channelType = mockChannelType(maxLength);
ChannelHelper helper = mock(ChannelHelper.class);
when(helper.getChannelProperties(any(NodeRef.class))).thenReturn(null);
NodeRef node = new NodeRef("test://channel/node");
ChannelImpl channel = new ChannelImpl(channelType, node, "Name", helper);
String nodeUrl ="http://foo/bar";
int endpoint = maxLength - nodeUrl.length();
String msg = "Here is a much longer message to truncate.";
String expMsg = msg.substring(0, endpoint) + nodeUrl;
channel.updateStatus(msg, nodeUrl);
verify(channelType).updateStatus(channel, expMsg, null);
}
public void testUpdateStatusNoMaxLength() throws Exception
{
ChannelType channelType = mockChannelType(0);
ChannelHelper helper = mock(ChannelHelper.class);
when(helper.getChannelProperties(any(NodeRef.class))).thenReturn(null);
NodeRef node = new NodeRef("test://channel/node");
ChannelImpl channel = new ChannelImpl(channelType, node, "Name", helper);
String nodeUrl ="http://foo/bar";
String msg = "Here is a much longer message to truncate.";
String expMsg = msg + nodeUrl;
channel.updateStatus(msg, nodeUrl);
verify(channelType).updateStatus(channel, expMsg, null);
}
private ChannelType mockChannelType(int maxLength)
{
ChannelType channelType = mock(ChannelType.class);
when(channelType.canPublishStatusUpdates()).thenReturn(true);
when(channelType.getMaximumStatusLength()).thenReturn(maxLength);
return channelType;
}
}

View File

@@ -466,8 +466,7 @@ public class PublishingEventHelper
//End the start task.
//TODO Replace with endStartTask() call after merge to HEAD.
List<WorkflowTask> tasks = workflowService.getTasksForWorkflowPath(path.getId());
WorkflowTask startTask = tasks.get(0);
WorkflowTask startTask = workflowService.getStartTask(instanceId);
workflowService.endTask(startTask.getId(), null);
return instanceId;
}

View File

@@ -112,26 +112,38 @@ public class PublishingEventProcessor
return;
}
String message = update.getMessage();
NodeRef node = update.getNodeToLinkTo();
if(node!= null)
{
String nodeUrl = publishChannel.getUrl(node);
if(nodeUrl != null)
{
message += " " + urlShortener.shortenUrl(nodeUrl);
}
}
String nodeUrl = getNodeUrl(publishChannel, update);
Set<String> channels = update.getChannelIds();
for (String channelId : channels)
{
Channel channel = channelService.getChannelById(channelId);
if(channel != null && channel.getChannelType().canPublishStatusUpdates())
if(channel != null)
{
channel.updateStatus(message);
channel.updateStatus(message, nodeUrl);
}
}
}
/**
* @param publishChannel
* @param update
* @return
*/
private String getNodeUrl(Channel publishChannel, StatusUpdate update)
{
NodeRef node = update.getNodeToLinkTo();
String nodeUrl = null;
if(node!= null)
{
nodeUrl = publishChannel.getUrl(node);
if(nodeUrl != null)
{
nodeUrl = " " + urlShortener.shortenUrl(nodeUrl);
}
}
return nodeUrl;
}
public void publishEvent(Channel channel, PublishingEvent event)
{
NodeRef eventNode = eventHelper.getPublishingEventNode(event.getId());
@@ -155,6 +167,8 @@ public class PublishingEventProcessor
if(NodeUtils.exists(publishedNode, nodeService))
{
channel.unPublish(publishedNode);
// Need to set as temporary to delete node instead of archiving.
nodeService.addAspect(publishedNode, ContentModel.ASPECT_TEMPORARY, null);
nodeService.deleteNode(publishedNode);
}
}

View File

@@ -57,7 +57,7 @@ public interface Channel
void publish(NodeRef nodeToPublish);
void unPublish(NodeRef nodeToUnpublish);
void updateStatus(String status);
void updateStatus(String status, String nodeUrl);
/**
* Returns the URL for some published content given the content node in the editorial environment.

View File

@@ -35,7 +35,7 @@ import org.springframework.core.io.Resource;
*/
public interface ChannelType
{
enum AuthStatus {AUTHORISED, RETRY, UNAUTHORISED};
enum AuthStatus {AUTHORISED, RETRY, UNAUTHORISED}
String getId();
QName getChannelNodeType();