[5577566] | 1 | package it.finki.charitable.controller;
|
---|
| 2 |
|
---|
[f0d5cb7] | 3 | import it.finki.charitable.entities.*;
|
---|
[ab49338] | 4 | import it.finki.charitable.services.*;
|
---|
[5577566] | 5 | import it.finki.charitable.util.FileUploadUtil;
|
---|
[a64f926] | 6 | import org.dom4j.rule.Mode;
|
---|
[7888b17] | 7 | import org.springframework.data.domain.*;
|
---|
[5577566] | 8 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
| 9 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
| 10 | import org.springframework.stereotype.Controller;
|
---|
| 11 | import org.springframework.ui.Model;
|
---|
| 12 | import org.springframework.util.StringUtils;
|
---|
| 13 | import org.springframework.web.bind.annotation.*;
|
---|
| 14 | import org.springframework.web.multipart.MultipartFile;
|
---|
| 15 |
|
---|
| 16 | import java.io.IOException;
|
---|
[b8dc761] | 17 | import java.time.Duration;
|
---|
[5577566] | 18 | import java.time.LocalDate;
|
---|
[6fa3d09] | 19 | import java.time.LocalDateTime;
|
---|
[7888b17] | 20 | import java.util.*;
|
---|
[5577566] | 21 | import java.util.stream.Collectors;
|
---|
| 22 |
|
---|
| 23 | @Controller
|
---|
| 24 | public class DonationPostController {
|
---|
| 25 |
|
---|
| 26 | private final DonationPostService donationPostService;
|
---|
| 27 | private final UserService userService;
|
---|
| 28 | private final FundsCollectedService fundsCollectedService;
|
---|
| 29 | private final DonationInformationService donationInformationService;
|
---|
[ab49338] | 30 | private final ReportPostService reportPostService;
|
---|
| 31 | private final ReasonService reasonService;
|
---|
[5577566] | 32 |
|
---|
[ab49338] | 33 | public DonationPostController(DonationPostService donationPostService, UserService userService, FundsCollectedService fundsCollectedService, DonationInformationService donationInformationService, ReportPostService reportPostService, ReasonService reasonService) {
|
---|
[5577566] | 34 | this.donationPostService = donationPostService;
|
---|
| 35 | this.userService = userService;
|
---|
| 36 | this.fundsCollectedService = fundsCollectedService;
|
---|
| 37 | this.donationInformationService = donationInformationService;
|
---|
[ab49338] | 38 | this.reportPostService = reportPostService;
|
---|
| 39 | this.reasonService = reasonService;
|
---|
[5577566] | 40 | }
|
---|
| 41 |
|
---|
| 42 | @RequestMapping("/upload")
|
---|
| 43 | public String upload() {
|
---|
| 44 | return "upload";
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | @RequestMapping(value = "/newPost", method = RequestMethod.POST)
|
---|
| 48 | public String newPost(Model model,
|
---|
| 49 | @RequestParam String title,
|
---|
| 50 | @RequestParam String fundsNeeded,
|
---|
| 51 | @RequestParam String currency,
|
---|
| 52 | @RequestParam String description,
|
---|
| 53 | @RequestParam String telekom,
|
---|
| 54 | @RequestParam String a1,
|
---|
[b8a8d06] | 55 | @RequestParam(defaultValue = "2020-01-01") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate dateDue,
|
---|
[5577566] | 56 | @RequestParam String bankAccount,
|
---|
| 57 | @RequestParam MultipartFile titleImage,
|
---|
| 58 | @RequestParam MultipartFile[] images,
|
---|
| 59 | @RequestParam MultipartFile[] moderatorImages) {
|
---|
| 60 |
|
---|
[b8a8d06] | 61 | System.out.println(moderatorImages.length);
|
---|
| 62 | if(titleImage.isEmpty() || (moderatorImages.length == 1 && moderatorImages[0].isEmpty())) {
|
---|
| 63 | model.addAttribute("error", true);
|
---|
| 64 | return "upload";
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | if(title.isBlank() || fundsNeeded.isBlank() || currency.isBlank() || description.isBlank() || bankAccount.isBlank() || dateDue.equals(LocalDate.of(2020,1,1))) {
|
---|
| 68 | model.addAttribute("error", true);
|
---|
| 69 | return "upload";
|
---|
| 70 | }
|
---|
[6fa3d09] | 71 | if (dateDue.isBefore(LocalDate.now()))
|
---|
| 72 | {
|
---|
| 73 | model.addAttribute("error", true);
|
---|
| 74 | return "upload";
|
---|
| 75 | }
|
---|
[b8a8d06] | 76 |
|
---|
[5577566] | 77 | DonationPost post = new DonationPost();
|
---|
| 78 | post.setTitle(title);
|
---|
| 79 |
|
---|
| 80 | try {
|
---|
| 81 | float funds = Float.parseFloat(fundsNeeded);
|
---|
[b8a8d06] | 82 | if (funds <= 0) {
|
---|
| 83 | model.addAttribute("error", true);
|
---|
| 84 | return "upload";
|
---|
| 85 | }
|
---|
[5577566] | 86 | post.setFundsNeeded(funds);
|
---|
| 87 | } catch (NumberFormatException e) {
|
---|
[b8a8d06] | 88 | model.addAttribute("error", true);
|
---|
| 89 | return "upload";
|
---|
[5577566] | 90 | }
|
---|
| 91 |
|
---|
| 92 | post.setCurrency(currency);
|
---|
| 93 | post.setDescription(description);
|
---|
| 94 | post.setDateDue(dateDue);
|
---|
| 95 | post.setBankAccount(bankAccount);
|
---|
[5306751] | 96 | post.setApproved(false);
|
---|
[b8dc761] | 97 | post.setCreatedAt(LocalDate.now());
|
---|
| 98 | long totalDays = Duration.between(post.getCreatedAt().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays();
|
---|
| 99 | if(totalDays < 10)
|
---|
| 100 | post.setRiskFactor(0);
|
---|
[5577566] | 101 |
|
---|
| 102 | List<String> phoneNumbers = Arrays.asList(telekom, a1);
|
---|
| 103 |
|
---|
| 104 | List<String> photos = new ArrayList<>();
|
---|
| 105 | photos.add(StringUtils.cleanPath(Objects.requireNonNull(titleImage.getOriginalFilename())));
|
---|
| 106 | Arrays.stream(images).filter(i -> !i.isEmpty()).forEach(i -> photos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
|
---|
| 107 |
|
---|
| 108 | List<MultipartFile> files = new ArrayList<>();
|
---|
| 109 | files.add(titleImage);
|
---|
| 110 | files.addAll(Arrays.stream(images).filter(i -> !i.isEmpty()).collect(Collectors.toList()));
|
---|
| 111 |
|
---|
| 112 | List<String> moderatorPhotos = new ArrayList<>();
|
---|
| 113 | Arrays.stream(moderatorImages).forEach(i -> moderatorPhotos.add(StringUtils.cleanPath(Objects.requireNonNull(i.getOriginalFilename()))));
|
---|
| 114 |
|
---|
| 115 | post.setPhoneNumbers(phoneNumbers);
|
---|
| 116 | post.setImages(photos);
|
---|
| 117 | post.setModeratorImages(moderatorPhotos);
|
---|
| 118 |
|
---|
| 119 | AppUser user = (AppUser) model.getAttribute("user");
|
---|
| 120 | post.setUser(user);
|
---|
| 121 |
|
---|
| 122 | DonationPost savedPost = donationPostService.save(post);
|
---|
| 123 |
|
---|
| 124 | for (MultipartFile file : files) {
|
---|
| 125 | String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
|
---|
| 126 | String uploadDir = "post-photos/" + savedPost.getId();
|
---|
| 127 | try {
|
---|
| 128 | FileUploadUtil.saveFile(uploadDir, fileName, file);
|
---|
| 129 | } catch (IOException e) {
|
---|
| 130 | e.printStackTrace();
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | for (MultipartFile file : moderatorImages) {
|
---|
| 136 |
|
---|
| 137 | String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
|
---|
| 138 | String uploadDir = "moderator-photos/" + savedPost.getId();
|
---|
| 139 | try {
|
---|
| 140 | FileUploadUtil.saveFile(uploadDir, fileName, file);
|
---|
| 141 | } catch (IOException e) {
|
---|
| 142 | e.printStackTrace();
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | return "upload";
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | @RequestMapping("/album")
|
---|
[276a8b6] | 150 | public String album(Model model,
|
---|
| 151 | @RequestParam int page,
|
---|
| 152 | @RequestParam String sort,
|
---|
[7888b17] | 153 | @RequestParam(required = false, defaultValue = "desc") String order,
|
---|
| 154 | @RequestParam(required = false, defaultValue = "all") String groupBy) {
|
---|
| 155 |
|
---|
| 156 | Sort s = Sort.by(sort);
|
---|
| 157 | s = order.equals("asc") ? s.ascending() : s.descending();
|
---|
| 158 | Pageable pageable = PageRequest.of(page - 1, 6, s);
|
---|
| 159 | Page<DonationPost> postList;
|
---|
| 160 | postList = donationPostService.findPaginated(page, 6, sort, order, true);
|
---|
| 161 |
|
---|
| 162 | if (!groupBy.equals("all")) {
|
---|
| 163 | List<DonationPost> allPosts = donationPostService.findAllByApproved(true);
|
---|
| 164 |
|
---|
| 165 | if (sort.equals("title")) {
|
---|
| 166 | if (order.equals("asc")) {
|
---|
| 167 | allPosts.sort(Comparator.comparing(DonationPost::getTitle, (s1, s2) -> s1.compareToIgnoreCase(s2)));
|
---|
| 168 | } else {
|
---|
| 169 | allPosts.sort(Comparator.comparing(DonationPost::getTitle, (s1, s2) -> s2.compareToIgnoreCase(s1)));
|
---|
| 170 | }
|
---|
| 171 | } else if (sort.equals("dateDue")) {
|
---|
| 172 | if (order.equals("asc")) {
|
---|
| 173 | allPosts.sort(Comparator.comparing(DonationPost::getDateDue));
|
---|
| 174 | } else {
|
---|
| 175 | allPosts.sort(Comparator.comparing(DonationPost::getDateDue).reversed());
|
---|
| 176 | }
|
---|
| 177 | } else if (sort.equals("fundsNeeded")) {
|
---|
| 178 | if (order.equals("asc")) {
|
---|
| 179 | allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded));
|
---|
| 180 | } else {
|
---|
| 181 | allPosts.sort(Comparator.comparing(DonationPost::getFundsNeeded).reversed());
|
---|
| 182 | }
|
---|
[b8dc761] | 183 | } else if (sort.equals("riskFactor")) {
|
---|
| 184 | if (order.equals("asc")) {
|
---|
| 185 | allPosts.sort(Comparator.comparing(DonationPost::getRiskFactor));
|
---|
| 186 | } else {
|
---|
| 187 | allPosts.sort(Comparator.comparing(DonationPost::getRiskFactor).reversed());
|
---|
| 188 | }
|
---|
[7888b17] | 189 | }
|
---|
| 190 |
|
---|
| 191 | if (groupBy.equals("completed")) {
|
---|
[b8dc761] | 192 | List<DonationPost> completed = allPosts.stream()
|
---|
| 193 | .filter(post -> post.getTotalFundsCollected() >= post.getFundsNeeded())
|
---|
| 194 | .collect(Collectors.toList());
|
---|
[7888b17] | 195 |
|
---|
| 196 | int start = (int) pageable.getOffset();
|
---|
| 197 | int end = Math.min((start + pageable.getPageSize()), completed.size());
|
---|
| 198 | if (start <= end) {
|
---|
| 199 | postList = new PageImpl<>(completed.subList(start, end), pageable, completed.size());
|
---|
| 200 | }
|
---|
| 201 | } else if (groupBy.equals("expired")) {
|
---|
| 202 | List<DonationPost> expired = allPosts.stream().filter(post -> {
|
---|
| 203 | double fundsCollected = post.getFundsCollected().stream().mapToDouble(FundsCollected::getFunds).sum();
|
---|
| 204 | return LocalDate.now().isAfter(post.getDateDue()) && fundsCollected < post.getFundsNeeded();
|
---|
| 205 | }).collect(Collectors.toList());
|
---|
| 206 |
|
---|
| 207 | int start = (int) pageable.getOffset();
|
---|
| 208 | int end = Math.min((start + pageable.getPageSize()), expired.size());
|
---|
| 209 | if (start <= end) {
|
---|
| 210 | postList = new PageImpl<>(expired.subList(start, end), pageable, expired.size());
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[ee0e297] | 215 | if (postList.getTotalElements() == 0) {
|
---|
[5577566] | 216 | model.addAttribute("noPosts", true);
|
---|
| 217 | return "album";
|
---|
| 218 | }
|
---|
[276a8b6] | 219 | model.addAttribute("totalPages", postList.getTotalPages());
|
---|
[5577566] | 220 | model.addAttribute("postList", postList);
|
---|
| 221 | return "album";
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | @RequestMapping("/post")
|
---|
| 225 | public String showPost(Model model, @RequestParam Long postid) {
|
---|
| 226 | DonationPost post = donationPostService.getById(postid);
|
---|
| 227 | if (post == null) {
|
---|
| 228 | model.addAttribute("notFound", true);
|
---|
| 229 | return "post";
|
---|
| 230 | }
|
---|
[ab49338] | 231 |
|
---|
[b8dc761] | 232 | AppUser currentUser = (AppUser) model.getAttribute("user");
|
---|
| 233 | if (post.getApproved() || (post.getUser().getUsername().equals(currentUser.getUsername()) && !post.getApproved())) {
|
---|
[ab49338] | 234 | AppUser user = post.getUser();
|
---|
| 235 | Moderator moderator = post.getModerator();
|
---|
| 236 | model.addAttribute("post", post);
|
---|
| 237 | model.addAttribute("createdByFirstName", user.getFirstName());
|
---|
| 238 | model.addAttribute("createdByLastName", user.getLastName());
|
---|
| 239 | if (moderator != null) {
|
---|
| 240 | model.addAttribute("moderatorFirstName", moderator.getFirstName());
|
---|
| 241 | model.addAttribute("moderatorLastName", moderator.getLastName());
|
---|
| 242 | }
|
---|
| 243 | } else {
|
---|
| 244 | model.addAttribute("notFound", true);
|
---|
| 245 | }
|
---|
[5577566] | 246 | return "post";
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[b8a8d06] | 249 | @RequestMapping(value="/donate", method = RequestMethod.POST)
|
---|
[5577566] | 250 | public String donate(Model model, @RequestParam Long postid,
|
---|
| 251 | @RequestParam String cardName,
|
---|
| 252 | @RequestParam String cardNumber,
|
---|
| 253 | @RequestParam String expiryDate,
|
---|
| 254 | @RequestParam String cvv,
|
---|
[b8a8d06] | 255 | @RequestParam String amount) {
|
---|
[5577566] | 256 |
|
---|
| 257 | DonationPost post = donationPostService.getById(postid);
|
---|
[b8a8d06] | 258 | if(post == null || !post.getApproved()) {
|
---|
| 259 | return "index";
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | float donatedAmount;
|
---|
| 263 | try {
|
---|
| 264 | donatedAmount = Float.parseFloat(amount);
|
---|
| 265 | if (donatedAmount <= 0) {
|
---|
| 266 | return String.format("redirect:/post?postid=%d&error", postid);
|
---|
| 267 | }
|
---|
| 268 | } catch (NumberFormatException e) {
|
---|
| 269 | return String.format("redirect:/post?postid=%d&error", postid);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | FundsCollected funds = new FundsCollected("Online donation", donatedAmount);
|
---|
[5577566] | 273 | fundsCollectedService.save(funds);
|
---|
| 274 |
|
---|
| 275 | post.getFundsCollected().add(funds);
|
---|
[b8dc761] | 276 | post.setTotalFundsCollected(post.getTotalFundsCollected() + donatedAmount);
|
---|
| 277 |
|
---|
[6fa3d09] | 278 | if(post.getRiskFactor() != 101 && post.getRiskFactor() != 102) {
|
---|
[b8dc761] | 279 | post.setRiskFactor(getRisk(post));
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[5577566] | 282 | donationPostService.save(post);
|
---|
| 283 |
|
---|
[b8a8d06] | 284 | DonationInformation donationInformation = new DonationInformation(donatedAmount, post.getId(), post.getTitle());
|
---|
[5577566] | 285 | donationInformationService.save(donationInformation);
|
---|
[f0d5cb7] | 286 | MainUser user = (MainUser) userService.loadUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
|
---|
[5577566] | 287 | user.getDonationInformation().add(donationInformation);
|
---|
| 288 | userService.saveUser(user);
|
---|
| 289 |
|
---|
| 290 | return String.format("redirect:/post?postid=%d", postid);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[b8a8d06] | 293 | @RequestMapping(value="/report", method = RequestMethod.POST)
|
---|
[ab49338] | 294 | public String report(@RequestParam Long postid,
|
---|
[a64f926] | 295 | @RequestParam String description,
|
---|
| 296 | Model model) {
|
---|
[ab49338] | 297 |
|
---|
| 298 | DonationPost donationPost = donationPostService.getById(postid);
|
---|
| 299 | ReportPost reportPost = reportPostService.findByDonationPost(donationPost);
|
---|
[7888b17] | 300 | if (reportPost == null) {
|
---|
[ab49338] | 301 | reportPost = new ReportPost();
|
---|
| 302 | reportPost.setDonationPost(donationPost);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | Reason reason = new Reason();
|
---|
[a64f926] | 306 | AppUser user = (AppUser) model.getAttribute("user");
|
---|
[ab49338] | 307 | reason.setUser(user);
|
---|
| 308 | reason.setDescription(description);
|
---|
| 309 | reasonService.save(reason);
|
---|
| 310 | reportPost.getReasons().add(reason);
|
---|
[0c37625] | 311 | reportPost.setNumReports(reportPost.getNumReports() + 1);
|
---|
[ab49338] | 312 | reportPostService.save(reportPost);
|
---|
| 313 | return String.format("redirect:/post?postid=%d", postid);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[5577566] | 316 | @ModelAttribute("user")
|
---|
| 317 | public AppUser addAttributes() {
|
---|
| 318 | if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
|
---|
[b8dc761] | 319 |
|
---|
| 320 | String email = SecurityContextHolder.getContext().getAuthentication().getName();
|
---|
| 321 | return userService.loadUserByUsername(email);
|
---|
[5577566] | 322 | }
|
---|
| 323 | return null;
|
---|
| 324 | }
|
---|
[b8dc761] | 325 |
|
---|
| 326 | private Integer getRisk(DonationPost post) {
|
---|
[6fa3d09] | 327 | int risk;
|
---|
[b8dc761] | 328 | if(post.getFundsNeeded() <= post.getTotalFundsCollected()) {
|
---|
[6fa3d09] | 329 | risk = 102;
|
---|
[b8dc761] | 330 | }
|
---|
[6fa3d09] | 331 | else
|
---|
| 332 | {
|
---|
| 333 | if (LocalDate.now().isAfter(post.getDateDue()))
|
---|
| 334 | {
|
---|
| 335 | risk=0;
|
---|
| 336 | }
|
---|
| 337 | else
|
---|
| 338 | {
|
---|
| 339 | float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays()+1);
|
---|
[a64f926] | 340 | float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+(24-LocalDateTime.now().getHour()/24f));
|
---|
[6fa3d09] | 341 |
|
---|
| 342 | if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) {
|
---|
| 343 | float hour=(float) LocalDateTime.now().getHour();
|
---|
| 344 | float mins=(float) LocalDateTime.now().getMinute();
|
---|
| 345 | hour=hour+(mins/(float)60);
|
---|
| 346 | float hourlyAverage=(dailyAverage/(float)24);
|
---|
| 347 | float neededhourlyAverage=(post.getFundsNeeded() - post.getTotalFundsCollected())/((float)24-hour);
|
---|
| 348 | risk = (int) (hourlyAverage/neededhourlyAverage*100);
|
---|
| 349 | if (risk>100)
|
---|
| 350 | {
|
---|
| 351 | risk=100;
|
---|
| 352 | }
|
---|
[b8dc761] | 353 |
|
---|
[6fa3d09] | 354 | }
|
---|
| 355 | else
|
---|
| 356 | {
|
---|
| 357 | System.out.println(dailyAverage + " " + neededAverage);
|
---|
| 358 | risk = (int) (dailyAverage / neededAverage * 100);
|
---|
| 359 |
|
---|
| 360 | if(risk > 100) {
|
---|
| 361 | risk = 100;
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
[b8dc761] | 366 | return risk;
|
---|
| 367 | }
|
---|
[5577566] | 368 | }
|
---|