2018-03-29 17:59:47 +02:00
package com.sismics.docs.core.dao ;
2013-07-27 18:33:20 +02:00
2015-05-17 22:20:34 +02:00
import com.sismics.docs.core.constant.AuditLogType ;
2013-08-16 23:48:35 +02:00
import com.sismics.docs.core.model.jpa.File ;
2015-05-17 22:20:34 +02:00
import com.sismics.docs.core.util.AuditLogUtil ;
2013-08-16 23:48:35 +02:00
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
2016-02-15 22:28:13 +01:00
* @param userId User ID
2013-07-27 18:33:20 +02:00
* @return New ID
* /
2016-02-15 22:28:13 +01:00
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 ) ;
2015-05-17 22:20:34 +02:00
// Create audit log
2016-02-15 22:28:13 +01:00
AuditLogUtil . create ( file , AuditLogType . CREATE , userId ) ;
2015-05-17 22:20:34 +02:00
2013-07-27 18:33:20 +02:00
return file . getId ( ) ;
}
2013-08-17 00:36:36 +02:00
/ * *
* Returns the list of all files .
2018-10-29 18:32:39 +01:00
*
* @param offset Offset
* @param limit Limit
2013-08-17 00:36:36 +02:00
* @return List of files
* /
@SuppressWarnings ( " unchecked " )
2018-10-29 18:32:39 +01:00
public List < File > findAll ( int offset , int limit ) {
2013-08-17 00:36:36 +02:00
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
Query q = em . createQuery ( " select f from File f where f.deleteDate is null " ) ;
2018-10-29 18:32:39 +01:00
q . setFirstResult ( offset ) ;
q . setMaxResults ( limit ) ;
2013-08-17 00:36:36 +02:00
return q . getResultList ( ) ;
}
2015-12-01 00:32:57 +01:00
/ * *
* 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
}
2015-03-06 22:40:33 +01: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 ;
}
2015-03-06 22:40:33 +01:00
}
2013-07-27 18:33:20 +02:00
/ * *
* Deletes a file .
*
* @param id File ID
2016-02-15 22:28:13 +01:00
* @param userId User ID
2013-07-27 18:33:20 +02:00
* /
2016-02-15 22:28:13 +01: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 ) ;
2015-05-17 22:20:34 +02:00
// Create audit log
2016-02-15 22:28:13 +01:00
AuditLogUtil . create ( fileDb , AuditLogType . DELETE , userId ) ;
2013-07-27 18:33:20 +02:00
}
2013-08-16 23:48:35 +02:00
/ * *
2015-03-06 21:13:09 +01:00
* Update a file .
2013-08-16 23:48:35 +02:00
*
* @param file File to update
* @return Updated file
* /
2015-03-06 21:13:09 +01:00
public File update ( File file ) {
2015-03-03 00:23:30 +01:00
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 ( ) ;
2015-03-03 00:23:30 +01:00
// 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 ( ) ) ;
2015-03-03 00:23:30 +01:00
return file ;
}
2020-05-17 21:00:01 +02:00
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
* /
2016-03-24 00:41:31 +01:00
public File getActiveById ( String id ) {
2013-07-27 18:33:20 +02:00
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2016-03-24 00:41:31 +01:00
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 {
2016-03-24 00:41:31 +01:00
return ( File ) q . getSingleResult ( ) ;
2013-07-27 18:33:20 +02:00
} catch ( NoResultException e ) {
return null ;
}
}
/ * *
2015-03-06 22:40:33 +01:00
* Get files by document ID or all orphan files of an user .
2013-07-27 18:33:20 +02:00
*
2016-04-18 00:00:46 +02:00
* @param userId User ID
2013-07-27 18:33:20 +02:00
* @param documentId Document ID
* @return List of files
* /
@SuppressWarnings ( " unchecked " )
2015-03-06 22:40:33 +01:00
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 " ) ;
2015-03-06 22:40:33 +01:00
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
}