Skip to content
Snippets Groups Projects
Commit e133ff1a authored by fz2907's avatar fz2907
Browse files

Added cleanup job, so when the agreement is done they can start give rating

parent c878f527
No related branches found
No related tags found
1 merge request!37Start AgreedMatchPage to show the ongoing matches and history matches for user...
Showing
with 246 additions and 13 deletions
...@@ -26,6 +26,7 @@ import static vt.CS5934.SwitchRoom.utility.UsefulTools.DATE_FORMAT; ...@@ -26,6 +26,7 @@ import static vt.CS5934.SwitchRoom.utility.UsefulTools.DATE_FORMAT;
@Component @Component
@EnableAsync @EnableAsync
@Async
public class OfferWishlistMatchingJob { public class OfferWishlistMatchingJob {
private final Logger logger = LoggerFactory.getLogger(OfferWishlistMatchingJob.class); private final Logger logger = LoggerFactory.getLogger(OfferWishlistMatchingJob.class);
...@@ -40,12 +41,11 @@ public class OfferWishlistMatchingJob { ...@@ -40,12 +41,11 @@ public class OfferWishlistMatchingJob {
WishlistItemRepository wishlistItemRepository; WishlistItemRepository wishlistItemRepository;
@Autowired @Autowired
WishlistWaitingMatchRepository wishlistWaitingMatchRepository; WishlistWaitingMatchRepository wishlistWaitingMatchRepository;
@Autowired @Autowired
MatchedWishlistRecordRepository matchedWishlistRecordRepository; MatchedWishlistRecordRepository matchedWishlistRecordRepository;
@Autowired
AgreedRecordRepository agreedRecordRepository;
@Async
@Scheduled(fixedDelayString = "${offer.wishlist.job.gap.seconds:60}000", @Scheduled(fixedDelayString = "${offer.wishlist.job.gap.seconds:60}000",
initialDelayString = "${offer.wishlist.job.init.delay.seconds:60}000") initialDelayString = "${offer.wishlist.job.init.delay.seconds:60}000")
public void jobStarter(){ public void jobStarter(){
...@@ -88,7 +88,23 @@ public class OfferWishlistMatchingJob { ...@@ -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 { ...@@ -267,16 +283,24 @@ public class OfferWishlistMatchingJob {
private void useWishlistItemAndOfferListBuildMatchedRecordDB(WishlistWaitingMatchModel wishlistItem, private void useWishlistItemAndOfferListBuildMatchedRecordDB(WishlistWaitingMatchModel wishlistItem,
List<OfferWaitingMatchModel> offerList) { List<OfferWaitingMatchModel> offerList) {
List<MatchedWishlistRecordModel> matchedRecordList = offerList.parallelStream() List<MatchedWishlistRecordModel> matchedRecordList = offerList.parallelStream()
.filter(offer-> isOverlapping(wishlistItem.getStartTime(),wishlistItem.getEndTime(), .filter(offer-> isWithin(wishlistItem.getStartTime(),wishlistItem.getEndTime(),
offer.getStartTime(),offer.getEndTime())) offer.getStartTime(),offer.getEndTime()) && checkAgreedOfferTime(offer, wishlistItem))
.map(offer -> new MatchedWishlistRecordModel(wishlistItem.getWishlistItemId(), .map(offer -> new MatchedWishlistRecordModel(wishlistItem.getWishlistItemId(),
offer.getOfferId(), LookupTables.MATCHING_RECORD_USER_RESULT.Waiting, 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(); .toList();
matchedWishlistRecordRepository.saveAll(matchedRecordList); 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( private ConcurrentMap<Long, List<OfferWaitingMatchModel>> loadOfferListIntoMap(
List<OfferWaitingMatchModel> offerRecords){ List<OfferWaitingMatchModel> offerRecords){
return offerRecords.parallelStream() return offerRecords.parallelStream()
...@@ -293,6 +317,10 @@ public class OfferWishlistMatchingJob { ...@@ -293,6 +317,10 @@ public class OfferWishlistMatchingJob {
return start1.before(end2) && start2.before(end1); 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: Change logs:
......
...@@ -4,15 +4,14 @@ package vt.CS5934.SwitchRoom.models; ...@@ -4,15 +4,14 @@ package vt.CS5934.SwitchRoom.models;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import javax.persistence.*; import javax.persistence.*;
import java.util.Date; import java.util.Date;
import java.util.concurrent.BlockingDeque;
@Entity @Entity
@Table(name = "success_record_table") @Table(name = "success_record_table")
@IdClass(MatchedRecordIdModel.class)
@NoArgsConstructor @NoArgsConstructor
public class AgreedRecordTable { public class AgreedRecordModel {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id; private Long id;
private Long stateCityCode; private Long stateCityCode;
private Long wishlistId; private Long wishlistId;
...@@ -26,8 +25,9 @@ public class AgreedRecordTable { ...@@ -26,8 +25,9 @@ public class AgreedRecordTable {
private String toOfferComment; private String toOfferComment;
@Column(length = 500) @Column(length = 500)
private String toVisitorComment; 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, Date endTime, Integer toOfferStar, Integer toVisitorStar, String toOfferComment,
String toVisitorComment) { String toVisitorComment) {
this.stateCityCode = stateCityCode; this.stateCityCode = stateCityCode;
...@@ -129,4 +129,12 @@ public class AgreedRecordTable { ...@@ -129,4 +129,12 @@ public class AgreedRecordTable {
public void setId(Long id) { public void setId(Long id) {
this.id = id; this.id = id;
} }
public boolean isOnGoing() {
return onGoing;
}
public void setOnGoing(boolean onGoing) {
this.onGoing = onGoing;
}
} }
...@@ -19,8 +19,11 @@ public class MatchedWishlistRecordModel { ...@@ -19,8 +19,11 @@ public class MatchedWishlistRecordModel {
@Id @Id
private Long wishlistItemId; private Long wishlistItemId;
private LookupTables.MATCHING_RECORD_USER_RESULT offerResult; private LookupTables.MATCHING_RECORD_USER_RESULT offerResult;
private LookupTables.MATCHING_RECORD_USER_RESULT wishlistResult; private LookupTables.MATCHING_RECORD_USER_RESULT wishlistResult;
private Date startTime;
private Date endTime;
private Long agreedRecordId;
@UpdateTimestamp @UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@Column(name = "modify_date") @Column(name = "modify_date")
...@@ -35,6 +38,18 @@ public class MatchedWishlistRecordModel { ...@@ -35,6 +38,18 @@ public class MatchedWishlistRecordModel {
this.wishlistResult = wishlistResult; 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() { public Long getWishlistItemId() {
return wishlistItemId; return wishlistItemId;
} }
...@@ -67,6 +82,30 @@ public class MatchedWishlistRecordModel { ...@@ -67,6 +82,30 @@ public class MatchedWishlistRecordModel {
this.wishlistResult = wishlistResult; 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() { public Date getModifyDate() {
return modifyDate; return modifyDate;
} }
......
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);
}
...@@ -4,6 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository; ...@@ -4,6 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
import vt.CS5934.SwitchRoom.models.MatchedRecordIdModel; import vt.CS5934.SwitchRoom.models.MatchedRecordIdModel;
import vt.CS5934.SwitchRoom.models.MatchedWishlistRecordModel; import vt.CS5934.SwitchRoom.models.MatchedWishlistRecordModel;
import java.util.Date;
import java.util.List; import java.util.List;
public interface MatchedWishlistRecordRepository extends JpaRepository<MatchedWishlistRecordModel, MatchedRecordIdModel> { public interface MatchedWishlistRecordRepository extends JpaRepository<MatchedWishlistRecordModel, MatchedRecordIdModel> {
...@@ -27,4 +28,7 @@ public interface MatchedWishlistRecordRepository extends JpaRepository<MatchedWi ...@@ -27,4 +28,7 @@ public interface MatchedWishlistRecordRepository extends JpaRepository<MatchedWi
void deleteAllByOfferId(Long offerId); void deleteAllByOfferId(Long offerId);
void deleteAllByWishlistItemId(Long wishlistItemId); void deleteAllByWishlistItemId(Long wishlistItemId);
Long countByWishlistItemId(Long wishlistItemId); Long countByWishlistItemId(Long wishlistItemId);
MatchedWishlistRecordModel findByOfferIdAndWishlistItemId(Long offerId, Long wishlistId);
List<MatchedWishlistRecordModel> findAllByOfferIdAndAgreedRecordIdIsNotNull(Long offerId);
List<MatchedWishlistRecordModel> findAllByEndTimeBefore(Date todayDate);
} }
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));
}
}
...@@ -4,9 +4,17 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -4,9 +4,17 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import vt.CS5934.SwitchRoom.models.AgreedRecordModel;
import vt.CS5934.SwitchRoom.models.MatchedWishlistRecordModel; 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.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; import java.util.List;
@Service @Service
...@@ -23,9 +31,80 @@ public class MatchedWishlistRecordService { ...@@ -23,9 +31,80 @@ public class MatchedWishlistRecordService {
*/ */
@Autowired @Autowired
MatchedWishlistRecordRepository matchedWishlistRecordRepository; MatchedWishlistRecordRepository matchedWishlistRecordRepository;
@Autowired
AgreedRecordRepository agreedRecordRepository;
@Autowired
UserOfferRepository userOfferRepository;
@Autowired
UserOfferWishlistLookUpRepository userOfferWishlistLookUpRepository;
public List<MatchedWishlistRecordModel> getOfferListWithIDFromDB(long id){ public List<MatchedWishlistRecordModel> getOfferListWithIDFromDB(long id){
logger.info("Reached getOfferListIDFromDB()"); logger.info("Reached getOfferListIDFromDB()");
return matchedWishlistRecordRepository.findAllByOfferId(id); 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);
}
}
} }
...@@ -16,4 +16,5 @@ spring.task.scheduling.pool.size = 1 ...@@ -16,4 +16,5 @@ spring.task.scheduling.pool.size = 1
#SpringBoot Offer Wishlist matching job fields: #SpringBoot Offer Wishlist matching job fields:
offer.wishlist.job.gap.seconds = 60 offer.wishlist.job.gap.seconds = 60
offer.wishlist.clear.old.job.gap.seconds = 86400
offer.wishlist.job.init.delay.seconds = 10 offer.wishlist.job.init.delay.seconds = 10
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment