Skip to content

Commit

Permalink
[io] FileStorage now receives a GenericFile
Browse files Browse the repository at this point in the history
  * Issue: BNN-281
  • Loading branch information
jcarvalho committed Dec 2, 2015
1 parent 343e988 commit bd956b1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,29 @@ public final class DomainStorage extends DomainStorage_Base {
}

@Override
public String store(String uniqueIdentification, byte[] content) {
public String store(GenericFile file, byte[] content) {
String uniqueIdentification = file.getContentKey();
final DomainObject existingRawData = FenixFramework.getDomainObject(uniqueIdentification);
if (existingRawData != null && existingRawData instanceof FileRawData) {
((FileRawData) existingRawData).delete();
}

if (content != null) {
return new FileRawData(uniqueIdentification, content).getExternalId();
return new FileRawData(uniqueIdentification == null ? file.getExternalId() : uniqueIdentification, content)
.getExternalId();
}
return null;
}

@Override
public byte[] read(String uniqueIdentification) {
final FileRawData rawData = FenixFramework.getDomainObject(uniqueIdentification);
public byte[] read(GenericFile file) {
final FileRawData rawData = FenixFramework.getDomainObject(file.getContentKey());
return rawData != null ? rawData.getContent() : null;
}

@Override
public InputStream readAsInputStream(String uniqueIdentification) {
byte[] read = read(uniqueIdentification);
public InputStream readAsInputStream(GenericFile file) {
byte[] read = read(file);
return read != null ? new ByteArrayInputStream(read) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,29 @@ public Set<GenericFile> getFileSet() {
/**
* Stores the given file in this storage, and associates it with the given identifier.
*
* This differs from the {@link #store(String, byte[])} variant in that it does not require the whole file to be loaded in
* This differs from the {@link #store(GenericFile, byte[])} variant in that it does not require the whole file to be loaded in
* memory.
*
* Due to performance reasons, the given file may be locked, moved to another location or even removed.
*
* @param uniqueIdentification
* The unique identifier for the newly created file
* @param genericFile
* The {@link GenericFile} instance to store
* @param file
* The file to store
* @return
* The identification associated with the newly created file
* @throws IOException
* If any error occurs while accessing the provided file or storing it in the underlying storage
*/
public String store(String uniqueIdentification, File file) throws IOException {
return store(uniqueIdentification, Files.toByteArray(file));
public String store(GenericFile genericFile, File file) throws IOException {
return store(genericFile, Files.toByteArray(file));
}

abstract public String store(String uniqueIdentification, byte[] content);
abstract public String store(GenericFile file, byte[] content);

abstract public byte[] read(String uniqueIdentification);
abstract public byte[] read(GenericFile file);

abstract public InputStream readAsInputStream(String uniqueIdentification);
abstract public InputStream readAsInputStream(GenericFile file);

public static DomainStorage createNewDomainStorage(final String name) {
return new DomainStorage(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public String getContentType() {
return super.getContentType();
}

@Override
public String getContentKey() {
//FIXME: remove when the framework enables read-only slots
return super.getContentKey();
}

/**
* Returns the checksum for this file, using the algorithm specified by {@link #getChecksumAlgorithm()}.
*
Expand Down Expand Up @@ -172,8 +178,7 @@ private void setContent(File file, String filename) throws IOException {
long size = file.length();
setSize(Long.valueOf(size));
final FileStorage fileStorage = getFileStorage();
final String uniqueIdentification =
fileStorage.store(Strings.isNullOrEmpty(getContentKey()) ? getExternalId() : getContentKey(), file);
final String uniqueIdentification = fileStorage.store(this, file);
setStorage(fileStorage);

if (Strings.isNullOrEmpty(uniqueIdentification)) {
Expand All @@ -188,8 +193,7 @@ private void setContent(byte[] content) {
long size = (content == null) ? 0 : content.length;
setSize(Long.valueOf(size));
final FileStorage fileStorage = getFileStorage();
final String uniqueIdentification =
fileStorage.store(Strings.isNullOrEmpty(getContentKey()) ? getExternalId() : getContentKey(), content);
final String uniqueIdentification = fileStorage.store(this, content);
setStorage(fileStorage);

if (Strings.isNullOrEmpty(uniqueIdentification) && content != null) {
Expand All @@ -203,11 +207,11 @@ private void setContent(byte[] content) {
}

public byte[] getContent() {
return getStorage().read(getContentKey());
return getStorage().read(this);
}

public InputStream getStream() {
return getStorage().readAsInputStream(getContentKey());
return getStorage().readAsInputStream(this);
}

public static void convertFileStorages(final FileStorage fileStorageToUpdate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ public Integer getTreeDirectoriesNameLength() {
}

@Override
public String store(String uniqueIdentification, File file) {

public String store(GenericFile genericFile, File file) {
String uniqueIdentification =
genericFile.getContentKey() == null ? genericFile.getExternalId() : genericFile.getContentKey();
final String fullPath = getFullPath(uniqueIdentification);

File directory = new File(fullPath);
Expand All @@ -116,7 +117,8 @@ public String store(String uniqueIdentification, File file) {
}

@Override
public String store(String uniqueIdentification, byte[] content) {
public String store(GenericFile file, byte[] content) {
String uniqueIdentification = file.getContentKey() == null ? file.getExternalId() : file.getContentKey();

final String fullPath = getFullPath(uniqueIdentification);

Expand Down Expand Up @@ -192,7 +194,8 @@ private String transformIDInPath(final String uniqueIdentification) {
}

@Override
public byte[] read(String uniqueIdentification) {
public byte[] read(GenericFile file) {
String uniqueIdentification = file.getContentKey();
try {
Map<String, FileWriteIntention> map = getPerTxBox().get();
if (map.containsKey(uniqueIdentification)) {
Expand All @@ -206,7 +209,8 @@ public byte[] read(String uniqueIdentification) {
}

@Override
public InputStream readAsInputStream(String uniqueIdentification) {
public InputStream readAsInputStream(GenericFile file) {
String uniqueIdentification = file.getContentKey();
try {
Map<String, FileWriteIntention> map = getPerTxBox().get();
if (map.containsKey(uniqueIdentification)) {
Expand Down

0 comments on commit bd956b1

Please sign in to comment.