2960 Import national subject categories.

For future use by supervisors to fill in as part of the publication metadata.
This commit is contained in:
Andreas Svanberg 2023-11-23 12:30:46 +01:00
parent cb2d389881
commit 3f8cf007e7
11 changed files with 277 additions and 1 deletions

@ -75,4 +75,6 @@ public interface DaisyAPI {
PublishingConsent getPublishingConsent(int projectId, int personId);
boolean setPublishingConsent(int projectId, int personId, PublishingConsentLevel publishingConsentLevel);
List<ResearchSubject> getNationalResearchSubjects(int organisationId);
}

@ -434,6 +434,15 @@ public class DaisyAPIImpl implements DaisyAPI {
}
}
@Override
public List<ResearchSubject> getNationalResearchSubjects(int organisationId) {
return units()
.path(Integer.toString(organisationId))
.path("nationalSubjectCategories")
.request(MediaType.APPLICATION_XML_TYPE)
.get(new GenericType<>() {});
}
private WebTarget program() {
return target()
.path(PROGRAM);

@ -25,5 +25,9 @@ public class GradingModule extends PrivateModule {
expose(ThesisApprovedHistoryService.class);
bind(ThesisSubmissionHistoryService.class).to(GradingHistory.class);
expose(ThesisSubmissionHistoryService.class);
bind(NationalSubjectCategoryRepository.class).to(NationalSubjectCategoryRepositoryImpl.class);
bind(NationalSubjectCategoryService.class).to(NationalSubjectCategoryServiceImpl.class);
expose(NationalSubjectCategoryService.class);
}
}

@ -0,0 +1,116 @@
package se.su.dsv.scipro.grading;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.util.Objects;
@Entity
@Table(name = "national_subject_category")
public class NationalSubjectCategory {
@Id
@GeneratedValue(strategy = jakarta.persistence.GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Basic
@Column(name = "external_id")
private Integer externalId;
@Basic
@Column(name = "swedish_name")
private String swedishName;
@Basic
@Column(name = "english_name")
private String englishName;
@Basic
@Column(name = "active")
private boolean active;
@Basic
@Column(name = "preselected")
private boolean preselected;
public NationalSubjectCategory() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getExternalId() {
return externalId;
}
public void setExternalId(Integer externalId) {
this.externalId = externalId;
}
public String getSwedishName() {
return swedishName;
}
public void setSwedishName(String swedishName) {
this.swedishName = swedishName;
}
public String getEnglishName() {
return englishName;
}
public void setEnglishName(String englishName) {
this.englishName = englishName;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public boolean isPreselected() {
return preselected;
}
public void setPreselected(boolean preselected) {
this.preselected = preselected;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof NationalSubjectCategory that)) {
return false;
}
if (this.id != null) {
return Objects.equals(this.id, that.id);
}
return Objects.equals(this.externalId, that.externalId)
&& Objects.equals(this.swedishName, that.swedishName)
&& Objects.equals(this.englishName, that.englishName)
&& Objects.equals(this.active, that.active)
&& Objects.equals(this.preselected, that.preselected);
}
@Override
public int hashCode() {
if (this.id != null) {
return Objects.hashCode(id);
}
return Objects.hash(externalId, swedishName, englishName, active, preselected);
}
}

@ -0,0 +1,9 @@
package se.su.dsv.scipro.grading;
import java.util.Optional;
public interface NationalSubjectCategoryRepository {
void save(NationalSubjectCategory nationalSubjectCategory);
Optional<NationalSubjectCategory> findByExternalId(Integer externalId);
}

@ -0,0 +1,40 @@
package se.su.dsv.scipro.grading;
import com.google.inject.persist.Transactional;
import jakarta.persistence.EntityManager;
import se.su.dsv.scipro.system.AbstractRepository;
import javax.inject.Inject;
import javax.inject.Provider;
import java.util.Optional;
public class NationalSubjectCategoryRepositoryImpl
extends AbstractRepository
implements NationalSubjectCategoryRepository
{
@Inject
public NationalSubjectCategoryRepositoryImpl(Provider<EntityManager> em) {
super(em);
}
@Override
@Transactional
public void save(NationalSubjectCategory nationalSubjectCategory) {
EntityManager entityManager = em();
if (entityManager.contains(nationalSubjectCategory)) {
entityManager.merge(nationalSubjectCategory);
}
else {
entityManager.persist(nationalSubjectCategory);
}
}
@Override
public Optional<NationalSubjectCategory> findByExternalId(Integer externalId) {
NationalSubjectCategory nationalSubjectCategory = from(QNationalSubjectCategory.nationalSubjectCategory)
.where(QNationalSubjectCategory.nationalSubjectCategory.externalId.eq(externalId))
.select(QNationalSubjectCategory.nationalSubjectCategory)
.fetchOne();
return Optional.ofNullable(nationalSubjectCategory);
}
}

