// Check if a subscription does NOT exist in the DB
boolean isNewSubscription(final String datastore, final String cmHandleId, final String xpath) {
final String cpsPathQuery =
"/datastores/datastore[@name='" + escapeQuotesByDoublingThem(datastore) + "']" +
"/cm-handles/cm-handle[@id='" + escapeQuotesByDoublingThem(cmHandleId) + "']" +
"/filters/filter[@xpath='" + escapeQuotesByDoublingThem(xpath) + "']";
final Collection<DataNode> existingNodes = queryDataNodes("NCMP-Admin", "cm-data-subscriptions", cpsPathQuery, OMIT_DESCENDANTS);
return existingNodes.isEmpty() || existingNodes.iterator().next().getLeaves().get("subscribers").isEmpty();
}
// This is needed in case subscribed xpath has single-quotes ' in it - that would break path query
private static String escapeQuotesByDoublingThem(final String value) {
return value.replace("'", "''");
}
Can be called like below :
final String datastoreName = cmSubscriptionPredicate.getScopeFilter().getDatastoreType().getDatastoreName():
for (final String cmHandleId : cmSubscriptionPredicate.getTargetFilter()) {
for (final String xpath : cmSubscriptionPredicate.getScopeFilter().getXpathFilters()) {
if (isNewSubscription(datastoreName, cmHandleId, xpath)) {
// will need to send this to dmi
}
}
} |