Advertisement
dmfrey

Untitled

Nov 24th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1.     protected Cart validateOrRevertPromotion( HttpServletRequest request, Cart cart ) {
  2.         log.trace( "validateOrRevertPromotion : enter" );
  3.  
  4.         Account account = null;
  5.         if( SecurityContextHolder.getContext().getAuthentication().isAuthenticated() ) {
  6.  
  7.             Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  8.             if( null != auth ) {
  9.                
  10.                 try {
  11.                     account = userService.authenticated( (String) auth.getPrincipal() );
  12.                 } catch( MyAccountException e ) {
  13.                     log.error( "validateOrRevertPromotion : error", e );
  14.  
  15.                     return cart;
  16.                 }
  17.  
  18.             } else {
  19.                
  20.                 log.trace( "validateOrRevertPromotion : user is not authenticated" );
  21.                 return cart;
  22.             }
  23.            
  24.         }
  25.        
  26.         if( account.getGuest() ) {
  27.  
  28.             if( null == cart.getPromotion() && StringUtils.isNotBlank( cart.getPromotionCode() ) ) {
  29.                
  30.                 cart = Cart.fromCartDetails( promotionUtils.populatePromotion( cart.toCartDetails() ) );
  31.                
  32.                 log.trace( "validateOrRevertPromotion : guest used promotion code '" + cart.getPromotionCode() + "'" );
  33.             }
  34.            
  35.             log.trace( "validateOrRevertPromotion : cart is for guest" );
  36.             return cart;
  37.         }
  38.    
  39.         try {
  40.             User user = userService.getUser( account.getGuid() );
  41.             if( StringUtils.isNotBlank( user.getCampaign() ) && ( user.isCampaignVerified() && !user.isCampaignRevoked() ) ) {
  42.                 log.trace( "validateOrRevertPromotion : authenticated user is part of campaign" );
  43.                
  44.                 ThirdPartyCampaign campaign = userService.getCampaign( user.getCampaign() );
  45.                 if( null != campaign ) {
  46.                    
  47.                     cart.setPromotionCode( campaign.getPromoCode() );
  48.                     cart = Cart.fromCartDetails( promotionUtils.populatePromotion( cart.toCartDetails() ) );
  49.  
  50.                     log.trace( "validateOrRevertPromotion : campaign added to cart" );
  51.                 }
  52.                
  53.                 log.trace( "validateOrRevertPromotion : cart is for authenticated user with campaign" );
  54.                 return cart;
  55.             } else {
  56.                
  57.                 if( null == cart.getPromotion() && StringUtils.isNotBlank( cart.getPromotionCode() ) ) {
  58.                    
  59.                     cart = Cart.fromCartDetails( promotionUtils.populatePromotion( cart.toCartDetails() ) );
  60.                    
  61.                     log.trace( "validateOrRevertPromotion : authenticated user used promotion code '" + cart.getPromotionCode() + "'" );
  62.                 }
  63.                
  64.                 log.trace( "validateOrRevertPromotion : cart is for authenticated user" );
  65.                 return cart;
  66.             }
  67.            
  68.         } catch( MyAccountException e ) {
  69.             log.error( "validateOrRevertPromotion : error", e );
  70.         }
  71.                
  72.  
  73.         log.trace( "validateOrRevertPromotion : exit, no promotion applied" );
  74.         return cart;
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement