mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
MT Share - initial checkpoint
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13459 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 Alfresco Software Limited.
|
||||
* Copyright (C) 2005-2009 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
@@ -38,6 +38,7 @@ import org.alfresco.repo.activities.feed.control.FeedControlDaoService;
|
||||
import org.alfresco.repo.activities.post.ActivityPostDAO;
|
||||
import org.alfresco.repo.activities.post.ActivityPostDaoService;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.repo.tenant.TenantService;
|
||||
import org.alfresco.service.cmr.activities.ActivityService;
|
||||
import org.alfresco.service.cmr.activities.FeedControl;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
@@ -69,6 +70,8 @@ public class ActivityServiceImpl implements ActivityService
|
||||
private AuthorityService authorityService;
|
||||
private FeedGenerator feedGenerator;
|
||||
|
||||
private TenantService tenantService;
|
||||
|
||||
private int maxFeedItems = 100;
|
||||
|
||||
private boolean userNamesAreCaseSensitive = false;
|
||||
@@ -108,32 +111,37 @@ public class ActivityServiceImpl implements ActivityService
|
||||
this.feedGenerator = feedGenerator;
|
||||
}
|
||||
|
||||
public void setTenantService(TenantService tenantService)
|
||||
{
|
||||
this.tenantService = tenantService;
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.activities.ActivityService#postActivity(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
|
||||
*/
|
||||
public void postActivity(String activityType, String network, String appTool, String activityData)
|
||||
public void postActivity(String activityType, String siteId, String appTool, String activityData)
|
||||
{
|
||||
postActivity(activityType, network, appTool, activityData, ActivityPostDAO.STATUS.PENDING);
|
||||
postActivity(activityType, siteId, appTool, activityData, ActivityPostDAO.STATUS.PENDING);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.activities.ActivityService#postActivity(java.lang.String, java.lang.String, java.lang.String, org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public void postActivity(String activityType, String network, String appTool, NodeRef nodeRef)
|
||||
public void postActivity(String activityType, String siteId, String appTool, NodeRef nodeRef)
|
||||
{
|
||||
ParameterCheck.mandatory("nodeRef", nodeRef);
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("{").append("\"nodeRef\":\"").append(nodeRef.toString()).append("\"").append("}");
|
||||
|
||||
postActivity(activityType, network, appTool, sb.toString(), ActivityPostDAO.STATUS.PENDING);
|
||||
postActivity(activityType, siteId, appTool, sb.toString(), ActivityPostDAO.STATUS.PENDING);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.activities.ActivityService#postActivity(java.lang.String, java.lang.String, java.lang.String, org.alfresco.service.cmr.repository.NodeRef, java.lang.String)
|
||||
*/
|
||||
public void postActivity(String activityType, String network, String appTool, NodeRef nodeRef, String name)
|
||||
public void postActivity(String activityType, String siteId, String appTool, NodeRef nodeRef, String name)
|
||||
{
|
||||
ParameterCheck.mandatory("nodeRef", nodeRef);
|
||||
|
||||
@@ -142,13 +150,13 @@ public class ActivityServiceImpl implements ActivityService
|
||||
.append("\"name\":\"").append(name).append("\"")
|
||||
.append("}");
|
||||
|
||||
postActivity(activityType, network, appTool, sb.toString(), ActivityPostDAO.STATUS.PENDING);
|
||||
postActivity(activityType, siteId, appTool, sb.toString(), ActivityPostDAO.STATUS.PENDING);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.service.cmr.activities.ActivityService#postActivity(java.lang.String, java.lang.String, java.lang.String, org.alfresco.service.cmr.repository.NodeRef, java.lang.String, org.alfresco.service.namespace.QName, org.alfresco.service.cmr.repository.NodeRef)
|
||||
*/
|
||||
public void postActivity(String activityType, String network, String appTool, NodeRef nodeRef, String name, QName typeQName, NodeRef parentNodeRef)
|
||||
public void postActivity(String activityType, String siteId, String appTool, NodeRef nodeRef, String name, QName typeQName, NodeRef parentNodeRef)
|
||||
{
|
||||
// primarily for delete node activities - eg. delete document, delete folder
|
||||
|
||||
@@ -163,23 +171,23 @@ public class ActivityServiceImpl implements ActivityService
|
||||
.append("\"parentNodeRef\":\"").append(parentNodeRef.toString()).append("\"")
|
||||
.append("}");
|
||||
|
||||
postActivity(activityType, network, appTool, sb.toString(), ActivityPostDAO.STATUS.PENDING);
|
||||
postActivity(activityType, siteId, appTool, sb.toString(), ActivityPostDAO.STATUS.PENDING);
|
||||
}
|
||||
|
||||
private void postActivity(String activityType, String siteNetwork, String appTool, String activityData, ActivityPostDAO.STATUS status)
|
||||
private void postActivity(String activityType, String siteId, String appTool, String activityData, ActivityPostDAO.STATUS status)
|
||||
{
|
||||
String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
|
||||
|
||||
try
|
||||
{
|
||||
// optional - default to empty string
|
||||
if (siteNetwork == null)
|
||||
if (siteId == null)
|
||||
{
|
||||
siteNetwork = "";
|
||||
siteId = "";
|
||||
}
|
||||
else if (siteNetwork.length() > MAX_LEN_SITE_ID)
|
||||
else if (siteId.length() > MAX_LEN_SITE_ID)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Invalid site network - exceeds " + MAX_LEN_SITE_ID + " chars: " + siteNetwork);
|
||||
throw new AlfrescoRuntimeException("Invalid siteId - exceeds " + MAX_LEN_SITE_ID + " chars: " + siteId);
|
||||
}
|
||||
|
||||
// optional - default to empty string
|
||||
@@ -235,7 +243,9 @@ public class ActivityServiceImpl implements ActivityService
|
||||
Date postDate = new Date();
|
||||
ActivityPostDAO activityPost = new ActivityPostDAO();
|
||||
activityPost.setUserId(currentUser);
|
||||
activityPost.setSiteNetwork(siteNetwork);
|
||||
|
||||
activityPost.setSiteNetwork(tenantService.getName(siteId));
|
||||
|
||||
activityPost.setAppTool(appTool);
|
||||
activityPost.setActivityData(activityData);
|
||||
activityPost.setActivityType(activityType);
|
||||
@@ -297,6 +307,8 @@ public class ActivityServiceImpl implements ActivityService
|
||||
List<ActivityFeedDAO> activityFeeds = null;
|
||||
if (siteId != null)
|
||||
{
|
||||
siteId = tenantService.getName(siteId);
|
||||
|
||||
activityFeeds = feedDaoService.selectUserFeedEntries(feedUserId, format, siteId);
|
||||
}
|
||||
else
|
||||
@@ -312,6 +324,8 @@ public class ActivityServiceImpl implements ActivityService
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
activityFeed.setSiteNetwork(tenantService.getBaseName(activityFeed.getSiteNetwork()));
|
||||
activityFeedEntries.add(activityFeed.getJSONString());
|
||||
}
|
||||
}
|
||||
@@ -343,6 +357,8 @@ public class ActivityServiceImpl implements ActivityService
|
||||
|
||||
try
|
||||
{
|
||||
siteId = tenantService.getName(siteId);
|
||||
|
||||
List<ActivityFeedDAO> activityFeeds = feedDaoService.selectSiteFeedEntries(siteId, format);
|
||||
|
||||
int count = 0;
|
||||
@@ -353,6 +369,8 @@ public class ActivityServiceImpl implements ActivityService
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
activityFeed.setSiteNetwork(tenantService.getBaseName(activityFeed.getSiteNetwork()));
|
||||
activityFeedEntries.add(activityFeed.getJSONString());
|
||||
}
|
||||
}
|
||||
@@ -504,4 +522,16 @@ public class ActivityServiceImpl implements ActivityService
|
||||
throw are;
|
||||
}
|
||||
}
|
||||
|
||||
private FeedControl getTenantFeedControl(FeedControl feedControl)
|
||||
{
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
private FeedControl getBaseFeedControl(FeedControl feedControl)
|
||||
{
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -1,338 +1,351 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.repo.activities.post.lookup;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.activities.post.ActivityPostDAO;
|
||||
import org.alfresco.repo.activities.post.ActivityPostDaoService;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.Path;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
import org.alfresco.service.cmr.security.PersonService;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.alfresco.util.PropertyCheck;
|
||||
import org.alfresco.util.VmShutdownListener;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.quartz.JobExecutionException;
|
||||
|
||||
/**
|
||||
* The post lookup component is responsible for updating posts that require a secondary lookup (to get additional activity data)
|
||||
*/
|
||||
public class PostLookup
|
||||
{
|
||||
private static Log logger = LogFactory.getLog(PostLookup.class);
|
||||
|
||||
private static VmShutdownListener vmShutdownListener = new VmShutdownListener(PostLookup.class.getName());
|
||||
|
||||
private ActivityPostDaoService postDaoService;
|
||||
private NodeService nodeService;
|
||||
private PermissionService permissionService;
|
||||
private TransactionService transactionService;
|
||||
private PersonService personService;
|
||||
|
||||
public void setPostDaoService(ActivityPostDaoService postDaoService)
|
||||
{
|
||||
this.postDaoService = postDaoService;
|
||||
}
|
||||
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
public void setPermissionService(PermissionService permissionService)
|
||||
{
|
||||
this.permissionService = permissionService;
|
||||
}
|
||||
|
||||
public void setTransactionService(TransactionService transactionService)
|
||||
{
|
||||
this.transactionService = transactionService;
|
||||
}
|
||||
|
||||
public void setPersonService(PersonService personService)
|
||||
{
|
||||
this.personService = personService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform basic checks to ensure that the necessary dependencies were injected.
|
||||
*/
|
||||
private void checkProperties()
|
||||
{
|
||||
PropertyCheck.mandatory(this, "postDaoService", postDaoService);
|
||||
PropertyCheck.mandatory(this, "nodeService", nodeService);
|
||||
PropertyCheck.mandatory(this, "permissionService", permissionService);
|
||||
PropertyCheck.mandatory(this, "transactionService", transactionService);
|
||||
PropertyCheck.mandatory(this, "personService", personService);
|
||||
}
|
||||
|
||||
public void execute() throws JobExecutionException
|
||||
{
|
||||
checkProperties();
|
||||
try
|
||||
{
|
||||
ActivityPostDAO params = new ActivityPostDAO();
|
||||
params.setStatus(ActivityPostDAO.STATUS.PENDING.toString());
|
||||
|
||||
List<ActivityPostDAO> activityPosts = postDaoService.selectPosts(params);
|
||||
|
||||
if (activityPosts.size() > 0)
|
||||
{
|
||||
logger.info("Update: " + activityPosts.size() + " activity posts");
|
||||
}
|
||||
|
||||
for (ActivityPostDAO activityPost : activityPosts)
|
||||
{
|
||||
try
|
||||
{
|
||||
postDaoService.startTransaction();
|
||||
|
||||
JSONObject jo = new JSONObject(new JSONTokener(activityPost.getActivityData()));
|
||||
String postUserId = activityPost.getUserId();
|
||||
|
||||
if (! jo.isNull("nodeRef"))
|
||||
{
|
||||
String nodeRefStr = jo.getString("nodeRef");
|
||||
NodeRef nodeRef = new NodeRef(nodeRefStr);
|
||||
|
||||
// lookup additional node data
|
||||
JSONObject activityData = lookupNode(nodeRef, postUserId, jo);
|
||||
|
||||
activityPost.setActivityData(activityData.toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
// lookup additional person data
|
||||
Pair<String, String> firstLastName = lookupPerson(postUserId);
|
||||
if (firstLastName != null)
|
||||
{
|
||||
jo.put("firstName", firstLastName.getFirst());
|
||||
jo.put("lastName", firstLastName.getSecond());
|
||||
|
||||
activityPost.setActivityData(jo.toString());
|
||||
}
|
||||
}
|
||||
|
||||
activityPost.setLastModified(new Date());
|
||||
|
||||
postDaoService.updatePost(activityPost.getId(), activityPost.getSiteNetwork(), activityPost.getActivityData(), ActivityPostDAO.STATUS.POSTED);
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
activityPost.setStatus(ActivityPostDAO.STATUS.POSTED.toString()); // for debug output
|
||||
logger.debug("Updated: " + activityPost);
|
||||
}
|
||||
|
||||
postDaoService.commitTransaction();
|
||||
}
|
||||
catch (JSONException e)
|
||||
{
|
||||
// log error, but consume exception (skip this post)
|
||||
logger.error("Skipping activity post " + activityPost.getId() + ": " + e);
|
||||
postDaoService.updatePostStatus(activityPost.getId(), ActivityPostDAO.STATUS.ERROR);
|
||||
|
||||
postDaoService.commitTransaction();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
logger.error("Exception during update of post", e);
|
||||
throw new JobExecutionException(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
postDaoService.endTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
logger.error("Exception during select of posts", e);
|
||||
throw new JobExecutionException(e);
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
// If the VM is shutting down, then ignore
|
||||
if (vmShutdownListener.isVmShuttingDown())
|
||||
{
|
||||
// Ignore
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error("Exception during update of posts", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Pair<String, String> lookupPerson(final String postUserId) throws JSONException
|
||||
{
|
||||
return AuthenticationUtil.runAs(new RunAsWork<Pair<String, String>>()
|
||||
{
|
||||
public Pair<String, String> doWork() throws Exception
|
||||
{
|
||||
RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
|
||||
|
||||
// wrap to make the request in a transaction
|
||||
RetryingTransactionCallback<Pair<String, String>> lookup = new RetryingTransactionCallback<Pair<String, String>>()
|
||||
{
|
||||
public Pair<String, String> execute() throws Throwable
|
||||
{
|
||||
String firstName = "";
|
||||
String lastName = "";
|
||||
|
||||
if (personService.personExists(postUserId))
|
||||
{
|
||||
NodeRef personRef = personService.getPerson(postUserId);
|
||||
|
||||
firstName = (String)nodeService.getProperty(personRef, ContentModel.PROP_FIRSTNAME);
|
||||
lastName = (String)nodeService.getProperty(personRef, ContentModel.PROP_LASTNAME);
|
||||
|
||||
return new Pair<String, String>(firstName, lastName);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// execute in txn
|
||||
return txnHelper.doInTransaction(lookup, true);
|
||||
}
|
||||
}, AuthenticationUtil.getSystemUserName());
|
||||
}
|
||||
|
||||
private JSONObject lookupNode(final NodeRef nodeRef, final String postUserId, final JSONObject jo) throws JSONException
|
||||
{
|
||||
return AuthenticationUtil.runAs(new RunAsWork<JSONObject>()
|
||||
{
|
||||
public JSONObject doWork() throws Exception
|
||||
{
|
||||
RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
|
||||
|
||||
// wrap to make the request in a transaction
|
||||
RetryingTransactionCallback<JSONObject> lookup = new RetryingTransactionCallback<JSONObject>()
|
||||
{
|
||||
public JSONObject execute() throws Throwable
|
||||
{
|
||||
String name = "";
|
||||
if (! jo.isNull("name"))
|
||||
{
|
||||
name = jo.getString("name");
|
||||
}
|
||||
|
||||
NodeRef parentNodeRef = null;
|
||||
if (! jo.isNull("parentNodeRef"))
|
||||
{
|
||||
parentNodeRef = new NodeRef(jo.getString("parentNodeRef"));
|
||||
}
|
||||
|
||||
|
||||
String typeQName = "";
|
||||
if (! jo.isNull("typeQName"))
|
||||
{
|
||||
typeQName = jo.getString("typeQName");
|
||||
}
|
||||
|
||||
String displayPath = "";
|
||||
Path path = null;
|
||||
String firstName = "";
|
||||
String lastName = "";
|
||||
|
||||
if (personService.personExists(postUserId))
|
||||
{
|
||||
// lookup firstname, lastname
|
||||
NodeRef personRef = personService.getPerson(postUserId);
|
||||
|
||||
firstName = (String)nodeService.getProperty(personRef, ContentModel.PROP_FIRSTNAME);
|
||||
lastName = (String)nodeService.getProperty(personRef, ContentModel.PROP_LASTNAME);
|
||||
}
|
||||
|
||||
if ((nodeRef != null) && (nodeService.exists(nodeRef)))
|
||||
{
|
||||
if (name.length() == 0)
|
||||
{
|
||||
// lookup node name
|
||||
name = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
|
||||
}
|
||||
|
||||
if (typeQName.length() == 0)
|
||||
{
|
||||
// lookup type
|
||||
typeQName = nodeService.getType(nodeRef).toPrefixString(); // TODO: missing the prefix ?
|
||||
}
|
||||
|
||||
if (parentNodeRef == null)
|
||||
{
|
||||
// lookup parent node
|
||||
parentNodeRef = nodeService.getPrimaryParent(nodeRef).getParentRef();
|
||||
}
|
||||
}
|
||||
|
||||
if ((parentNodeRef != null) && (nodeService.exists(parentNodeRef)))
|
||||
{
|
||||
// parent node exists, lookup parent node path
|
||||
path = nodeService.getPath(parentNodeRef);
|
||||
}
|
||||
|
||||
if (path != null)
|
||||
{
|
||||
// lookup display path
|
||||
displayPath = path.toDisplayPath(nodeService, permissionService);
|
||||
|
||||
// note: for now, also tack on the node name
|
||||
displayPath += "/" + name;
|
||||
}
|
||||
|
||||
// merge with existing activity data
|
||||
jo.put("name", name);
|
||||
jo.put("nodeRef", nodeRef.toString());
|
||||
jo.put("typeQName", typeQName);
|
||||
jo.put("parentNodeRef", (parentNodeRef != null ? parentNodeRef.toString() : null));
|
||||
jo.put("displayPath", displayPath);
|
||||
jo.put("firstName", firstName);
|
||||
jo.put("lastName", lastName);
|
||||
|
||||
return jo;
|
||||
}
|
||||
};
|
||||
|
||||
// execute in txn
|
||||
return txnHelper.doInTransaction(lookup, true);
|
||||
}
|
||||
}, AuthenticationUtil.getSystemUserName());
|
||||
}
|
||||
/*
|
||||
* Copyright (C) 2005-2009 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.repo.activities.post.lookup;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.activities.post.ActivityPostDAO;
|
||||
import org.alfresco.repo.activities.post.ActivityPostDaoService;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
|
||||
import org.alfresco.repo.tenant.TenantService;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.Path;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
import org.alfresco.service.cmr.security.PersonService;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.alfresco.util.PropertyCheck;
|
||||
import org.alfresco.util.VmShutdownListener;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.quartz.JobExecutionException;
|
||||
|
||||
/**
|
||||
* The post lookup component is responsible for updating posts that require a secondary lookup (to get additional activity data)
|
||||
*/
|
||||
public class PostLookup
|
||||
{
|
||||
private static Log logger = LogFactory.getLog(PostLookup.class);
|
||||
|
||||
private static VmShutdownListener vmShutdownListener = new VmShutdownListener(PostLookup.class.getName());
|
||||
|
||||
private ActivityPostDaoService postDaoService;
|
||||
private NodeService nodeService;
|
||||
private PermissionService permissionService;
|
||||
private TransactionService transactionService;
|
||||
private PersonService personService;
|
||||
private TenantService tenantService;
|
||||
|
||||
public void setPostDaoService(ActivityPostDaoService postDaoService)
|
||||
{
|
||||
this.postDaoService = postDaoService;
|
||||
}
|
||||
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
public void setPermissionService(PermissionService permissionService)
|
||||
{
|
||||
this.permissionService = permissionService;
|
||||
}
|
||||
|
||||
public void setTransactionService(TransactionService transactionService)
|
||||
{
|
||||
this.transactionService = transactionService;
|
||||
}
|
||||
|
||||
public void setPersonService(PersonService personService)
|
||||
{
|
||||
this.personService = personService;
|
||||
}
|
||||
|
||||
public void setTenantService(TenantService tenantService)
|
||||
{
|
||||
this.tenantService = tenantService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform basic checks to ensure that the necessary dependencies were injected.
|
||||
*/
|
||||
private void checkProperties()
|
||||
{
|
||||
PropertyCheck.mandatory(this, "postDaoService", postDaoService);
|
||||
PropertyCheck.mandatory(this, "nodeService", nodeService);
|
||||
PropertyCheck.mandatory(this, "permissionService", permissionService);
|
||||
PropertyCheck.mandatory(this, "transactionService", transactionService);
|
||||
PropertyCheck.mandatory(this, "personService", personService);
|
||||
PropertyCheck.mandatory(this, "tenantService", tenantService);
|
||||
}
|
||||
|
||||
public void execute() throws JobExecutionException
|
||||
{
|
||||
checkProperties();
|
||||
try
|
||||
{
|
||||
ActivityPostDAO params = new ActivityPostDAO();
|
||||
params.setStatus(ActivityPostDAO.STATUS.PENDING.toString());
|
||||
|
||||
List<ActivityPostDAO> activityPosts = postDaoService.selectPosts(params);
|
||||
|
||||
if (activityPosts.size() > 0)
|
||||
{
|
||||
logger.info("Update: " + activityPosts.size() + " activity posts");
|
||||
}
|
||||
|
||||
for (final ActivityPostDAO activityPost : activityPosts)
|
||||
{
|
||||
try
|
||||
{
|
||||
postDaoService.startTransaction();
|
||||
|
||||
final JSONObject jo = new JSONObject(new JSONTokener(activityPost.getActivityData()));
|
||||
final String postUserId = activityPost.getUserId();
|
||||
|
||||
// MT share
|
||||
String tenantDomain = tenantService.getUserDomain(postUserId);
|
||||
|
||||
AuthenticationUtil.runAs(new RunAsWork<Object>()
|
||||
{
|
||||
public Object doWork() throws Exception
|
||||
{
|
||||
if (! jo.isNull("nodeRef"))
|
||||
{
|
||||
String nodeRefStr = jo.getString("nodeRef");
|
||||
NodeRef nodeRef = new NodeRef(nodeRefStr);
|
||||
|
||||
// lookup additional node data
|
||||
JSONObject activityData = lookupNode(nodeRef, postUserId, jo);
|
||||
|
||||
activityPost.setActivityData(activityData.toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
// lookup additional person data
|
||||
Pair<String, String> firstLastName = lookupPerson(postUserId);
|
||||
if (firstLastName != null)
|
||||
{
|
||||
jo.put("firstName", firstLastName.getFirst());
|
||||
jo.put("lastName", firstLastName.getSecond());
|
||||
|
||||
activityPost.setActivityData(jo.toString());
|
||||
}
|
||||
}
|
||||
|
||||
activityPost.setLastModified(new Date());
|
||||
|
||||
postDaoService.updatePost(activityPost.getId(), activityPost.getSiteNetwork(), activityPost.getActivityData(), ActivityPostDAO.STATUS.POSTED);
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
activityPost.setStatus(ActivityPostDAO.STATUS.POSTED.toString()); // for debug output
|
||||
logger.debug("Updated: " + activityPost);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}, tenantService.getDomainUser(AuthenticationUtil.getSystemUserName(), tenantDomain));
|
||||
|
||||
postDaoService.commitTransaction();
|
||||
}
|
||||
catch (JSONException e)
|
||||
{
|
||||
// log error, but consume exception (skip this post)
|
||||
logger.error("Skipping activity post " + activityPost.getId() + ": " + e);
|
||||
postDaoService.updatePostStatus(activityPost.getId(), ActivityPostDAO.STATUS.ERROR);
|
||||
|
||||
postDaoService.commitTransaction();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
logger.error("Exception during update of post", e);
|
||||
throw new JobExecutionException(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
postDaoService.endTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
logger.error("Exception during select of posts", e);
|
||||
throw new JobExecutionException(e);
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
// If the VM is shutting down, then ignore
|
||||
if (vmShutdownListener.isVmShuttingDown())
|
||||
{
|
||||
// Ignore
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error("Exception during update of posts", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Pair<String, String> lookupPerson(final String postUserId) throws JSONException
|
||||
{
|
||||
RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
|
||||
|
||||
// wrap to make the request in a transaction
|
||||
RetryingTransactionCallback<Pair<String, String>> lookup = new RetryingTransactionCallback<Pair<String, String>>()
|
||||
{
|
||||
public Pair<String, String> execute() throws Throwable
|
||||
{
|
||||
String firstName = "";
|
||||
String lastName = "";
|
||||
|
||||
if (personService.personExists(postUserId))
|
||||
{
|
||||
NodeRef personRef = personService.getPerson(postUserId);
|
||||
|
||||
firstName = (String)nodeService.getProperty(personRef, ContentModel.PROP_FIRSTNAME);
|
||||
lastName = (String)nodeService.getProperty(personRef, ContentModel.PROP_LASTNAME);
|
||||
|
||||
return new Pair<String, String>(firstName, lastName);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// execute in txn
|
||||
return txnHelper.doInTransaction(lookup, true);
|
||||
}
|
||||
|
||||
private JSONObject lookupNode(final NodeRef nodeRef, final String postUserId, final JSONObject jo) throws JSONException
|
||||
{
|
||||
return AuthenticationUtil.runAs(new RunAsWork<JSONObject>()
|
||||
{
|
||||
public JSONObject doWork() throws Exception
|
||||
{
|
||||
RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
|
||||
|
||||
// wrap to make the request in a transaction
|
||||
RetryingTransactionCallback<JSONObject> lookup = new RetryingTransactionCallback<JSONObject>()
|
||||
{
|
||||
public JSONObject execute() throws Throwable
|
||||
{
|
||||
String name = "";
|
||||
if (! jo.isNull("name"))
|
||||
{
|
||||
name = jo.getString("name");
|
||||
}
|
||||
|
||||
NodeRef parentNodeRef = null;
|
||||
if (! jo.isNull("parentNodeRef"))
|
||||
{
|
||||
parentNodeRef = new NodeRef(jo.getString("parentNodeRef"));
|
||||
}
|
||||
|
||||
|
||||
String typeQName = "";
|
||||
if (! jo.isNull("typeQName"))
|
||||
{
|
||||
typeQName = jo.getString("typeQName");
|
||||
}
|
||||
|
||||
String displayPath = "";
|
||||
Path path = null;
|
||||
String firstName = "";
|
||||
String lastName = "";
|
||||
|
||||
if (personService.personExists(postUserId))
|
||||
{
|
||||
// lookup firstname, lastname
|
||||
NodeRef personRef = personService.getPerson(postUserId);
|
||||
|
||||
firstName = (String)nodeService.getProperty(personRef, ContentModel.PROP_FIRSTNAME);
|
||||
lastName = (String)nodeService.getProperty(personRef, ContentModel.PROP_LASTNAME);
|
||||
}
|
||||
|
||||
if ((nodeRef != null) && (nodeService.exists(nodeRef)))
|
||||
{
|
||||
if (name.length() == 0)
|
||||
{
|
||||
// lookup node name
|
||||
name = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
|
||||
}
|
||||
|
||||
if (typeQName.length() == 0)
|
||||
{
|
||||
// lookup type
|
||||
typeQName = nodeService.getType(nodeRef).toPrefixString(); // TODO: missing the prefix ?
|
||||
}
|
||||
|
||||
if (parentNodeRef == null)
|
||||
{
|
||||
// lookup parent node
|
||||
parentNodeRef = nodeService.getPrimaryParent(nodeRef).getParentRef();
|
||||
}
|
||||
}
|
||||
|
||||
if ((parentNodeRef != null) && (nodeService.exists(parentNodeRef)))
|
||||
{
|
||||
// parent node exists, lookup parent node path
|
||||
path = nodeService.getPath(parentNodeRef);
|
||||
}
|
||||
|
||||
if (path != null)
|
||||
{
|
||||
// lookup display path
|
||||
displayPath = path.toDisplayPath(nodeService, permissionService);
|
||||
|
||||
// note: for now, also tack on the node name
|
||||
displayPath += "/" + name;
|
||||
}
|
||||
|
||||
// merge with existing activity data
|
||||
jo.put("name", name);
|
||||
jo.put("nodeRef", nodeRef.toString());
|
||||
jo.put("typeQName", typeQName);
|
||||
jo.put("parentNodeRef", (parentNodeRef != null ? parentNodeRef.toString() : null));
|
||||
jo.put("displayPath", displayPath);
|
||||
jo.put("firstName", firstName);
|
||||
jo.put("lastName", lastName);
|
||||
|
||||
return jo;
|
||||
}
|
||||
};
|
||||
|
||||
// execute in txn
|
||||
return txnHelper.doInTransaction(lookup, true);
|
||||
}
|
||||
}, AuthenticationUtil.getSystemUserName());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user