Initial Subscription Cervice check-in

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28425 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Florian Mü
2011-06-16 11:57:04 +00:00
parent 2445f01771
commit a9eb35e67f
31 changed files with 2426 additions and 127 deletions

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2005-2011 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.domain.subscriptions;
import org.alfresco.query.PagingRequest;
import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.subscriptions.PagingFollowingResults;
import org.alfresco.service.cmr.subscriptions.PagingSubscriptionResults;
import org.alfresco.service.cmr.subscriptions.SubscriptionItemTypeEnum;
public abstract class AbstractSubscriptionsDAO implements SubscriptionsDAO
{
protected NodeDAO nodeDAO;
protected PersonService personService;
public final void setNodeDAO(NodeDAO nodeDAO)
{
this.nodeDAO = nodeDAO;
}
public final void setPersonService(PersonService personService)
{
this.personService = personService;
}
@Override
public abstract PagingSubscriptionResults selectSubscriptions(String userId, SubscriptionItemTypeEnum type,
PagingRequest pagingRequest);
@Override
public abstract int countSubscriptions(String userId, SubscriptionItemTypeEnum type);
@Override
public abstract void insertSubscription(String userId, NodeRef node);
@Override
public abstract void deleteSubscription(String userId, NodeRef node);
@Override
public abstract boolean hasSubscribed(String userId, NodeRef node);
@Override
public abstract PagingFollowingResults selectFollowers(String userId, PagingRequest pagingRequest);
@Override
public abstract int countFollowers(String userId);
protected NodeRef getUserNodeRef(String userId)
{
return personService.getPerson(userId, false);
}
}

View File

