1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-13 09:46:17 +00:00

#67: relations between documents (server-side)

This commit is contained in:
jendib
2016-03-06 21:06:23 +01:00
parent ca8c525de0
commit 0525754337
6 changed files with 237 additions and 56 deletions

View File

@@ -184,6 +184,11 @@ public class DocumentDao {
q.setParameter("dateNow", dateNow);
q.executeUpdate();
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();
// Create audit log
AuditLogUtil.create(documentDb, AuditLogType.DELETE, userId);
}

View File

@@ -0,0 +1,97 @@
package com.sismics.docs.core.dao.jpa;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.sismics.docs.core.dao.jpa.dto.RelationDto;
import com.sismics.docs.core.model.jpa.Relation;
import com.sismics.util.context.ThreadLocalContext;
/**
* Relation DAO.
*
* @author bgamard
*/
public class RelationDao {
/**
* Get all relations from/to a document.
*
* @param documentId Document ID
* @return List of relations
*/
@SuppressWarnings("unchecked")
public List<RelationDto> getByDocumentId(String documentId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
StringBuilder sb = new StringBuilder("select d.DOC_ID_C, d.DOC_TITLE_C ");
sb.append(" from T_RELATION r ");
sb.append(" join T_DOCUMENT d on d.DOC_ID_C = r.REL_IDDOCFROM_C and r.REL_IDDOCFROM_C != :documentId or d.DOC_ID_C = r.REL_IDDOCTO_C and r.REL_IDDOCTO_C != :documentId ");
sb.append(" where (r.REL_IDDOCFROM_C = :documentId or r.REL_IDDOCTO_C = :documentId) ");
sb.append(" and r.REL_DELETEDATE_D is null ");
sb.append(" order by d.DOC_TITLE_C ");
// Perform the query
Query q = em.createNativeQuery(sb.toString());
q.setParameter("documentId", documentId);
List<Object[]> l = q.getResultList();
// Assemble results
List<RelationDto> relationDtoList = new ArrayList<RelationDto>();
for (Object[] o : l) {
int i = 0;
RelationDto relationDto = new RelationDto();
relationDto.setId((String) o[i++]);
relationDto.setTitle((String) o[i++]);
relationDtoList.add(relationDto);
}
return relationDtoList;
}
/**
* Update relations on a document.
*
* @param documentId Document ID
* @param documentIdSet Set of document ID
*/
public void updateRelationList(String documentId, Set<String> documentIdSet) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get current relations from this document
Query q = em.createQuery("select r from Relation r where r.fromDocumentId = :documentId and r.deleteDate is null");
q.setParameter("documentId", documentId);
@SuppressWarnings("unchecked")
List<Relation> relationList = q.getResultList();
// Deleting relations no longer there
for (Relation relation : relationList) {
if (!documentIdSet.contains(relation.getToDocumentId())) {
relation.setDeleteDate(new Date());
}
}
// Adding new relations
for (String targetDocId : documentIdSet) {
boolean found = false;
for (Relation relation : relationList) {
if (relation.getToDocumentId().equals(targetDocId)) {
found = true;
break;
}
}
if (!found) {
Relation relation = new Relation();
relation.setId(UUID.randomUUID().toString());
relation.setFromDocumentId(documentId);
relation.setToDocumentId(targetDocId);
em.persist(relation);
}
}
}
}

View File

@@ -0,0 +1,34 @@
package com.sismics.docs.core.dao.jpa.dto;
/**
* Tag DTO.
*
* @author bgamard
*/
public class RelationDto {
/**
* Document ID.
*/
private String id;
/**
* Document title.
*/
private String title;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@@ -26,74 +26,34 @@ public class TagDto {
*/
private String parentId;
/**
* Getter of id.
*
* @return the id
*/
public String getId() {
return id;
}
/**
* Setter of id.
*
* @param id id
*/
public void setId(String id) {
this.id = id;
}
/**
* Getter of name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Setter of name.
*
* @param name name
*/
public void setName(String name) {
this.name = name;
}
/**
* Getter of color.
*
* @return the color
*/
public String getColor() {
return color;
}
/**
* Setter of color.
*
* @param color color
*/
public void setColor(String color) {
this.color = color;
}
/**
* Getter of parentId.
*
* @return the parentId
*/
public String getParentId() {
return parentId;
}
/**
* Setter of parentId.
*
* @param color parentId
*/
public void setParentId(String parentId) {
this.parentId = parentId;
}