Subversion Repositories SmartDukaan

Rev

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

Rev 555 Rev 572
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.model.v1.user.ShoppingCartException;
3
import in.shop2020.model.v1.user.ShoppingCartException;
-
 
4
import in.shop2020.model.v1.user.UserContextService;
4
import in.shop2020.serving.controllers.BaseController;
5
import in.shop2020.serving.controllers.BaseController;
5
import in.shop2020.serving.pages.PageContentKeys;
6
import in.shop2020.serving.pages.PageContentKeys;
6
import in.shop2020.serving.pages.PageEnum;
7
import in.shop2020.serving.pages.PageEnum;
7
import in.shop2020.serving.pages.PageManager;
8
import in.shop2020.serving.pages.PageManager;
8
import in.shop2020.serving.utils.Utils;
9
import in.shop2020.serving.utils.Utils;
Line 10... Line 11...
10
 
11
 
11
import java.util.*;
12
import java.util.*;
12
 
13
 
13
import org.apache.juli.logging.Log;
14
import org.apache.juli.logging.Log;
14
import org.apache.juli.logging.LogFactory;
15
import org.apache.juli.logging.LogFactory;
15
import org.apache.struts2.convention.annotation.Result;
-
 
16
import org.apache.struts2.convention.annotation.Results;
-
 
17
import org.apache.struts2.interceptor.ParameterAware;
16
import org.apache.struts2.interceptor.ParameterAware;
18
import org.apache.struts2.rest.DefaultHttpHeaders;
17
import org.apache.struts2.rest.DefaultHttpHeaders;
19
import org.apache.struts2.rest.HttpHeaders;
18
import org.apache.struts2.rest.HttpHeaders;
20
import org.apache.thrift.TException;
19
import org.apache.thrift.TException;
21
 
20
 
Line 24... Line 23...
24
	private static final long serialVersionUID = 1L;
23
	private static final long serialVersionUID = 1L;
25
	private static Log log = LogFactory.getLog(CartController.class);
24
	private static Log log = LogFactory.getLog(CartController.class);
26
	Map<String, String[]> reqparams = null;
25
	Map<String, String[]> reqparams = null;
27
	
26
	
28
	private Map<String,String> htmlSnippets;
27
	private Map<String,String> htmlSnippets;
29
	private PageManager pageManager = null;
28
	private String errorMsg = "";
30
	
29
	
31
	public CartController(){
30
	public CartController(){
32
		super();
31
		super();
33
		pageManager = PageManager.getPageManager();	
-
 
34
	}
32
	}
35
	
33
	
36
	 // GET /cart
34
	 // GET /cart
37
	 public HttpHeaders index() {
35
	 public HttpHeaders index() {
38
    	long userId = userinfo.getUserId();
36
    	long userId = userinfo.getUserId();
-
 
37
    	try {
-
 
38
			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
-
 
39
			if(!userClient.validateCart(userinfo.getCartId()))
-
 
40
				errorMsg = "Your cart has been updated.";
-
 
41
		} catch (Exception e) {
-
 
42
			// This exception can be ignored for showing the cart. Not so
-
 
43
			// innocent when this occurs at the time of checkout or when the
-
 
44
			// user is proceeding to pay.
-
 
45
			e.printStackTrace();
-
 
46
		}
39
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
47
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
40
		
48
		
41
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
49
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
42
		params.put(PageContentKeys.IS_LOGGED_IN, userinfo.isLoggedIn()+"");
50
		params.put(PageContentKeys.IS_LOGGED_IN, userinfo.isLoggedIn()+"");
43
		params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
51
		params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");		
44
		
-
 
45
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
52
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
46
		params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
53
		params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
-
 
54
		params.put(PageContentKeys.ERROR_MSG, errorMsg);
47
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
55
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
48
		
56
		
49
    	return new DefaultHttpHeaders("index").disableCaching();
57
    	return new DefaultHttpHeaders("index").disableCaching();
50
	 }
58
	 }
51
 
59
 
52
		// POST /entity
60
	// POST /entity
