Skip to content

Commit

Permalink
Fix/prevent wrong duplicates (#165)
Browse files Browse the repository at this point in the history
* feat : check state-data date on SU update only

* fix : liquibase xsd

* fix: sonar compliance
  • Loading branch information
SimonDmz authored Nov 27, 2023
1 parent 7a54067 commit 21ecfd7
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 155 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>fr.insee.queen</groupId>
<artifactId>queen</artifactId>
<version>3.5.45</version>
<version>3.5.46</version>
<packaging>war</packaging>
<name>Queen-Back-Office</name>
<description>Back-office services for Queen</description>
Expand Down Expand Up @@ -459,4 +459,3 @@
</pluginManagement>
</build>
</project>

Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
import fr.insee.queen.api.dto.surveyunit.SurveyUnitResponseDto;

import java.util.List;
import java.util.Optional;

public interface SimpleApiRepository {

void updateSurveyUnitData(String id, JsonNode data);

void updateSurveyUnitComment(String id, JsonNode comment);

void updateSurveyUnitPersonalization(String id, JsonNode personalization);

void updateSurveyUnitStateDate(String id, JsonNode stateData);

String getCampaignIdFromSuId(String id);

boolean idCampaignIsPresent(String id);

Optional<Long> findStateDataDateBySurveyUnitId(String id);

void createSurveyUnit(String campaignId, SurveyUnitResponseDto surveyUnitResponseDto);

void deleteParadataEventsBySU(List<String> lstSU);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

Expand All @@ -29,27 +30,27 @@ public class SimplePostgreSQLRepository implements SimpleApiRepository {

@Override
public void updateSurveyUnitData(String id, JsonNode data) {
updateJsonValueOfSurveyUnit("data",id,data);
updateJsonValueOfSurveyUnit("data", id, data);
}

@Override
public void updateSurveyUnitComment(String id, JsonNode comment) {
updateJsonValueOfSurveyUnit("comment",id,comment);
public void updateSurveyUnitComment(String id, JsonNode comment) {
updateJsonValueOfSurveyUnit("comment", id, comment);
}

@Override
public void updateSurveyUnitPersonalization(String id, JsonNode personalization) {
updateJsonValueOfSurveyUnit("personalization",id, personalization);
updateJsonValueOfSurveyUnit("personalization", id, personalization);
}

public String getCampaignIdFromSuId(String id){
String qStringGetCampaignID= "SELECT campaign_id FROM survey_unit WHERE id=?";
public String getCampaignIdFromSuId(String id) {
String qStringGetCampaignID = "SELECT campaign_id FROM survey_unit WHERE id=?";
return jdbcTemplate.queryForObject(
qStringGetCampaignID, new Object[] { id }, String.class);
}

@Override
public void updateSurveyUnitStateDate(String id, JsonNode stateData){
public void updateSurveyUnitStateDate(String id, JsonNode stateData) {

String qStringGetSU = "SELECT count(*) FROM state_data WHERE survey_unit_id=?";
Long date = stateData.get("date").longValue();
Expand All @@ -59,75 +60,73 @@ public void updateSurveyUnitStateDate(String id, JsonNode stateData){
int nbStateData = jdbcTemplate.queryForObject(
qStringGetSU, new Object[] { id }, Integer.class);

if(nbStateData>0) {
if (nbStateData > 0) {
String qString = "UPDATE state_data SET current_page=?, date=?, state=? WHERE survey_unit_id=?";
jdbcTemplate.update(qString, currentPage, date, state, id);
}else{
} else {
LOGGER.info("INSERT state_data for reporting unit with id {}", id);
String qString = "INSERT INTO state_data (id, current_page, date, state, survey_unit_id) VALUES (?, ?, ?, ?, ?)";
jdbcTemplate.update(qString, UUID.randomUUID(), currentPage, date, state, id);
}

}



@Override
public void createSurveyUnit(String campaignId, SurveyUnitResponseDto surveyUnitResponseDto) {
String su ="INSERT INTO survey_unit (id, campaign_id, questionnaire_model_id)\n" +
String su = "INSERT INTO survey_unit (id, campaign_id, questionnaire_model_id)\n" +
"VALUES (?,?,?)\n" +
"ON CONFLICT (id) DO UPDATE SET campaign_id=?, questionnaire_model_id=?";
jdbcTemplate.update(su,
surveyUnitResponseDto.getId(),
campaignId, surveyUnitResponseDto.getQuestionnaireId(),
campaignId, surveyUnitResponseDto.getQuestionnaireId());

insertJsonValueOfSurveyUnit("data",surveyUnitResponseDto.getId(),surveyUnitResponseDto.getData());
insertJsonValueOfSurveyUnit("comment",surveyUnitResponseDto.getId(),surveyUnitResponseDto.getComment());
insertJsonValueOfSurveyUnit("personalization",surveyUnitResponseDto.getId(),surveyUnitResponseDto.getPersonalization());
insertSurveyUnitStateDate(surveyUnitResponseDto.getId(),surveyUnitResponseDto.getStateData());
insertJsonValueOfSurveyUnit("data", surveyUnitResponseDto.getId(), surveyUnitResponseDto.getData());
insertJsonValueOfSurveyUnit("comment", surveyUnitResponseDto.getId(), surveyUnitResponseDto.getComment());
insertJsonValueOfSurveyUnit("personalization", surveyUnitResponseDto.getId(),
surveyUnitResponseDto.getPersonalization());
insertSurveyUnitStateDate(surveyUnitResponseDto.getId(), surveyUnitResponseDto.getStateData());
}

@Override
public void deleteParadataEventsBySU(List<String> lstSU) {
String values = lstSU.stream().map(id->"?").collect(Collectors.joining(","));
String values = lstSU.stream().map(id -> "?").collect(Collectors.joining(","));
StringBuilder qStringBuilder = new StringBuilder("DELETE FROM paradata_event AS paradataEvent ")
.append("WHERE paradataEvent.value ->> 'idSU' IN (%s)");
String qString = String.format(qStringBuilder.toString(), values);
jdbcTemplate.update(qString, lstSU.toArray());
}

private void insertSurveyUnitStateDate(String surveyUnitId, StateDataDto stateData){
private void insertSurveyUnitStateDate(String surveyUnitId, StateDataDto stateData) {
StateDataDto stateDataNpeProof = stateData != null ? stateData : new StateDataDto();
Long date = stateDataNpeProof.getDate();
String state = stateDataNpeProof.getState() != null ? stateDataNpeProof.getState().name() : null;
String currentPage = stateDataNpeProof.getCurrentPage();
String qString = "INSERT INTO state_data (id,current_page,date,state,survey_unit_id) VALUES (?,?,?,?,?)";
jdbcTemplate.update(qString,UUID.randomUUID(),currentPage,date,state,surveyUnitId);
jdbcTemplate.update(qString, UUID.randomUUID(), currentPage, date, state, surveyUnitId);
}

private void insertJsonValueOfSurveyUnit(String table, String surveyUnitId, JsonNode jsonValue){
String qString = String.format("INSERT INTO %s (id, value, survey_unit_id) VALUES (?,?,?)",table);
private void insertJsonValueOfSurveyUnit(String table, String surveyUnitId, JsonNode jsonValue) {
String qString = String.format("INSERT INTO %s (id, value, survey_unit_id) VALUES (?,?,?)", table);
PGobject json = new PGobject();
json.setType("json");
try {
json.setValue(jsonValue.toString());
} catch (SQLException throwables) {
LOGGER.error("Error when inserting in {} - {}",table,throwables.getMessage());
LOGGER.error("Error when inserting in {} - {}", table, throwables.getMessage());
throwables.printStackTrace();
}
jdbcTemplate.update(qString,UUID.randomUUID(),json,surveyUnitId);
jdbcTemplate.update(qString, UUID.randomUUID(), json, surveyUnitId);
}


private void updateJsonValueOfSurveyUnit(String table, String surveyUnitId, JsonNode jsonValue) {
String qString = String.format("UPDATE %s SET value=? WHERE survey_unit_id=?",table);
String qString = String.format("UPDATE %s SET value=? WHERE survey_unit_id=?", table);
PGobject q = new PGobject();
q.setType("json");
try {
q.setValue(jsonValue.toString());
} catch (SQLException throwables) {
LOGGER.error("Error when updating in {} - {}",table,throwables.getMessage());
LOGGER.error("Error when updating in {} - {}", table, throwables.getMessage());
throwables.printStackTrace();
}
jdbcTemplate.update(qString, q, surveyUnitId);
Expand All @@ -144,4 +143,17 @@ public boolean idCampaignIsPresent(String id) {
}
return true;
}

@Override
public Optional<Long> findStateDataDateBySurveyUnitId(String id) {
String qStringGetStateDataWhereSurveyUnitId = "SELECT date FROM state_data WHERE survey_unit_id=?";
Long result;
try {
result = jdbcTemplate.queryForObject(
qStringGetStateDataWhereSurveyUnitId, new Object[] { id }, Long.class);
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
return Optional.of(result);
}
}
Loading

0 comments on commit 21ecfd7

Please sign in to comment.