| 1 | package edu.ucsb.cs156.happiercows.jobs; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 4 | import edu.ucsb.cs156.happiercows.entities.CommonsPlus; | |
| 5 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 6 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
| 7 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 8 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 9 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 10 | import edu.ucsb.cs156.happiercows.services.jobs.JobContext; | |
| 11 | import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer; | |
| 12 | import edu.ucsb.cs156.happiercows.services.CommonsPlusBuilderService; | |
| 13 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy; | |
| 14 | import lombok.AllArgsConstructor; | |
| 15 | import lombok.Getter; | |
| 16 | ||
| 17 | @AllArgsConstructor | |
| 18 | public class UpdateCowHealthJob implements JobContextConsumer { | |
| 19 | ||
| 20 |     @Getter | |
| 21 |     private CommonsRepository commonsRepository; | |
| 22 |     @Getter | |
| 23 |     private UserCommonsRepository userCommonsRepository; | |
| 24 |     @Getter | |
| 25 |     private UserRepository userRepository; | |
| 26 |     @Getter | |
| 27 |     private CommonsPlusBuilderService commonsPlusBuilderService; | |
| 28 | ||
| 29 |     @Override | |
| 30 |     public void accept(JobContext ctx) throws Exception { | |
| 31 | 
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
        ctx.log("Updating cow health..."); | 
| 32 | ||
| 33 | ||
| 34 |         Iterable<Commons> allCommons = commonsRepository.findAll(); | |
| 35 |         Iterable<CommonsPlus> allCommonsPlus = commonsPlusBuilderService.convertToCommonsPlus(allCommons); | |
| 36 | ||
| 37 |         for (CommonsPlus commonsPlus : allCommonsPlus) { | |
| 38 | ||
| 39 | ||
| 40 |             Commons commons = commonsPlus.getCommons(); | |
| 41 |              | |
| 42 | 
1
1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::runUpdateJobInCommons → KILLED | 
            runUpdateJobInCommons(commons, commonsPlus, commonsPlusBuilderService, commonsRepository, userCommonsRepository, ctx); | 
| 43 |              | |
| 44 |         } | |
| 45 | ||
| 46 | 
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
        ctx.log("Cow health has been updated!"); | 
| 47 |     } | |
| 48 | ||
| 49 |     // exposed for testing | |
| 50 |     public static double calculateNewCowHealthUsingStrategy( | |
| 51 |             CowHealthUpdateStrategy strategy, | |
| 52 |             CommonsPlus commonsPlus, | |
| 53 |             UserCommons userCommons, | |
| 54 |             int totalCows | |
| 55 |     ) { | |
| 56 |         var health = strategy.calculateNewCowHealth(commonsPlus, userCommons, totalCows); | |
| 57 | 
1
1. calculateNewCowHealthUsingStrategy : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealthUsingStrategy → KILLED | 
        return Math.max(0, Math.min(health, 100)); | 
| 58 |     } | |
| 59 | ||
| 60 |     public static void calculateCowDeaths(UserCommons userCommons, JobContext ctx) { | |
| 61 | 
1
1. calculateCowDeaths : negated conditional → KILLED | 
        if (userCommons.getCowHealth() == 0.0) { | 
| 62 | 
2
1. calculateCowDeaths : Replaced integer addition with subtraction → KILLED 2. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowDeaths → KILLED  | 
            userCommons.setCowDeaths(userCommons.getCowDeaths() + userCommons.getNumOfCows()); | 
| 63 | 
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED | 
            userCommons.setNumOfCows(0); | 
| 64 | 
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED | 
            userCommons.setCowHealth(100.0); | 
| 65 | ||
| 66 | 
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
            ctx.log(" " + userCommons.getCowDeaths() + " cows for this user died." ); | 
| 67 |         } | |
| 68 |     } | |
| 69 | ||
| 70 |     public static void runUpdateJobInCommons(Commons commons, CommonsPlus commonsPlus, CommonsPlusBuilderService commonsPlusBuilderService, CommonsRepository commonsRepository, UserCommonsRepository userCommonsRepository, JobContext ctx){ | |
| 71 | 
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
        ctx.log("Commons " + commons.getName() + ", degradationRate: " + commons.getDegradationRate() + ", effectiveCapacity: " + commonsPlus.getEffectiveCapacity()); | 
| 72 | ||
| 73 | 
1
1. lambda$runUpdateJobInCommons$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInCommons$0 → KILLED | 
            int numUsers = commonsRepository.getNumUsers(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumUsers(" + commons.getId() + ")")); | 
| 74 | ||
| 75 | 
1
1. runUpdateJobInCommons : negated conditional → KILLED | 
            if (numUsers==0) { | 
| 76 | 
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
                ctx.log("No users in this commons, skipping"); | 
| 77 |                 return; | |
| 78 |             } | |
| 79 | ||
| 80 |             int carryingCapacity = commonsPlus.getEffectiveCapacity(); | |
| 81 |             Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId()); | |
| 82 | ||
| 83 | 
1
1. lambda$runUpdateJobInCommons$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInCommons$1 → KILLED | 
            Integer totalCows = commonsRepository.getNumCows(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumCows(" + commons.getId() + ")")); | 
| 84 | ||
| 85 | 
2
1. runUpdateJobInCommons : changed conditional boundary → KILLED 2. runUpdateJobInCommons : negated conditional → KILLED  | 
            var isAboveCapacity = totalCows > carryingCapacity; | 
| 86 | 
1
1. runUpdateJobInCommons : negated conditional → KILLED | 
            var cowHealthUpdateStrategy = isAboveCapacity ? commons.getAboveCapacityHealthUpdateStrategy() : commons.getBelowCapacityHealthUpdateStrategy(); | 
| 87 | ||
| 88 |             for (UserCommons userCommons : allUserCommons) { | |
| 89 |                 User user = userCommons.getUser(); | |
| 90 | ||
| 91 |                 var newCowHealth = calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, commonsPlusBuilderService.toCommonsPlus(commons), userCommons, totalCows); | |
| 92 | 
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
                ctx.log("User: " + user.getFullName() + ", numCows: " + userCommons.getNumOfCows() + ", cowHealth: " + userCommons.getCowHealth()); | 
| 93 | ||
| 94 |                 double oldHealth = userCommons.getCowHealth(); | |
| 95 | 
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED | 
                userCommons.setCowHealth(newCowHealth); | 
| 96 | 
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED | 
                calculateCowDeaths(userCommons, ctx); | 
| 97 | ||
| 98 | 
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
                ctx.log(" old cow health: " + oldHealth + ", new cow health: " + userCommons.getCowHealth()); | 
| 99 |                 userCommonsRepository.save(userCommons); | |
| 100 |             } | |
| 101 | ||
| 102 |     } | |
| 103 | } | |
Mutations | ||
| 31 | 
 
 1.1  | 
|
| 42 | 
 
 1.1  | 
|
| 46 | 
 
 1.1  | 
|
| 57 | 
 
 1.1  | 
|
| 61 | 
 
 1.1  | 
|
| 62 | 
 
 1.1 2.2  | 
|
| 63 | 
 
 1.1  | 
|
| 64 | 
 
 1.1  | 
|
| 66 | 
 
 1.1  | 
|
| 71 | 
 
 1.1  | 
|
| 73 | 
 
 1.1  | 
|
| 75 | 
 
 1.1  | 
|
| 76 | 
 
 1.1  | 
|
| 83 | 
 
 1.1  | 
|
| 85 | 
 
 1.1 2.2  | 
|
| 86 | 
 
 1.1  | 
|
| 92 | 
 
 1.1  | 
|
| 95 | 
 
 1.1  | 
|
| 96 | 
 
 1.1  | 
|
| 98 | 
 
 1.1  |