1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-13 17:56:20 +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();
}
}

View File

@@ -13,5 +13,7 @@
<class>com.sismics.docs.core.model.jpa.User</class>
<class>com.sismics.docs.core.model.jpa.RoleBaseFunction</class>
<class>com.sismics.docs.core.model.jpa.Document</class>
<class>com.sismics.docs.core.model.jpa.Tag</class>
<class>com.sismics.docs.core.model.jpa.DocumentTag</class>
</persistence-unit>
</persistence>

View File

@@ -8,11 +8,16 @@ create cached table T_DOCUMENT ( DOC_ID_C varchar(36) not null, DOC_IDUSER_C var
create memory table T_USER ( USE_ID_C varchar(36) not null, USE_IDLOCALE_C varchar(10) not null, USE_IDROLE_C varchar(36) not null, USE_USERNAME_C varchar(50) not null, USE_PASSWORD_C varchar(60) not null, USE_EMAIL_C varchar(100) not null, USE_THEME_C varchar(100) not null, USE_FIRSTCONNECTION_B bit not null, USE_CREATEDATE_D datetime not null, USE_DELETEDATE_D datetime, primary key (USE_ID_C) );
create memory table T_ROLE ( ROL_ID_C varchar(36) not null, ROL_NAME_C varchar(36) not null, ROL_CREATEDATE_D datetime not null, ROL_DELETEDATE_D datetime, primary key (ROL_ID_C) );
create memory table T_ROLE_BASE_FUNCTION ( RBF_ID_C varchar(36) not null, RBF_IDROLE_C varchar(36) not null, RBF_IDBASEFUNCTION_C varchar(20) not null, RBF_CREATEDATE_D datetime not null, RBF_DELETEDATE_D datetime, primary key (RBF_ID_C) );
create cached table T_TAG ( TAG_ID_C varchar(36) not null, TAG_IDUSER_C varchar(36) not null, TAG_NAME_C varchar(36) not null, TAG_CREATEDATE_D datetime, TAG_DELETEDATE_D datetime, primary key (TAG_ID_C) );
create cached table T_DOCUMENT_TAG ( DOT_ID_C varchar(36) not null, DOT_IDDOCUMENT_C varchar(36) not null, DOT_IDTAG_C varchar(36) not null, primary key (DOT_ID_C) );
alter table T_AUTHENTICATION_TOKEN add constraint FK_AUT_IDUSER_C foreign key (AUT_IDUSER_C) references T_USER (USE_ID_C) on delete restrict on update restrict;
alter table T_DOCUMENT add constraint FK_DOC_IDUSER_C foreign key (DOC_IDUSER_C) references T_USER (USE_ID_C) on delete restrict on update restrict;
alter table T_FILE add constraint FK_FIL_IDDOC_C foreign key (FIL_IDDOC_C) references T_DOCUMENT (DOC_ID_C) on delete restrict on update restrict;
alter table T_USER add constraint FK_USE_IDLOCALE_C foreign key (USE_IDLOCALE_C) references T_LOCALE (LOC_ID_C) on delete restrict on update restrict;
alter table T_USER add constraint FK_USE_IDROLE_C foreign key (USE_IDROLE_C) references T_ROLE (ROL_ID_C) on delete restrict on update restrict;
alter table T_TAG add constraint FK_TAG_IDUSER_C foreign key (TAG_IDUSER_C) references T_USER (USE_ID_C) on delete restrict on update restrict;
alter table T_DOCUMENT_TAG add constraint FK_DOT_IDDOCUMENT_C foreign key (DOT_IDDOCUMENT_C) references T_DOCUMENT (DOC_ID_C) on delete restrict on update restrict;
alter table T_DOCUMENT_TAG add constraint FK_DOT_IDTAG_C foreign key (DOT_IDTAG_C) references T_TAG (TAG_ID_C) on delete restrict on update restrict;
alter table T_ROLE_BASE_FUNCTION add constraint FK_RBF_IDROLE_C foreign key (RBF_IDROLE_C) references T_ROLE (ROL_ID_C) on delete restrict on update restrict;
alter table T_ROLE_BASE_FUNCTION add constraint FK_RBF_IDBASEFUNCTION_C foreign key (RBF_IDBASEFUNCTION_C) references T_BASE_FUNCTION (BAF_ID_C) on delete restrict on update restrict;
insert into T_CONFIG(CFG_ID_C, CFG_VALUE_C) values('DB_VERSION', '0');