docs/docs-core/src/main/java/com/sismics/docs/core/dao/FileDao.java

208 lines
6.4 KiB
Java
Raw Normal View History

2018-03-29 17:59:47 +02:00
package com.sismics.docs.core.dao;
2013-07-27 18:33:20 +02:00
import com.sismics.docs.core.constant.AuditLogType;
import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.util.AuditLogUtil;
import com.sismics.util.context.ThreadLocalContext;
2013-07-27 18:33:20 +02:00
2018-01-28 12:44:11 +01:00
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;
import java.util.UUID;
2013-07-27 18:33:20 +02:00
/**
* File DAO.
*
* @author bgamard
*/
public class FileDao {
/**
* Creates a new file.
*
* @param file File
* @param userId User ID
2013-07-27 18:33:20 +02:00
* @return New ID
*/
public String create(File file, String userId) {
2013-07-27 18:33:20 +02:00
// Create the UUID
file.setId(UUID.randomUUID().toString());
// Create the file
EntityManager em = ThreadLocalContext.get().getEntityManager();
file.setCreateDate(new Date());
em.persist(file);
// Create audit log
AuditLogUtil.create(file, AuditLogType.CREATE, userId);
2013-07-27 18:33:20 +02:00
return file.getId();
}
/**
* Returns the list of all files.
*
* @param offset Offset
* @param limit Limit
* @return List of files
*/
@SuppressWarnings("unchecked")
public List<File> findAll(int offset, int limit) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.deleteDate is null");
q.setFirstResult(offset);
q.setMaxResults(limit);
return q.getResultList();
}
/**
* Returns the list of all files from a user.
*
* @param userId User ID
* @return List of files
*/
@SuppressWarnings("unchecked")
public List<File> findByUserId(String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.userId = :userId and f.deleteDate is null");
q.setParameter("userId", userId);
return q.getResultList();
}
2013-07-27 18:33:20 +02:00
/**
* Returns an active file.
*
* @param id File ID
* @return Document
*/
public File getFile(String id) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null");
q.setParameter("id", id);
2015-11-16 02:22:51 +01:00
try {
return (File) q.getSingleResult();
} catch (NoResultException e) {
return null;
}
2013-07-27 18:33:20 +02:00
}
/**
* Returns an active file.
*
* @param id File ID
* @param userId User ID
* @return Document
*/
public File getFile(String id, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.id = :id and f.userId = :userId and f.deleteDate is null");
q.setParameter("id", id);
q.setParameter("userId", userId);
2015-11-16 02:22:51 +01:00
try {
return (File) q.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
2013-07-27 18:33:20 +02:00
/**
* Deletes a file.
*
* @param id File ID
* @param userId User ID
2013-07-27 18:33:20 +02:00
*/
public void delete(String id, String userId) {
2013-07-27 18:33:20 +02:00
EntityManager em = ThreadLocalContext.get().getEntityManager();
2013-08-13 23:15:58 +02:00
// Get the file
2013-07-27 18:33:20 +02:00
Query q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null");
q.setParameter("id", id);
File fileDb = (File) q.getSingleResult();
2013-08-13 23:15:58 +02:00
// Delete the file
2013-07-27 18:33:20 +02:00
Date dateNow = new Date();
fileDb.setDeleteDate(dateNow);
// Create audit log
AuditLogUtil.create(fileDb, AuditLogType.DELETE, userId);
2013-07-27 18:33:20 +02:00
}
/**
2015-03-06 21:13:09 +01:00
* Update a file.
*
* @param file File to update
* @return Updated file
*/
2015-03-06 21:13:09 +01:00
public File update(File file) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the file
Query q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null");
q.setParameter("id", file.getId());
2018-01-28 12:44:11 +01:00
File fileDb = (File) q.getSingleResult();
// Update the file
2018-01-28 12:44:11 +01:00
fileDb.setDocumentId(file.getDocumentId());
2018-03-23 12:52:42 +01:00
fileDb.setName(file.getName());
2018-01-28 12:44:11 +01:00
fileDb.setContent(file.getContent());
fileDb.setOrder(file.getOrder());
fileDb.setMimeType(file.getMimeType());
2018-11-23 14:54:11 +01:00
fileDb.setVersionId(file.getVersionId());
fileDb.setLatestVersion(file.isLatestVersion());
return file;
}
2013-07-27 18:33:20 +02:00
/**
* Gets a file by its ID.
*
2013-08-13 23:15:58 +02:00
* @param id File ID
* @return File
2013-07-27 18:33:20 +02:00
*/
public File getActiveById(String id) {
2013-07-27 18:33:20 +02:00
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null");
q.setParameter("id", id);
2013-07-27 18:33:20 +02:00
try {
return (File) q.getSingleResult();
2013-07-27 18:33:20 +02:00
} catch (NoResultException e) {
return null;
}
}
/**
* Get files by document ID or all orphan files of an user.
2013-07-27 18:33:20 +02:00
*
* @param userId User ID
2013-07-27 18:33:20 +02:00
* @param documentId Document ID
* @return List of files
*/
@SuppressWarnings("unchecked")
public List<File> getByDocumentId(String userId, String documentId) {
2013-07-27 18:33:20 +02:00
EntityManager em = ThreadLocalContext.get().getEntityManager();
2015-03-06 21:23:50 +01:00
if (documentId == null) {
2018-11-23 14:54:11 +01:00
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);
2015-03-06 21:23:50 +01:00
return q.getResultList();
}
2018-11-23 14:54:11 +01:00
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");
2013-07-27 18:33:20 +02:00
q.setParameter("documentId", documentId);
2013-08-03 00:53:58 +02:00
return q.getResultList();
2013-07-27 18:33:20 +02:00
}
2019-01-30 21:14:07 +01:00
/**
* Get all files from a version.
*
* @param versionId Version ID
* @return List of files
*/
@SuppressWarnings("unchecked")
public List<File> getByVersionId(String versionId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.versionId = :versionId and f.deleteDate is null order by f.order asc");
q.setParameter("versionId", versionId);
return q.getResultList();
}
2013-07-27 18:33:20 +02:00
}