@ -0,0 +1,9 @@
package se.su.dsv.scipro.grading;
import java.util.Optional;
public interface NationalSubjectCategoryService {
Optional<NationalSubjectCategory> findByExternalId(Integer externalId);
void save(NationalSubjectCategory nationalSubjectCategory);
}

@ -0,0 +1,25 @@
package se.su.dsv.scipro.grading;
import javax.inject.Inject;
import java.util.Optional;
public class NationalSubjectCategoryServiceImpl
implements NationalSubjectCategoryService
{
private final NationalSubjectCategoryRepository nationalSubjectCategoryRepository;
@Inject
public NationalSubjectCategoryServiceImpl(NationalSubjectCategoryRepository nationalSubjectCategoryRepository) {
this.nationalSubjectCategoryRepository = nationalSubjectCategoryRepository;
}
@Override
public Optional<NationalSubjectCategory> findByExternalId(Integer externalId) {
return nationalSubjectCategoryRepository.findByExternalId(externalId);
}
@Override
public void save(NationalSubjectCategory nationalSubjectCategory) {
nationalSubjectCategoryRepository.save(nationalSubjectCategory);
}
}

@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS `national_subject_category` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`external_id` INT NOT NULL,
`swedish_name` VARCHAR(255) NOT NULL,
`english_name` VARCHAR(255) NOT NULL,
`active` BOOLEAN NOT NULL,
`preselected` BOOLEAN NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `U_national_subject_category_external_id` (`external_id`)
);

@ -15,11 +15,13 @@ public class DaisyWorkerInitialization {
Provider<UserImportWorker> userImportWorker,
Provider<ProjectFinalizer> projectFinalizer,
Provider<RejectedThesisWorker> rejectedThesisWorkerProvider,
Provider<GradingCompletedMilestoneActivator> gradingFinalizer) {
Provider<GradingCompletedMilestoneActivator> gradingFinalizer,
Provider<ImportNationalCategories> importNationalCategories) {
scheduler.schedule("Export projects to daisy").runBy(projectExporter).dailyAt(1, 0);
scheduler.schedule("Remote supervisor (and projects) bulk import").runBy(userImportWorker).dailyAt(1, 30);
scheduler.schedule("Mark projects as completed").runBy(projectFinalizer).dailyAt(2, 0);
scheduler.schedule("Mark the 'grading completed' milestone as completed").runBy(gradingFinalizer).dailyAt(2, 30);
scheduler.schedule("Reject thesis based on examiner rejection in Daisy").runBy(rejectedThesisWorkerProvider).every(5, TimeUnit.MINUTES);
scheduler.schedule("Import national subject categories").runBy(importNationalCategories).dailyAt(3, 0);
}
}

@ -0,0 +1,50 @@
package se.su.dsv.scipro.integration.daisy.workers;
import se.su.dsv.scipro.daisyExternal.http.DaisyAPI;
import se.su.dsv.scipro.grading.NationalSubjectCategory;
import se.su.dsv.scipro.grading.NationalSubjectCategoryService;
import se.su.dsv.scipro.io.dto.ResearchSubject;
import se.su.dsv.scipro.workerthreads.AbstractWorker;
import javax.inject.Inject;
import java.util.List;
import java.util.Optional;
public class ImportNationalCategories extends AbstractWorker {
private static final int DSV = 4;
private final DaisyAPI daisyAPI;
private final NationalSubjectCategoryService nationalSubjectCategoryService;
@Inject
public ImportNationalCategories(DaisyAPI daisyAPI, NationalSubjectCategoryService nationalSubjectCategoryService) {
this.daisyAPI = daisyAPI;
this.nationalSubjectCategoryService = nationalSubjectCategoryService;
}
@Override
protected void doWork() {
List<ResearchSubject> nationalResearchSubjects = daisyAPI.getNationalResearchSubjects(DSV);
for (ResearchSubject researchSubject : nationalResearchSubjects) {
Optional<NationalSubjectCategory> nationalSubjectCategory =
nationalSubjectCategoryService.findByExternalId(researchSubject.getExternalID());
if (nationalSubjectCategory.isEmpty()) {
NationalSubjectCategory newSubjectCategory = new NationalSubjectCategory();
newSubjectCategory.setExternalId(researchSubject.getExternalID());
newSubjectCategory.setSwedishName(researchSubject.getName());
newSubjectCategory.setEnglishName(researchSubject.getNameEn());
newSubjectCategory.setActive(researchSubject.isActive());
newSubjectCategory.setPreselected(researchSubject.isPreselected());
nationalSubjectCategoryService.save(newSubjectCategory);
}
else {
NationalSubjectCategory existingSubjectCategory = nationalSubjectCategory.get();
existingSubjectCategory.setSwedishName(researchSubject.getName());
existingSubjectCategory.setEnglishName(researchSubject.getNameEn());
existingSubjectCategory.setActive(researchSubject.isActive());
existingSubjectCategory.setPreselected(researchSubject.isPreselected());
nationalSubjectCategoryService.save(existingSubjectCategory);
}
}
}
}