Search in sources :

Example 1 with ProjectState

use of de.tudarmstadt.ukp.clarin.webanno.model.ProjectState 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);
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ProjectState(de.tudarmstadt.ukp.clarin.webanno.model.ProjectState) NoResultException(javax.persistence.NoResultException) ProjectStateChangedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent)

Aggregations

ProjectStateChangedEvent (de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectStateChangedEvent)1 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)1 ProjectState (de.tudarmstadt.ukp.clarin.webanno.model.ProjectState)1 NoResultException (javax.persistence.NoResultException)1