Subversion Repositories SmartDukaan

Rev

Rev 36047 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36047 Rev 36050
Line 38... Line 38...
38
import org.apache.commons.io.output.ByteArrayOutputStream;
38
import org.apache.commons.io.output.ByteArrayOutputStream;
39
import org.apache.logging.log4j.LogManager;
39
import org.apache.logging.log4j.LogManager;
40
import org.apache.logging.log4j.Logger;
40
import org.apache.logging.log4j.Logger;
41
import org.springframework.beans.factory.annotation.Autowired;
41
import org.springframework.beans.factory.annotation.Autowired;
42
import org.springframework.beans.factory.annotation.Value;
42
import org.springframework.beans.factory.annotation.Value;
43
import org.springframework.cache.CacheManager;
-
 
44
import org.springframework.cache.interceptor.SimpleKey;
-
 
45
import org.springframework.core.io.InputStreamResource;
43
import org.springframework.core.io.InputStreamResource;
46
import org.springframework.http.HttpHeaders;
44
import org.springframework.http.HttpHeaders;
47
import org.springframework.http.HttpStatus;
45
import org.springframework.http.HttpStatus;
48
import org.springframework.http.ResponseEntity;
46
import org.springframework.http.ResponseEntity;
49
import org.springframework.mock.web.MockHttpServletRequest;
47
import org.springframework.mock.web.MockHttpServletRequest;
Line 103... Line 101...
103
    private NotificationService notificationService;
101
    private NotificationService notificationService;
104
    @Autowired
102
    @Autowired
105
    private CookiesProcessor cookiesProcessor;
103
    private CookiesProcessor cookiesProcessor;
106
    @Autowired
104
    @Autowired
107
    private OfferService offerService;
105
    private OfferService offerService;
108
    @Autowired
-
 
109
    private CacheManager oneDayCacheManager;
-
 
110
 
-
 
111
    @Autowired
-
 
112
    private CacheManager redisCacheManager;
-
 
113
 
-
 
114
    @Autowired
-
 
115
    private CacheManager redisShortCacheManager;
-
 
116
 
-
 
117
    @Autowired
-
 
118
    private CacheManager thirtyMinsTimeOutCacheManager;
-
 
119
 
106
 
120
    @Autowired
107
    @Autowired
121
    private Gson gson;
108
    private Gson gson;
122
 
109
 
123
    @Autowired
110
    @Autowired
Line 159... Line 146...
159
    @RequestMapping(value = "/createOffer", method = RequestMethod.POST)
146
    @RequestMapping(value = "/createOffer", method = RequestMethod.POST)
160
    public String createOffer(HttpServletRequest request, @RequestBody CreateOfferRequest createOfferRequest,
147
    public String createOffer(HttpServletRequest request, @RequestBody CreateOfferRequest createOfferRequest,
161
                              Model model) throws Exception {
148
                              Model model) throws Exception {
162
        LOGGER.info("createOfferRequest [{}]", createOfferRequest);
149
        LOGGER.info("createOfferRequest [{}]", createOfferRequest);
163
        offerService.addOfferService(createOfferRequest);
150
        offerService.addOfferService(createOfferRequest);
164
        oneDayCacheManager.getCache("allOffers").evict(YearMonth.from(createOfferRequest.getStartDate()));
151
        offerService.evictOfferCaches(createOfferRequest.getId());
165
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
152
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
166
        return "response";
153
        return "response";
167
 
154
 
168
    }
155
    }
169
 
156
 
Line 188... Line 175...
188
        List<Offer> offers = offerRepository.selectAllByIds(offerIds);
175
        List<Offer> offers = offerRepository.selectAllByIds(offerIds);
189
 
176
 
190
        //Consider only offers that have opposite status
177
        //Consider only offers that have opposite status
191
        offers = offers.stream().filter(x -> x.isActive() != active).collect(Collectors.toList());
178
        offers = offers.stream().filter(x -> x.isActive() != active).collect(Collectors.toList());
192
 
179
 
193
        Set<YearMonth> yearMonthsToEvict = new HashSet<>();
-
 
194
        for (Offer offer : offers) {
180
        for (Offer offer : offers) {
195
            offer.setActive(active);
181
            offer.setActive(active);
196
            yearMonthsToEvict.add(YearMonth.from(offer.getStartDate()));
182
            offerService.evictOfferCaches(offer.getId());
197
        }
183
        }
