From e133ff1aa8343622bdf719a8dede20c7887fd40b Mon Sep 17 00:00:00 2001 From: Frank Zhang <fz2907@vt.edu> Date: Wed, 23 Nov 2022 21:03:26 -0500 Subject: [PATCH] Added cleanup job, so when the agreement is done they can start give rating --- .../jobs/OfferWishlistMatchingJob.java | 44 +++++++++-- ...ecordTable.java => AgreedRecordModel.java} | 16 +++- .../models/MatchedWishlistRecordModel.java | 41 +++++++++- .../repositories/AgreedRecordRepository.java | 17 ++++ .../MatchedWishlistRecordRepository.java | 4 + .../services/CommentRatingService.java | 57 +++++++++++++ .../MatchedWishlistRecordService.java | 79 +++++++++++++++++++ .../src/main/resources/application.properties | 1 + 8 files changed, 246 insertions(+), 13 deletions(-) rename BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/{AgreedRecordTable.java => AgreedRecordModel.java} (90%) create mode 100644 BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/repositories/AgreedRecordRepository.java create mode 100644 BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/services/CommentRatingService.java diff --git a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/jobs/OfferWishlistMatchingJob.java b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/jobs/OfferWishlistMatchingJob.java index 8abe78e6..0f081723 100644 --- a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/jobs/OfferWishlistMatchingJob.java +++ b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/jobs/OfferWishlistMatchingJob.java @@ -26,6 +26,7 @@ import static vt.CS5934.SwitchRoom.utility.UsefulTools.DATE_FORMAT; @Component @EnableAsync +@Async public class OfferWishlistMatchingJob { private final Logger logger = LoggerFactory.getLogger(OfferWishlistMatchingJob.class); @@ -40,12 +41,11 @@ public class OfferWishlistMatchingJob { WishlistItemRepository wishlistItemRepository; @Autowired WishlistWaitingMatchRepository wishlistWaitingMatchRepository; - @Autowired MatchedWishlistRecordRepository matchedWishlistRecordRepository; + @Autowired + AgreedRecordRepository agreedRecordRepository; - - @Async @Scheduled(fixedDelayString = "${offer.wishlist.job.gap.seconds:60}000", initialDelayString = "${offer.wishlist.job.init.delay.seconds:60}000") public void jobStarter(){ @@ -88,7 +88,23 @@ public class OfferWishlistMatchingJob { } - + @Scheduled(fixedDelayString = "${offer.wishlist.clear.old.job.gap.seconds}000", + initialDelayString = "${offer.wishlist.job.init.delay.seconds:60}000") + public void clearOldJobStarter(){ + logger.info("clearOldJobStarter() Started..."); + try{ + List<MatchedWishlistRecordModel> oldRecords = matchedWishlistRecordRepository + .findAllByEndTimeBefore(new Date()); + List<Long>agreedRecordIds = oldRecords.stream().map(MatchedWishlistRecordModel::getAgreedRecordId) + .collect(Collectors.toList()); + List<AgreedRecordModel> agreedRecordModels = agreedRecordRepository.findAllByIdIn(agreedRecordIds); + agreedRecordModels.forEach(item->item.setOnGoing(false)); + agreedRecordRepository.saveAll(agreedRecordModels); + matchedWishlistRecordRepository.deleteAll(oldRecords); + }catch (Exception e){ + logger.error("Error occur when clear old matched records"); + } + } /** @@ -267,16 +283,24 @@ public class OfferWishlistMatchingJob { private void useWishlistItemAndOfferListBuildMatchedRecordDB(WishlistWaitingMatchModel wishlistItem, List<OfferWaitingMatchModel> offerList) { List<MatchedWishlistRecordModel> matchedRecordList = offerList.parallelStream() - .filter(offer-> isOverlapping(wishlistItem.getStartTime(),wishlistItem.getEndTime(), - offer.getStartTime(),offer.getEndTime())) + .filter(offer-> isWithin(wishlistItem.getStartTime(),wishlistItem.getEndTime(), + offer.getStartTime(),offer.getEndTime()) && checkAgreedOfferTime(offer, wishlistItem)) .map(offer -> new MatchedWishlistRecordModel(wishlistItem.getWishlistItemId(), offer.getOfferId(), LookupTables.MATCHING_RECORD_USER_RESULT.Waiting, - LookupTables.MATCHING_RECORD_USER_RESULT.Waiting)) + LookupTables.MATCHING_RECORD_USER_RESULT.Waiting, + wishlistItem.getStartTime(), wishlistItem.getEndTime())) .toList(); - matchedWishlistRecordRepository.saveAll(matchedRecordList); } + private boolean checkAgreedOfferTime(OfferWaitingMatchModel offer, WishlistWaitingMatchModel wishlistItem) { + AgreedRecordModel agreedRecordList = agreedRecordRepository + .findOneByOfferIdAndStartTimeGreaterThanEqualAndStartTimeLessThanEqualOrOfferIdAndEndTimeGreaterThanEqualAndEndTimeLessThanEqual( + offer.getOfferId(),wishlistItem.getStartTime(), wishlistItem.getEndTime(), + offer.getOfferId(),wishlistItem.getStartTime(), wishlistItem.getEndTime()); + return agreedRecordList == null; + } + private ConcurrentMap<Long, List<OfferWaitingMatchModel>> loadOfferListIntoMap( List<OfferWaitingMatchModel> offerRecords){ return offerRecords.parallelStream() @@ -293,6 +317,10 @@ public class OfferWishlistMatchingJob { return start1.before(end2) && start2.before(end1); } + private boolean isWithin(Date start1, Date end1, Date start2, Date end2){ + return start2.before(start1) && end1.before(end2); + } + } /* Change logs: diff --git a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/AgreedRecordTable.java b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/AgreedRecordModel.java similarity index 90% rename from BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/AgreedRecordTable.java rename to BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/AgreedRecordModel.java index f7a9e7a2..294a9804 100644 --- a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/AgreedRecordTable.java +++ b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/AgreedRecordModel.java @@ -4,15 +4,14 @@ package vt.CS5934.SwitchRoom.models; import lombok.NoArgsConstructor; import javax.persistence.*; import java.util.Date; +import java.util.concurrent.BlockingDeque; @Entity @Table(name = "success_record_table") -@IdClass(MatchedRecordIdModel.class) @NoArgsConstructor -public class AgreedRecordTable { +public class AgreedRecordModel { @Id @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name = "id", nullable = false) private Long id; private Long stateCityCode; private Long wishlistId; @@ -26,8 +25,9 @@ public class AgreedRecordTable { private String toOfferComment; @Column(length = 500) private String toVisitorComment; + private boolean onGoing = true; - public AgreedRecordTable(Long stateCityCode, Long wishlistId, Long wishlistUserId, Long offerId, Date startTime, + public AgreedRecordModel(Long stateCityCode, Long wishlistId, Long wishlistUserId, Long offerId, Date startTime, Date endTime, Integer toOfferStar, Integer toVisitorStar, String toOfferComment, String toVisitorComment) { this.stateCityCode = stateCityCode; @@ -129,4 +129,12 @@ public class AgreedRecordTable { public void setId(Long id) { this.id = id; } + + public boolean isOnGoing() { + return onGoing; + } + + public void setOnGoing(boolean onGoing) { + this.onGoing = onGoing; + } } diff --git a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/MatchedWishlistRecordModel.java b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/MatchedWishlistRecordModel.java index 8d47292f..90ccfd77 100644 --- a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/MatchedWishlistRecordModel.java +++ b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/models/MatchedWishlistRecordModel.java @@ -19,8 +19,11 @@ public class MatchedWishlistRecordModel { @Id private Long wishlistItemId; private LookupTables.MATCHING_RECORD_USER_RESULT offerResult; - private LookupTables.MATCHING_RECORD_USER_RESULT wishlistResult; + private Date startTime; + private Date endTime; + private Long agreedRecordId; + @UpdateTimestamp @Temporal(TemporalType.TIMESTAMP) @Column(name = "modify_date") @@ -35,6 +38,18 @@ public class MatchedWishlistRecordModel { this.wishlistResult = wishlistResult; } + public MatchedWishlistRecordModel(Long offerId, Long wishlistItemId, + LookupTables.MATCHING_RECORD_USER_RESULT offerResult, + LookupTables.MATCHING_RECORD_USER_RESULT wishlistResult, + Date startTime, Date endTime) { + this.offerId = offerId; + this.wishlistItemId = wishlistItemId; + this.offerResult = offerResult; + this.wishlistResult = wishlistResult; + this.startTime = startTime; + this.endTime = endTime; + } + public Long getWishlistItemId() { return wishlistItemId; } @@ -67,6 +82,30 @@ public class MatchedWishlistRecordModel { this.wishlistResult = wishlistResult; } + public Date getStartTime() { + return startTime; + } + + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + + public Date getEndTime() { + return endTime; + } + + public void setEndTime(Date endTime) { + this.endTime = endTime; + } + + public Long getAgreedRecordId() { + return agreedRecordId; + } + + public void setAgreedRecordId(Long agreedRecordId) { + this.agreedRecordId = agreedRecordId; + } + public Date getModifyDate() { return modifyDate; } diff --git a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/repositories/AgreedRecordRepository.java b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/repositories/AgreedRecordRepository.java new file mode 100644 index 00000000..38c4c39b --- /dev/null +++ b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/repositories/AgreedRecordRepository.java @@ -0,0 +1,17 @@ +package vt.CS5934.SwitchRoom.repositories; + +import org.springframework.data.jpa.repository.JpaRepository; +import vt.CS5934.SwitchRoom.models.AgreedRecordModel; + +import java.util.Date; +import java.util.List; + +public interface AgreedRecordRepository extends JpaRepository<AgreedRecordModel, Long> { + + List<AgreedRecordModel> findAllByWishlistUserId(Long wishlistUserId); + List<AgreedRecordModel> findAllByOfferId(Long offerId); + AgreedRecordModel findByOfferIdAndWishlistId(Long offerId, Long wishlistId); + AgreedRecordModel findOneByOfferIdAndStartTimeGreaterThanEqualAndStartTimeLessThanEqualOrOfferIdAndEndTimeGreaterThanEqualAndEndTimeLessThanEqual( + Long offerId1,Date startTime1, Date endTime1, Long offerId2,Date startTime2, Date endTime2); + List<AgreedRecordModel> findAllByIdIn(List<Long> ids); +} diff --git a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/repositories/MatchedWishlistRecordRepository.java b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/repositories/MatchedWishlistRecordRepository.java index e9ef530a..06acd97c 100644 --- a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/repositories/MatchedWishlistRecordRepository.java +++ b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/repositories/MatchedWishlistRecordRepository.java @@ -4,6 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import vt.CS5934.SwitchRoom.models.MatchedRecordIdModel; import vt.CS5934.SwitchRoom.models.MatchedWishlistRecordModel; +import java.util.Date; import java.util.List; public interface MatchedWishlistRecordRepository extends JpaRepository<MatchedWishlistRecordModel, MatchedRecordIdModel> { @@ -27,4 +28,7 @@ public interface MatchedWishlistRecordRepository extends JpaRepository<MatchedWi void deleteAllByOfferId(Long offerId); void deleteAllByWishlistItemId(Long wishlistItemId); Long countByWishlistItemId(Long wishlistItemId); + MatchedWishlistRecordModel findByOfferIdAndWishlistItemId(Long offerId, Long wishlistId); + List<MatchedWishlistRecordModel> findAllByOfferIdAndAgreedRecordIdIsNotNull(Long offerId); + List<MatchedWishlistRecordModel> findAllByEndTimeBefore(Date todayDate); } diff --git a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/services/CommentRatingService.java b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/services/CommentRatingService.java new file mode 100644 index 00000000..e51593a5 --- /dev/null +++ b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/services/CommentRatingService.java @@ -0,0 +1,57 @@ +package vt.CS5934.SwitchRoom.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import vt.CS5934.SwitchRoom.models.AgreedRecordModel; +import vt.CS5934.SwitchRoom.repositories.AgreedRecordRepository; + +import java.util.List; +import java.util.Optional; + +@Service +public class CommentRatingService { + + private final Logger logger = LoggerFactory.getLogger(CommentRatingService.class); + @Autowired + private AgreedRecordRepository agreedRecordRepository; + + public List<AgreedRecordModel> getRecordsByOfferId(Long OfferId){ + return agreedRecordRepository.findAllByOfferId(OfferId); + } + + public List<AgreedRecordModel> getRecordsByWishlistUserId(Long wishlistUserId){ + return agreedRecordRepository.findAllByWishlistUserId(wishlistUserId); + } + + public void setRecordDone(Long offerId, Long wishlistId){ + AgreedRecordModel record = agreedRecordRepository.findByOfferIdAndWishlistId(offerId, wishlistId); + if(record!=null){ + record.setOnGoing(false); + agreedRecordRepository.save(record); + }else{ + logger.error("Can not find AgreedRecordModel with OfferId: {}, and wishlistId: {}", offerId, wishlistId); + } + } + + public void addCommentToHost(String comment, Long agreedRecordId){ + Optional<AgreedRecordModel> record = agreedRecordRepository.findById(agreedRecordId); + record.ifPresent(item -> item.setToOfferComment(comment)); + } + + public void addRatingToHost(int rating, Long agreedRecordId){ + Optional<AgreedRecordModel> record = agreedRecordRepository.findById(agreedRecordId); + record.ifPresent(item -> item.setToOfferStar(rating)); + } + + public void addCommentToVisitor(String comment, Long agreedRecordId){ + Optional<AgreedRecordModel> record = agreedRecordRepository.findById(agreedRecordId); + record.ifPresent(item -> item.setToVisitorComment(comment)); + } + + public void addRatingToVisitor(int rating, Long agreedRecordId){ + Optional<AgreedRecordModel> record = agreedRecordRepository.findById(agreedRecordId); + record.ifPresent(item -> item.setToVisitorStar(rating)); + } +} diff --git a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/services/MatchedWishlistRecordService.java b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/services/MatchedWishlistRecordService.java index 8840f63b..1932bd07 100644 --- a/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/services/MatchedWishlistRecordService.java +++ b/BackendFolder/SwitchRoom/src/main/java/vt/CS5934/SwitchRoom/services/MatchedWishlistRecordService.java @@ -4,9 +4,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import vt.CS5934.SwitchRoom.models.AgreedRecordModel; import vt.CS5934.SwitchRoom.models.MatchedWishlistRecordModel; +import vt.CS5934.SwitchRoom.models.UserOfferModel; +import vt.CS5934.SwitchRoom.models.UserOfferWishlistLookUpModel; +import vt.CS5934.SwitchRoom.repositories.AgreedRecordRepository; import vt.CS5934.SwitchRoom.repositories.MatchedWishlistRecordRepository; +import vt.CS5934.SwitchRoom.repositories.UserOfferRepository; +import vt.CS5934.SwitchRoom.repositories.UserOfferWishlistLookUpRepository; +import vt.CS5934.SwitchRoom.utility.LookupTables; +import java.util.Date; import java.util.List; @Service @@ -23,9 +31,80 @@ public class MatchedWishlistRecordService { */ @Autowired MatchedWishlistRecordRepository matchedWishlistRecordRepository; + @Autowired + AgreedRecordRepository agreedRecordRepository; + @Autowired + UserOfferRepository userOfferRepository; + @Autowired + UserOfferWishlistLookUpRepository userOfferWishlistLookUpRepository; public List<MatchedWishlistRecordModel> getOfferListWithIDFromDB(long id){ logger.info("Reached getOfferListIDFromDB()"); return matchedWishlistRecordRepository.findAllByOfferId(id); } + + public void hostAcceptedOrUpdate(Long offerId, Long wishlistId, Date startTime, Date endTime){ + MatchedWishlistRecordModel record = matchedWishlistRecordRepository + .findByOfferIdAndWishlistItemId(offerId, wishlistId); + if(record != null){ + record.setStartTime(startTime); + record.setEndTime(endTime); + record.setOfferResult(LookupTables.MATCHING_RECORD_USER_RESULT.Accepted); + matchedWishlistRecordRepository.save(record); + }else{ + logger.error("Can not find the matched record with offerId: {}, and wishlistItemId:{}", + offerId, wishlistId); + } + } + + public void hostReject(Long offerId, Long wishlistId){ + MatchedWishlistRecordModel record = matchedWishlistRecordRepository + .findByOfferIdAndWishlistItemId(offerId, wishlistId); + if(record != null){ + record.setOfferResult(LookupTables.MATCHING_RECORD_USER_RESULT.Declined); + if(record.getAgreedRecordId() != null){ + agreedRecordRepository.deleteById(record.getAgreedRecordId()); + } + matchedWishlistRecordRepository.save(record); + }else{ + logger.error("Can not find the matched record with offerId: {}, and wishlistItemId:{}", + offerId, wishlistId); + } + } + + public void visitorReject(Long offerId, Long wishlistId){ + MatchedWishlistRecordModel record = matchedWishlistRecordRepository + .findByOfferIdAndWishlistItemId(offerId, wishlistId); + if(record != null){ + record.setWishlistResult(LookupTables.MATCHING_RECORD_USER_RESULT.Declined); + if(record.getAgreedRecordId() != null){ + agreedRecordRepository.deleteById(record.getAgreedRecordId()); + } + matchedWishlistRecordRepository.save(record); + }else{ + logger.error("Can not find the matched record with offerId: {}, and wishlistItemId:{}", + offerId, wishlistId); + } + } + + public void visitorAccepted(Long offerId, Long wishlistId){ + MatchedWishlistRecordModel record = matchedWishlistRecordRepository + .findByOfferIdAndWishlistItemId(offerId, wishlistId); + if(record != null){ + record.setWishlistResult(LookupTables.MATCHING_RECORD_USER_RESULT.Accepted); + UserOfferModel offerModel = userOfferRepository.findByUserId(offerId); + UserOfferWishlistLookUpModel offerWishLookup = userOfferWishlistLookUpRepository + .findByWishlistItemId(wishlistId); + AgreedRecordModel agreedRecord = new AgreedRecordModel( + offerModel.getStateCityCode(), wishlistId, offerWishLookup.getUserId(), offerId, + record.getStartTime(), record.getEndTime(), null, null, + null, null); + agreedRecord = agreedRecordRepository.save(agreedRecord); + record.setAgreedRecordId(agreedRecord.getId()); + matchedWishlistRecordRepository.save(record); + }else{ + logger.error("Can not find the matched record with offerId: {}, and wishlistItemId:{}", + offerId, wishlistId); + } + } } diff --git a/BackendFolder/SwitchRoom/src/main/resources/application.properties b/BackendFolder/SwitchRoom/src/main/resources/application.properties index 45dc0326..fe1051f5 100644 --- a/BackendFolder/SwitchRoom/src/main/resources/application.properties +++ b/BackendFolder/SwitchRoom/src/main/resources/application.properties @@ -16,4 +16,5 @@ spring.task.scheduling.pool.size = 1 #SpringBoot Offer Wishlist matching job fields: offer.wishlist.job.gap.seconds = 60 +offer.wishlist.clear.old.job.gap.seconds = 86400 offer.wishlist.job.init.delay.seconds = 10 \ No newline at end of file -- GitLab