Skip to content

Commit

Permalink
Merge pull request #269 from MORE-Platform/191-clear-limesurvey-refer…
Browse files Browse the repository at this point in the history
…ence

#191 Clear LimeSurvey reference when importing a Study
  • Loading branch information
ja-fra authored May 2, 2024
2 parents 07cfbef + af39d5a commit 7146d2e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public P validate(P values) {
}
}

public P preImport(P values) { return values; }

public WebComponent getWebComponent() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class LimeSurveyObservationFactory<C extends LimeSurveyObservation<P>, P extends ObservationProperties>
extends ObservationFactory<C, P> {

private static List<Value> properties = List.of(
private static final List<Value> properties = List.of(
/* TODO enable Autocomplete in FE
new AutocompleteValue("limeSurveyId", "surveys")
.setName("Survey")
Expand Down Expand Up @@ -81,6 +81,12 @@ public LimeSurveyObservation<ObservationProperties> create(MoreObservationSDK sd
return new LimeSurveyObservation(sdk, validate((P)properties), limeSurveyRequestService);
}

@Override
public ObservationProperties preImport(ObservationProperties properties) {
properties.remove(LimeSurveyObservation.LIME_SURVEY_ID);
return properties;
}

@Override
public JsonNode handleAPICall(String slug, User user, JsonNode input) throws ApiCallException {
String filter = Optional.ofNullable(input.get("filter")).map(JsonNode::asText).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public StudyImportExport exportStudy(Long studyId, User user) {

@Transactional
public Study importStudy(StudyImportExport studyImport, AuthenticatedUser user) {
Study newStudy = studyService.createStudy(studyImport.getStudy(), user);
Long studyId = newStudy.getStudyId();
final Study newStudy = studyService.createStudy(studyImport.getStudy(), user);
final Long studyId = newStudy.getStudyId();

studyImport.getStudyGroups().forEach(studyGroup ->
studyGroupService.importStudyGroup(studyId, studyGroup)
Expand All @@ -109,14 +109,12 @@ public Study importStudy(StudyImportExport studyImport, AuthenticatedUser user)
observationService.importObservation(studyId, observation)
);
studyImport.getInterventions().forEach(intervention ->
interventionService.importIntervention(studyId, intervention)
);
studyImport.getTriggers().forEach((interventionId, trigger) ->
interventionService.importTrigger(studyId, interventionId, trigger)
);
studyImport.getActions().forEach((interventionId, actionList) ->
actionList.forEach(action ->
interventionService.importAction(studyId, interventionId, action))
interventionService.importIntervention(
studyId,
intervention,
studyImport.getTriggers().get(intervention.getInterventionId()),
studyImport.getActions().getOrDefault(intervention.getInterventionId(), Collections.emptyList())
)
);
return newStudy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@
import io.redlink.more.studymanager.core.component.Component;
import io.redlink.more.studymanager.core.exception.ConfigurationValidationException;
import io.redlink.more.studymanager.core.factory.ActionFactory;
import io.redlink.more.studymanager.core.factory.TriggerFactory;
import io.redlink.more.studymanager.core.properties.ActionProperties;
import io.redlink.more.studymanager.core.properties.TriggerProperties;
import io.redlink.more.studymanager.core.validation.ConfigurationValidationReport;
import io.redlink.more.studymanager.exception.BadRequestException;
import io.redlink.more.studymanager.exception.NotFoundException;
import io.redlink.more.studymanager.model.Action;
import io.redlink.more.studymanager.core.factory.TriggerFactory;
import io.redlink.more.studymanager.model.Intervention;
import io.redlink.more.studymanager.model.Study;
import io.redlink.more.studymanager.model.Trigger;
import io.redlink.more.studymanager.repository.InterventionRepository;
import io.redlink.more.studymanager.repository.StudyRepository;
import io.redlink.more.studymanager.sdk.MoreSDK;
import io.redlink.more.studymanager.utils.LoggingUtils;

import java.text.ParseException;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.quartz.CronExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.springframework.transaction.annotation.Transactional;

@Service
public class InterventionService {
Expand Down Expand Up @@ -69,8 +69,25 @@ public Intervention addIntervention(Intervention intervention) {
return repository.insert(intervention);
}

public Intervention importIntervention(Long studyId, Intervention intervention) {
return repository.importIntervention(studyId, intervention);
@Transactional
public Intervention importIntervention(Long studyId, Intervention intervention, Trigger trigger, List<Action> actions) {
Intervention imported = repository.importIntervention(studyId, intervention);

{
Trigger validated = validateTrigger(trigger);
TriggerFactory factory = factory(validated);
validated.setProperties((TriggerProperties) factory.preImport(validated.getProperties()));
repository.importTrigger(studyId, imported.getInterventionId(), validateTrigger(validated));
}

actions.forEach(a -> {
Action validated = validateAction(a);
ActionFactory factory = factory(validated);
validated.setProperties((ActionProperties) factory.preImport(validated.getProperties()));
repository.importAction(studyId, imported.getInterventionId(), validateAction(validated));
});

return imported;
}

public List<Intervention> listInterventions(Long studyId) {
Expand All @@ -96,10 +113,6 @@ public Action createAction(Long studyId, Integer interventionId, Action action)
return repository.createAction(studyId, interventionId, validateAction(action));
}

public Action importAction(Long studyId, Integer interventionId, Action action) {
return repository.importAction(studyId, interventionId, validateAction(action));
}

public Action getActionByIds(Long studyId, Integer interventionId, Integer actionId) {
return repository.getActionByIds(studyId, interventionId, actionId);
}
Expand All @@ -123,10 +136,6 @@ public Trigger updateTrigger(Long studyId, Integer interventionId, Trigger trigg
return repository.updateTrigger(studyId, interventionId, validateTrigger(trigger));
}

public Trigger importTrigger(Long studyId, Integer interventionId, Trigger trigger) {
return repository.importTrigger(studyId, interventionId, validateTrigger(trigger));
}

public Trigger getTriggerByIds(Long studyId, Integer interventionId) {
return repository.getTriggerByIds(studyId, interventionId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.redlink.more.studymanager.core.component.Component;
import io.redlink.more.studymanager.core.exception.ConfigurationValidationException;
import io.redlink.more.studymanager.core.factory.ObservationFactory;
import io.redlink.more.studymanager.core.properties.ObservationProperties;
import io.redlink.more.studymanager.exception.BadRequestException;
import io.redlink.more.studymanager.exception.NotFoundException;
import io.redlink.more.studymanager.model.Observation;
Expand Down Expand Up @@ -48,7 +49,11 @@ public Observation addObservation(Observation observation) {
}

public Observation importObservation(Long studyId, Observation observation) {
return repository.doImport(studyId, validate(observation));
Observation validated = validate(observation);
ObservationFactory factory = factory(validated);
ObservationProperties props = (ObservationProperties) factory.preImport(validated.getProperties());
validated.setProperties(props);
return repository.doImport(studyId, validated);
}

public void deleteObservation(Long studyId, Integer observationId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ public class ImportExportServiceTest {
private ArgumentCaptor<Trigger> triggerCaptor;

@Captor
private ArgumentCaptor<Action> actionCaptor;

@Captor
private ArgumentCaptor<Integer> idIntCaptor;
private ArgumentCaptor<List<Action>> actionCaptor;

@Captor
private ArgumentCaptor<Long> idLongCaptor;
Expand Down Expand Up @@ -161,16 +158,20 @@ void testImportStudy() {
when(observationService.importObservation(any(), any()))
.thenAnswer(invocationOnMock ->
((Observation) invocationOnMock.getArgument(1)).setStudyId(studyId));
when(interventionService.importIntervention(any(), any()))
when(interventionService.importIntervention(any(), any(), any(), any()))
.thenAnswer(invocationOnMock ->
((Intervention) invocationOnMock.getArgument(1)).setStudyId(studyId));

importExportService.importStudy(studyImport, currentUser);

ArgumentCaptor<StudyGroup> studyGroupCaptor = ArgumentCaptor.forClass(StudyGroup.class);
verify(studyGroupService, times(2)).importStudyGroup(idLongCaptor.capture(), studyGroupCaptor.capture());
assertThat(studyGroupCaptor.getAllValues()).hasSize(2);
assertThat(studyGroupCaptor.getAllValues().get(0).getStudyGroupId()).isEqualTo(2);
assertThat(studyGroupCaptor.getAllValues().get(1).getStudyGroupId()).isEqualTo(3);

verify(observationService, times(2)).importObservation(idLongCaptor.capture(), observationCaptor.capture());
verify(interventionService, times(2)).importIntervention(idLongCaptor.capture(),interventionCaptor.capture());
verify(interventionService, times(1)).importTrigger(idLongCaptor.capture(), idIntCaptor.capture(), triggerCaptor.capture());
verify(interventionService, times(1)).importAction(idLongCaptor.capture(), idIntCaptor.capture(), actionCaptor.capture());
verify(interventionService, times(2)).importIntervention(idLongCaptor.capture(), interventionCaptor.capture(), triggerCaptor.capture(), actionCaptor.capture());

assertThat(observationCaptor.getAllValues().get(0).getObservationId()).isEqualTo(1);
assertThat(observationCaptor.getAllValues().get(0).getStudyGroupId()).isEqualTo(3);
Expand All @@ -182,8 +183,6 @@ void testImportStudy() {
assertThat(interventionCaptor.getAllValues().get(1).getStudyGroupId()).isEqualTo(3);
assertThat(interventionCaptor.getAllValues().get(1).getInterventionId()).isEqualTo(3);

assertThat(idIntCaptor.getAllValues().get(0)).isEqualTo(3);
assertThat(idIntCaptor.getAllValues().get(1)).isEqualTo(2);
assertThat(idLongCaptor.getAllValues()).allMatch(Predicate.isEqual(1L));
}

Expand Down

0 comments on commit 7146d2e

Please sign in to comment.