#18: Group and user group DB model

This commit is contained in:
jendib 2016-03-16 22:14:25 +01:00
parent de3f055323
commit eb5f207cc1
9 changed files with 316 additions and 6 deletions

View File

@ -0,0 +1,123 @@
package com.sismics.docs.core.dao.jpa;
import java.util.Date;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import com.sismics.docs.core.constant.AuditLogType;
import com.sismics.docs.core.model.jpa.Group;
import com.sismics.docs.core.model.jpa.UserGroup;
import com.sismics.docs.core.util.AuditLogUtil;
import com.sismics.util.context.ThreadLocalContext;
/**
* Group DAO.
*
* @author bgamard
*/
public class GroupDao {
/**
* Gets a group by its ID.
*
* @param id Group ID
* @return Group
*/
public Group getById(String id) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
try {
return em.find(Group.class, id);
} catch (NoResultException e) {
return null;
}
}
/**
* Creates a new group.
*
* @param group Group
* @param userId User ID
* @return New ID
* @throws Exception
*/
public String create(Group group, String userId) {
// Create the UUID
group.setId(UUID.randomUUID().toString());
// Create the group
EntityManager em = ThreadLocalContext.get().getEntityManager();
em.persist(group);
// Create audit log
AuditLogUtil.create(group, AuditLogType.CREATE, userId);
return group.getId();
}
/**
* Deletes a group.
*
* @param groupId Group ID
* @param userId User ID
*/
public void delete(String groupId, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the group
Query q = em.createQuery("select g from Group g where g.id = :id and g.deleteDate is null");
q.setParameter("id", groupId);
Group groupDb = (Group) q.getSingleResult();
// Delete the group
Date dateNow = new Date();
groupDb.setDeleteDate(dateNow);
// Delete linked data
q = em.createQuery("update UserGroup ug set ug.deleteDate = :dateNow where ug.groupId = :groupId and ug.deleteDate is not null");
q.setParameter("dateNow", dateNow);
q.setParameter("groupId", groupId);
q.executeUpdate();
// Create audit log
AuditLogUtil.create(groupDb, AuditLogType.DELETE, userId);
}
/**
* Add an user to a group.
*
* @param group Group
* @return New ID
* @throws Exception
*/
public String addMember(UserGroup userGroup) {
// Create the UUID
userGroup.setId(UUID.randomUUID().toString());
// Create the user group
EntityManager em = ThreadLocalContext.get().getEntityManager();
em.persist(userGroup);
return userGroup.getId();
}
/**
* Remove an user from a group.
*
* @param groupId Group ID
*/
public void removeMember(String userGroupId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the user group
Query q = em.createQuery("select ug from UserGroup ug where ug.id = :id and ug.deleteDate is null");
q.setParameter("id", userGroupId);
UserGroup userGroupDb = (UserGroup) q.getSingleResult();
// Delete the user group
Date dateNow = new Date();
userGroupDb.setDeleteDate(dateNow);
}
}

View File

@ -33,13 +33,13 @@ public class DocumentTag implements Serializable {
/** /**
* Document ID. * Document ID.
*/ */
@Column(name = "DOT_IDDOCUMENT_C", length = 36) @Column(name = "DOT_IDDOCUMENT_C", nullable = false, length = 36)
private String documentId; private String documentId;
/** /**
* Tag ID. * Tag ID.
*/ */
@Column(name = "DOT_IDTAG_C", length = 36) @Column(name = "DOT_IDTAG_C", nullable = false, length = 36)
private String tagId; private String tagId;
/** /**
@ -83,6 +83,7 @@ public class DocumentTag implements Serializable {
@Override @Override
public String toString() { public String toString() {
return MoreObjects.toStringHelper(this) return MoreObjects.toStringHelper(this)
.add("id", id)
.add("documentId", documentId) .add("documentId", documentId)
.add("tagId", tagId) .add("tagId", tagId)
.toString(); .toString();

View File

@ -0,0 +1,91 @@
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.MoreObjects;
/**
* Group entity.
*
* @author bgamard
*/
@Entity
@Table(name = "T_GROUP")
public class Group implements Loggable {
/**
* Group ID.
*/
@Id
@Column(name = "GRP_ID_C", nullable = false, length = 36)
private String id;
/**
* Vocabulary value.
*/
@Column(name = "GRP_IDPARENT_C", length = 36)
private String parentId;
/**
* Group name.
*/
@Column(name = "GRP_NAME_C", nullable = false, length = 50)
private String name;
/**
* Deletion date.
*/
@Column(name = "GRP_DELETEDATE_D")
private Date deleteDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Date getDeleteDate() {
return deleteDate;
}
public void setDeleteDate(Date deleteDate) {
this.deleteDate = deleteDate;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("id", id)
.add("parentId", parentId)
.add("name", name)
.toString();
}
@Override
public String toMessage() {
return name;
}
}

View File

@ -0,0 +1,91 @@
package com.sismics.docs.core.model.jpa;
import java.io.Serializable;
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.MoreObjects;
/**
* Link between an user and a group.
*
* @author bgamard
*/
@Entity
@Table(name = "T_USER_GROUP")
public class UserGroup implements Serializable {
/**
* Serial version UID.
*/
private static final long serialVersionUID = 1L;
/**
* User group ID.
*/
@Id
@Column(name = "UGP_ID_C", length = 36)
private String id;
/**
* User ID.
*/
@Column(name = "UGP_IDUSER_C", nullable = false, length = 36)
private String userId;
/**
* Group ID.
*/
@Column(name = "UGP_IDGROUP_C", nullable = false, length = 36)
private String groupId;
/**
* Deletion date.
*/
@Column(name = "UGP_DELETEDATE_D")
private Date deleteDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public Date getDeleteDate() {
return deleteDate;
}
public void setDeleteDate(Date deleteDate) {
this.deleteDate = deleteDate;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("id", id)
.add("userId", userId)
.add("groupId", groupId)
.toString();
}
}

View File

@ -1 +1 @@
db.version=7 db.version=8

View File

@ -0,0 +1,4 @@
create memory table T_GROUP ( GRP_ID_C varchar(36) not null, GRP_IDPARENT_C varchar(36), GRP_NAME_C varchar(50) not null, GRP_DELETEDATE_D datetime, primary key (GRP_ID_C) );
create memory table T_USER_GROUP ( UGP_ID_C varchar(36) not null, UGP_IDUSER_C varchar(36) not null, UGP_IDGROUP_C varchar(36) not null, UGP_DELETEDATE_D datetime, primary key (UGP_ID_C) );
update T_CONFIG set CFG_VALUE_C = '8' where CFG_ID_C = 'DB_VERSION';

View File

@ -1,3 +1,3 @@
api.current_version=${project.version} api.current_version=${project.version}
api.min_version=1.0 api.min_version=1.0
db.version=7 db.version=8

View File

@ -1,3 +1,3 @@
api.current_version=${project.version} api.current_version=${project.version}
api.min_version=1.0 api.min_version=1.0
db.version=7 db.version=8

View File

@ -1,3 +1,3 @@
api.current_version=${project.version} api.current_version=${project.version}
api.min_version=1.0 api.min_version=1.0
db.version=7 db.version=8