@@ -0,0 +1,230 @@
/*
* Copyright (C) 2005-2011 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.domain.subscriptions;
import junit.framework.TestCase;
import org.alfresco.query.PagingRequest;
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.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.subscriptions.PagingFollowingResults;
import org.alfresco.service.cmr.subscriptions.PagingSubscriptionResults;
import org.alfresco.service.cmr.subscriptions.SubscriptionItemTypeEnum;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
public class SubscriptionDAOTest extends TestCase
{
private ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private TransactionService transactionService;
private RetryingTransactionHelper txnHelper;
private PersonService personService;
private SubscriptionsDAO subscriptionsDAO;
protected NodeRef getUserNodeRef(final String userId)
{
final PersonService ps = personService;
return AuthenticationUtil.runAs(new RunAsWork<NodeRef>()
{
@Override
public NodeRef doWork() throws Exception
{
return ps.getPerson(userId);
}
}, AuthenticationUtil.getSystemUserName());
}
protected void insert(final String userId, final NodeRef node) throws Exception
{
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
subscriptionsDAO.insertSubscription(userId, node);
return null;
}
};
txnHelper.doInTransaction(callback, false, false);
}
protected void delete(final String userId, final NodeRef node) throws Exception
{
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
subscriptionsDAO.deleteSubscription(userId, node);
return null;
}
};
txnHelper.doInTransaction(callback, false, false);
}
protected int count(final String userId) throws Exception
{
RetryingTransactionCallback<Integer> callback = new RetryingTransactionCallback<Integer>()
{
public Integer execute() throws Throwable
{
return subscriptionsDAO.countSubscriptions(userId, SubscriptionItemTypeEnum.USER);
}
};
return txnHelper.doInTransaction(callback, false, false);
}
protected boolean hasSubscribed(final String userId, final NodeRef node) throws Exception
{
RetryingTransactionCallback<Boolean> callback = new RetryingTransactionCallback<Boolean>()
{
public Boolean execute() throws Throwable
{
return subscriptionsDAO.hasSubscribed(userId, node);
}
};
return txnHelper.doInTransaction(callback, false, false);
}
protected PagingSubscriptionResults select(final String userId) throws Exception
{
RetryingTransactionCallback<PagingSubscriptionResults> callback = new RetryingTransactionCallback<PagingSubscriptionResults>()
{
public PagingSubscriptionResults execute() throws Throwable
{
return subscriptionsDAO.selectSubscriptions(userId, SubscriptionItemTypeEnum.USER, new PagingRequest(
100000, null));
}
};
return txnHelper.doInTransaction(callback, false, false);
}
protected int countFollowers(final String userId) throws Exception
{
RetryingTransactionCallback<Integer> callback = new RetryingTransactionCallback<Integer>()
{
public Integer execute() throws Throwable
{
return subscriptionsDAO.countFollowers(userId);
}
};
return txnHelper.doInTransaction(callback, false, false);
}
protected PagingFollowingResults selectFollowing(final String userId) throws Exception
{
RetryingTransactionCallback<PagingFollowingResults> callback = new RetryingTransactionCallback<PagingFollowingResults>()
{
public PagingFollowingResults execute() throws Throwable
{
return subscriptionsDAO.selectFollowing(userId, new PagingRequest(100000, null));
}
};
return txnHelper.doInTransaction(callback, false, false);
}
protected PagingFollowingResults selectFollowers(final String userId) throws Exception
{
RetryingTransactionCallback<PagingFollowingResults> callback = new RetryingTransactionCallback<PagingFollowingResults>()
{
public PagingFollowingResults execute() throws Throwable
{
return subscriptionsDAO.selectFollowers(userId, new PagingRequest(100000, null));
}
};
return txnHelper.doInTransaction(callback, false, false);
}
@Override
public void setUp() throws Exception
{
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
transactionService = serviceRegistry.getTransactionService();
txnHelper = transactionService.getRetryingTransactionHelper();
personService = serviceRegistry.getPersonService();
subscriptionsDAO = (SubscriptionsDAO) ctx.getBean("subscriptionsDAO");
}
public void testInsertAndDelete() throws Exception
{
String userId = "admin";
String userId2 = "guest";
NodeRef nodeRef = getUserNodeRef(userId2);
// check subscription first
if (hasSubscribed(userId, nodeRef))
{
delete(userId, nodeRef);
}
boolean hasSubscribed = hasSubscribed(userId, nodeRef);
assertFalse(hasSubscribed);
// count subscriptions
int count = count(userId);
assertTrue(count >= 0);
// insert
insert(userId, nodeRef);
insert(userId, nodeRef);
assertEquals(count + 1, count(userId));
assertTrue(hasSubscribed(userId, nodeRef));
// select
PagingSubscriptionResults psr = select(userId);
assertNotNull(psr);
assertNotNull(psr.getPage());
assertTrue(psr.getPage().contains(nodeRef));
PagingFollowingResults following = selectFollowing(userId);
assertNotNull(following);
assertNotNull(following.getPage());
assertTrue(following.getPage().contains(userId2));
assertEquals(psr.getPage().size(), following.getPage().size());
// count followers
int followerCount = countFollowers(userId2);
assertTrue(followerCount >= 0);
// select followers
PagingFollowingResults followers = selectFollowers(userId2);
assertNotNull(followers);
assertNotNull(followers.getPage());
assertTrue(followers.getPage().contains(userId));
// delete
delete(userId, nodeRef);
assertEquals(count, count(userId));
assertFalse(hasSubscribed(userId, nodeRef));
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2005-2011 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.domain.subscriptions;
public class SubscriptionEntity
{
private Long userNodeId;
private Long nodeId;
public Long getUserNodeId()
{
return userNodeId;
}
public void setUserNodeId(Long userNodeId)
{
this.userNodeId = userNodeId;
}
public Long getNodeId()
{
return nodeId;
}
public void setNodeId(Long nodeId)
{
this.nodeId = nodeId;
}
public boolean getFalse()
{
return false;
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (C) 2005-2011 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.domain.subscriptions;
import org.alfresco.service.cmr.repository.NodeRef;
public class SubscriptionNodeEntity
{
private String protocol;
private String identifier;
private String id;
public String getProtocol()
{
return protocol;
}
public void setProtocol(String protocol)
{
this.protocol = protocol;
}
public String getIdentifier()
{
return identifier;
}
public void setIdentifier(String identifier)
{
this.identifier = identifier;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public NodeRef getNodeRef()
{
return new NodeRef(protocol, identifier, id);
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2005-2011 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.domain.subscriptions;
import org.alfresco.query.PagingRequest;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.subscriptions.PagingFollowingResults;
import org.alfresco.service.cmr.subscriptions.PagingSubscriptionResults;
import org.alfresco.service.cmr.subscriptions.SubscriptionItemTypeEnum;
public interface SubscriptionsDAO
{
PagingSubscriptionResults selectSubscriptions(String userId, SubscriptionItemTypeEnum type,
PagingRequest pagingRequest);
int countSubscriptions(String userId, SubscriptionItemTypeEnum type);
void insertSubscription(String userId, NodeRef node);
void deleteSubscription(String userId, NodeRef node);
boolean hasSubscribed(String userId, NodeRef node);
PagingFollowingResults selectFollowing(String userId, PagingRequest pagingRequest);
PagingFollowingResults selectFollowers(String userId, PagingRequest pagingRequest);
int countFollowers(String userId);
}

View File

@@ -0,0 +1,293 @@
/*
* Copyright (C) 2005-2011 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.domain.subscriptions.ibatis;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.query.PagingRequest;
import org.alfresco.repo.domain.qname.QNameDAO;
import org.alfresco.repo.domain.subscriptions.AbstractSubscriptionsDAO;
import org.alfresco.repo.domain.subscriptions.SubscriptionEntity;
import org.alfresco.repo.domain.subscriptions.SubscriptionNodeEntity;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.subscriptions.PagingFollowingResults;
import org.alfresco.service.cmr.subscriptions.PagingFollowingResultsImpl;
import org.alfresco.service.cmr.subscriptions.PagingSubscriptionResults;
import org.alfresco.service.cmr.subscriptions.PagingSubscriptionResultsImpl;
import org.alfresco.service.cmr.subscriptions.SubscriptionItemTypeEnum;
import org.alfresco.util.Pair;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
public class SubscriptionsDAOImpl extends AbstractSubscriptionsDAO
{
private SqlSessionTemplate template;
private QNameDAO qnameDAO;
public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate)
{
this.template = sqlSessionTemplate;
}
public final void setQNameDAO(QNameDAO qnameDAO)
{
this.qnameDAO = qnameDAO;
}
@Override
public PagingSubscriptionResults selectSubscriptions(String userId, SubscriptionItemTypeEnum type,
PagingRequest pagingRequest)
{
if (userId == null)
{
throw new IllegalArgumentException("User Id may not be null!");
}
if (type == null)
{
throw new IllegalArgumentException("Type may not be null!");
}
NodeRef userNodeRef = getUserNodeRef(userId);
Pair<Long, NodeRef> userPair = nodeDAO.getNodePair(userNodeRef);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userNodeId", userPair.getFirst());
map.put("false", Boolean.FALSE);
@SuppressWarnings("unchecked")
List<SubscriptionNodeEntity> nodeList = (List<SubscriptionNodeEntity>) template.selectList(
"alfresco.subscriptions.select_Subscriptions", map, new RowBounds(pagingRequest.getSkipCount(),
pagingRequest.getMaxItems() + 1));
boolean hasMore = nodeList.size() > pagingRequest.getMaxItems();
List<NodeRef> result = new ArrayList<NodeRef>(nodeList.size());
for (SubscriptionNodeEntity sne : nodeList)
{
result.add(sne.getNodeRef());
if (result.size() == pagingRequest.getMaxItems())
{
break;
}
}
Integer totalCount = null;
if (pagingRequest.getRequestTotalCountMax() > 0)
{
totalCount = countSubscriptions(userId, type);
}
return new PagingSubscriptionResultsImpl(result, hasMore, totalCount);
}
@Override
public int countSubscriptions(String userId, SubscriptionItemTypeEnum type)
{
if (userId == null)
{
throw new IllegalArgumentException("User Id may not be null!");
}
if (type == null)
{
throw new IllegalArgumentException("Type may not be null!");
}
NodeRef userNodeRef = getUserNodeRef(userId);
Pair<Long, NodeRef> userPair = nodeDAO.getNodePair(userNodeRef);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userNodeId", userPair.getFirst());
map.put("false", Boolean.FALSE);
Number count = (Number) template.selectOne("alfresco.subscriptions.select_countSubscriptions", map);
return count.intValue();
}
@Override
public void insertSubscription(String userId, NodeRef node)
{
if (userId == null)
{
throw new IllegalArgumentException("User Id may not be null!");
}
if (node == null)
{
throw new IllegalArgumentException("Node may not be null!");
}
NodeRef userNodeRef = getUserNodeRef(userId);
Pair<Long, NodeRef> userPair = nodeDAO.getNodePair(userNodeRef);
Pair<Long, NodeRef> nodePair = nodeDAO.getNodePair(node);
SubscriptionEntity se = new SubscriptionEntity();
se.setUserNodeId(userPair.getFirst());
se.setNodeId(nodePair.getFirst());
if (((Number) template.selectOne("alfresco.subscriptions.select_hasSubscribed", se)).intValue() == 0)
{
template.insert("alfresco.subscriptions.insert_Subscription", se);
}
}
@Override
public void deleteSubscription(String userId, NodeRef node)
{
if (userId == null)
{
throw new IllegalArgumentException("User Id may not be null!");
}
if (node == null)
{
throw new IllegalArgumentException("Node may not be null!");
}
NodeRef userNodeRef = getUserNodeRef(userId);
Pair<Long, NodeRef> userPair = nodeDAO.getNodePair(userNodeRef);
Pair<Long, NodeRef> nodePair = nodeDAO.getNodePair(node);
SubscriptionEntity se = new SubscriptionEntity();
se.setUserNodeId(userPair.getFirst());
se.setNodeId(nodePair.getFirst());
template.delete("alfresco.subscriptions.delete_Subscription", se);
}
@Override
public boolean hasSubscribed(String userId, NodeRef node)
{
if (userId == null)
{
throw new IllegalArgumentException("User Id may not be null!");
}
if (node == null)
{
throw new IllegalArgumentException("Node may not be null!");
}
NodeRef userNodeRef = getUserNodeRef(userId);
Pair<Long, NodeRef> userPair = nodeDAO.getNodePair(userNodeRef);
Pair<Long, NodeRef> nodePair = nodeDAO.getNodePair(node);
SubscriptionEntity se = new SubscriptionEntity();
se.setUserNodeId(userPair.getFirst());
se.setNodeId(nodePair.getFirst());
return ((Number) template.selectOne("alfresco.subscriptions.select_hasSubscribed", se)).intValue() == 1;
}
@Override
public PagingFollowingResults selectFollowing(String userId, PagingRequest pagingRequest)
{
if (userId == null)
{
throw new IllegalArgumentException("User Id may not be null!");
}
NodeRef userNodeRef = getUserNodeRef(userId);
Pair<Long, NodeRef> userPair = nodeDAO.getNodePair(userNodeRef);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userIdQname", qnameDAO.getQName(ContentModel.PROP_USERNAME).getFirst());
map.put("userNodeId", userPair.getFirst());
map.put("false", Boolean.FALSE);
@SuppressWarnings("unchecked")
List<String> userList = (List<String>) template.selectList("alfresco.subscriptions.select_Following", map,
new RowBounds(pagingRequest.getSkipCount(), pagingRequest.getMaxItems() + 1));
boolean hasMore = userList.size() > pagingRequest.getMaxItems();
if (hasMore && userList.size() > 0)
{
userList.remove(userList.size() - 1);
}
Integer totalCount = null;
if (pagingRequest.getRequestTotalCountMax() > 0)
{
totalCount = countFollowers(userId);
}
return new PagingFollowingResultsImpl(userList, hasMore, totalCount);
}
@Override
public PagingFollowingResults selectFollowers(String userId, PagingRequest pagingRequest)
{
if (userId == null)
{
throw new IllegalArgumentException("User Id may not be null!");
}
NodeRef userNodeRef = getUserNodeRef(userId);
Pair<Long, NodeRef> userPair = nodeDAO.getNodePair(userNodeRef);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userIdQname", qnameDAO.getQName(ContentModel.PROP_USERNAME).getFirst());
map.put("userNodeId", userPair.getFirst());
map.put("false", Boolean.FALSE);
@SuppressWarnings("unchecked")
List<String> userList = (List<String>) template.selectList("alfresco.subscriptions.select_Followers", map,
new RowBounds(pagingRequest.getSkipCount(), pagingRequest.getMaxItems() + 1));
boolean hasMore = userList.size() > pagingRequest.getMaxItems();
if (hasMore && userList.size() > 0)
{
userList.remove(userList.size() - 1);
}
Integer totalCount = null;
if (pagingRequest.getRequestTotalCountMax() > 0)
{
totalCount = countFollowers(userId);
}
return new PagingFollowingResultsImpl(userList, hasMore, totalCount);
}
@Override
public int countFollowers(String userId)
{
if (userId == null)
{
throw new IllegalArgumentException("User Id may not be null!");
}
NodeRef userNodeRef = getUserNodeRef(userId);
Pair<Long, NodeRef> userPair = nodeDAO.getNodePair(userNodeRef);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userNodeId", userPair.getFirst());
map.put("false", Boolean.FALSE);
Number count = (Number) template.selectOne("alfresco.subscriptions.select_countFollowers", map);
return count.intValue();
}
}