private SampleIdentity findOrCreateIdentity(DetailedSample descendant, SampleIdentity identity) throws IOException, MisoNamingException { if (identity.getId() != SampleImpl.UNSAVED_ID) { return (SampleIdentity) sampleStore.getSample(identity.getId()); } else { // If samples are being bulk received for the same new donor, they will all have a null parentId. // After the new donor's Identity is created, the following samples need to be parented to that now-existing Identity. Collection<SampleIdentity> newlyCreated = getIdentitiesByExternalNameOrAliasAndProject(identity.getExternalName(), descendant.getProject().getId(), true); if (newlyCreated.size() > 1) { throw new IllegalArgumentException( "IdentityId is required since there are multiple identities with external name " + identity.getExternalName() + " in project " + descendant.getProject().getId()); } else if (newlyCreated.size() == 1) { Sample parent = newlyCreated.iterator().next(); if (parent == null) throw new IllegalArgumentException("Parent sample does not exist"); else return (SampleIdentity) parent; } else { try { return createParentIdentity(descendant, identity); } catch (SQLException e) { throw new IOException(e); } } } }
/** * Adds any external names in the sample's attached identity to the identity stored in the database * * @param sample * @throws IOException */ private void addExternalNames(DetailedSample sample, SampleIdentity identityCopy) throws IOException { if (identityCopy == null || identityCopy.getExternalName() == null) return; SampleIdentity identity = (SampleIdentity) get(getIdentity(sample).getId()); Set<String> identityExternalNames = SampleIdentityImpl.getSetFromString(identity.getExternalName()); Set<String> tempExternalNames = SampleIdentityImpl.getSetFromString(identityCopy.getExternalName()); Set<String> lowerCaseIdentityExternalNames = identityExternalNames.stream().map(String::toLowerCase).collect(Collectors.toSet()); for (String name : tempExternalNames) { if (!lowerCaseIdentityExternalNames.contains(name.toLowerCase()) && !(isUniqueExternalNameWithinProjectRequired() && (identity.getProject() == null || getIdentitiesByExternalNameOrAliasAndProject(name, identity.getProject().getId(), true).size() > 0))) { identityExternalNames.add(name); } } if (identityExternalNames.size() > lowerCaseIdentityExternalNames.size()) { identity.setExternalName(String.join(",", identityExternalNames)); identity.setChangeDetails(authorizationManager.getCurrentUser()); sampleStore.update(identity); } }
if (!matches.isEmpty()) { for (SampleIdentity match : matches) { if (match.getId() != sample.getId()) { Set<String> matchExtNames = SampleIdentityImpl.getSetFromString(match.getExternalName()); Set<String> newExtNames = SampleIdentityImpl.getSetFromString(newExternalName);