Uploading works now with Envers.

pull/10/head
Jens Pelzetter 2021-06-30 19:50:10 +02:00
parent f022a9540d
commit 36c9ec8a61
2 changed files with 101 additions and 15 deletions

View File

@ -86,7 +86,7 @@ public class BinaryAsset extends Asset implements Serializable {
@Column(name = "ASSET_DATA") @Column(name = "ASSET_DATA")
@Lob @Lob
@Basic(fetch = FetchType.LAZY) @Basic(fetch = FetchType.LAZY)
@NotAudited // @NotAudited
// private byte[] data; // private byte[] data;
private Blob data; private Blob data;

View File

@ -69,7 +69,7 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
public void copyDataToOutputStream( public void copyDataToOutputStream(
final BinaryAsset asset, final OutputStream outputStream final BinaryAsset asset, final OutputStream outputStream
) { ) {
try ( Connection connection = dataSource.getConnection()) { try ( Connection connection = dataSource.getConnection()) {
connection.setAutoCommit(false); connection.setAutoCommit(false);
final PreparedStatement stmt = connection final PreparedStatement stmt = connection
.prepareStatement( .prepareStatement(
@ -122,7 +122,7 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
} }
final Blob data = BlobProxy.generateProxy( final Blob data = BlobProxy.generateProxy(
Files.newInputStream(tmpFilePath), -1 new UploadInputStream(tmpFilePath), -1
); );
asset.setFileName(fileName); asset.setFileName(fileName);
asset.setData(data); asset.setData(data);
@ -143,9 +143,9 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
} }
assetRepo.save(asset); assetRepo.save(asset);
entityManager.flush(); // entityManager.flush();
//
updateAudTable(asset.getObjectId()); // updateAudTable(asset.getObjectId());
// try ( Connection connection = dataSource.getConnection()) { // try ( Connection connection = dataSource.getConnection()) {
// final PreparedStatement stmt = connection // final PreparedStatement stmt = connection
@ -165,9 +165,9 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
// } // }
} }
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRES_NEW)
private void updateAudTable(final long assetId) { private void updateAudTable(final long assetId) {
try (Connection connection = dataSource.getConnection()) { try ( Connection connection = dataSource.getConnection()) {
final PreparedStatement findRevStmt = connection final PreparedStatement findRevStmt = connection
.prepareStatement( .prepareStatement(
"SELECT rev FROM ccm_cms.binary_assets_aud WHERE object_id = ? ORDER BY rev DESC LIMIT 1" "SELECT rev FROM ccm_cms.binary_assets_aud WHERE object_id = ? ORDER BY rev DESC LIMIT 1"
@ -175,7 +175,7 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
findRevStmt.setLong(1, assetId); findRevStmt.setLong(1, assetId);
final long rev; final long rev;
try(ResultSet resultSet = findRevStmt.executeQuery()) { try ( ResultSet resultSet = findRevStmt.executeQuery()) {
resultSet.next(); resultSet.next();
rev = resultSet.getLong("rev"); rev = resultSet.getLong("rev");
} }
@ -189,9 +189,95 @@ public class BinaryAssetBlobDataProvider implements BinaryAssetDataProvider {
updateDataStmt.setLong(3, rev); updateDataStmt.setLong(3, rev);
updateDataStmt.execute(); updateDataStmt.execute();
} catch(SQLException ex) { } catch (SQLException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
} }
private class UploadInputStream extends InputStream {
private final Path tmpFilePath;
private InputStream inputStream;
public UploadInputStream(final Path tmpFilePath) {
this.tmpFilePath = tmpFilePath;
}
@Override
public int available() throws IOException {
openNewInputStreamIfNecessary();
return inputStream.available();
}
@Override
public void close() throws IOException {
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
}
@Override
public void mark(final int readLimit) {
try {
openNewInputStreamIfNecessary();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
inputStream.mark(readLimit);
}
@Override
public boolean markSupported() {
try {
openNewInputStreamIfNecessary();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return inputStream.markSupported();
}
@Override
public int read() throws IOException {
openNewInputStreamIfNecessary();
return inputStream.read();
}
@Override
public int read(final byte[] data) throws IOException {
openNewInputStreamIfNecessary();
return inputStream.read(data);
}
@Override
public int read(final byte[] data, final int offset, final int length)
throws IOException {
openNewInputStreamIfNecessary();
return inputStream.read(data, offset, length);
}
@Override
public void reset() throws IOException {
if (inputStream == null) {
openNewInputStreamIfNecessary();
} else {
inputStream.reset();
}
}
@Override
public long skip(long nBytes) throws IOException {
openNewInputStreamIfNecessary();
return inputStream.skip(nBytes);
}
private void openNewInputStreamIfNecessary() throws IOException {
if (inputStream == null) {
inputStream = Files.newInputStream(tmpFilePath);
}
}
}
} }