diff --git a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentDatabaseHandler.java b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentDatabaseHandler.java index 0a404e5150..dc7f0d5737 100644 --- a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentDatabaseHandler.java +++ b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentDatabaseHandler.java @@ -455,10 +455,10 @@ private void setMainLicenses(Component component) { * Add new release to the database */ public AddDocumentRequestSummary addComponent(Component component, String user) throws SW360Exception { - if(isDuplicateUsingVcs(component.getVcs())){ + if(isDuplicateUsingVcs(component, true)){ final AddDocumentRequestSummary addDocumentRequestSummary = new AddDocumentRequestSummary() .setRequestStatus(AddDocumentRequestStatus.DUPLICATE); - Set duplicates = componentRepository.getComponentIdsByVCS(component.getVcs().toLowerCase()); + Set duplicates = componentRepository.getComponentIdsByVCS(component.getVcs(), true); if (duplicates.size() == 1) { duplicates.forEach(addDocumentRequestSummary::setId); } @@ -605,11 +605,15 @@ private boolean isDuplicate(String componentName, boolean caseInsenstive) { return duplicates.size()>0; } - private boolean isDuplicateUsingVcs(String vcsUrl){ + private boolean isDuplicateUsingVcs(Component component, boolean caseInsenstive){ + return isDuplicateUsingVcs(component.getVcs(), caseInsenstive); + } + + private boolean isDuplicateUsingVcs(String vcsUrl, boolean caseInsenstive){ if (isNullEmptyOrWhitespace(vcsUrl)) { return false; } - Set duplicates = componentRepository.getComponentIdsByVCS(vcsUrl.toLowerCase()); + Set duplicates = componentRepository.getComponentIdsByVCS(vcsUrl, caseInsenstive); return duplicates.size()>0; } diff --git a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentRepository.java b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentRepository.java index ab65af69ec..3f494454e1 100644 --- a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentRepository.java +++ b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/ComponentRepository.java @@ -153,6 +153,12 @@ public class ComponentRepository extends SummaryAwareRepository { " } " + "}"; + private static final String BY_VCS_LOWERCASE = "function(doc) {" + + " if (doc.type == 'component') {" + + " emit(doc.vcs.toLowerCase().trim(), doc._id);" + + " } " + + "}"; + public ComponentRepository(DatabaseConnectorCloudant db, ReleaseRepository releaseRepository, VendorRepository vendorRepository) { super(Component.class, db, new ComponentSummary(releaseRepository, vendorRepository)); Map views = new HashMap(); @@ -172,6 +178,7 @@ public ComponentRepository(DatabaseConnectorCloudant db, ReleaseRepository relea views.put("bymainlicense", createMapReduce(BY_MAIN_LICENSE, null)); views.put("byvendor", createMapReduce(BY_VENDOR, null)); views.put("byVCS", createMapReduce(BY_VCS, null)); + views.put("byVCSLowercase", createMapReduce(BY_VCS_LOWERCASE, null)); initStandardDesignDocument(views, db); } @@ -222,9 +229,11 @@ public Set getComponentIdsByName(String name, boolean caseInsenstive) { return queryForIdsAsValue("byname", name); } - public Set getComponentIdsByVCS(String vcs){ - Set componentIds = queryForIdsAsValue("byVCS", vcs); - return componentIds; + public Set getComponentIdsByVCS(String vcs, boolean caseInsenstive){ + if(caseInsenstive){ + return queryForIdsAsValue("byVCSLowercase", vcs.toLowerCase()); + } + return queryForIdsAsValue("byVCS", vcs); } public List searchComponentByName(String name, boolean caseSensitive) { diff --git a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/PackageDatabaseHandler.java b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/PackageDatabaseHandler.java index 56318d6214..687fa0e447 100644 --- a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/PackageDatabaseHandler.java +++ b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/PackageDatabaseHandler.java @@ -422,10 +422,10 @@ private List getPackageByNameAndVersion(String pkgName, String pkgVersi } private List getPackageByPurl(String purl) { - if (purl == null) { + if (isNullEmptyOrWhitespace(purl)) { return Collections.emptyList(); } - return packageRepository.searchByPurl(purl); + return packageRepository.searchByPurl(purl, true); } private void copyImmutableFields(Package destination, Package source) { diff --git a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/PackageRepository.java b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/PackageRepository.java index ec1d2220d0..ae016087fc 100644 --- a/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/PackageRepository.java +++ b/backend/src-common/src/main/java/org/eclipse/sw360/datahandler/db/PackageRepository.java @@ -49,6 +49,7 @@ public class PackageRepository extends DatabaseRepositoryCloudantClient private static final String BY_RELEASE_ID = "function(doc) { if (doc.type == 'package') { emit(doc.releaseId, doc._id); } }"; private static final String BY_LICENSE_IDS = "function(doc) { if (doc.type == 'package') { if (doc.licenseIds) { emit(doc.licenseIds.join(), doc._id); } else { emit('', doc._id); } } }"; private static final String BY_PURL = "function(doc) { if (doc.type == 'package') { emit(doc.purl.trim(), doc._id) } }"; + private static final String BY_PURL_LOWERCASE = "function(doc) { if (doc.type == 'package') { emit(doc.purl.toLowerCase().trim(), doc._id) } }"; public PackageRepository(DatabaseConnectorCloudant db) { super(db, Package.class); @@ -63,6 +64,7 @@ public PackageRepository(DatabaseConnectorCloudant db) { views.put("byReleaseId", createMapReduce(BY_RELEASE_ID, null)); views.put("byLicenseIds", createMapReduce(BY_LICENSE_IDS, null)); views.put("byPurl", createMapReduce(BY_PURL, null)); + views.put("byPurlLowercase", createMapReduce(BY_PURL_LOWERCASE, null)); initStandardDesignDocument(views, db); } @@ -107,9 +109,13 @@ public List searchByNameAndVersion(String name, String version, boolean return releasesMatchingNameAndVersion; } - public List searchByPurl(String purl) { - List packagesMatchingPurl - = new ArrayList(queryView("byPurl", purl)); + public List searchByPurl(String purl, boolean caseInsenstive) { + List packagesMatchingPurl; + if(caseInsenstive){ + packagesMatchingPurl = new ArrayList(queryView("byPurlLowercase", purl.toLowerCase())); + }else{ + packagesMatchingPurl = new ArrayList(queryView("byPurl", purl)); + } return packagesMatchingPurl; }