use of de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent in project webanno by webanno.
the class WebhookService method onApplicationEvent.
@TransactionalEventListener(fallbackExecution = true)
@Async
public void onApplicationEvent(ApplicationEvent aEvent) {
String topic = EVENT_TOPICS.get(aEvent.getClass());
if (topic == null) {
return;
}
Object message;
switch(topic) {
case PROJECT_STATE:
message = new ProjectStateChangeMessage((ProjectStateChangedEvent) aEvent);
break;
case DOCUMENT_STATE:
message = new DocumentStateChangeMessage((DocumentStateChangedEvent) aEvent);
break;
case ANNOTATION_STATE:
message = new AnnotationStateChangeMessage((AnnotationStateChangeEvent) aEvent);
break;
default:
return;
}
for (Webhook hook : configuration.getGlobalHooks()) {
if (!hook.isEnabled() || !hook.getTopics().contains(topic)) {
continue;
}
try {
// Configure rest template without SSL certification check if that is disabled.
RestTemplate restTemplate;
if (hook.isVerifyCertificates()) {
restTemplate = restTemplateBuilder.build();
} else {
restTemplate = restTemplateBuilder.requestFactory(getNonValidatingRequestFactory()).build();
}
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
requestHeaders.set(X_AERO_NOTIFICATION, topic);
// If a secret is set, then add a digest header that allows the client to verify
// the message integrity
String json = JSONUtil.toJsonString(message);
if (isNotBlank(hook.getSecret())) {
String digest = DigestUtils.shaHex(hook.getSecret() + json);
requestHeaders.set(X_AERO_SIGNATURE, digest);
}
HttpEntity<?> httpEntity = new HttpEntity<Object>(json, requestHeaders);
restTemplate.postForEntity(hook.getUrl(), httpEntity, Void.class);
} catch (Exception e) {
log.error("Unable to invoke webhook [{}]", hook, e);
}
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent in project webanno by webanno.
the class WebhookServiceTest method test.
@Test
public void test() {
Webhook hook = new Webhook();
hook.setUrl("http://localhost:" + port + "/test/subscribe");
hook.setTopics(asList(PROJECT_STATE, ANNOTATION_STATE, DOCUMENT_STATE));
hook.setEnabled(true);
webhooksConfiguration.setGlobalHooks(asList(hook));
Project project = new Project();
project.setState(ProjectState.NEW);
project.setId(1l);
SourceDocument doc = new SourceDocument();
doc.setProject(project);
doc.setId(2l);
doc.setState(SourceDocumentState.ANNOTATION_IN_PROGRESS);
AnnotationDocument ann = new AnnotationDocument();
ann.setProject(project);
ann.setId(3l);
ann.setDocument(doc);
ann.setState(AnnotationDocumentState.FINISHED);
applicationEventPublisher.publishEvent(new ProjectStateChangedEvent(this, project, ProjectState.CURATION_FINISHED));
applicationEventPublisher.publishEvent(new DocumentStateChangedEvent(this, doc, SourceDocumentState.NEW));
applicationEventPublisher.publishEvent(new AnnotationStateChangeEvent(this, ann, AnnotationDocumentState.IN_PROGRESS));
assertEquals(1, testService.projectStateChangeMsgs.size());
assertEquals(1, testService.docStateChangeMsgs.size());
assertEquals(1, testService.annStateChangeMsgs.size());
}
use of de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent in project webanno by webanno.
the class DocumentServiceImpl method recalculateProjectState.
private void recalculateProjectState(Project aProject) {
Project project;
try {
project = projectService.getProject(aProject.getId());
} catch (NoResultException e) {
// updating its state. So then we do nothing here.
return;
}
String query = "SELECT new " + SourceDocumentStateStats.class.getName() + "(" + "COUNT(*), " + "SUM(CASE WHEN state = :an THEN 1 ELSE 0 END), " + "SUM(CASE WHEN (state = :aip OR state is NULL) THEN 1 ELSE 0 END), " + "SUM(CASE WHEN state = :af THEN 1 ELSE 0 END), " + "SUM(CASE WHEN state = :cip THEN 1 ELSE 0 END), " + "SUM(CASE WHEN state = :cf THEN 1 ELSE 0 END)) " + "FROM SourceDocument " + "WHERE project = :project";
SourceDocumentStateStats stats = entityManager.createQuery(query, SourceDocumentStateStats.class).setParameter("project", aProject).setParameter("an", SourceDocumentState.NEW).setParameter("aip", SourceDocumentState.ANNOTATION_IN_PROGRESS).setParameter("af", SourceDocumentState.ANNOTATION_FINISHED).setParameter("cip", SourceDocumentState.CURATION_IN_PROGRESS).setParameter("cf", SourceDocumentState.CURATION_FINISHED).getSingleResult();
ProjectState oldState = project.getState();
if (stats.total == stats.cf) {
project.setState(ProjectState.CURATION_FINISHED);
} else if (stats.total == stats.af) {
project.setState(ProjectState.ANNOTATION_FINISHED);
} else if (stats.total == stats.an) {
project.setState(ProjectState.NEW);
} else if (stats.cip > 0) {
project.setState(ProjectState.CURATION_IN_PROGRESS);
} else if (stats.aip > 0) {
project.setState(ProjectState.ANNOTATION_IN_PROGRESS);
} else {
throw new IllegalStateException("Unable to determine project state.");
}
if (!Objects.equals(oldState, project.getState())) {
applicationEventPublisher.publishEvent(new ProjectStateChangedEvent(this, project, oldState));
}
projectService.updateProject(project);
}
Aggregations