198
        // Evict partner-to-offer mapping (which partners see which offers)
-
 
199
        for (YearMonth ymToEvict : yearMonthsToEvict) {
-
 
200
            redisCacheManager.getCache("catalog.published_yearmonth").evict(ymToEvict);
-
 
201
            oneDayCacheManager.getCache("allOffers").evict(ymToEvict);
-
 
202
        }
-
 
203
        // Evict partner's published offer list with achievement data
-
 
204
        oneDayCacheManager.getCache("publishedOffersWithAchievement").clear();
-
 
205
        redisShortCacheManager.getCache("todayOffers").clear();
-
 
206
        // Evict all partner offer caches (price circular)
-
 
207
        thirtyMinsTimeOutCacheManager.getCache("partnerOffers").clear();
-
 
208
        if (active) {
184
        if (active) {
209
            for (Offer offer : offers) {
185
            for (Offer offer : offers) {
210
                this.sendNotification(offer);
186
                this.sendNotification(offer);
211
            }
187
            }
212
        }
188
        }
Line 219... Line 195...
219
    @RequestMapping(value = "/offers/publishAll", method = RequestMethod.POST)
195
    @RequestMapping(value = "/offers/publishAll", method = RequestMethod.POST)
220
    public ResponseEntity<?> publishAllUnpublished(@RequestParam YearMonth yearMonth)
196
    public ResponseEntity<?> publishAllUnpublished(@RequestParam YearMonth yearMonth)
221
            throws ProfitMandiBusinessException, Exception {
197
            throws ProfitMandiBusinessException, Exception {
222
        List<Offer> published = offerService.publishAllUnpublished(yearMonth);
198
        List<Offer> published = offerService.publishAllUnpublished(yearMonth);
223
        if (!published.isEmpty()) {
199
        if (!published.isEmpty()) {
224
            // Evict partner-to-offer mapping and offer listing
-
 
225
            redisCacheManager.getCache("catalog.published_yearmonth").evict(yearMonth);
-
 
226
            oneDayCacheManager.getCache("allOffers").evict(yearMonth);
-
 
227
            oneDayCacheManager.getCache("publishedOffersWithAchievement").clear();
-
 
228
        redisShortCacheManager.getCache("todayOffers").clear();
-
 
229
            // Evict all partner offer caches (price circular)
-
 
230
            thirtyMinsTimeOutCacheManager.getCache("partnerOffers").clear();
-
 
231
            for (Offer offer : published) {
200
            for (Offer offer : published) {
-
 
201
                offerService.evictOfferCaches(offer.getId());
232
                this.sendNotification(offer);
202
                this.sendNotification(offer);
233
            }
203
            }
234
        }
204
        }
235
        return responseSender.ok(published.size() + " offers published");
205
        return responseSender.ok(published.size() + " offers published");
236
    }
206
    }
Line 673... Line 643...
673
            message = "Partner(s) removed from Offer #" + offerId + ". New Offer #" + newOfferId + " created (Unpublished).";
643
            message = "Partner(s) removed from Offer #" + offerId + ". New Offer #" + newOfferId + " created (Unpublished).";
674
        } else {
644
        } else {
675
            message = "Partner(s) removed from Offer #" + offerId + ".";
645
            message = "Partner(s) removed from Offer #" + offerId + ".";
676
        }
646
        }
677
 
647
 
678
        // Evict partner-to-offer mapping (removed partners no longer see this offer)
-
 
679
        oneDayCacheManager.getCache("allOffers").evict(ym);
-
 
680
        redisCacheManager.getCache("catalog.published_yearmonth").evict(ym);
-
 
681
        oneDayCacheManager.getCache("publishedOffersWithAchievement").clear();
-
 
682
        redisShortCacheManager.getCache("todayOffers").clear();
648
        offerService.evictOfferCaches(offerId);
683
        thirtyMinsTimeOutCacheManager.getCache("partnerOffers").clear();
-
 
684
 
649
 
685
        Map<String, Object> response = new HashMap<>();
650
        Map<String, Object> response = new HashMap<>();
686
        response.put("message", message);
651
        response.put("message", message);