53
		public String create() {
61
	public String create() {
54
	    	log.info("CartController.create");
62
		log.info("CartController.create");
55
	    	
-
 
56
	    	printParams();
-
 
57
	    	
-
 
58
	    	long userId = userinfo.getUserId();
-
 
59
	    	boolean isSessionId = !userinfo.isLoggedIn();
-
 
60
	    	
-
 
61
	    	log.info("item id is " + this.reqparams.get("productid"));
-
 
62
	    	
-
 
63
	    	String itemIds = "1000008_1000005"; 
-
 
64
	    	if(this.reqparams.get("productid") != null){
-
 
65
	    		itemIds = this.reqparams.get("productid")[0];
-
 
66
	    	}
-
 
67
 
63
 
68
			long cartId = 0;
64
		printParams();
-
 
65
 
-
 
66
		long userId = userinfo.getUserId();
-
 
67
		long cartId = userinfo.getCartId();
-
 
68
 
-
 
69
		log.info("item id is " + this.reqparams.get("productid"));
-
 
70
 
-
 
71
		String itemIds = "1000008_1000005";
-
 
72
		if (this.reqparams.get("productid") != null) {
-
 
73
			itemIds = this.reqparams.get("productid")[0];
69
		
74
		}
-
 
75
 
70
			StringTokenizer tokenizer = new StringTokenizer(itemIds,"_");
76
		StringTokenizer tokenizer = new StringTokenizer(itemIds, "_");
71
			while(tokenizer.hasMoreTokens()){
77
		while (tokenizer.hasMoreTokens()) {
72
				long itemId = Long.parseLong(tokenizer.nextToken());
78
			long itemId = Long.parseLong(tokenizer.nextToken());
73
				
79
 
74
				try {
80
			try {
75
					UserContextServiceClient userServiceClient = new UserContextServiceClient();
81
				UserContextServiceClient userServiceClient = new UserContextServiceClient();
76
					in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
82
				UserContextService.Client userClient = userServiceClient.getClient();
-
 
83
				if (cartId == 0)
77
					cartId = userClient.createCart(userId);
84
					cartId = userClient.createCart(userId);
78
					userClient.addItemToCart(cartId, itemId, 1);
85
				userClient.addItemToCart(cartId, itemId, 1);
79
			
-
 
80
				} catch (ShoppingCartException e) {
-
 
81
					e.printStackTrace();
-
 
82
				} catch (TException e) {
-
 
83
					e.printStackTrace();
-
 
84
				} catch (Exception e) {
-
 
85
					e.printStackTrace();
-
 
86
				}
-
 
87
 
86
 
-
 
87
			} catch (ShoppingCartException e) {
-
 
88
				e.printStackTrace();
-
 
89
			} catch (TException e) {
-
 
90
				e.printStackTrace();
-
 
91
			} catch (Exception e) {
-
 
92
				e.printStackTrace();
88
			}
93
			}
89
 
94
 
90
	    	//long cartId = Utils.addItemToCart(Long.parseLong(itemId), userId, isSessionId);
-
 
91
	    	
95
		}
-
 
96
 
92
	    	userinfo.setCartId(cartId);
97
		userinfo.setCartId(cartId);
93
	    	userinfo.setTotalItems(Utils.getNumberOfItemsInCart(cartId) );
98
		userinfo.setTotalItems(Utils.getNumberOfItemsInCart(cartId));
94
	    	
-
 
95
	    	this.session.setAttribute("userinfo", userinfo);
-
 
96
	    	
99
 
97
			return "success";
100
		return "success";
98
	    }
-
 
99
		
101
	}		
100
 
102
 
101
		// DELETE /entity
103
		// DELETE /entity
102
		public String destroy() {
104
		public String destroy() {
103
	    	log.info("CartController.destroy");
105
	    	log.info("CartController.destroy");
104
	    	printParams();
106
	    	printParams();
Line 108... Line 110...
108
			if(userinfo.getCartId() == -1){
110
			if(userinfo.getCartId() == -1){
109
				log.info("Cart does not exist. Nothing to delete.");
111
				log.info("Cart does not exist. Nothing to delete.");
110
			}else{
112
			}else{
111
				if(Utils.deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
113
				if(Utils.deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
112
					userinfo.setTotalItems(userinfo.getTotalItems() - 1 );
114
					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.
-
 
116
					//setUserSessionInfo();
-
 
117
					return "delsuccess";	
115
					return "delsuccess";	
118
				}
116
				}
119
			}
117
			}
120
			return "delfailure";
118
			return "delfailure";
121
		}
119
		}