Subversion Repositories SmartDukaan

Rev

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

Rev 536 Rev 555
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
3
import in.shop2020.model.v1.user.ShoppingCartException;
4
import in.shop2020.serving.controllers.BaseController;
4
import in.shop2020.serving.controllers.BaseController;
5
import in.shop2020.serving.pages.PageContentKeys;
5
import in.shop2020.serving.pages.PageContentKeys;
6
import in.shop2020.serving.pages.PageEnum;
6
import in.shop2020.serving.pages.PageEnum;
7
import in.shop2020.serving.pages.PageManager;
7
import in.shop2020.serving.pages.PageManager;
8
import in.shop2020.serving.utils.Utils;
8
import in.shop2020.serving.utils.Utils;
9
import in.shop2020.thrift.clients.ShoppingCartClient;
-
 
10
import in.shop2020.thrift.clients.UserContextServiceClient;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
11
 
10
 
12
import java.util.*;
11
import java.util.*;
13
 
12
 
14
import org.apache.juli.logging.Log;
13
import org.apache.juli.logging.Log;
Line 34... Line 33...
34
		pageManager = PageManager.getPageManager();	
33
		pageManager = PageManager.getPageManager();	
35
	}
34
	}
36
	
35
	
37
	 // GET /cart
36
	 // GET /cart
