// if we can match to the entitlement id do it.
// we need a new list to hold the ones that are left
Set<Subscription> subscriptionsStillToImport = new HashSet<Subscription>();
for (Subscription subscription : subsToImport) {
Subscription local = null;
Map<String, Subscription> map = existingSubsByUpstreamPool.get(
subscription.getUpstreamPoolId());
if (map == null || map.isEmpty()) {
createSubscription(subscription);
log.info("Creating new subscription for incoming entitlement with id [" +
subscription.getUpstreamEntitlementId() +
"]");
continue;
}
local = map.get(subscription.getUpstreamEntitlementId());
if (local != null) {
mergeSubscription(subscription, local, map);
log.info("Merging subscription for incoming entitlement id [" +
subscription.getUpstreamEntitlementId() +
"] into subscription with existing entitlement id [" +
local.getUpstreamEntitlementId() +
"]. Entitlement id match.");
}
else {
subscriptionsStillToImport.add(subscription);
log.warn("Subscription for incoming entitlement id [" +
subscription.getUpstreamEntitlementId() +
"] does not have an entitlement id match " +
"in the current subscriptions for the upstream pool id [" +
subscription.getUpstreamPoolId() +
"]");
}
}
// matches will be made against the upstream pool id and quantity.
// we need a new list to hold the ones that are left
List<Subscription> subscriptionsNeedQuantityMatch = new ArrayList<Subscription>();
for (Subscription subscription : subscriptionsStillToImport) {
Subscription local = null;
Map<String, Subscription> map = existingSubsByUpstreamPool.get(
subscription.getUpstreamPoolId());
if (map == null) {
map = new HashMap<String, Subscription>();
}
for (Subscription localSub : map.values()) {
if (localSub.getQuantity().equals(subscription.getQuantity())) {
local = localSub;
break;
}
}
if (local != null) {
mergeSubscription(subscription, local, map);
log.info("Merging subscription for incoming entitlement id [" +
subscription.getUpstreamEntitlementId() +
"] into subscription with existing entitlement id [" +
local.getUpstreamEntitlementId() +
"]. Exact quantity match.");
}
else {
subscriptionsNeedQuantityMatch.add(subscription);
log.warn("Subscription for incoming entitlement id [" +
subscription.getUpstreamEntitlementId() +
"] does not have an exact quantity match " +
"in the current subscriptions for the upstream pool id [" +
subscription.getUpstreamPoolId() +
"]");
}
}
// matches will be made against the upstream pool id and quantity.
// quantities will just match by position from highest to lowest
// we need a new list to hold the ones that are left
Subscription[] inNeed = subscriptionsNeedQuantityMatch.toArray(
new Subscription[0]);
Arrays.sort(inNeed, new QuantityComparator());
for (Subscription subscription : inNeed) {
Subscription local = null;
Map<String, Subscription> map = existingSubsByUpstreamPool.get(
subscription.getUpstreamPoolId());
if (map == null || map.isEmpty()) {
createSubscription(subscription);
log.info("Creating new subscription for incoming entitlement with id [" +
subscription.getUpstreamEntitlementId() +
"]");
continue;
}
Subscription[] locals = map.values().toArray(new Subscription[0]);
Arrays.sort(locals, new QuantityComparator());
local = locals[0];
mergeSubscription(subscription, local, map);
log.info("Merging subscription for incoming entitlement id [" +
subscription.getUpstreamEntitlementId() +
"] into subscription with existing entitlement id [" +
local.getUpstreamEntitlementId() +
"]. Ordered quantity match.");
}
deleteRemainingLocalSubscriptions(existingSubsByUpstreamPool);
}