687
        response.put("newOfferId", newOfferId);
652
        response.put("newOfferId", newOfferId);
688
        response.put("yearMonth", ym.toString());
653
        response.put("yearMonth", ym.toString());
Line 695... Line 660...
695
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
660
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
696
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
661
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
697
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
662
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
698
        }
663
        }
699
        offerService.addPartnersToOffer(offerId, fofoIds);
664
        offerService.addPartnersToOffer(offerId, fofoIds);
-
 
665
        offerService.evictOfferCaches(offerId);
700
 
666
 
701
        Offer offer = offerRepository.selectById(offerId);
667
        Offer offer = offerRepository.selectById(offerId);
702
        YearMonth ym = YearMonth.from(offer.getStartDate());
668
        YearMonth ym = YearMonth.from(offer.getStartDate());
703
        // Evict partner-to-offer mapping (added partners now see this offer)
-
 
704
        oneDayCacheManager.getCache("allOffers").evict(ym);
-
 
705
        redisCacheManager.getCache("catalog.published_yearmonth").evict(ym);
-
 
706
        oneDayCacheManager.getCache("publishedOffersWithAchievement").clear();
-
 
707
        redisShortCacheManager.getCache("todayOffers").clear();
-
 
708
        thirtyMinsTimeOutCacheManager.getCache("partnerOffers").clear();
-
 
709
 
-
 
710
        Map<String, Object> response = new HashMap<>();
669
        Map<String, Object> response = new HashMap<>();
711
        response.put("message", "Partner(s) added to Offer #" + offerId + ".");
670
        response.put("message", "Partner(s) added to Offer #" + offerId + ".");
712
        response.put("yearMonth", ym.toString());
671
        response.put("yearMonth", ym.toString());
713
        return responseSender.ok(response);
672
        return responseSender.ok(response);
714
    }
673
    }
Line 719... Line 678...
719
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
678
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
720
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
679
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
721
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
680
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
722
        }
681
        }
723
        offerService.updateOfferTargets(offerId, targets);
682
        offerService.updateOfferTargets(offerId, targets);
724
        evictOfferCaches(offerId);
683
        offerService.evictOfferCaches(offerId);
725
        return responseSender.ok("Targets updated for Offer #" + offerId);
684
        return responseSender.ok("Targets updated for Offer #" + offerId);
726
    }
685
    }
727
 
686
 
728
    @RequestMapping(value = "/offer/updateSlabs", method = RequestMethod.POST, consumes = "application/json")
687
    @RequestMapping(value = "/offer/updateSlabs", method = RequestMethod.POST, consumes = "application/json")
729
    public ResponseEntity<?> updateOfferSlabs(HttpServletRequest request,
688
    public ResponseEntity<?> updateOfferSlabs(HttpServletRequest request,
Line 731... Line 690...
731
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
690
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
732
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
691
        if (!roleManager.isAdmin(loginDetails.getRoleIds())) {
733
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
692
            throw new ProfitMandiBusinessException("Unauthorized", "Unauthorized", "");
734
        }
693
        }
735
        offerService.updateOfferSlabs(updateRequest);
694
        offerService.updateOfferSlabs(updateRequest);
736
        evictOfferCaches(updateRequest.getOfferId());
695
        offerService.evictOfferCaches(updateRequest.getOfferId());
737
        return responseSender.ok("Slabs updated for Offer #" + updateRequest.getOfferId());
696
        return responseSender.ok("Slabs updated for Offer #" + updateRequest.getOfferId());
738
    }
697
    }
739
 
698
 
740
    private void evictOfferCaches(int offerId) {
-
 
741
        Offer offer = offerRepository.selectById(offerId);
-
 
742
        YearMonth ym = YearMonth.from(offer.getStartDate());
-
 
743
        oneDayCacheManager.getCache("allOffers").evict(ym);
-
 
744
        redisCacheManager.getCache("catalog.published_yearmonth").evict(ym);
-
 
745
        oneDayCacheManager.getCache("publishedOffersWithAchievement").clear();
-
 
746
        redisShortCacheManager.getCache("todayOffers").clear();
-
 
747
        thirtyMinsTimeOutCacheManager.getCache("partnerOffers").clear();
-
 
748
    }
-
 
749
 
699
 
750
}
700
}
751
701