1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-14 10:16:21 +00:00

#243: webhook CRUD

This commit is contained in:
Benjamin Gamard
2018-10-16 19:04:04 +02:00
parent b5f0612e78
commit dfdd5f8d20
14 changed files with 547 additions and 6 deletions

View File

@@ -0,0 +1,15 @@
package com.sismics.docs.core.constant;
/**
* Webhook events.
*
* @author bgamard
*/
public enum WebhookEvent {
DOCUMENT_CREATED,
DOCUMENT_UPDATED,
DOCUMENT_DELETED,
FILE_CREATED,
FILE_UPDATED,
FILE_DELETED
}

View File

@@ -0,0 +1,119 @@
package com.sismics.docs.core.dao;
import com.google.common.base.Joiner;
import com.sismics.docs.core.dao.criteria.WebhookCriteria;
import com.sismics.docs.core.dao.dto.WebhookDto;
import com.sismics.docs.core.model.jpa.Webhook;
import com.sismics.docs.core.util.jpa.QueryParam;
import com.sismics.docs.core.util.jpa.QueryUtil;
import com.sismics.docs.core.util.jpa.SortCriteria;
import com.sismics.util.context.ThreadLocalContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.sql.Timestamp;
import java.util.*;
/**
* Webhook DAO.
*
* @author bgamard
*/
public class WebhookDao {
/**
* Returns the list of all webhooks.
*
* @param criteria Search criteria
* @param sortCriteria Sort criteria
* @return List of webhooks
*/
public List<WebhookDto> findByCriteria(WebhookCriteria criteria, SortCriteria sortCriteria) {
Map<String, Object> parameterMap = new HashMap<>();
List<String> criteriaList = new ArrayList<>();
StringBuilder sb = new StringBuilder("select w.WHK_ID_C as c0, w.WHK_EVENT_C as c1, w.WHK_URL_C as c2, w.WHK_CREATEDATE_D as c3 ");
sb.append(" from T_WEBHOOK w ");
// Add search criterias
criteriaList.add("w.WHK_DELETEDATE_D is null");
if (!criteriaList.isEmpty()) {
sb.append(" where ");
sb.append(Joiner.on(" and ").join(criteriaList));
}
// Perform the search
QueryParam queryParam = QueryUtil.getSortedQueryParam(new QueryParam(sb.toString(), parameterMap), sortCriteria);
@SuppressWarnings("unchecked")
List<Object[]> l = QueryUtil.getNativeQuery(queryParam).getResultList();
// Assemble results
List<WebhookDto> webhookDtoList = new ArrayList<>();
for (Object[] o : l) {
int i = 0;
WebhookDto webhookDto = new WebhookDto()
.setId((String) o[i++])
.setEvent((String) o[i++])
.setUrl((String) o[i++])
.setCreateTimestamp(((Timestamp) o[i]).getTime());
webhookDtoList.add(webhookDto);
}
return webhookDtoList;
}
/**
* Returns a webhook by ID.
*
* @param id Webhook ID
* @return Webhook
*/
public Webhook getActiveById(String id) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select w from Webhook w where w.id = :id and w.deleteDate is null");
q.setParameter("id", id);
try {
return (Webhook) q.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
/**
* Creates a new webhook.
*
* @param webhook Webhook
* @return New ID
*/
public String create(Webhook webhook) {
// Create the UUID
webhook.setId(UUID.randomUUID().toString());
// Create the webhook
EntityManager em = ThreadLocalContext.get().getEntityManager();
webhook.setCreateDate(new Date());
em.persist(webhook);
return webhook.getId();
}
/**
* Deletes a webhook.
*
* @param webhookId Webhook ID
*/
public void delete(String webhookId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the group
Query q = em.createQuery("select w from Webhook w where w.id = :id and w.deleteDate is null");
q.setParameter("id", webhookId);
Webhook webhookDb = (Webhook) q.getSingleResult();
// Delete the group
Date dateNow = new Date();
webhookDb.setDeleteDate(dateNow);
}
}

View File

@@ -0,0 +1,9 @@
package com.sismics.docs.core.dao.criteria;
/**
* Webhook criteria.
*
* @author bgamard
*/
public class WebhookCriteria {
}

View File

@@ -0,0 +1,74 @@
package com.sismics.docs.core.dao.dto;
/**
* Webhook DTO.
*
* @author bgamard
*/
public class WebhookDto {
/**
* Webhook ID.
*/
private String id;
/**
* Event.
*/
private String event;
/**
* URL.
*/
private String url;
/**
* Creation date.
*/
private Long createTimestamp;
public String getId() {
return id;
}
public WebhookDto setId(String id) {
this.id = id;
return this;
}
public String getEvent() {
return event;
}
public WebhookDto setEvent(String event) {
this.event = event;
return this;
}
public String getUrl() {
return url;
}
public WebhookDto setUrl(String url) {
this.url = url;
return this;
}
public Long getCreateTimestamp() {
return createTimestamp;
}
public WebhookDto setCreateTimestamp(Long createTimestamp) {
this.createTimestamp = createTimestamp;
return this;
}
@Override
public boolean equals(Object obj) {
return id.equals(((WebhookDto) obj).getId());
}
@Override
public int hashCode() {
return id.hashCode();
}
}

View File

@@ -69,6 +69,7 @@ public class FileProcessingAsyncListener {
* @param event File updated event
*/
@Subscribe
@AllowConcurrentEvents
public void on(final FileUpdatedAsyncEvent event) {
if (log.isInfoEnabled()) {
log.info("File updated event: " + event.toString());

View File

@@ -0,0 +1,108 @@
package com.sismics.docs.core.model.jpa;
import com.google.common.base.MoreObjects;
import com.sismics.docs.core.constant.WebhookEvent;
import javax.persistence.*;
import java.util.Date;
/**
* Webhook entity.
*
* @author bgamard
*/
@Entity
@Table(name = "T_WEBHOOK")
public class Webhook implements Loggable {
/**
* Webhook ID.
*/
@Id
@Column(name = "WHK_ID_C", nullable = false, length = 36)
private String id;
/**
* Event.
*/
@Column(name = "WHK_EVENT_C", nullable = false, length = 50)
@Enumerated(EnumType.STRING)
private WebhookEvent event;
/**
* URL.
*/
@Column(name = "WHK_URL_C", nullable = false, length = 1024)
private String url;
/**
* Creation date.
*/
@Column(name = "WHK_CREATEDATE_D", nullable = false)
private Date createDate;
/**
* Deletion date.
*/
@Column(name = "WHK_DELETEDATE_D")
private Date deleteDate;
public String getId() {
return id;
}
public Webhook setId(String id) {
this.id = id;
return this;
}
public WebhookEvent getEvent() {
return event;
}
public Webhook setEvent(WebhookEvent event) {
this.event = event;
return this;
}
public String getUrl() {
return url;
}
public Webhook setUrl(String url) {
this.url = url;
return this;
}
public Date getCreateDate() {
return createDate;
}
public Webhook setCreateDate(Date createDate) {
this.createDate = createDate;
return this;
}
@Override
public Date getDeleteDate() {
return deleteDate;
}
public Webhook setDeleteDate(Date deleteDate) {
this.deleteDate = deleteDate;
return this;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("id", id)
.add("event", event)
.add("url", url)
.toString();
}
@Override
public String toMessage() {
return url;
}
}

View File

@@ -1 +1 @@
db.version=19
db.version=20

View File

@@ -0,0 +1,3 @@
create table T_WEBHOOK ( WHK_ID_C varchar(36) not null, WHK_EVENT_C varchar(50) not null, WHK_URL_C varchar(1024) not null, WHK_CREATEDATE_D datetime not null, WHK_DELETEDATE_D datetime, primary key (WHK_ID_C) );
update T_CONFIG set CFG_VALUE_C = '20' where CFG_ID_C = 'DB_VERSION';