1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-20 13:11:41 +00:00

Server side tag system

This commit is contained in:
jendib
2013-07-29 22:55:32 +02:00
parent 74132103bb
commit 62b6512996
14 changed files with 808 additions and 6 deletions

View File

@@ -127,6 +127,8 @@ public class DocumentDao {
parameterMap.put("search", "%" + criteria.getSearch() + "%");
}
criteriaList.add("d.DOC_DELETEDATE_D is null");
if (!criteriaList.isEmpty()) {
sb.append(" where ");
sb.append(Joiner.on(" and ").join(criteriaList));

View File

@@ -0,0 +1,182 @@
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.NoResultException;
import javax.persistence.Query;
import com.sismics.docs.core.dao.jpa.dto.TagDto;
import com.sismics.docs.core.model.jpa.DocumentTag;
import com.sismics.docs.core.model.jpa.Tag;
import com.sismics.util.context.ThreadLocalContext;
/**
* Tag DAO.
*
* @author bgamard
*/
public class TagDao {
/**
* Gets a tag by its ID.
*
* @param id Tag ID
* @return Tag
*/
public Tag getById(String id) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
try {
return em.find(Tag.class, id);
} catch (NoResultException e) {
return null;
}
}
/**
* Returns the list of all tags.
*
* @return List of tags
*/
@SuppressWarnings("unchecked")
public List<Tag> getByUserId(String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select t from Tag t where t.userId = :userId and t.deleteDate is null order by t.id");
q.setParameter("userId", userId);
return q.getResultList();
}
/**
* Update tags on a document.
*
* @param documentId
* @param tagIdSet
*/
public void updateTagList(String documentId, Set<String> tagIdSet) {
// Delete old tag links
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("delete DocumentTag dt where dt.documentId = :documentId");
q.setParameter("documentId", documentId);
q.executeUpdate();
// Create new tag links
for (String tagId : tagIdSet) {
DocumentTag documentTag = new DocumentTag();
documentTag.setId(UUID.randomUUID().toString());
documentTag.setDocumentId(documentId);
documentTag.setTagId(tagId);
em.persist(documentTag);
}
}
/**
* Returns tag list on a document.
* @param documentId
* @return
*/
@SuppressWarnings("unchecked")
public List<TagDto> getByDocumentId(String documentId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
StringBuilder sb = new StringBuilder("select t.TAG_ID_C, t.TAG_NAME_C from T_DOCUMENT_TAG dt ");
sb.append(" join T_TAG t on t.TAG_ID_C = dt.DOT_IDTAG_C ");
sb.append(" where dt.DOT_IDDOCUMENT_C = :documentId and t.TAG_DELETEDATE_D is null ");
// Perform the query
Query q = em.createNativeQuery(sb.toString());
q.setParameter("documentId", documentId);
List<Object[]> l = q.getResultList();
// Assemble results
List<TagDto> tagDtoList = new ArrayList<TagDto>();
for (Object[] o : l) {
int i = 0;
TagDto tagDto = new TagDto();
tagDto.setId((String) o[i++]);
tagDto.setName((String) o[i++]);
tagDtoList.add(tagDto);
}
return tagDtoList;
}
/**
* Creates a new tag.
*
* @param tag Tag
* @return New ID
* @throws Exception
*/
public String create(Tag tag) {
// Create the UUID
tag.setId(UUID.randomUUID().toString());
// Create the tag
EntityManager em = ThreadLocalContext.get().getEntityManager();
tag.setCreateDate(new Date());
em.persist(tag);
return tag.getId();
}
/**
* Returns a tag by user ID and name.
* @param userId User ID
* @param name Name
* @return Tag
*/
public Tag getByUserIdAndName(String userId, String name) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select t from Tag t where t.name = :name and t.userId = :userId and t.deleteDate is null");
q.setParameter("userId", userId);
q.setParameter("name", name);
try {
return (Tag) q.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
/**
* Returns a tag by user ID and name.
* @param userId User ID
* @param tagId Tag ID
* @return Tag
*/
public Tag getByUserIdAndTagId(String userId, String tagId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select t from Tag t where t.id = :tagId and t.userId = :userId and t.deleteDate is null");
q.setParameter("userId", userId);
q.setParameter("tagId", tagId);
try {
return (Tag) q.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
/**
* Deletes a tag.
*
* @param tagId Tag ID
*/
public void delete(String tagId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the tag
Query q = em.createQuery("select t from Tag t where t.id = :id and t.deleteDate is null");
q.setParameter("id", tagId);
Tag tagDb = (Tag) q.getSingleResult();
// Delete the tag
Date dateNow = new Date();
tagDb.setDeleteDate(dateNow);
// Delete linked data
q = em.createQuery("delete DocumentTag dt where dt.tagId = :tagId");
q.setParameter("tagId", tagId);
q.executeUpdate();
}
}

View File

@@ -5,7 +5,7 @@ import javax.persistence.Id;
/**
* Document DTO.
*
* @author jtremeaux
* @author bgamard
*/
public class DocumentDto {
/**

View File

@@ -0,0 +1,57 @@
package com.sismics.docs.core.dao.jpa.dto;
import javax.persistence.Id;
/**
* Tag DTO.
*
* @author bgamard
*/
public class TagDto {
/**
* Tag ID.
*/
@Id
private String id;
/**
* Name.
*/
private String name;
/**
* 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;
}
}

View File

@@ -0,0 +1,107 @@
package com.sismics.docs.core.model.jpa;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import com.google.common.base.Objects;
/**
* Link between a document and a tag.
*
* @author bgamard
*/
@Entity
@Table(name = "T_DOCUMENT_TAG")
public class DocumentTag implements Serializable {
/**
* Serial version UID.
*/
private static final long serialVersionUID = 1L;
/**
* Document tag ID.
*/
@Id
@Column(name = "DOT_ID_C", length = 36)
private String id;
/**
* Document ID.
*/
@Id
@Column(name = "DOT_IDDOCUMENT_C", length = 36)
private String documentId;
/**
* Tag ID.
*/
@Id
@Column(name = "DOT_IDTAG_C", length = 36)
private String tagId;
/**
* Getter of id.
*
* @return id
*/
public String getId() {
return id;
}
/**
* Setter of id.
*
* @param id id
*/
public void setId(String id) {
this.id = id;
}
/**
* Getter de documentId.
*
* @return the documentId
*/
public String getDocumentId() {
return documentId;
}
/**
* Setter de documentId.
*
* @param documentId documentId
*/
public void setDocumentId(String documentId) {
this.documentId = documentId;
}
/**
* Getter de tagId.
*
* @return the tagId
*/
public String getTagId() {
return tagId;
}
/**
* Setter de tagId.
*
* @param tagId tagId
*/
public void setTagId(String tagId) {
this.tagId = tagId;
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("documentId", documentId)
.add("tagId", tagId)
.toString();
}
}

View File

@@ -0,0 +1,148 @@
package com.sismics.docs.core.model.jpa;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import com.google.common.base.Objects;
/**
* Tag.
*
* @author bgamard
*/
@Entity
@Table(name = "T_TAG")
public class Tag {
/**
* Tag ID.
*/
@Id
@Column(name = "TAG_ID_C", length = 36)
private String id;
/**
* Tag name.
*/
@Column(name = "TAG_NAME_C", nullable = false, length = 36)
private String name;
/**
* User ID.
*/
@Column(name = "TAG_IDUSER_C", nullable = false, length = 36)
private String userId;
/**
* Creation date.
*/
@Column(name = "TAG_CREATEDATE_D", nullable = false)
private Date createDate;
/**
* Deletion date.
*/
@Column(name = "TAG_DELETEDATE_D")
private Date deleteDate;
/**
* Getter of id.
*
* @return id
*/
public String getId() {
return id;
}
/**
* Setter of id.
*
* @param id id
*/
public void setId(String id) {
this.id = id;
}
/**
* Getter of userId.
*
* @return the userId
*/
public String getUserId() {
return userId;
}
/**
* Setter of userId.
*
* @param userId userId
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
* Getter of name.
*
* @return name
*/
public String getName() {
return name;
}
/**
* Setter of name.
*
* @param name name
*/
public void setName(String name) {
this.name = name;
}
/**
* Getter of createDate.
*
* @return createDate
*/
public Date getCreateDate() {
return createDate;
}
/**
* Setter of createDate.
*
* @param createDate createDate
*/
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
/**
* Getter of deleteDate.
*
* @return deleteDate
*/
public Date getDeleteDate() {
return deleteDate;
}
/**
* Setter of deleteDate.
*
* @param deleteDate deleteDate
*/
public void setDeleteDate(Date deleteDate) {
this.deleteDate = deleteDate;
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.add("name", name)
.toString();
}
}