* @param session
*/
public JCRNodeWrapper subscribe(final String subscribableIdentifier,
final Map<String, Map<String, Object>> subscribers, JCRSessionWrapper session) {
JCRNodeWrapper newSubscriptionNode = null;
try {
JCRNodeWrapper target = session.getNodeByIdentifier(subscribableIdentifier);
if (!target.isNodeType(JMIX_SUBSCRIBABLE)) {
logger.warn("The target node {} does not have the " + JMIX_SUBSCRIBABLE + " mixin." +
" No subscriptions can be created.", target.getPath());
return null;
}
JCRNodeWrapper subscriptionsNode = target.getNode(J_SUBSCRIPTIONS);
String targetPath = subscriptionsNode.getPath();
if (target.isLocked() || subscriptionsNode.isLocked()) {
logger.info("The target {} is locked and no subscriptions can be created. Skipping {} subscribers.",
targetPath, subscribers.size());
}
boolean allowUnregisteredUsers = target.hasProperty(J_ALLOW_UNREGISTERED_USERS) ?
target.getProperty(J_ALLOW_UNREGISTERED_USERS).getBoolean() : true;
for (Map.Entry<String, Map<String, Object>> subscriber : subscribers.entrySet()) {
String username = subscriber.getKey();
String provider = null;
if (username.charAt(0) == '{') {
// we deal with a registered user
username = StringUtils.substringAfter(subscriber.getKey(), "}");
provider = StringUtils.substringBetween(subscriber.getKey(), "{", "}");
} else if (!allowUnregisteredUsers) {
logger.info(
"The target {} does not allow unregistered users to be subscribed. Skipping subscription for {}.",
targetPath, subscriber.getKey());
continue;
}
if (getSubscription(target, subscriber.getKey(), session) == null) {
if (logger.isDebugEnabled()) {
logger.debug("Creating subscription to the {} for {}.", targetPath, subscriber.getKey());
}
session.checkout(subscriptionsNode);
newSubscriptionNode = subscriptionsNode
.addNode(JCRContentUtils.findAvailableNodeName(subscriptionsNode, "subscription"),
JNT_SUBSCRIPTION);
newSubscriptionNode.setProperty(J_SUBSCRIBER, username);
if (provider != null) {
newSubscriptionNode.setProperty(J_PROVIDER, provider);