38
	 public HttpHeaders index() {
37
	 public HttpHeaders index() {
39
    	long userId = 0;
38
    	long userId = userinfo.getUserId();
40
    	boolean isSessionId = true;
-
 
41
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
39
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
42
		
40
		
43
		if(userinfo.isLoggedIn()){
-
 
44
			userId = userinfo.getUserId();
-
 
45
			isSessionId = false;
-
 
46
		}
-
 
47
		else{
-
 
48
			userId = userinfo.getSessionId();
-
 
49
			isSessionId = true;
-
 
50
		}
-
 
51
		
-
 
52
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
41
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
53
		params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
42
		params.put(PageContentKeys.IS_LOGGED_IN, userinfo.isLoggedIn()+"");
54
		params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
43
		params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
55
		
44
		
56
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
45
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
57
		params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
46
		params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
58
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
47
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
Line 64... Line 53...
64
		public String create() {
53
		public String create() {
65
	    	log.info("CartController.create");
54
	    	log.info("CartController.create");
66
	    	
55
	    	
67
	    	printParams();
56
	    	printParams();
68
	    	
57
	    	
69
	    	long userId = 0;
58
	    	long userId = userinfo.getUserId();
70
	    	boolean isSessionId = true;
59
	    	boolean isSessionId = !userinfo.isLoggedIn();
71
	    	
60
	    	
72
	    	log.info("item id is " + this.reqparams.get("productid"));
61
	    	log.info("item id is " + this.reqparams.get("productid"));
73
	    	
62
	    	
74
	    	String itemIds = "1000008_1000005"; 
63
	    	String itemIds = "1000008_1000005"; 
75
	    	if(this.reqparams.get("productid") != null){
64
	    	if(this.reqparams.get("productid") != null){
76
	    		itemIds = this.reqparams.get("productid")[0];
65
	    		itemIds = this.reqparams.get("productid")[0];
77
	    	}
66
	    	}
78
	    	
-
 
79
	 	
-
 
80
//		    	log.info("list of item ids is " + this.request.getParameter("productid"));
-
 
81
//				String itemIds = this.request.getParameter("productid");
-
 
82
			
67
 
83
			if(userinfo.isLoggedIn()){
-
 
84
				userId = userinfo.getUserId();
-
 
85
				isSessionId = false;
-
 
86
			}
-
 
87
			else{
-
 
88
				userId = userinfo.getSessionId();
-
 
89
				isSessionId = true;
-
 
90
			}
-
 
91
			long cartId = 0;
68
			long cartId = 0;
92
		
69
		
93
			StringTokenizer tokenizer = new StringTokenizer(itemIds,"_");
70
			StringTokenizer tokenizer = new StringTokenizer(itemIds,"_");
94
			int numberOfItems = tokenizer.countTokens();
-
 
95
			while(tokenizer.hasMoreTokens()){
71
			while(tokenizer.hasMoreTokens()){
96
				long itemId = Long.parseLong(tokenizer.nextToken());
72
				long itemId = Long.parseLong(tokenizer.nextToken());
97
				//cartId = Utils.addItemToCart(itemId, userId, isSessionId);
-
 
98
				
73
				
99
				try {
74
				try {
100
					ShoppingCartClient shoppingCartClient = new ShoppingCartClient();
75
					UserContextServiceClient userServiceClient = new UserContextServiceClient();
101
					in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
76
					in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
102
					cartId = cartClient.createCart(userId, isSessionId);
77
					cartId = userClient.createCart(userId);
103
					cartClient.addItemToCart(cartId, itemId, 1);
78
					userClient.addItemToCart(cartId, itemId, 1);
104
			
79
			
105
				} catch (ShoppingCartException e) {
80
				} catch (ShoppingCartException e) {
106
					// TODO Auto-generated catch block
-
 
107
					e.printStackTrace();
81
					e.printStackTrace();
108
				} catch (TException e) {
82
				} catch (TException e) {
109
					// TODO Auto-generated catch block
-
 
110
					e.printStackTrace();
83
					e.printStackTrace();
111
				} catch (Exception e) {
84
				} catch (Exception e) {
112
					// TODO Auto-generated catch block
-
 
113
					e.printStackTrace();
85
					e.printStackTrace();
114
				}
86
				}
115
 
87
 
116
			}
88
			}
117
 
89
 
Line 136... Line 108...
136
			if(userinfo.getCartId() == -1){
108
			if(userinfo.getCartId() == -1){
137
				log.info("Cart does not exist. Nothing to delete.");
109
				log.info("Cart does not exist. Nothing to delete.");
138
			}else{
110
			}else{
139
				if(Utils.deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
111
				if(Utils.deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
140
					userinfo.setTotalItems(userinfo.getTotalItems() - 1 );
112
					userinfo.setTotalItems(userinfo.getTotalItems() - 1 );
-
 
113
				// FIXME: Remove the next line of code. We should not be
-
 
114
				// required to set the user info again and again if we update the
-
 
115
				// same referenced object.
141
					setUserSessionInfo();
116
					//setUserSessionInfo();
142
					return "delsuccess";	
117
					return "delsuccess";	
143
				}
118
				}
144
			}
119
			}
145
			return "delfailure";
120
			return "delfailure";
146
		}
121
		}
Line 391... Line 366...
391
			int NumberOfLines = cartClient.getShadowCart(cartId).getLinesSize();
366
			int NumberOfLines = cartClient.getShadowCart(cartId).getLinesSize();
392
			this.session.setAttribute("totalitems", NumberOfLines+"");
367
			this.session.setAttribute("totalitems", NumberOfLines+"");
393
 
368
 
394
			
369
			
395
		} catch (ShoppingCartException e) {
370
		} catch (ShoppingCartException e) {
396
			// TODO Auto-generated catch block
-
 
397
			e.printStackTrace();
371
			e.printStackTrace();
398
		} catch (TException e) {
372
		} catch (TException e) {
399
			// TODO Auto-generated catch block
-
 
400
			e.printStackTrace();
373
			e.printStackTrace();
401
		} catch (Exception e) {
374
		} catch (Exception e) {
402
			// TODO Auto-generated catch block
-
 
403
			e.printStackTrace();
375
			e.printStackTrace();
404
		}
376
		}
405
		
377
		
406
		//if user is logged in create  new cart
378
		//if user is logged in create  new cart
407
		//if( userClient.getState(userId, false).isIsLoggedIn()){
379
		//if( userClient.getState(userId, false).isIsLoggedIn()){