/** * Deletes a tag. * * @param tagId Tag ID * @param userId User ID */ public void delete(String tagId, String userId) { 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( "update DocumentTag dt set dt.deleteDate = :dateNow where dt.tagId = :tagId and dt.deleteDate is not null"); q.setParameter("dateNow", dateNow); q.setParameter("tagId", tagId); q.executeUpdate(); q = em.createQuery( "update Acl a set a.deleteDate = :dateNow where a.sourceId = :tagId and a.deleteDate is null"); q.setParameter("tagId", tagId); q.setParameter("dateNow", dateNow); q.executeUpdate(); // Create audit log AuditLogUtil.create(tagDb, AuditLogType.DELETE, userId); }
/** * 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; } }
/** * Creates a new tag. * * @param tag Tag * @param userId User ID * @return New ID */ public String create(Tag tag, String userId) { // Create the UUID tag.setId(UUID.randomUUID().toString()); // Create the tag EntityManager em = ThreadLocalContext.get().getEntityManager(); tag.setCreateDate(new Date()); em.persist(tag); // Create audit log AuditLogUtil.create(tag, AuditLogType.CREATE, userId); return tag.getId(); }
/** * Update a tag. * * @param tag Tag to update * @param userId User ID * @return Updated tag */ public Tag update(Tag tag, String userId) { 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", tag.getId()); Tag tagFromDb = (Tag) q.getSingleResult(); // Update the tag tagFromDb.setName(tag.getName()); tagFromDb.setColor(tag.getColor()); tagFromDb.setParentId(tag.getParentId()); // Create audit log AuditLogUtil.create(tagFromDb, AuditLogType.UPDATE, userId); return tagFromDb; }
/** * Update tags on a document. * * @param documentId Document ID * @param tagIdSet Set of tag ID */ public void updateTagList(String documentId, Set<String> tagIdSet) { EntityManager em = ThreadLocalContext.get().getEntityManager(); // Get current tag links Query q = em.createQuery( "select dt from DocumentTag dt where dt.documentId = :documentId and dt.deleteDate is null"); q.setParameter("documentId", documentId); @SuppressWarnings("unchecked") List<DocumentTag> documentTagList = q.getResultList(); // Deleting tags no longer linked for (DocumentTag documentTag : documentTagList) { if (!tagIdSet.contains(documentTag.getTagId())) { documentTag.setDeleteDate(new Date()); } } // Adding new tag links for (String tagId : tagIdSet) { boolean found = false; for (DocumentTag documentTag : documentTagList) { if (documentTag.getTagId().equals(tagId)) { found = true; break; } } if (!found) { DocumentTag documentTag = new DocumentTag(); documentTag.setId(UUID.randomUUID().toString()); documentTag.setDocumentId(documentId); documentTag.setTagId(tagId); em.persist(documentTag); } } }