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 ;
2023-04-09 21:31:53 +02:00
import jakarta.persistence.EntityManager ;
import jakarta.persistence.NoResultException ;
2023-10-08 22:07:01 +02:00
import jakarta.persistence.Query ;
2023-04-09 21:31:53 +02:00
import jakarta.persistence.TypedQuery ;
2023-10-08 22:07:01 +02:00
2022-03-20 11:36:28 +01:00
import java.util.Collections ;
2018-01-28 12:44:11 +01:00
import java.util.Date ;
2023-10-08 22:07:01 +02:00
import java.util.HashMap ;
2018-01-28 12:44:11 +01:00
import java.util.List ;
2023-10-08 22:07:01 +02:00
import java.util.Map ;
2018-01-28 12:44:11 +01:00
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
* /
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 ( ) ;
2022-03-21 11:36:25 +01:00
TypedQuery < File > q = em . createQuery ( " select f from File f where f.deleteDate is null " , File . class ) ;
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
* /
public List < File > findByUserId ( String userId ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2022-03-21 11:36:25 +01:00
TypedQuery < File > q = em . createQuery ( " select f from File f where f.userId = :userId and f.deleteDate is null " , File . class ) ;
2015-12-01 00:32:57 +01:00
q . setParameter ( " userId " , userId ) ;
return q . getResultList ( ) ;
}
2022-04-15 10:18:39 +02:00
/ * *
* Returns a list of active files .
*
* @param ids Files IDs
* @return List of files
* /
public List < File > getFiles ( List < String > ids ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
TypedQuery < File > q = em . createQuery ( " select f from File f where f.id in :ids and f.deleteDate is null " , File . class ) ;
q . setParameter ( " ids " , ids ) ;
return q . getResultList ( ) ;
}
2015-12-01 00:32:57 +01:00
2013-07-27 18:33:20 +02:00
/ * *
2022-04-15 10:18:39 +02:00
* Returns an active file or null .
2013-07-27 18:33:20 +02:00
*
* @param id File ID
2022-03-21 11:36:25 +01:00
* @return File
2013-07-27 18:33:20 +02:00
* /
public File getFile ( String id ) {
2022-04-15 10:18:39 +02:00
List < File > files = getFiles ( List . of ( id ) ) ;
if ( files . isEmpty ( ) ) {
2015-11-16 02:22:51 +01:00
return null ;
2022-04-15 10:18:39 +02:00
} else {
return files . get ( 0 ) ;
2015-11-16 02:22:51 +01:00
}
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
2022-03-21 11:36:25 +01:00
* @return File
2015-03-06 22:40:33 +01:00
* /
public File getFile ( String id , String userId ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2022-03-21 11:36:25 +01:00
TypedQuery < File > q = em . createQuery ( " select f from File f where f.id = :id and f.userId = :userId and f.deleteDate is null " , File . class ) ;
2015-03-06 22:40:33 +01:00
q . setParameter ( " id " , id ) ;
q . setParameter ( " userId " , userId ) ;
2015-11-16 02:22:51 +01:00
try {
2022-03-21 11:36:25 +01:00
return q . getSingleResult ( ) ;
2015-11-16 02:22:51 +01:00
} 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
2022-03-21 11:36:25 +01:00
TypedQuery < File > q = em . createQuery ( " select f from File f where f.id = :id and f.deleteDate is null " , File . class ) ;
2013-07-27 18:33:20 +02:00
q . setParameter ( " id " , id ) ;
2022-03-21 11:36:25 +01:00
File fileDb = q . getSingleResult ( ) ;
2013-07-27 18:33:20 +02:00
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
2022-03-21 11:36:25 +01:00
TypedQuery < File > q = em . createQuery ( " select f from File f where f.id = :id and f.deleteDate is null " , File . class ) ;
2015-03-03 00:23:30 +01:00
q . setParameter ( " id " , file . getId ( ) ) ;
2022-03-21 11:36:25 +01:00
File fileDb = 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 ( ) ) ;
2023-09-14 16:50:39 +02:00
fileDb . setSize ( file . getSize ( ) ) ;
2018-11-23 14:54:11 +01:00
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 ( ) ;
2022-03-21 11:36:25 +01:00
TypedQuery < File > q = em . createQuery ( " select f from File f where f.id = :id and f.deleteDate is null " , File . class ) ;
2016-03-24 00:41:31 +01:00
q . setParameter ( " id " , id ) ;
2013-07-27 18:33:20 +02:00
try {
2022-03-21 11:36:25 +01:00
return q . getSingleResult ( ) ;
2013-07-27 18:33:20 +02:00
} catch ( NoResultException e ) {
return null ;
}
}
/ * *
2022-03-20 11:36:28 +01:00
* Get files by document ID or all orphan files of a 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
* /
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 ) {
2022-03-20 11:36:28 +01:00
TypedQuery < File > 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 " , File . class ) ;
2015-03-06 22:40:33 +01:00
q . setParameter ( " userId " , userId ) ;
2015-03-06 21:23:50 +01:00
return q . getResultList ( ) ;
2022-03-20 11:36:28 +01:00
} else {
return getByDocumentsIds ( Collections . singleton ( documentId ) ) ;
2015-03-06 21:23:50 +01:00
}
2022-03-20 11:36:28 +01:00
}
/ * *
* Get files by documents IDs .
*
* @param documentIds Documents IDs
* @return List of files
* /
public List < File > getByDocumentsIds ( Iterable < String > documentIds ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
TypedQuery < File > q = em . createQuery ( " select f from File f where f.documentId in :documentIds and f.latestVersion = true and f.deleteDate is null order by f.order asc " , File . class ) ;
q . setParameter ( " documentIds " , documentIds ) ;
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
2023-10-08 22:07:01 +02:00
/ * *
* Get files count by documents IDs .
*
* @param documentIds Documents IDs
* @return the number of files per document id
* /
public Map < String , Long > countByDocumentsIds ( Iterable < String > documentIds ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
Query q = em . createQuery ( " select f.documentId, count(*) from File f where f.documentId in :documentIds and f.latestVersion = true and f.deleteDate is null group by (f.documentId) " ) ;
q . setParameter ( " documentIds " , documentIds ) ;
Map < String , Long > result = new HashMap < > ( ) ;
q . getResultList ( ) . forEach ( o - > {
Object [ ] resultLine = ( Object [ ] ) o ;
result . put ( ( String ) resultLine [ 0 ] , ( Long ) resultLine [ 1 ] ) ;
} ) ;
return result ;
}
2019-01-30 21:14:07 +01:00
/ * *
* Get all files from a version .
*
* @param versionId Version ID
* @return List of files
* /
public List < File > getByVersionId ( String versionId ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2022-03-21 11:36:25 +01:00
TypedQuery < File > q = em . createQuery ( " select f from File f where f.versionId = :versionId and f.deleteDate is null order by f.order asc " , File . class ) ;
2019-01-30 21:14:07 +01:00
q . setParameter ( " versionId " , versionId ) ;
return q . getResultList ( ) ;
}
2023-09-14 16:50:39 +02:00
public List < File > getFilesWithUnknownSize ( int limit ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
TypedQuery < File > q = em . createQuery ( " select f from File f where f.size = :size and f.deleteDate is null order by f.order asc " , File . class ) ;
q . setParameter ( " size " , File . UNKNOWN_SIZE ) ;
q . setMaxResults ( limit ) ;
return q . getResultList ( ) ;
}
2013-07-27 18:33:20 +02:00
}