fix bug: change sql-query in localArtifactRepository (#820)

Also-by: Nazife Basbaz <nazife.basbaz@bosch-si.com>
Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>
This commit is contained in:
Nazife Basbaz
2019-06-05 16:07:07 +02:00
committed by Jeroen Laverman
parent b8ca7d24c8
commit fde0cbdf16
31 changed files with 237 additions and 136 deletions

View File

@@ -40,6 +40,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
/**
@@ -98,20 +99,35 @@ public class JpaArtifactManagement implements ArtifactManagement {
public Artifact create(final ArtifactUpload artifactUpload) {
final String filename = artifactUpload.getFilename();
final long moduleId = artifactUpload.getModuleId();
AbstractDbArtifact result = null;
final SoftwareModule softwareModule = getModuleAndThrowExceptionIfThatFails(moduleId);
final Artifact existing = checkForExistingArtifact(filename, artifactUpload.overrideExisting(),
softwareModule);
final Artifact existing = checkForExistingArtifact(filename, artifactUpload.overrideExisting(), softwareModule);
assertArtifactQuota(moduleId, 1);
assertMaxArtifactSizeQuota(filename, moduleId, artifactUpload.getFilesize());
assertMaxArtifactStorageQuota(filename, artifactUpload.getFilesize());
return getOrCreateArtifact(artifactUpload)
.map(artifact -> storeArtifactMetadata(softwareModule, filename, artifact, existing))
.orElse(null);
}
private Optional<AbstractDbArtifact> getOrCreateArtifact(final ArtifactUpload artifactUpload) {
final String providedSha1Sum = artifactUpload.getProvidedSha1Sum();
AbstractDbArtifact artifact = null;
if (!StringUtils.isEmpty(providedSha1Sum)) {
artifact = artifactRepository.getArtifactBySha1(tenantAware.getCurrentTenant(), providedSha1Sum);
}
artifact = (artifact == null) ? storeArtifact(artifactUpload) : artifact;
return Optional.ofNullable(artifact);
}
private AbstractDbArtifact storeArtifact(final ArtifactUpload artifactUpload) {
try {
result = artifactRepository.store(tenantAware.getCurrentTenant(), artifactUpload.getInputStream(), filename,
artifactUpload.getContentType(),
return artifactRepository.store(tenantAware.getCurrentTenant(), artifactUpload.getInputStream(),
artifactUpload.getFilename(), artifactUpload.getContentType(),
new DbArtifactHash(artifactUpload.getProvidedSha1Sum(), artifactUpload.getProvidedMd5Sum()));
} catch (final ArtifactStoreException e) {
throw new ArtifactUploadFailedException(e);
@@ -122,11 +138,6 @@ public class JpaArtifactManagement implements ArtifactManagement {
throw new InvalidMD5HashException(e.getMessage(), e);
}
}
if (result == null) {
return null;
}
return storeArtifactMetadata(softwareModule, filename, result, existing);
}
private void assertArtifactQuota(final long id, final int requested) {
@@ -166,12 +177,12 @@ public class JpaArtifactManagement implements ArtifactManagement {
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public boolean clearArtifactBinary(final String sha1Hash, final long moduleId) {
if (localArtifactRepository.existsWithSha1HashAndSoftwareModuleIdIsNot(sha1Hash, moduleId)) {
final long count = localArtifactRepository.countBySha1HashAndTenantAndSoftwareModuleDeletedIsFalse(sha1Hash,
tenantAware.getCurrentTenant());
if (count > 1) {
// there are still other artifacts that need the binary
return false;
}
try {
LOG.debug("deleting artifact from repository {}", sha1Hash);
artifactRepository.deleteBySha1(tenantAware.getCurrentTenant(), sha1Hash);

View File

@@ -56,19 +56,18 @@ public interface LocalArtifactRepository extends BaseEntityRepository<JpaArtifac
List<Artifact> findBySha1Hash(String sha1Hash);
/**
* Verifies if an artifact exists that has given hash and is still related
* to a {@link SoftwareModule} other than a given one and not
* {@link SoftwareModule#isDeleted()}.
*
* Counts current elements based on the sha1 and tenant, as well as having
* the {@link SoftwareModule} property 'deleted' with value 'false
*
* @param sha1
* to search for
* @param moduleId
* to ignore in relationship check
* the sha1 of the {@link Artifact}
* @param tenant
* the current tenant
*
* @return <code>true</code> if such an artifact exists
* @return the count of the elements
*/
@Query("SELECT CASE WHEN COUNT(a)>0 THEN 'true' ELSE 'false' END FROM JpaArtifact a WHERE a.sha1Hash = :sha1 AND a.softwareModule.id != :moduleId AND a.softwareModule.deleted = 0")
boolean existsWithSha1HashAndSoftwareModuleIdIsNot(@Param("sha1") String sha1, @Param("moduleId") Long moduleId);
long countBySha1HashAndTenantAndSoftwareModuleDeletedIsFalse(@Param("sha1") String sha1,
@Param("tenant") String tenant);
/**
* Searches for a {@link Artifact} based on given gridFsFileName.

View File

@@ -469,7 +469,7 @@ public final class RSQLUtility {
// values JPA can do it by it's own but not for classes like enums.
// So we need to transform the given value string into the enum
// class.
final Class<? extends Object> javaType = fieldPath.getJavaType();
final Class<?> javaType = fieldPath.getJavaType();
if (javaType != null && javaType.isEnum()) {
return transformEnumValue(node, value, javaType);
}
@@ -485,7 +485,7 @@ public final class RSQLUtility {
}
private Object convertBooleanValue(final ComparisonNode node, final String value,
final Class<? extends Object> javaType) {
final Class<?> javaType) {
try {
return simpleTypeConverter.convertIfNecessary(value, javaType);
} catch (final TypeMismatchException e) {
@@ -513,7 +513,7 @@ public final class RSQLUtility {
// https://jira.sonarsource.com/browse/SONARJAVA-1478
@SuppressWarnings({ "rawtypes", "unchecked", "squid:S2095" })
private static Object transformEnumValue(final ComparisonNode node, final String value,
final Class<? extends Object> javaType) {
final Class<?> javaType) {
final Class<? extends Enum> tmpEnumType = (Class<? extends Enum>) javaType;
try {
return Enum.valueOf(tmpEnumType, value.toUpperCase());
@@ -680,13 +680,10 @@ public final class RSQLUtility {
final char escapeChar) {
final String escaped;
switch (database) {
case SQL_SERVER:
if (database == Database.SQL_SERVER) {
escaped = transformedValue.replace("%", "[%]").replace("_", "[_]");
break;
default:
} else {
escaped = transformedValue.replace("%", escapeChar + "%").replace("_", escapeChar + "_");
break;
}
return escaped.replace(LIKE_WILDCARD, '%');