diff --git a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/ExternalRessourceService.java b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/ExternalRessourceService.java index aaed6e73..1d314e1c 100644 --- a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/ExternalRessourceService.java +++ b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/ExternalRessourceService.java @@ -52,6 +52,9 @@ public class ExternalRessourceService { @Autowired private RestTemplateService restTemplateService; + @Autowired + private GratificationService gratificationService; + private static final Logger LOGGER = LoggerFactory.getLogger(ExternalRessourceService.class); private static final String ERROR_NO_CONNECTION = "No connection to backend - please adjust the url to the SonarQube server"; @@ -94,6 +97,7 @@ public List generateStandardTasksFromSonarQubeIssuesForWorld(final standardTasks = sonarQubeIssues.stream().map(sonarQubeIssue -> toTask(sonarQubeIssue, world)) .collect(Collectors.toList()); } + LOGGER.info("Mapping done."); return standardTasks; } @@ -106,18 +110,22 @@ private StandardTask toTask(final SonarQubeIssue sonarQubeIssue, final World wor } private StandardTask loadTask(final SonarQubeIssue sonarQubeIssue, final World world, final Long gold, - final Long xp, final Integer debt, final SonarQuestStatus status) { - StandardTask sonarQubeTask = standardTaskRepository.findByKey(sonarQubeIssue.getKey()); - if (sonarQubeTask == null) { + final Long xp, final Integer debt, final SonarQuestStatus newStatus) { + StandardTask savedTask = standardTaskRepository.findByKey(sonarQubeIssue.getKey()); + if (savedTask == null) { // new issue from SonarQube: Create new task - sonarQubeTask = new StandardTask(sonarQubeIssue.getMessage(), status, gold, xp, null, world, + savedTask = new StandardTask(sonarQubeIssue.getMessage(), newStatus, gold, xp, null, world, sonarQubeIssue.getKey(), sonarQubeIssue.getComponent(), sonarQubeIssue.getSeverity(), sonarQubeIssue.getType(), debt, sonarQubeIssue.getKey()); - } else { - // issue already in SonarQuest database: update the task - sonarQubeTask.setStatus(status); } - return sonarQubeTask; + else { + final SonarQuestStatus lastStatus = savedTask.getStatus(); + if (newStatus == SonarQuestStatus.SOLVED && lastStatus == SonarQuestStatus.OPEN) { + gratificationService.rewardUserForSolvingTask(savedTask); + } + savedTask.setStatus(newStatus); + } + return savedTask; } public List getSonarQubeProjects() { diff --git a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/GratificationService.java b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/GratificationService.java index 2596e647..2442b7d2 100644 --- a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/GratificationService.java +++ b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/GratificationService.java @@ -24,6 +24,7 @@ import com.viadee.sonarquest.entities.Task; import com.viadee.sonarquest.entities.User; import com.viadee.sonarquest.interfaces.UserGratification; +import com.viadee.sonarquest.repositories.StandardTaskRepository; @Service public class GratificationService implements UserGratification { @@ -34,11 +35,15 @@ public class GratificationService implements UserGratification { @Autowired private LevelService levelService; + @Autowired + private StandardTaskRepository taskRepo; + private static final Logger LOGGER = LoggerFactory.getLogger(GratificationService.class); @Override @Transactional public synchronized void rewardUserForSolvingTask(final Task task) { + LOGGER.debug("Task ID {} has changed the status from OPEN to SOLVED, rewarding users...", task.getId()); final Participation participation = task.getParticipation(); if (participation != null) { final User user = participation.getUser(); @@ -49,6 +54,8 @@ public synchronized void rewardUserForSolvingTask(final Task task) { addSkillReward(user); user.setLevel(levelService.getLevelByUserXp(user.getXp())); userService.save(user); + } else { + LOGGER.info("No SQUser participations found for task {}, so no rewards are paid out", task.getKey()); } } diff --git a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/StandardTaskService.java b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/StandardTaskService.java index 9c321bb0..9000e7e8 100644 --- a/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/StandardTaskService.java +++ b/sonarQuest-backend/src/main/java/com/viadee/sonarquest/services/StandardTaskService.java @@ -23,9 +23,6 @@ public class StandardTaskService { @Autowired private ExternalRessourceService externalRessourceService; - @Autowired - private GratificationService gratificationService; - @Autowired private StandardTaskRepository standardTaskRepository; @@ -38,6 +35,9 @@ public class StandardTaskService { @Autowired private WorldRepository worldRepository; + @Autowired + private GratificationService gratificationService; + @Autowired private NamedParameterJdbcTemplate template; @@ -50,6 +50,7 @@ public void updateStandardTasks(final World world) { adventureService.updateAdventures(); } + @Transactional public StandardTask updateStandardTask(final StandardTask task) { final SonarQuestStatus oldStatus = getLastState(task); final SonarQuestStatus newStatus = task.getStatus(); @@ -73,6 +74,7 @@ public void setExternalRessourceService(final ExternalRessourceService externalR this.externalRessourceService = externalRessourceService; } + @Transactional public void save(final StandardTask standardTask) { final World world = worldRepository.findByProject(standardTask.getWorld().getProject()); final StandardTask st = new StandardTask(