Changeset b8dc761 for src/main/java/it/finki/charitable
- Timestamp:
- 01/05/22 15:57:29 (3 years ago)
- Branches:
- master
- Children:
- 6fa3d09
- Parents:
- 881a233
- Location:
- src/main/java/it/finki/charitable
- Files:
-
- 3 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/it/finki/charitable/controller/DonationPostController.java
r881a233 rb8dc761 14 14 15 15 import java.io.IOException; 16 import java.time.Duration; 16 17 import java.time.LocalDate; 17 18 import java.util.*; … … 87 88 post.setBankAccount(bankAccount); 88 89 post.setApproved(false); 90 post.setCreatedAt(LocalDate.now()); 91 long totalDays = Duration.between(post.getCreatedAt().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays(); 92 if(totalDays < 10) 93 post.setRiskFactor(0); 89 94 90 95 List<String> phoneNumbers = Arrays.asList(telekom, a1); … … 169 174 allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded).reversed()); 170 175 } 176 } else if (sort.equals("riskFactor")) { 177 if (order.equals("asc")) { 178 allPosts.sort(Comparator.comparing(DonationPost::getRiskFactor)); 179 } else { 180 allPosts.sort(Comparator.comparing(DonationPost::getRiskFactor).reversed()); 181 } 171 182 } 172 183 173 184 if (groupBy.equals("completed")) { 174 List<DonationPost> completed = allPosts.stream().filter(post -> { 175 double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum(); 176 return fundsCollected >= post.getFundsNeeded(); 177 }).collect(Collectors.toList()); 185 List<DonationPost> completed = allPosts.stream() 186 .filter(post -> post.getTotalFundsCollected() >= post.getFundsNeeded()) 187 .collect(Collectors.toList()); 178 188 179 189 int start = (int) pageable.getOffset(); … … 213 223 } 214 224 215 if (post.getApproved() || (post.getUser().getUsername().equals(SecurityContextHolder.getContext().getAuthentication().getName()) && !post.getApproved())) { 225 AppUser currentUser = (AppUser) model.getAttribute("user"); 226 if (post.getApproved() || (post.getUser().getUsername().equals(currentUser.getUsername()) && !post.getApproved())) { 216 227 AppUser user = post.getUser(); 217 228 Moderator moderator = post.getModerator(); … … 223 234 model.addAttribute("moderatorLastName", moderator.getLastName()); 224 235 } 225 double total = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();226 model.addAttribute("total", total);227 236 } else { 228 237 model.addAttribute("notFound", true); … … 250 259 return String.format("redirect:/post?postid=%d&error", postid); 251 260 } 252 post.setFundsNeeded(donatedAmount);253 261 } catch (NumberFormatException e) { 254 262 return String.format("redirect:/post?postid=%d&error", postid); … … 259 267 260 268 post.getFundsCollected().add(funds); 269 post.setTotalFundsCollected(post.getTotalFundsCollected() + donatedAmount); 270 271 if(post.getRiskFactor() != 101) { 272 post.setRiskFactor(getRisk(post)); 273 } 274 261 275 donationPostService.save(post); 262 276 … … 295 309 public AppUser addAttributes() { 296 310 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 297 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 311 312 String email = SecurityContextHolder.getContext().getAuthentication().getName(); 313 return userService.loadUserByUsername(email); 298 314 } 299 315 return null; 300 316 } 317 318 private Integer getRisk(DonationPost post) { 319 float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays()+1); 320 float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+1); 321 322 System.out.println(dailyAverage + " " + neededAverage); 323 int risk = (int) (dailyAverage / neededAverage * 100); 324 325 if(risk > 100) { 326 risk = 100; 327 } 328 329 if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) { 330 risk = 0; 331 } 332 333 if(post.getFundsNeeded() <= post.getTotalFundsCollected()) { 334 risk = 101; 335 } 336 337 return risk; 338 } 301 339 } -
src/main/java/it/finki/charitable/controller/HomeController.java
r881a233 rb8dc761 129 129 @ModelAttribute("user") 130 130 public AppUser addAttributes() { 131 if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 132 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 131 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 132 String email = SecurityContextHolder.getContext().getAuthentication().getName(); 133 System.out.println(email); 134 return userService.loadUserByUsername(email); 133 135 } 134 136 return null; -
src/main/java/it/finki/charitable/controller/UserProfileController.java
r881a233 rb8dc761 115 115 fundsCollectedService.save(funds); 116 116 post.getFundsCollected().add(funds); 117 post.setTotalFundsCollected(post.getTotalFundsCollected() + amount); 117 118 donationPostService.save(post); 118 119 } … … 122 123 @ModelAttribute("user") 123 124 public AppUser addAttributes() { 124 if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 125 return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 125 if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") { 126 127 String email = SecurityContextHolder.getContext().getAuthentication().getName(); 128 return userService.loadUserByUsername(email); 126 129 } 127 130 return null; -
src/main/java/it/finki/charitable/entities/DonationPost.java
r881a233 rb8dc761 61 61 private List<FundsCollected> fundsCollected = new ArrayList<>(); 62 62 63 private Float totalFundsCollected = 0f; 64 private LocalDate createdAt; 65 private Integer riskFactor = 101; 66 63 67 @Transient 64 68 public List<String> getImagesPath() { … … 222 226 this.fundsCollected = fundsCollected; 223 227 } 228 229 public Float getTotalFundsCollected() { 230 return totalFundsCollected; 231 } 232 233 public void setTotalFundsCollected(Float totalFundsCollected) { 234 this.totalFundsCollected = totalFundsCollected; 235 } 236 237 public LocalDate getCreatedAt() { 238 return createdAt; 239 } 240 241 public void setCreatedAt(LocalDate createdAt) { 242 this.createdAt = createdAt; 243 } 244 245 public Integer getRiskFactor() { 246 return riskFactor; 247 } 248 249 public void setRiskFactor(Integer riskFactor) { 250 this.riskFactor = riskFactor; 251 } 224 252 } -
src/main/java/it/finki/charitable/security/SecurityConfig.java
r881a233 rb8dc761 3 3 import it.finki.charitable.entities.UserRole; 4 4 import it.finki.charitable.services.UserService; 5 import org.springframework.beans.factory.annotation.Autowired; 5 6 import org.springframework.context.annotation.Configuration; 6 7 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 7 8 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8 9 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 import org.springframework.security.core.Authentication;10 10 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 11 11 import org.springframework.security.web.DefaultRedirectStrategy; … … 14 14 import org.springframework.security.web.util.matcher.AntPathRequestMatcher; 15 15 16 import javax.servlet.ServletException;17 import javax.servlet.http.HttpServletRequest;18 import javax.servlet.http.HttpServletResponse;19 import java.io.IOException;20 21 16 @Configuration 22 17 public class SecurityConfig extends WebSecurityConfigurerAdapter { 18 19 @Autowired 20 private UserO2AuthService userO2AuthService; 21 @Autowired 22 private O2AuthSuccessHandler o2AuthSuccessHandler; 23 23 24 24 private final UserService userService; … … 41 41 "/album/**", 42 42 "/post", 43 "/post-photos/**" 43 "/post-photos/**", 44 "/oauth2/authorization/google" 44 45 }; 45 46 … … 58 59 .successHandler(authenticationSuccessHandler) 59 60 .and() 61 .oauth2Login() 62 .loginPage("/login") 63 .userInfoEndpoint() 64 .userService(userO2AuthService) 65 .and() 66 .successHandler(o2AuthSuccessHandler) 67 .and() 60 68 .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 61 69 .logoutSuccessUrl("/").deleteCookies("remember-me") … … 73 81 }; 74 82 83 84 75 85 @Override 76 86 protected void configure(AuthenticationManagerBuilder auth) throws Exception { -
src/main/java/it/finki/charitable/services/EmailService.java
r881a233 rb8dc761 1 1 package it.finki.charitable.services; 2 2 3 import it.finki.charitable.entities.EmailMessage; 3 4 import org.springframework.mail.SimpleMailMessage; 4 5 import org.springframework.mail.javamail.JavaMailSender; 6 import org.springframework.scheduling.annotation.Async; 5 7 import org.springframework.stereotype.Component; 6 8 … … 9 11 10 12 private final JavaMailSender javaMailSender; 13 private final EmailMessageService emailMessageService; 11 14 12 public EmailService(JavaMailSender javaMailSender ) {15 public EmailService(JavaMailSender javaMailSender, EmailMessageService emailMessageService) { 13 16 this.javaMailSender = javaMailSender; 17 this.emailMessageService = emailMessageService; 14 18 } 15 19 … … 20 24 21 25 String text = "Verify your account on the following link\n" + 22 "http://localhost: 8080/validate?token=" + token;26 "http://localhost:9091/validate?token=" + token; 23 27 message.setText(text); 24 javaMailSender.send(message);28 sendMail(message); 25 29 } 26 30 … … 30 34 message.setSubject(subject); 31 35 32 String text = "Your post has been approved\n" + "http://localhost: 8080/post?postid=" + postId;36 String text = "Your post has been approved\n" + "http://localhost:9091/post?postid=" + postId; 33 37 message.setText(text); 34 javaMailSender.send(message);38 sendMail(message); 35 39 } 36 40 … … 43 47 "Moderator:\n" + description; 44 48 message.setText(text); 45 javaMailSender.send(message);49 sendMail(message); 46 50 } 47 51 … … 54 58 "Moderator:\n" + description; 55 59 message.setText(text); 56 javaMailSender.send(message); 60 sendMail(message); 61 } 62 63 @Async 64 public void sendMail(SimpleMailMessage message) { 65 try { 66 javaMailSender.send(message); 67 68 } catch (Exception e) { 69 emailMessageService.save(new EmailMessage(message.getTo()[0], message.getSubject(), message.getText())); 70 } 57 71 } 58 72 } -
src/main/java/it/finki/charitable/util/AutomaticEvents.java
r881a233 rb8dc761 2 2 3 3 import it.finki.charitable.entities.AppUser; 4 import it.finki.charitable.entities.DonationPost; 5 import it.finki.charitable.entities.EmailMessage; 4 6 import it.finki.charitable.security.ConfirmationToken; 5 import it.finki.charitable.services. ConfirmationTokenService;6 import it.finki.charitable.services.UserService;7 import it.finki.charitable.services.*; 8 import org.springframework.mail.SimpleMailMessage; 7 9 import org.springframework.scheduling.annotation.Scheduled; 8 10 import org.springframework.stereotype.Component; 9 11 12 import java.time.Duration; 13 import java.time.LocalDate; 10 14 import java.util.List; 15 import java.util.stream.Collectors; 11 16 12 17 @Component … … 15 20 private final UserService userService; 16 21 private final ConfirmationTokenService confirmationTokenService; 22 private final DonationPostService donationPostService; 23 private final EmailMessageService emailMessageService; 24 private final EmailService emailService; 17 25 18 public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService ) {26 public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService, DonationPostService donationPostService, EmailMessageService emailMessageService, EmailService emailService) { 19 27 this.userService = userService; 20 28 this.confirmationTokenService = confirmationTokenService; 29 this.donationPostService = donationPostService; 30 this.emailMessageService = emailMessageService; 31 this.emailService = emailService; 21 32 } 22 33 … … 32 43 } 33 44 } 45 46 @Scheduled(cron = "0 0 0 * * *") 47 public void setRisk() { 48 System.out.println("cron"); 49 List<DonationPost> donationPosts = donationPostService.findAll(); 50 donationPosts = donationPosts.stream().filter(post -> { 51 long daysToEnd = Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays(); 52 long totalDays = Duration.between(post.getCreatedAt().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays(); 53 System.out.println(daysToEnd + " " + totalDays); 54 55 if(totalDays < 10) 56 return true; 57 58 return (daysToEnd * 1f/totalDays) * 100 < 75; 59 }).collect(Collectors.toList()); 60 61 donationPosts.forEach(post -> { 62 float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays() + 1); 63 float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+1); 64 65 System.out.println(dailyAverage + " " + neededAverage); 66 int risk = (int) (dailyAverage / neededAverage * 100); 67 68 if(risk > 100) { 69 risk = 100; 70 } 71 72 if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) { 73 risk = 0; 74 } 75 76 if(post.getFundsNeeded() <= post.getTotalFundsCollected()) { 77 risk = 101; 78 } 79 80 post.setRiskFactor(risk); 81 donationPostService.save(post); 82 }); 83 } 84 85 @Scheduled(cron = "0 0 * * * *") 86 public void sendMessages() { 87 List<EmailMessage> messages = emailMessageService.findAll(); 88 for(EmailMessage message: messages) { 89 SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); 90 simpleMailMessage.setTo(message.getSendTo()); 91 simpleMailMessage.setSubject(message.getSubject()); 92 simpleMailMessage.setText(message.getText()); 93 emailService.sendMail(simpleMailMessage); 94 emailMessageService.delete(message); 95 } 96 } 34 97 }
Note:
See TracChangeset
for help on using the changeset viewer.