@RequiredArgsConstructor
public class AlternateIdChecker {
+ public enum Operation {
+ CREATE, UPDATE
+ }
+
private final InventoryPersistence inventoryPersistence;
private static final String NO_CURRENT_ALTERNATE_ID = "";
}
/**
- * Check all alternate ids of a batch of NEW cm handles.
+ * Check all alternate ids of a batch of cm handles.
* Includes cross-checks in the batch itself for duplicates. Only the first entry encountered wil be accepted.
- * This method can only be used for NEW cm handle registrations NOT for updating existing ones
*
* @param newNcmpServiceCmHandles the proposed new cm handles
+ * @param operation type of operation being executed
* @return collection of cm handles ids which are acceptable
*/
- public Collection<String> getIdsOfCmHandlesWithAcceptableAlternateId(
- final Collection<NcmpServiceCmHandle> newNcmpServiceCmHandles) {
+ public Collection<String> getIdsOfCmHandlesWithRejectedAlternateId(
+ final Collection<NcmpServiceCmHandle> newNcmpServiceCmHandles,
+ final Operation operation) {
final Set<String> acceptedAlternateIds = new HashSet<>(newNcmpServiceCmHandles.size());
- final Collection<String> acceptedCmHandleIds = new ArrayList<>(newNcmpServiceCmHandles.size());
+ final Collection<String> rejectedCmHandleIds = new ArrayList<>();
for (final NcmpServiceCmHandle ncmpServiceCmHandle : newNcmpServiceCmHandles) {
final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
final String proposedAlternateId = ncmpServiceCmHandle.getAlternateId();
log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already "
+ "assigned to a different cm handle (in this batch)", cmHandleId);
} else {
- isAcceptable = canApplyAlternateId(cmHandleId, NO_CURRENT_ALTERNATE_ID, proposedAlternateId);
+ if (Operation.CREATE.equals(operation)) {
+ isAcceptable = canApplyAlternateId(cmHandleId, NO_CURRENT_ALTERNATE_ID, proposedAlternateId);
+ } else {
+ isAcceptable = canApplyAlternateId(cmHandleId, proposedAlternateId);
+ }
}
}
if (isAcceptable) {
acceptedAlternateIds.add(proposedAlternateId);
- acceptedCmHandleIds.add(cmHandleId);
+ } else {
+ rejectedCmHandleIds.add(cmHandleId);
}
}
- return acceptedCmHandleIds;
+ return rejectedCmHandleIds;
}
private boolean alternateIdAlreadyInDb(final String alternateId) {