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 ;
2015-05-09 14:44:19 +02:00
import com.sismics.docs.core.constant.PermType ;
2018-03-29 17:59:47 +02:00
import com.sismics.docs.core.dao.dto.DocumentDto ;
2013-07-27 18:33:20 +02:00
import com.sismics.docs.core.model.jpa.Document ;
2015-05-17 22:20:34 +02:00
import com.sismics.docs.core.util.AuditLogUtil ;
2013-07-27 18:33:20 +02:00
import com.sismics.util.context.ThreadLocalContext ;
2023-04-09 21:31:53 +02:00
import jakarta.persistence.EntityManager ;
import jakarta.persistence.NoResultException ;
import jakarta.persistence.Query ;
import jakarta.persistence.TypedQuery ;
2017-11-21 09:49:33 +01:00
import java.sql.Timestamp ;
2018-03-29 17:59:47 +02:00
import java.util.Date ;
import java.util.List ;
import java.util.UUID ;
2017-11-21 09:49:33 +01:00
2013-07-27 18:33:20 +02:00
/ * *
* Document DAO .
*
* @author bgamard
* /
public class DocumentDao {
/ * *
* Creates a new document .
*
* @param document Document
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 ( Document document , String userId ) {
2013-07-27 18:33:20 +02:00
// Create the UUID
document . setId ( UUID . randomUUID ( ) . toString ( ) ) ;
2018-03-12 14:15:00 +01:00
document . setUpdateDate ( new Date ( ) ) ;
2013-07-27 18:33:20 +02:00
// Create the document
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
em . persist ( document ) ;
2015-05-17 22:20:34 +02:00
// Create audit log
2016-02-15 22:28:13 +01:00
AuditLogUtil . create ( document , AuditLogType . CREATE , userId ) ;
2015-05-17 22:20:34 +02:00
2013-07-27 18:33:20 +02:00
return document . getId ( ) ;
}
2013-08-17 14:16:55 +02:00
/ * *
2016-03-14 01:39:29 +01:00
* Returns the list of all active documents .
2018-10-29 18:32:39 +01:00
*
* @param offset Offset
* @param limit Limit
2013-08-17 14:16:55 +02:00
* @return List of documents
* /
2018-10-29 18:32:39 +01:00
public List < Document > findAll ( int offset , int limit ) {
2013-08-17 14:16:55 +02:00
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2022-04-15 10:18:39 +02:00
TypedQuery < Document > q = em . createQuery ( " select d from Document d where d.deleteDate is null " , Document . class ) ;
2018-10-29 18:32:39 +01:00
q . setFirstResult ( offset ) ;
q . setMaxResults ( limit ) ;
2013-08-17 14:16:55 +02:00
return q . getResultList ( ) ;
}
2018-10-29 18:32:39 +01:00
2015-12-01 00:32:57 +01:00
/ * *
2016-03-14 01:39:29 +01:00
* Returns the list of all active documents from a user .
2015-12-01 00:32:57 +01:00
*
* @param userId User ID
* @return List of documents
* /
public List < Document > findByUserId ( String userId ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2022-04-15 10:18:39 +02:00
TypedQuery < Document > q = em . createQuery ( " select d from Document d where d.userId = :userId and d.deleteDate is null " , Document . class ) ;
2015-12-01 00:32:57 +01:00
q . setParameter ( " userId " , userId ) ;
return q . getResultList ( ) ;
}
2013-08-14 20:51:08 +02:00
/ * *
2016-03-14 01:39:29 +01:00
* Returns an active document with permission checking .
2013-08-14 20:51:08 +02:00
*
* @param id Document ID
2016-03-14 01:39:29 +01:00
* @param perm Permission needed
2016-04-30 01:52:24 +02:00
* @param targetIdList List of targets
2013-08-14 20:51:08 +02:00
* @return Document
* /
2016-03-15 22:44:50 +01:00
public DocumentDto getDocument ( String id , PermType perm , List < String > targetIdList ) {
2016-05-01 22:03:39 +02:00
AclDao aclDao = new AclDao ( ) ;
if ( ! aclDao . checkPermission ( id , perm , targetIdList ) ) {
return null ;
}
2013-08-14 20:51:08 +02:00
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2023-10-19 18:34:04 +02:00
StringBuilder sb = new StringBuilder ( " select distinct d.DOC_ID_C, d.DOC_TITLE_C, d.DOC_DESCRIPTION_C, d.DOC_SUBJECT_C, d.DOC_IDENTIFIER_C, d.DOC_PUBLISHER_C, d.DOC_FORMAT_C, d.DOC_SOURCE_C, d.DOC_TYPE_C, d.DOC_COVERAGE_C, d.DOC_RIGHTS_C, d.DOC_CREATEDATE_D, d.DOC_UPDATEDATE_D, d.DOC_LANGUAGE_C, d.DOC_IDFILE_C, " ) ;
2018-03-21 18:58:50 +01:00
sb . append ( " (select count(s.SHA_ID_C) from T_SHARE s, T_ACL ac where ac.ACL_SOURCEID_C = d.DOC_ID_C and ac.ACL_TARGETID_C = s.SHA_ID_C and ac.ACL_DELETEDATE_D is null and s.SHA_DELETEDATE_D is null) shareCount, " ) ;
sb . append ( " (select count(f.FIL_ID_C) from T_FILE f where f.FIL_DELETEDATE_D is null and f.FIL_IDDOC_C = d.DOC_ID_C) fileCount, " ) ;
2015-05-10 13:45:39 +02:00
sb . append ( " u.USE_USERNAME_C " ) ;
2016-03-19 19:41:28 +01:00
sb . append ( " from T_DOCUMENT d " ) ;
sb . append ( " join T_USER u on d.DOC_IDUSER_C = u.USE_ID_C " ) ;
2016-05-01 22:03:39 +02:00
sb . append ( " where d.DOC_ID_C = :id and d.DOC_DELETEDATE_D is null " ) ;
2018-03-21 22:44:23 +01:00
2015-05-10 13:45:39 +02:00
Query q = em . createNativeQuery ( sb . toString ( ) ) ;
2013-08-14 20:51:08 +02:00
q . setParameter ( " id " , id ) ;
2016-05-01 22:03:39 +02:00
2016-04-30 01:52:24 +02:00
Object [ ] o ;
2015-11-16 02:22:51 +01:00
try {
o = ( Object [ ] ) q . getSingleResult ( ) ;
} catch ( NoResultException e ) {
return null ;
}
2015-05-10 13:45:39 +02:00
DocumentDto documentDto = new DocumentDto ( ) ;
int i = 0 ;
documentDto . setId ( ( String ) o [ i + + ] ) ;
documentDto . setTitle ( ( String ) o [ i + + ] ) ;
documentDto . setDescription ( ( String ) o [ i + + ] ) ;
2016-02-13 18:47:13 +01:00
documentDto . setSubject ( ( String ) o [ i + + ] ) ;
documentDto . setIdentifier ( ( String ) o [ i + + ] ) ;
2016-02-14 22:47:49 +01:00
documentDto . setPublisher ( ( String ) o [ i + + ] ) ;
documentDto . setFormat ( ( String ) o [ i + + ] ) ;
documentDto . setSource ( ( String ) o [ i + + ] ) ;
documentDto . setType ( ( String ) o [ i + + ] ) ;
documentDto . setCoverage ( ( String ) o [ i + + ] ) ;
documentDto . setRights ( ( String ) o [ i + + ] ) ;
2015-05-10 13:45:39 +02:00
documentDto . setCreateTimestamp ( ( ( Timestamp ) o [ i + + ] ) . getTime ( ) ) ;
2018-03-12 14:15:00 +01:00
documentDto . setUpdateTimestamp ( ( ( Timestamp ) o [ i + + ] ) . getTime ( ) ) ;
2015-05-10 13:45:39 +02:00
documentDto . setLanguage ( ( String ) o [ i + + ] ) ;
2023-10-19 18:34:04 +02:00
documentDto . setFileId ( ( String ) o [ i + + ] ) ;
2015-05-10 13:45:39 +02:00
documentDto . setShared ( ( ( Number ) o [ i + + ] ) . intValue ( ) > 0 ) ;
documentDto . setFileCount ( ( ( Number ) o [ i + + ] ) . intValue ( ) ) ;
2016-04-30 01:52:24 +02:00
documentDto . setCreator ( ( String ) o [ i ] ) ;
2015-05-10 13:45:39 +02:00
return documentDto ;
2013-08-14 20:51:08 +02:00
}
2013-07-27 18:33:20 +02:00
/ * *
* Deletes a document .
*
* @param id Document 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 ( ) ;
// Get the document
2022-04-15 10:18:39 +02:00
TypedQuery < Document > dq = em . createQuery ( " select d from Document d where d.id = :id and d.deleteDate is null " , Document . class ) ;
dq . setParameter ( " id " , id ) ;
Document documentDb = dq . getSingleResult ( ) ;
2013-07-27 18:33:20 +02:00
// Delete the document
Date dateNow = new Date ( ) ;
documentDb . setDeleteDate ( dateNow ) ;
// Delete linked data
2022-04-15 10:18:39 +02:00
Query q = em . createQuery ( " update File f set f.deleteDate = :dateNow where f.documentId = :documentId and f.deleteDate is null " ) ;
2013-08-14 20:51:08 +02:00
q . setParameter ( " documentId " , id ) ;
q . setParameter ( " dateNow " , dateNow ) ;
q . executeUpdate ( ) ;
2015-12-01 00:32:57 +01:00
q = em . createQuery ( " update Acl a set a.deleteDate = :dateNow where a.sourceId = :documentId and a.deleteDate is null " ) ;
q . setParameter ( " documentId " , id ) ;
q . setParameter ( " dateNow " , dateNow ) ;
q . executeUpdate ( ) ;
2015-05-09 14:44:19 +02:00
2015-12-01 00:32:57 +01:00
q = em . createQuery ( " update DocumentTag dt set dt.deleteDate = :dateNow where dt.documentId = :documentId and dt.deleteDate is not null " ) ;
2013-08-14 20:51:08 +02:00
q . setParameter ( " documentId " , id ) ;
2013-07-27 18:33:20 +02:00
q . setParameter ( " dateNow " , dateNow ) ;
q . executeUpdate ( ) ;
2015-05-17 22:20:34 +02:00
2016-03-06 21:06:23 +01:00
q = em . createQuery ( " update Relation r set r.deleteDate = :dateNow where (r.fromDocumentId = :documentId or r.toDocumentId = :documentId) and r.deleteDate is not null " ) ;
q . setParameter ( " documentId " , id ) ;
q . setParameter ( " dateNow " , dateNow ) ;
q . executeUpdate ( ) ;
2015-05-17 22:20:34 +02:00
// Create audit log
2016-02-15 22:28:13 +01:00
AuditLogUtil . create ( documentDb , AuditLogType . DELETE , userId ) ;
2013-07-27 18:33:20 +02:00
}
/ * *
2016-03-14 01:39:29 +01:00
* Gets an active document by its ID .
2013-07-27 18:33:20 +02:00
*
* @param id Document ID
* @return Document
* /
public Document getById ( String id ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2022-04-15 10:18:39 +02:00
TypedQuery < Document > q = em . createQuery ( " select d from Document d where d.id = :id and d.deleteDate is null " , Document . class ) ;
2016-03-14 01:39:29 +01:00
q . setParameter ( " id " , id ) ;
2013-07-27 18:33:20 +02:00
try {
2022-04-15 10:18:39 +02:00
return q . getSingleResult ( ) ;
2013-07-27 18:33:20 +02:00
} catch ( NoResultException e ) {
return null ;
}
}
2015-05-17 22:20:34 +02:00
/ * *
2018-11-01 16:27:35 +01:00
* Update a document and log the action .
2015-05-17 22:20:34 +02:00
*
* @param document Document to update
2016-02-15 22:28:13 +01:00
* @param userId User ID
2015-05-17 22:20:34 +02:00
* @return Updated document
* /
2016-02-15 22:28:13 +01:00
public Document update ( Document document , String userId ) {
2018-11-01 16:27:35 +01:00
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2015-05-17 22:20:34 +02:00
// Get the document
2022-04-15 10:18:39 +02:00
TypedQuery < Document > q = em . createQuery ( " select d from Document d where d.id = :id and d.deleteDate is null " , Document . class ) ;
2015-05-17 22:20:34 +02:00
q . setParameter ( " id " , document . getId ( ) ) ;
2022-04-15 10:18:39 +02:00
Document documentDb = q . getSingleResult ( ) ;
2018-11-01 16:27:35 +01:00
2015-05-17 22:20:34 +02:00
// Update the document
2018-01-28 12:44:11 +01:00
documentDb . setTitle ( document . getTitle ( ) ) ;
documentDb . setDescription ( document . getDescription ( ) ) ;
documentDb . setSubject ( document . getSubject ( ) ) ;
documentDb . setIdentifier ( document . getIdentifier ( ) ) ;
documentDb . setPublisher ( document . getPublisher ( ) ) ;
documentDb . setFormat ( document . getFormat ( ) ) ;
documentDb . setSource ( document . getSource ( ) ) ;
documentDb . setType ( document . getType ( ) ) ;
documentDb . setCoverage ( document . getCoverage ( ) ) ;
documentDb . setRights ( document . getRights ( ) ) ;
documentDb . setCreateDate ( document . getCreateDate ( ) ) ;
documentDb . setLanguage ( document . getLanguage ( ) ) ;
2018-11-01 16:27:35 +01:00
documentDb . setFileId ( document . getFileId ( ) ) ;
2018-03-12 14:15:00 +01:00
documentDb . setUpdateDate ( new Date ( ) ) ;
2019-02-07 14:50:47 +01:00
// Create audit log
AuditLogUtil . create ( documentDb , AuditLogType . UPDATE , userId ) ;
2018-01-28 12:44:11 +01:00
return documentDb ;
2015-05-17 22:20:34 +02:00
}
2017-11-21 09:49:33 +01:00
2019-02-07 14:50:47 +01:00
/ * *
* Update the file ID on a document .
*
* @param document Document
* /
public void updateFileId ( Document document ) {
2019-02-07 15:17:38 +01:00
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
2019-12-28 13:53:42 +01:00
Query query = em . createNativeQuery ( " update T_DOCUMENT d set DOC_IDFILE_C = :fileId, DOC_UPDATEDATE_D = :updateDate where d.DOC_ID_C = :id " ) ;
2019-02-07 16:55:58 +01:00
query . setParameter ( " updateDate " , new Date ( ) ) ;
2019-02-07 14:50:47 +01:00
query . setParameter ( " fileId " , document . getFileId ( ) ) ;
query . setParameter ( " id " , document . getId ( ) ) ;
2019-02-07 15:17:38 +01:00
query . executeUpdate ( ) ;
2019-02-07 14:50:47 +01:00
}
2017-11-21 09:49:33 +01:00
/ * *
* Returns the number of documents .
*
* @return Number of documents
* /
public long getDocumentCount ( ) {
EntityManager em = ThreadLocalContext . get ( ) . getEntityManager ( ) ;
Query query = em . createNativeQuery ( " select count(d.DOC_ID_C) from T_DOCUMENT d where d.DOC_DELETEDATE_D is null " ) ;
return ( ( Number ) query . getSingleResult ( ) ) . longValue ( ) ;
}
2013-07-27 18:33:20 +02:00
}