Commit d6b68a34 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added RepositoryFileData and RepositoryImageData for metadata management

parent b4824395
......@@ -148,17 +148,16 @@ public class RepositoryFile extends BaseEntity implements BaseMetadata {
}
}
// TODO See if we can have a setter instead
/*
* (non-Javadoc)
*
* @see org.genesys2.server.filerepository.metadata.BaseMetadata#getIdentifier()
*/
// of this formula.
// TODO See if we can have a setter instead of this formula.
@Override
@Transient
public String getIdentifier() {
return "urn:uuid:" + uuid.toString();
return uuid == null ? null : "urn:uuid:" + uuid.toString();
}
/**
......@@ -181,6 +180,9 @@ public class RepositoryFile extends BaseEntity implements BaseMetadata {
// not persisted
@Transient
public String getFilename() {
if (uuid == null)
return null;
final StringBuffer sb = new StringBuffer();
sb.append(uuid.toString());
if (extension != null) {
......
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.filerepository.model;
import java.io.Serializable;
import java.util.Date;
import org.genesys2.server.filerepository.metadata.BaseMetadata;
/**
* Data only!
*/
public class RepositoryFileData implements BaseMetadata, Serializable {
private static final long serialVersionUID = -6507809172296678924L;
private String title;
private String subject;
private String description;
private String creator;
private String created;
private String rightsHolder;
private String accessRights;
private String license;
private String format;
private String extent;
private String bibliographicCitation;
private Date dateSubmitted;
private Date modified;
private String identifier;
private String originalFilename;
private String contentType;
@Override
public String getAccessRights() {
return accessRights;
}
@Override
public String getBibliographicCitation() {
return bibliographicCitation;
}
@Override
public String getCreated() {
return created;
}
@Override
public String getCreator() {
return creator;
}
@Override
public Date getDateSubmitted() {
return dateSubmitted;
}
@Override
public String getDescription() {
return description;
}
@Override
public String getExtent() {
return extent;
}
@Override
public String getFormat() {
return format;
}
@Override
public String getIdentifier() {
return identifier;
}
@Override
public String getLicense() {
return license;
}
@Override
public Date getModified() {
return modified;
}
@Override
public String getRightsHolder() {
return rightsHolder;
}
@Override
public String getSubject() {
return subject;
}
@Override
public String getTitle() {
return title;
}
public void setAccessRights(String accessRights) {
this.accessRights = accessRights;
}
public void setBibliographicCitation(String bibliographicCitation) {
this.bibliographicCitation = bibliographicCitation;
}
public void setCreated(String created) {
this.created = created;
}
public void setCreator(String creator) {
this.creator = creator;
}
public void setDateSubmitted(Date dateSubmitted) {
this.dateSubmitted = dateSubmitted;
}
public void setDescription(String description) {
this.description = description;
}
public void setExtent(String extent) {
this.extent = extent;
}
public void setFormat(String format) {
this.format = format;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public void setLicense(String license) {
this.license = license;
}
public void setModified(Date modified) {
this.modified = modified;
}
public void setRightsHolder(String rightsHolder) {
this.rightsHolder = rightsHolder;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setTitle(String title) {
this.title = title;
}
public void applyTo(RepositoryFile repositoryFile) {
repositoryFile.setAccessRights(accessRights);
repositoryFile.setBibliographicCitation(bibliographicCitation);
repositoryFile.setCreated(created);
repositoryFile.setCreator(creator);
repositoryFile.setDescription(description);
repositoryFile.setExtent(extent);
repositoryFile.setLicense(license);
repositoryFile.setRightsHolder(rightsHolder);
repositoryFile.setSubject(subject);
repositoryFile.setTitle(title);
repositoryFile.setOriginalFilename(originalFilename);
repositoryFile.setContentType(contentType);
}
public void setOriginalFilename(String originalFilename) {
this.originalFilename = originalFilename;
}
public String getOriginalFilename() {
return originalFilename;
}
public static RepositoryFileData from(RepositoryFile repositoryFile) {
if (repositoryFile == null)
return null;
RepositoryFileData rd = new RepositoryFileData();
rd.title = repositoryFile.getTitle();
rd.subject = repositoryFile.getSubject();
rd.description = repositoryFile.getDescription();
rd.creator = repositoryFile.getCreator();
rd.created = repositoryFile.getCreated();
rd.rightsHolder = repositoryFile.getRightsHolder();
rd.accessRights = repositoryFile.getAccessRights();
rd.license = repositoryFile.getLicense();
rd.format = repositoryFile.getFormat();
rd.extent = repositoryFile.getExtent();
rd.bibliographicCitation = repositoryFile.getBibliographicCitation();
rd.dateSubmitted = repositoryFile.getDateSubmitted();
rd.modified = repositoryFile.getModified();
rd.identifier = repositoryFile.getIdentifier();
rd.originalFilename = repositoryFile.getOriginalFilename();
rd.contentType = repositoryFile.getContentType();
return rd;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getContentType() {
return contentType;
}
}
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.filerepository.model;
import java.io.Serializable;
import org.genesys2.server.filerepository.metadata.ImageMetadata;
/**
* Data only!
*/
public class RepositoryImageData extends RepositoryFileData implements ImageMetadata, Serializable {
private static final long serialVersionUID = 1047696679600823747L;
private int width;
private int height;
private Orientation orientation;
@Override
public int getHeight() {
return height;
}
@Override
public Orientation getOrientation() {
return orientation;
}
@Override
public int getWidth() {
return width;
}
public void setHeight(int height) {
this.height = height;
}
public void setOrientation(Orientation orientation) {
this.orientation = orientation;
}
public void setWidth(int width) {
this.width = width;
}
public void applyTo(RepositoryImage repositoryImage) {
super.applyTo(repositoryImage);
repositoryImage.setWidth(width);
repositoryImage.setHeight(height);
repositoryImage.setOrientation(orientation);
}
}
......@@ -25,7 +25,9 @@ import org.genesys2.server.filerepository.InvalidRepositoryPathException;
import org.genesys2.server.filerepository.NoSuchRepositoryFileException;
import org.genesys2.server.filerepository.metadata.ImageMetadata;
import org.genesys2.server.filerepository.model.RepositoryFile;
import org.genesys2.server.filerepository.model.RepositoryFileData;
import org.genesys2.server.filerepository.model.RepositoryImage;
import org.genesys2.server.filerepository.model.RepositoryImageData;
import org.springframework.data.domain.Pageable;
// TODO: Auto-generated Javadoc
......@@ -96,11 +98,12 @@ public interface RepositoryService {
/**
* Update file metadata. The update is based on the record UUID.
*
* @param uuid UUID of the repository file
* @param fileData the file data
* @return the updated RepositoryFile
* @throws NoSuchRepositoryFileException when file is not available in the repository
*/
RepositoryFile updateMetadata(RepositoryFile fileData) throws NoSuchRepositoryFileException;
RepositoryFile updateMetadata(UUID uuid, RepositoryFileData fileData) throws NoSuchRepositoryFileException;
/**
* Update file bytes.
......@@ -167,11 +170,12 @@ public interface RepositoryService {
/**
* Update image metadata. The update is based on the record UUID.
*
* @param repositoryImage the file metadata
* @param uuid Repository image UUID
* @param imageData the file metadata
* @return the updated metadata
* @throws NoSuchRepositoryFileException when file is not available in the repository
*/
RepositoryImage updateMetadata(RepositoryImage repositoryImage) throws NoSuchRepositoryFileException;
RepositoryImage updateMetadata(UUID uuid, RepositoryImageData imageData) throws NoSuchRepositoryFileException;
/**
* Remove repository image
......
......@@ -32,7 +32,9 @@ import org.genesys2.server.filerepository.InvalidRepositoryFileDataException;
import org.genesys2.server.filerepository.InvalidRepositoryPathException;
import org.genesys2.server.filerepository.NoSuchRepositoryFileException;
import org.genesys2.server.filerepository.model.RepositoryFile;
import org.genesys2.server.filerepository.model.RepositoryFileData;
import org.genesys2.server.filerepository.model.RepositoryImage;
import org.genesys2.server.filerepository.model.RepositoryImageData;
import org.genesys2.server.filerepository.persistence.RepositoryFilePersistence;
import org.genesys2.server.filerepository.persistence.RepositoryImagePersistence;
import org.genesys2.server.filerepository.service.BytesStorageService;
......@@ -257,15 +259,17 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
*/
@Override
@Transactional
public RepositoryFile updateMetadata(final RepositoryFile fileData) throws NoSuchRepositoryFileException {
if (fileData == null)
public RepositoryFile updateMetadata(final UUID uuid, final RepositoryFileData fileData) throws NoSuchRepositoryFileException {
RepositoryFile repositoryFile = repositoryFilePersistence.findByUuid(uuid);
if (repositoryFile == null)
throw new NoSuchRepositoryFileException();
RepositoryFile repositoryFile;
if (fileData.getContentType().startsWith("image")) {
repositoryFile = repositoryImagePersistence.save((RepositoryImage) fileData);
fileData.applyTo(repositoryFile);
if (repositoryFile.getContentType().startsWith("image")) {
repositoryFile = repositoryImagePersistence.save((RepositoryImage) repositoryFile);
} else {
repositoryFile = repositoryFilePersistence.save(fileData);
repositoryFile = repositoryFilePersistence.save(repositoryFile);
}
return repositoryFile;
......@@ -277,10 +281,12 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
* @see org.genesys2.server.filerepository.service.RepositoryService#updateMetadata(org.genesys2.server.filerepository. model.RepositoryImage)
*/
@Override
public RepositoryImage updateMetadata(RepositoryImage repositoryImage) throws NoSuchRepositoryFileException {
public RepositoryImage updateMetadata(UUID uuid, RepositoryImageData imageData) throws NoSuchRepositoryFileException {
RepositoryImage repositoryImage = repositoryImagePersistence.findByUuid(uuid);
if (repositoryImage == null)
throw new NoSuchRepositoryFileException();
imageData.applyTo(repositoryImage);
repositoryImage = repositoryImagePersistence.save(repositoryImage);
return repositoryImage;
......@@ -358,7 +364,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
return repositoryFile;
}
@Override
@Transactional
public RepositoryImage removeImage(RepositoryImage repositoryImage) throws NoSuchRepositoryFileException, IOException {
......
......@@ -24,6 +24,7 @@ import org.genesys2.server.filerepository.NoSuchRepositoryFileException;
import org.genesys2.server.filerepository.config.DatabaseConfig;
import org.genesys2.server.filerepository.config.ServiceBeanConfig;
import org.genesys2.server.filerepository.model.RepositoryFile;
import org.genesys2.server.filerepository.model.RepositoryFileData;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -154,8 +155,9 @@ public class FileRepositoryExtensionTest {
FileRepositoryTestUtil.checkFile(repoFile, initialPath, initialOriginalFilename, initialExtension, initialContentType);
repoFile.setOriginalFilename(originalFilename);
repoFile = fileRepoService.updateMetadata(repoFile);
RepositoryFileData fileData = RepositoryFileData.from(repoFile);
fileData.setOriginalFilename(originalFilename);
repoFile = fileRepoService.updateMetadata(repoFile.getUuid(), fileData);
return FileRepositoryTestUtil.checkFile(repoFile, initialPath, originalFilename, expectedExtension, initialContentType);
......
......@@ -29,6 +29,7 @@ import org.genesys2.server.filerepository.NoSuchRepositoryFileException;
import org.genesys2.server.filerepository.config.DatabaseConfig;
import org.genesys2.server.filerepository.config.ServiceBeanConfig;
import org.genesys2.server.filerepository.model.RepositoryFile;
import org.genesys2.server.filerepository.model.RepositoryFileData;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -130,12 +131,14 @@ public class FileRepositoryUpdateTest {
*/
@Test
public void originalFilenameChangeUpdatesExtension() throws NoSuchRepositoryFileException {
RepositoryFile fileData = fileRepoService.getFile(fileUuid);
RepositoryFile repositoryFile = fileRepoService.getFile(fileUuid);
final String extension = ".png";
final String originalFilename = "originalFilename2" + extension;
RepositoryFileData fileData = RepositoryFileData.from(repositoryFile);
fileData.setOriginalFilename(originalFilename);
fileData = fileRepoService.updateMetadata(fileData);
assertThat("Extension was not updated", fileData.getExtension(), equalTo(extension));
repositoryFile = fileRepoService.updateMetadata(repositoryFile.getUuid(), fileData);
assertThat("Extension was not updated", repositoryFile.getExtension(), equalTo(extension));
}
/**
......@@ -145,10 +148,10 @@ public class FileRepositoryUpdateTest {
*/
@Test
public void addMetadataToFile() throws NoSuchRepositoryFileException {
final RepositoryFile fileData = fileRepoService.getFile(fileUuid);
final RepositoryFile repositoryFile = fileRepoService.getFile(fileUuid);
FileRepositoryTestUtil.checkFile(fileData, initialPath, initialOriginalFilename, initialExtension, initialContentType);
FileRepositoryTestUtil.assertMetadataIsBlank(fileData);
FileRepositoryTestUtil.checkFile(repositoryFile, initialPath, initialOriginalFilename, initialExtension, initialContentType);
FileRepositoryTestUtil.assertMetadataIsBlank(repositoryFile);
final String bibliographicCitation = "bibliographicCitation";
final String accessRights = "accessRights";
......@@ -164,6 +167,7 @@ public class FileRepositoryUpdateTest {
final String subject = "subject";
final String title = "File Repository Requirements";
RepositoryFileData fileData=new RepositoryFileData();
fileData.setAccessRights(accessRights);
fileData.setBibliographicCitation(bibliographicCitation);
fileData.setCreated(created);
......@@ -177,12 +181,12 @@ public class FileRepositoryUpdateTest {
fileData.setSubject(subject);
fileData.setTitle(title);
final RepositoryFile repoFile = fileRepoService.updateMetadata(fileData);
final RepositoryFile repoFile = fileRepoService.updateMetadata(repositoryFile.getUuid(), fileData);
// Entity test
assertThat("RepositoryFile cannot be null", repoFile, notNullValue());
assertThat("RepositoryFile#UUID cannot be null", repoFile.getUuid(), notNullValue());
assertThat("UUIDs must match", repoFile.getUuid(), equalTo(fileData.getUuid()));
assertThat("UUIDs must match", repoFile.getUuid(), equalTo(repositoryFile.getUuid()));
FileRepositoryTestUtil.checkFile(repoFile, initialPath, originalFilename2, extension2, contentType2);
FileRepositoryTestUtil.checkMetadata(repoFile, bibliographicCitation, accessRights, created, creator, description, extent, license, rightsHolder, subject, title);
......
Supports Markdown
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