* @throws RepositoryException in case of a JCR error
* @throws InvalidQueryException if the query syntax is invalid
*/
public JCRNodeWrapper getSubscription(JCRNodeWrapper target, String user, JCRSessionWrapper session)
throws InvalidQueryException, RepositoryException {
QueryManager queryManager = session.getWorkspace().getQueryManager();
if (queryManager == null) {
logger.error("Unable to obtain QueryManager instance");
return null;
}
String subscriber = user;
String provider = null;
if (user.charAt(0) == '{') {
// we deal with a registered user
subscriber = StringUtils.substringAfter(user, "}");
provider = StringUtils.substringBetween(user, "{", "}");
}
StringBuilder q = new StringBuilder(64);
q.append("select * from [" + JNT_SUBSCRIPTION + "] where [" + J_SUBSCRIBER + "]='").append(subscriber)
.append("'");
if (provider != null) {
q.append(" and [" + J_PROVIDER + "]='").append(provider).append("'");
}
q.append(" and").append(" isdescendantnode([").append(target.getPath()).append("])");
Query query = queryManager.createQuery(q.toString(), Query.JCR_SQL2);
query.setLimit(1);
final NodeIterator nodeIterator = query.execute().getNodes();
if (nodeIterator.hasNext()) {
return (JCRNodeWrapper) nodeIterator.nextNode();
}