3227 Sync reviewer changes to Daisy immediately

This commit is contained in:
Andreas Svanberg 2023-11-02 10:52:17 +01:00
parent 09318002a8
commit c91817c55a
2 changed files with 71 additions and 0 deletions
daisy-integration/src/main/java/se/su/dsv/scipro/integration/daisy

@ -29,6 +29,7 @@ public class DaisyModule extends ServletModule {
bind(UserImportWorker.class);
bind(ProjectExporter.class);
bind(DaisyWorkerInitialization.class).asEagerSingleton();
bind(SyncReviewerWithDaisy.class).asEagerSingleton();
Multibinder.newSetBinder(binder(), UserImportService.class)
.addBinding().to(ExternalImporterDaisyImpl.class);

@ -0,0 +1,70 @@
package se.su.dsv.scipro.integration.daisy;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import jakarta.ws.rs.core.Response;
import se.su.dsv.scipro.daisyExternal.http.DaisyAPI;
import se.su.dsv.scipro.io.dto.Person;
import se.su.dsv.scipro.io.dto.ProjectParticipant;
import se.su.dsv.scipro.io.dto.Role;
import se.su.dsv.scipro.project.Project;
import se.su.dsv.scipro.project.ReviewerAssignedEvent;
import se.su.dsv.scipro.system.User;
import javax.inject.Inject;
import java.util.Objects;
import java.util.Set;
class SyncReviewerWithDaisy {
private static final System.Logger LOGGER = System.getLogger(SyncReviewerWithDaisy.class.getName());
private final DaisyAPI daisyAPI;
@Inject
SyncReviewerWithDaisy(DaisyAPI daisyAPI, EventBus eventBus) {
eventBus.register(this);
this.daisyAPI = daisyAPI;
}
@Subscribe
public void reviewerAssigned(ReviewerAssignedEvent event) {
Project project = event.getProject();
User reviewer = event.getReviewer();
if (project.getIdentifier() == null || reviewer.getIdentifier() == null) {
return;
}
try {
Set<ProjectParticipant> contributors = daisyAPI.getContributors(project.getIdentifier());
for (ProjectParticipant contributor : contributors) {
if (Objects.equals(
contributor.getPerson().getId(),
reviewer.getIdentifier()) && Objects.equals(Role.REVIEWER, contributor.getRole()))
{
return;
}
}
for (ProjectParticipant contributor : contributors) {
if (Objects.equals(Role.REVIEWER, contributor.getRole())) {
daisyAPI.deleteThesisPerson(project.getIdentifier(), contributor.getPerson().getId());
}
}
Person person = new Person();
person.setId(reviewer.getIdentifier());
ProjectParticipant contributor = new ProjectParticipant();
contributor.setRole(Role.REVIEWER);
contributor.setPerson(person);
try (var response = daisyAPI.addContributor(project.getIdentifier(), contributor)) {
Response.Status.Family family = response.getStatusInfo().getFamily();
if (family == Response.Status.Family.CLIENT_ERROR) {
LOGGER.log(System.Logger.Level.ERROR, "Faulty reviewer sync: {0}", response.getHeaderString("X-Error-Message"));
}
}
}
catch (RuntimeException ignored) {
// ignore errors and let them be fixed later by the daily sync
}
}
}