Permalink
Browse files

KERN-178 Create an activity service

  • Loading branch information...
1 parent adf4ce8 commit 0b18dc6b5c9aeff4a1d60dc20c36917463d46d80 @lancespeelmon lancespeelmon committed Sep 15, 2009
@@ -18,6 +18,9 @@
package org.sakaiproject.kernel.activity;
import static org.sakaiproject.kernel.api.activity.ActivityConstants.ACTOR_PROPERTY;
+import static org.sakaiproject.kernel.api.activity.ActivityConstants.REQUEST_PARAM_APPLICATION_ID;
+import static org.sakaiproject.kernel.api.activity.ActivityConstants.REQUEST_PARAM_TEMPLATE_ID;
+import static org.sakaiproject.kernel.api.activity.ActivityConstants.ACTIVITY_STORE_NAME;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
@@ -65,7 +68,6 @@
* interface="org.osgi.service.event.EventAdmin"
*/
public class ActivityCreateServlet extends SlingAllMethodsServlet {
- private static final String ACTIVITY_STORE_NAME = "activity";
private static final long serialVersionUID = 1375206766455341437L;
private static final Logger LOG = LoggerFactory.getLogger(ActivityCreateServlet.class);
private EventAdmin eventAdmin;
@@ -85,14 +87,14 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
}
// Let's perform some validation on the request parameters. Do we have the minimum
// required?
- RequestParameter applicationId = request.getRequestParameter("applicationId");
- if (applicationId == null || "".equals(applicationId.getString())) {
+ RequestParameter applicationId = request.getRequestParameter(REQUEST_PARAM_APPLICATION_ID);
+ if (applicationId == null || "".equals(applicationId.toString())) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"The applicationId parameter must not be null");
return;
}
- RequestParameter templateId = request.getRequestParameter("templateId");
- if (templateId == null || "".equals(templateId.getString())) {
+ RequestParameter templateId = request.getRequestParameter(REQUEST_PARAM_TEMPLATE_ID);
+ if (templateId == null || "".equals(templateId.toString())) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"The templateId parameter must not be null");
return;
@@ -63,7 +63,7 @@
public void handleEvent(Event event) {
LOG.debug("handleEvent(Event {})", event);
final String activityItemPath = (String) event.getProperty("activityItemPath");
- LOG.debug("activityItemPath={}", activityItemPath);
+ LOG.info("Processing activity: {}", activityItemPath);
Session session = null;
try {
session = slingRepository.loginAdministrative(null);
@@ -72,57 +72,15 @@ public void handleEvent(Event event) {
// process activity
// hints will be attached to the activity as to where we need to deliver
// for example: connections, siteA, siteB
+ // from Nico: /sites/siteid/activityFeed.json
// Let's try the the simpler connections case first
String actor = activity.getProperty(ACTOR_PROPERTY).getString();
if (actor == null || "".equals(actor)) { // we must know the actor
throw new IllegalStateException("Could not determine actor of activity: "
+ activity);
}
- // get the users connected to the actor and distribute to them
- String activityFeedPath = null;
- List<String> connections = connectionManager.getConnectedUsers(actor,
- ConnectionState.ACCEPTED);
- if (connections == null || connections.size() <= 0) {
- LOG.debug("{} acted but has no connections; nothing to do.", actor);
- } else { // actor has connections
- for (String connection : connections) {
- // deliver to each connection
- LOG.debug("{} acted; delivering activity to connection: {}", new Object[] {
- actor, connection });
- activityFeedPath = PathUtils.toInternalHashedPath(_USER_PRIVATE, connection,
- "/activityFeed");
- LOG.debug("activityFeedPath for connection {}={}", new Object[] { connection,
- activityFeedPath });
- // ensure the activityFeed node with the proper type
- Node activityFeedNode = JcrUtils.deepGetOrCreateNode(session,
- activityFeedPath);
- if (activityFeedNode.isNew()) {
- activityFeedNode.setProperty(
- JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY,
- ActivityConstants.ACTIVITY_FEED_RESOURCE_TYPE);
- session.save();
- }
- // activityFeed exists, let's continue with delivery
- final String deliveryPath = PathUtils.toInternalHashedPath(activityFeedPath,
- UUID.randomUUID().toString(), "");
- LOG.debug("final deliveryPath={}", deliveryPath);
- final String parentPath = deliveryPath.substring(0, deliveryPath
- .lastIndexOf("/"));
- // ensure the parent path exists before we copy source activity
- final Node parentNode = JcrUtils.deepGetOrCreateNode(session, parentPath);
- if (parentNode.isNew()) {
- session.save();
- }
- // now copy the activity from the store to the feed
- session.getWorkspace().copy(activity.getPath(), deliveryPath);
- // next let's create a source property to refer back to the original item
- // in the ActivityStore
- Node feedItem = (Node) session.getItem(deliveryPath);
- feedItem.setProperty(SOURCE_PROPERTY, activity.getPath());
- session.save();
- }
- }
-
+ // TODO assume we were passed the connections hint; need to check
+ deliverActivityToConnections(session, activity, actor);
} else {
LOG.error("Could not process activity: {}", activityItemPath);
throw new Error("Could not process activity: " + activityItemPath);
@@ -139,6 +97,65 @@ public void handleEvent(Event event) {
}
+ private void deliverActivityToConnections(Session session, Node activity, String actor)
+ throws RepositoryException {
+ LOG.debug("deliverActivityToConnections(Session {}, Node {}, String {})",
+ new Object[] { session, activity, actor });
+ // get the users connected to the actor and distribute to them
+ String activityFeedPath = null;
+ List<String> connections = connectionManager.getConnectedUsers(actor,
+ ConnectionState.ACCEPTED);
+ if (connections == null || connections.size() <= 0) {
+ LOG.debug("{} acted but has no connections; nothing to do.", actor);
+ } else { // actor has connections
+ for (String connection : connections) {
+ // deliver to each connection
+ LOG.debug("{} acted; delivering activity to connection: {}", new Object[] {
+ actor, connection });
+ // /_user/private is a BigStore, get the hashed path
+ activityFeedPath = PathUtils.toInternalHashedPath(_USER_PRIVATE, connection,
+ "/activityFeed");
+ deliverActivityToFeed(session, activity, activityFeedPath);
+ }
+ }
+ }
+
+ private void deliverActivityToFeed(Session session, Node activity,
+ String activityFeedPath) throws RepositoryException {
+ // ensure the activityFeed node with the proper type
+ Node activityFeedNode = JcrUtils.deepGetOrCreateNode(session, activityFeedPath);
+ if (activityFeedNode.isNew()) {
+ activityFeedNode.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY,
+ ActivityConstants.ACTIVITY_FEED_RESOURCE_TYPE);
+ session.save();
+ }
+ // activityFeed exists, let's continue with delivery
+ // activityFeed is a BigStore, get the hashed (real) path
+ final String deliveryPath = PathUtils.toInternalHashedPath(activityFeedPath, UUID
+ .randomUUID().toString(), "");
+ // ensure the parent path exists before we copy source activity
+ final String parentPath = deliveryPath.substring(0, deliveryPath.lastIndexOf("/"));
+ final Node parentNode = JcrUtils.deepGetOrCreateNode(session, parentPath);
+ if (parentNode.isNew()) {
+ session.save();
+ }
+ // now copy the activity from the store to the feed
+ copyActivityItem(session, activity.getPath(), deliveryPath);
+ }
+
+ private void copyActivityItem(Session session, String source, String destination)
+ throws RepositoryException {
+ LOG.debug("copyActivityItem(Session {}, String {}, String {})", new Object[] {
+ session, source, destination });
+ // now copy the activity from the source to the destination
+ session.getWorkspace().copy(source, destination);
+ // next let's create a source property to refer back to the original item
+ // in the ActivityStore
+ Node feedItem = (Node) session.getItem(destination);
+ feedItem.setProperty(SOURCE_PROPERTY, source);
+ session.save();
+ }
+
protected void bindSlingRepository(SlingRepository slingRepository) {
this.slingRepository = slingRepository;
}
@@ -21,6 +21,10 @@
*
*/
public interface ActivityConstants {
+ public static final String REQUEST_PARAM_APPLICATION_ID = "applicationId";
+ public static final String REQUEST_PARAM_TEMPLATE_ID = "templateId";
+ public static final String ACTIVITY_STORE_NAME = "activity";
+
/**
* JCR folder name for templates.
*/
@@ -47,6 +47,12 @@ public void setUp() throws Exception {
return result;
}
+ protected <T> T createMock(String name, Class<T> c) {
+ T result = org.easymock.EasyMock.createMock(name, c);
+ mocks.add(result);
+ return result;
+ }
+
protected void replay() {
org.easymock.EasyMock.replay(mocks.toArray());
}
@@ -89,6 +89,7 @@
<module>bundles/specs/ejb</module>
<module>libraries/utils</module>
<module>bundles/activemq</module>
+ <module>bundles/activity</module>
<!-- these are sling extension bundles that replace sling classes with patched versions -->
<module>bundles/sling/api</module>
<module>bundles/sling/resource</module>
@@ -195,8 +196,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
- <source>1.5</source>
- <target>1.5</target>
+ <source>1.6</source>
+ <target>1.6</target>
<showDeprecation>true</showDeprecation>
<compilerArgument>-Xlint:unchecked,deprecation,fallthrough,finally</compilerArgument>
<fork>true</fork>

0 comments on commit 0b18dc6

Please sign in to comment.