Commit 9425a0ed authored by Matija Obreza's avatar Matija Obreza
Browse files

RepositoryService method signatures updated

- Removed uuid as parameter
parent ee79457c
......@@ -7,3 +7,4 @@ target
doc
data
effective.pom
.flattened-pom.xml
......@@ -31,6 +31,10 @@ import org.springframework.stereotype.Repository;
/**
* The Interface RepositoryFilePersistence.
*/
/**
* @author Matija Obreza
*
*/
@Repository
public interface RepositoryFilePersistence extends RepositoryPersistence<RepositoryFile> {
......@@ -42,6 +46,16 @@ public interface RepositoryFilePersistence extends RepositoryPersistence<Reposit
*/
RepositoryFile findByUuid(UUID uuid);
/**
* Find by uuid and version.
*
* @param uuid the file uuid
* @param version the version
* @return the repository file
*/
RepositoryFile findByUuidAndVersion(UUID uuid, int version);
/**
* Find by path.
*
......
......@@ -20,6 +20,7 @@ import java.util.List;
import java.util.UUID;
import org.genesys.filerepository.RepositoryPersistence;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.model.RepositoryImage;
import org.springframework.stereotype.Repository;
......@@ -38,6 +39,15 @@ public interface RepositoryImagePersistence extends RepositoryPersistence<Reposi
*/
RepositoryImage findByUuid(UUID uuid);
/**
* Find by uuid and version
*
* @param fileUuid
* @param version
* @return
*/
RepositoryImage findByUuidAndVersion(UUID fileUuid, int version);
/**
* Find by path.
*
......@@ -45,4 +55,5 @@ public interface RepositoryImagePersistence extends RepositoryPersistence<Reposi
* @return the list
*/
List<RepositoryImage> findByPath(String repositoryPath);
}
......@@ -53,8 +53,8 @@ public interface RepositoryService {
* @throws IOException when things go wrong on bytes storage level
*/
@PreAuthorize("isAuthenticated()")
RepositoryFile addFile(String repositoryPath, String originalFilename, String contentType, byte[] bytes, RepositoryFile metaData)
throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException;
RepositoryFile addFile(String repositoryPath, String originalFilename, String contentType, byte[] bytes, RepositoryFile metaData) throws InvalidRepositoryPathException,
InvalidRepositoryFileDataException, IOException;
/**
* Add a new image to the file repository.
......@@ -71,8 +71,8 @@ public interface RepositoryService {
* @throws IOException when things go wrong on bytes storage level
*/
@PreAuthorize("isAuthenticated()")
RepositoryImage addImage(String repositoryPath, String originalFilename, String contentType, byte[] bytes, RepositoryImage metaData)
throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException;
RepositoryImage addImage(String repositoryPath, String originalFilename, String contentType, byte[] bytes, RepositoryImage metaData) throws InvalidRepositoryPathException,
InvalidRepositoryFileDataException, IOException;
/**
* Get repository file by its UUID.
......@@ -85,6 +85,18 @@ public interface RepositoryService {
@PostAuthorize("hasRole('ADMINISTRATOR') or hasPermission(returnObject, 'read')")
RepositoryFile getFile(UUID fileUuid) throws NoSuchRepositoryFileException;
/**
* Get repository file by its UUID and version
*
* @param fileUuid the file uuid
* @param version the record version
* @return the file
* @throws NoSuchRepositoryFileException when file is not available in the
* repository
*/
@PostAuthorize("hasRole('ADMINISTRATOR') or hasPermission(returnObject, 'read')")
<T extends RepositoryFile> T getFile(UUID fileUuid, int version) throws NoSuchRepositoryFileException;
/**
* Get repository file by its path and filename.
*
......@@ -143,7 +155,7 @@ public interface RepositoryService {
* repository
*/
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#fileData, 'write')")
<T extends RepositoryFile> T updateMetadata(UUID uuid, T fileData) throws NoSuchRepositoryFileException;
<T extends RepositoryFile> T updateMetadata(T fileData) throws NoSuchRepositoryFileException;
/**
* Update file bytes.
......@@ -156,7 +168,7 @@ public interface RepositoryService {
* @throws IOException Signals that an I/O exception has occurred.
*/
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#fileData, 'write')")
RepositoryFile updateBytes(RepositoryFile fileData, String contentType, byte[] bytes) throws NoSuchRepositoryFileException, IOException;
<T extends RepositoryFile> T updateBytes(T fileData, String contentType, byte[] bytes) throws NoSuchRepositoryFileException, IOException;
/**
* Load image to validate data and contentType, update image bytes and generate
......@@ -248,7 +260,7 @@ public interface RepositoryService {
* repository
*/
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#imageData, 'write')")
RepositoryImage updateImageMetadata(UUID uuid, RepositoryImage imageData) throws NoSuchRepositoryFileException;
RepositoryImage updateImageMetadata(RepositoryImage imageData) throws NoSuchRepositoryFileException;
/**
* Remove repository image.
......
......@@ -109,7 +109,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
* org.genesys.filerepository.model.RepositoryFile)
*/
@Override
@Transactional(rollbackFor=Throwable.class)
@Transactional(rollbackFor = Throwable.class)
public RepositoryFile addFile(final String repositoryPath, final String originalFilename, String contentType, final byte[] bytes, final RepositoryFile metaData)
throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {
......@@ -241,6 +241,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
}
file = repositoryImagePersistence.findByUuid(fileUuid);
if (file == null) {
throw new NoSuchRepositoryFileException();
}
......@@ -248,6 +249,23 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
return file;
}
@SuppressWarnings("unchecked")
@Override
public <T extends RepositoryFile> T getFile(UUID fileUuid, int version) throws NoSuchRepositoryFileException {
RepositoryFile file = repositoryFilePersistence.findByUuidAndVersion(fileUuid, version);
if (file != null) {
return (T) file;
}
file = repositoryImagePersistence.findByUuidAndVersion(fileUuid, version);
if (file == null) {
throw new NoSuchRepositoryFileException();
}
return (T) file;
}
/*
* (non-Javadoc)
* @see org.genesys.filerepository.service.RepositoryService#getFile(java.lang.
......@@ -303,7 +321,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
return repositoryFiles;
}
@Override
public Stream<RepositoryFile> streamFiles(String repositoryPath) {
return repositoryFilePersistence.streamByBasePath(repositoryPath);
......@@ -317,13 +335,13 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
@SuppressWarnings("unchecked")
@Override
@Transactional
public <T extends RepositoryFile> T updateMetadata(final UUID uuid, final T fileData) throws NoSuchRepositoryFileException {
RepositoryFile repositoryFile = repositoryFilePersistence.findByUuid(uuid);
public <T extends RepositoryFile> T updateMetadata(final T fileData) throws NoSuchRepositoryFileException {
RepositoryFile repositoryFile = repositoryFilePersistence.findByUuidAndVersion(fileData.getUuid(), fileData.getVersion());
if (repositoryFile == null) {
throw new NoSuchRepositoryFileException();
}
if (fileData instanceof RepositoryImage) {
return (T) updateImageMetadata(uuid, (RepositoryImage) fileData);
return (T) updateImageMetadata((RepositoryImage) fileData);
}
repositoryFile.apply(fileData);
......@@ -339,8 +357,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
*/
@Override
@Transactional
public RepositoryImage updateImageMetadata(final UUID uuid, final RepositoryImage imageData) throws NoSuchRepositoryFileException {
final RepositoryImage repositoryImage = repositoryImagePersistence.findByUuid(uuid);
public RepositoryImage updateImageMetadata(final RepositoryImage imageData) throws NoSuchRepositoryFileException {
final RepositoryImage repositoryImage = repositoryImagePersistence.findByUuidAndVersion(imageData.getUuid(), imageData.getVersion());
if (repositoryImage == null) {
throw new NoSuchRepositoryFileException();
}
......@@ -356,29 +375,32 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
* @see org.genesys.filerepository.service.RepositoryService#updateBytes(
* org.genesys.filerepository.model .RepositoryFile, java.lang.String, byte[])
*/
@SuppressWarnings("unchecked")
@Override
@Transactional
public RepositoryFile updateBytes(final RepositoryFile repositoryFile, String contentType, final byte[] bytes) throws NoSuchRepositoryFileException, IOException {
if (repositoryFile == null) {
public <T extends RepositoryFile> T updateBytes(final T repositoryFile, String contentType, final byte[] bytes) throws NoSuchRepositoryFileException, IOException {
T storedFile = getFile(repositoryFile.getUuid(), repositoryFile.getVersion());
if (storedFile == null) {
throw new NoSuchRepositoryFileException();
}
if (repositoryFile instanceof RepositoryImage) {
return updateImageBytes((RepositoryImage) repositoryFile, contentType, bytes);
if (storedFile instanceof RepositoryImage) {
return (T) updateImageBytes((RepositoryImage) repositoryFile, contentType, bytes);
}
contentType = updateContentTypeIfNecessary(contentType, bytes);
// Calculate SHA-1 and MD5 sums
LOG.debug("updateByes length={}", bytes.length);
repositoryFile.setSha1Sum(DigestUtils.sha1Hex(bytes));
repositoryFile.setMd5Sum(DigestUtils.md5Hex(bytes));
repositoryFile.setSize(bytes.length);
storedFile.setSha1Sum(DigestUtils.sha1Hex(bytes));
storedFile.setMd5Sum(DigestUtils.md5Hex(bytes));
storedFile.setSize(bytes.length);
LOG.debug("updateByes length={} repoFile.size={}", bytes.length, repositoryFile.getSize());
repositoryFile.setContentType(contentType);
bytesStorageService.upsert(repositoryFile.getStoragePath(), repositoryFile.getFilename(), bytes);
storedFile.setContentType(contentType);
bytesStorageService.upsert(storedFile.getStoragePath(), storedFile.getFilename(), bytes);
return repositoryFilePersistence.save(repositoryFile);
return repositoryFilePersistence.save(storedFile);
}
/*
......@@ -389,22 +411,24 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
@Override
@Transactional
public RepositoryImage updateImageBytes(final RepositoryImage repositoryImage, String contentType, final byte[] bytes) throws NoSuchRepositoryFileException, IOException {
if (repositoryImage == null) {
RepositoryImage storedFile = getFile(repositoryImage.getUuid(), repositoryImage.getVersion());
if (storedFile == null) {
throw new NoSuchRepositoryFileException();
}
contentType = updateContentTypeIfNecessary(contentType, bytes);
// Calculate SHA-1 and MD5 sums
repositoryImage.setSha1Sum(DigestUtils.sha1Hex(bytes));
repositoryImage.setMd5Sum(DigestUtils.md5Hex(bytes));
repositoryImage.setSize(bytes.length);
storedFile.setSha1Sum(DigestUtils.sha1Hex(bytes));
storedFile.setMd5Sum(DigestUtils.md5Hex(bytes));
storedFile.setSize(bytes.length);
repositoryImage.setContentType(contentType);
storedFile.setContentType(contentType);
fillImageProperties(repositoryImage, bytes);
fillImageProperties(storedFile, bytes);
bytesStorageService.upsert(repositoryImage.getStoragePath(), repositoryImage.getFilename(), bytes);
bytesStorageService.upsert(storedFile.getStoragePath(), storedFile.getFilename(), bytes);
return repositoryImagePersistence.save(repositoryImage);
return repositoryImagePersistence.save(storedFile);
}
/**
......
......@@ -161,7 +161,7 @@ public class FileRepositoryExtensionTest {
FileRepositoryTestUtil.checkFile(repoFile, initialPath, initialOriginalFilename, initialExtension, initialContentType);
repoFile.setOriginalFilename(originalFilename);
repoFile = fileRepoService.updateMetadata(repoFile.getUuid(), repoFile);
repoFile = fileRepoService.updateMetadata(repoFile);
return FileRepositoryTestUtil.checkFile(repoFile, initialPath, originalFilename, expectedExtension, initialContentType);
......
......@@ -138,7 +138,7 @@ public class FileRepositoryUpdateTest {
final String originalFilename = "originalFilename2" + extension;
repositoryFile.setOriginalFilename(originalFilename);
repositoryFile = fileRepoService.updateMetadata(repositoryFile.getUuid(), repositoryFile);
repositoryFile = fileRepoService.updateMetadata(repositoryFile);
assertThat("Extension was not updated", repositoryFile.getExtension(), equalTo(extension));
}
......@@ -181,7 +181,7 @@ public class FileRepositoryUpdateTest {
repositoryFile.setSubject(subject);
repositoryFile.setTitle(title);
final RepositoryFile repoFile = fileRepoService.updateMetadata(repositoryFile.getUuid(), repositoryFile);
final RepositoryFile repoFile = fileRepoService.updateMetadata(repositoryFile);
// Entity test
assertThat("RepositoryFile cannot be null", repoFile, notNullValue());
......
......@@ -100,7 +100,7 @@ public class MetadataTest {
assertThat(meta.getHeight(), is(7));
repoImage1.setOriginalFilename("test.png");
repoImage1 = repositoryService.updateMetadata(repoImage1.getUuid(), repoImage1);
repoImage1 = repositoryService.updateMetadata(repoImage1);
metadata = bytesStorageService.get(repoImage1.getStoragePath(), repoImage1.getMetadataFilename());
meta = objectMapper.readValue(metadata, RepositoryImage.class);
assertThat(meta.getOriginalFilename(), is(repoImage1.getOriginalFilename()));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment