mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 01:36:18 +00:00
#256: versioning API
This commit is contained in:
@@ -148,7 +148,9 @@ public class FileDao {
|
||||
fileDb.setContent(file.getContent());
|
||||
fileDb.setOrder(file.getOrder());
|
||||
fileDb.setMimeType(file.getMimeType());
|
||||
|
||||
fileDb.setVersionId(file.getVersionId());
|
||||
fileDb.setLatestVersion(file.isLatestVersion());
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
@@ -180,11 +182,11 @@ public class FileDao {
|
||||
public List<File> getByDocumentId(String userId, String documentId) {
|
||||
EntityManager em = ThreadLocalContext.get().getEntityManager();
|
||||
if (documentId == null) {
|
||||
Query q = em.createQuery("select f from File f where f.documentId is null and f.deleteDate is null and f.userId = :userId order by f.createDate asc");
|
||||
Query q = em.createQuery("select f from File f where f.documentId is null and f.deleteDate is null and f.latestVersion = true and f.userId = :userId order by f.createDate asc");
|
||||
q.setParameter("userId", userId);
|
||||
return q.getResultList();
|
||||
}
|
||||
Query q = em.createQuery("select f from File f where f.documentId = :documentId and f.deleteDate is null order by f.order asc");
|
||||
Query q = em.createQuery("select f from File f where f.documentId = :documentId and f.latestVersion = true and f.deleteDate is null order by f.order asc");
|
||||
q.setParameter("documentId", documentId);
|
||||
return q.getResultList();
|
||||
}
|
||||
|
||||
@@ -71,6 +71,24 @@ public class File implements Loggable {
|
||||
@Column(name = "FIL_ORDER_N")
|
||||
private Integer order;
|
||||
|
||||
/**
|
||||
* Version ID.
|
||||
*/
|
||||
@Column(name = "FIL_IDVERSION_C")
|
||||
private String versionId;
|
||||
|
||||
/**
|
||||
* Version number (starting at 0).
|
||||
*/
|
||||
@Column(name = "FIL_VERSION_N", nullable = false)
|
||||
private Integer version;
|
||||
|
||||
/**
|
||||
* True if it's the latest version of the file.
|
||||
*/
|
||||
@Column(name = "FIL_LATESTVERSION_B", nullable = false)
|
||||
private boolean latestVersion;
|
||||
|
||||
/**
|
||||
* Private key to decrypt the file.
|
||||
* Not saved to database, of course.
|
||||
@@ -160,6 +178,33 @@ public class File implements Loggable {
|
||||
this.privateKey = privateKey;
|
||||
}
|
||||
|
||||
public String getVersionId() {
|
||||
return versionId;
|
||||
}
|
||||
|
||||
public File setVersionId(String versionId) {
|
||||
this.versionId = versionId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public File setVersion(Integer version) {
|
||||
this.version = version;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isLatestVersion() {
|
||||
return latestVersion;
|
||||
}
|
||||
|
||||
public File setLatestVersion(boolean latestVersion) {
|
||||
this.latestVersion = latestVersion;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper(this)
|
||||
|
||||
@@ -212,7 +212,7 @@ public class InboxService extends AbstractScheduledService {
|
||||
}
|
||||
|
||||
// Save the document, create the base ACLs
|
||||
document = DocumentUtil.createDocument(document, "admin");
|
||||
DocumentUtil.createDocument(document, "admin");
|
||||
|
||||
// Add the tag
|
||||
String tagId = ConfigUtil.getConfigStringValue(ConfigType.INBOX_TAG);
|
||||
@@ -232,7 +232,7 @@ public class InboxService extends AbstractScheduledService {
|
||||
|
||||
// Add files to the document
|
||||
for (EmailUtil.FileContent fileContent : mailContent.getFileContentList()) {
|
||||
FileUtil.createFile(fileContent.getName(), fileContent.getFile(), fileContent.getSize(),
|
||||
FileUtil.createFile(fileContent.getName(), null, fileContent.getFile(), fileContent.getSize(),
|
||||
document.getLanguage(), "admin", document.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ public class FileUtil {
|
||||
* Create a new file.
|
||||
*
|
||||
* @param name File name, can be null
|
||||
* @param previousFileId ID of the previous version of the file, if the new file is a new version
|
||||
* @param unencryptedFile Path to the unencrypted file
|
||||
* @param fileSize File size
|
||||
* @param language File language, can be null if associated to no document
|
||||
@@ -106,7 +107,7 @@ public class FileUtil {
|
||||
* @return File ID
|
||||
* @throws Exception e
|
||||
*/
|
||||
public static String createFile(String name, Path unencryptedFile, long fileSize, String language, String userId, String documentId) throws Exception {
|
||||
public static String createFile(String name, String previousFileId, Path unencryptedFile, long fileSize, String language, String userId, String documentId) throws Exception {
|
||||
// Validate mime type
|
||||
String mimeType;
|
||||
try {
|
||||
@@ -132,22 +133,42 @@ public class FileUtil {
|
||||
}
|
||||
}
|
||||
|
||||
// Get files of this document
|
||||
FileDao fileDao = new FileDao();
|
||||
int order = 0;
|
||||
if (documentId != null) {
|
||||
for (File file : fileDao.getByDocumentId(userId, documentId)) {
|
||||
file.setOrder(order++);
|
||||
}
|
||||
}
|
||||
|
||||
// Create the file
|
||||
// Prepare the file
|
||||
File file = new File();
|
||||
file.setOrder(order);
|
||||
file.setOrder(0);
|
||||
file.setVersion(0);
|
||||
file.setLatestVersion(true);
|
||||
file.setDocumentId(documentId);
|
||||
file.setName(StringUtils.abbreviate(name, 200));
|
||||
file.setMimeType(mimeType);
|
||||
file.setUserId(userId);
|
||||
|
||||
// Get files of this document
|
||||
FileDao fileDao = new FileDao();
|
||||
if (documentId != null) {
|
||||
if (previousFileId == null) {
|
||||
// It's not a new version, so put it in last order
|
||||
file.setOrder(fileDao.getByDocumentId(userId, documentId).size());
|
||||
} else {
|
||||
// It's a new version, update the previous version
|
||||
File previousFile = fileDao.getActiveById(previousFileId);
|
||||
if (previousFile == null || !previousFile.getDocumentId().equals(documentId)) {
|
||||
throw new IOException("Previous version mismatch");
|
||||
}
|
||||
|
||||
if (previousFile.getVersionId() == null) {
|
||||
previousFile.setVersionId(UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
previousFile.setLatestVersion(false);
|
||||
file.setVersionId(previousFile.getVersionId());
|
||||
file.setVersion(previousFile.getVersion() + 1);
|
||||
|
||||
fileDao.update(previousFile);
|
||||
}
|
||||
}
|
||||
|
||||
// Create the file
|
||||
String fileId = fileDao.create(file, userId);
|
||||
|
||||
// Save the file
|
||||
|
||||
@@ -1 +1 @@
|
||||
db.version=21
|
||||
db.version=22
|
||||
@@ -0,0 +1,5 @@
|
||||
alter table T_FILE add column FIL_VERSION_N int not null default 0;
|
||||
alter table T_FILE add column FIL_LATESTVERSION_B bit not null default 1;
|
||||
alter table T_FILE add column FIL_IDVERSION_C varchar(36);
|
||||
|
||||
update T_CONFIG set CFG_VALUE_C = '22' where CFG_ID_C = 'DB_VERSION';
|
||||
Reference in New Issue
Block a user