2515 Sync theses' research area to Daisy

This commit is contained in:
Andreas Svanberg 2022-05-09 13:49:25 +02:00
parent f5535c36ef
commit ddc28cc598
7 changed files with 61 additions and 42 deletions
core/src/main/java/se/su/dsv/scipro/daisyExternal/http
daisy-integration/src
main/java/se/su/dsv/scipro
test/java/se/su/dsv/scipro
view/src/main/java/se/su/dsv/scipro/grading

@ -32,7 +32,7 @@ public interface DaisyAPI {
Response deleteContributor(Integer projectId, Integer personId);
Response createProject(Thesis project);
Response createProject(ThesisToBeCreated project);
Response addAuthor(Integer projectId, StudentProjectParticipant author);
@ -44,7 +44,7 @@ public interface DaisyAPI {
Optional<Thesis> getThesis(Integer projectIdentifier);
void updateThesis(Thesis thesis);
void updateThesis(final Integer id, ThesisToBeUpdated thesis);
void deleteAuthor(Integer projectId, Integer authorId);

@ -170,10 +170,10 @@ public class DaisyAPIImpl implements DaisyAPI {
}
@Override
public Response createProject(Thesis project) {
public Response createProject(ThesisToBeCreated project) {
return thesis()
.request(MediaType.APPLICATION_JSON_TYPE)
.post(json(project));
.post(xml(project));
}
@Override
@ -221,9 +221,9 @@ public class DaisyAPIImpl implements DaisyAPI {
}
@Override
public void updateThesis(Thesis thesis) {
public void updateThesis(final Integer id, ThesisToBeUpdated thesis) {
thesis()
.path(String.valueOf(thesis.getId()))
.path(String.valueOf(id))
.request()
.put(xml(thesis));
}

@ -170,7 +170,7 @@ public class ProjectExporter extends AbstractWorker {
}
private void updateInDaisy(final Project project, final Optional<FinalThesis> finalThesis, final Thesis daisyThesis) {
Unit unit = new Unit();
UnitWithID unit = new UnitWithID();
unit.setId(project.getHeadSupervisor().getUnit().getIdentifier());
Calendar startDate = Calendar.getInstance();
@ -185,35 +185,28 @@ public class ProjectExporter extends AbstractWorker {
.map(FinalThesis::getEnglishTitle)
.orElseGet(daisyThesis::getTitleEn);
Thesis thesis = new Thesis();
thesis.setId(project.getIdentifier());
thesis.setLevel(mapLevel(project));
ThesisToBeUpdated thesis = new ThesisToBeUpdated();
thesis.setTitle(truncate(title));
thesis.setTitleEn(englishTitle != null ? truncate(englishTitle) : null);
thesis.setUnit(unit);
thesis.setAborted(project.getProjectStatus() == ProjectStatus.INACTIVE);
thesis.setFinished(daisyThesis.isFinished());
thesis.setStartDate(startDate);
thesis.setEndDate(SciProUtilities.toCalendar(project.getExpectedEndDate()));
final ThesisToBeUpdated.ResearchAreas researchAreas = new ThesisToBeUpdated.ResearchAreas();
if (project.getResearchArea() != null && project.getResearchArea().getIdentifier() != null) {
final ResearchArea2 researchArea2 = new ResearchArea2();
researchArea2.setId(project.getResearchArea().getIdentifier());
researchAreas.getResearchAreas().add(researchArea2);
}
thesis.setResearchAreas(researchAreas);
daisyAPI.updateThesis(thesis);
daisyAPI.updateThesis(project.getIdentifier(), thesis);
}
private static String truncate(String projectTitle) {
return projectTitle.length() > MAX_TITLE_LENGTH ? projectTitle.substring(0, MAX_TITLE_LENGTH) : projectTitle;
}
private static EducationalLevel mapLevel(Project project) {
switch (project.getProjectType().getDegreeType()) {
case BACHELOR:
return EducationalLevel.FIRST_CYCLE;
case MAGISTER: // fall-through
case MASTER:
return EducationalLevel.SECOND_CYCLE;
}
throw new IllegalArgumentException("Trying to export a project that has no valid degree level: " + project);
}
private void updateAuthors(Project project) throws ExternalExportException {
Set<StudentProjectParticipant> authors = daisyAPI.getAuthors(project.getIdentifier());
for (StudentProjectParticipant author : authors) {

@ -42,19 +42,23 @@ public class ExternalExporterDaisyImpl implements ExternalExporter {
@Override
public void createProject(final Project project, final Unit unit)
throws ExternalExportException {
EducationalLevel level = toDaisyLevel(project);
se.su.dsv.scipro.io.dto.Unit unitDTO = new se.su.dsv.scipro.io.dto.Unit();
UnitWithID unitDTO = new UnitWithID();
unitDTO.setId(unit.getIdentifier());
Thesis exportProjectDTO = new Thesis();
ThesisToBeCreated exportProjectDTO = new ThesisToBeCreated();
exportProjectDTO.setTitle(truncate(project.getTitle()));
Calendar startDate = Calendar.getInstance();
startDate.setTime(project.getDateCreated());
exportProjectDTO.setStartDate(startDate);
exportProjectDTO.setEndDate(SciProUtilities.toCalendar(project.getExpectedEndDate()));
exportProjectDTO.setLevel(level);
exportProjectDTO.setUnit(unitDTO);
final ThesisToBeCreated.ResearchAreas researchAreas = new ThesisToBeCreated.ResearchAreas();
if (project.getResearchArea() != null && project.getResearchArea().getIdentifier() != null) {
final ResearchArea2 researchArea2 = new ResearchArea2();
researchArea2.setId(project.getResearchArea().getIdentifier());
researchAreas.getResearchAreas().add(researchArea2);
}
exportProjectDTO.setResearchAreas(researchAreas);
Response response = api.createProject(exportProjectDTO);

@ -32,6 +32,7 @@ import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -109,9 +110,9 @@ public class ProjectExporterTest {
public void updates_existing_projects() {
projectExporter.run();
ArgumentCaptor<Thesis> captor = ArgumentCaptor.forClass(Thesis.class);
verify(daisyAPI).updateThesis(captor.capture());
Thesis thesis = captor.getValue();
ArgumentCaptor<ThesisToBeUpdated> captor = ArgumentCaptor.forClass(ThesisToBeUpdated.class);
verify(daisyAPI).updateThesis(eq(exportedProject.getIdentifier()), captor.capture());
ThesisToBeUpdated thesis = captor.getValue();
assertSameData(exportedProject, thesis);
}
@ -169,11 +170,9 @@ public class ProjectExporterTest {
verify(externalExporter).addContributorToProject(exportedProject, user, Role.ASSISTANT_SUPERVISOR);
}
private static void assertSameData(Project project, Thesis thesis) {
private static void assertSameData(Project project, ThesisToBeUpdated thesis) {
assertEquals(project.getTitle(), thesis.getTitle());
assertEquals(project.getProjectStatus() == ProjectStatus.COMPLETED, thesis.isFinished());
assertEquals(project.getProjectStatus() == ProjectStatus.INACTIVE, thesis.isAborted());
assertEquals(EducationalLevel.SECOND_CYCLE, thesis.getLevel());
assertEquals(project.getHeadSupervisor().getUnit().getIdentifier(), thesis.getUnit().getId());
}
}

@ -7,7 +7,7 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import se.su.dsv.scipro.daisyExternal.http.DaisyAPI;
import se.su.dsv.scipro.io.dto.Thesis;
import se.su.dsv.scipro.io.dto.ThesisToBeCreated;
import se.su.dsv.scipro.project.Project;
import se.su.dsv.scipro.system.DegreeType;
import se.su.dsv.scipro.system.ProjectType;
@ -15,7 +15,7 @@ import se.su.dsv.scipro.system.Unit;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.Date;
import java.util.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@ -37,7 +37,7 @@ public class ExternalExporterDaisyImplTest {
public void setUp() {
daisy = new ExternalExporterDaisyImpl(daisyAPI);
when(daisyAPI.createProject(any(Thesis.class))).thenReturn(Response.created(URI.create("/1")).build());
when(daisyAPI.createProject(any(ThesisToBeCreated.class))).thenReturn(Response.created(URI.create("/1")).build());
}
@Test
@ -46,9 +46,11 @@ public class ExternalExporterDaisyImplTest {
String longerTitle = title + "more title";
Project project = Project.builder().title(longerTitle).projectType(BACHELOR).dateCreated(new Date()).build();
daisy.createProject(project, new Unit());
final Unit unit = new Unit();
unit.setIdentifier(4);
daisy.createProject(project, unit);
ArgumentCaptor<Thesis> captor = ArgumentCaptor.forClass(Thesis.class);
ArgumentCaptor<ThesisToBeCreated> captor = ArgumentCaptor.forClass(ThesisToBeCreated.class);
verify(daisyAPI).createProject(captor.capture());
assertThat(captor.getValue().getTitle(), is(title));
@ -59,9 +61,11 @@ public class ExternalExporterDaisyImplTest {
Date daisyStartDate = new Date();
Project project = Project.builder().title("hej").projectType(BACHELOR).dateCreated(daisyStartDate).build();
daisy.createProject(project, new Unit());
final Unit unit = new Unit();
unit.setIdentifier(8);
daisy.createProject(project, unit);
ArgumentCaptor<Thesis> captor = ArgumentCaptor.forClass(Thesis.class);
ArgumentCaptor<ThesisToBeCreated> captor = ArgumentCaptor.forClass(ThesisToBeCreated.class);
verify(daisyAPI).createProject(captor.capture());
assertEquals(captor.getValue().getStartDate().getTime(), daisyStartDate);

@ -23,6 +23,8 @@ import se.su.dsv.scipro.finalthesis.FinalThesis;
import se.su.dsv.scipro.finalthesis.FinalThesisService;
import se.su.dsv.scipro.io.dto.ResearchArea2;
import se.su.dsv.scipro.io.dto.ThesisPublication;
import se.su.dsv.scipro.io.dto.ThesisToBeUpdated;
import se.su.dsv.scipro.io.dto.UnitWithID;
import se.su.dsv.scipro.oauth.OAuth;
import se.su.dsv.scipro.oauth.OAuthService;
import se.su.dsv.scipro.profile.UserLabel;
@ -250,6 +252,23 @@ public class SupervisorGradingPage extends AbstractSupervisorProjectDetailsPage
private void sendTitlesToDaisy(final Project project, final FinalThesis finalThesis) {
daisyAPI.getThesis(project.getIdentifier()).ifPresent(thesis -> {
final ThesisToBeUpdated thesisToBeUpdated = new ThesisToBeUpdated();
thesisToBeUpdated.setStartDate(thesis.getStartDate());
thesisToBeUpdated.setAborted(thesis.isAborted());
thesisToBeUpdated.setEndDate(thesis.getEndDate());
final UnitWithID unitWithID = new UnitWithID();
unitWithID.setId(thesis.getUnit().getId());
thesisToBeUpdated.setUnit(unitWithID);
final ThesisToBeUpdated.ResearchAreas researchAreas = new ThesisToBeUpdated.ResearchAreas();
thesis.getResearchAreas().getResearchAreas().forEach(researchArea -> {
final ResearchArea2 researchArea2 = new ResearchArea2();
researchArea2.setId(researchArea.getId());
researchAreas.getResearchAreas().add(researchArea2);
});
thesisToBeUpdated.setResearchAreas(researchAreas);
if (finalThesis.getSwedishTitle() != null) {
thesis.setTitle(finalThesis.getSwedishTitle());
}
@ -257,7 +276,7 @@ public class SupervisorGradingPage extends AbstractSupervisorProjectDetailsPage
thesis.setTitle(finalThesis.getEnglishTitle());
}
thesis.setTitleEn(finalThesis.getEnglishTitle());
daisyAPI.updateThesis(thesis);
daisyAPI.updateThesis(project.getIdentifier(), thesisToBeUpdated);
});
}