Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.controllers;
2
 
555 chandransh 3
import in.shop2020.model.v1.user.Cart;
4
import in.shop2020.model.v1.user.ShoppingCartException;
507 rajveer 5
import in.shop2020.model.v1.user.Address;
6
import in.shop2020.model.v1.user.AddressType;
7
import in.shop2020.model.v1.user.UserContextService.Client;
8
import in.shop2020.serving.controllers.BaseController;
9
import in.shop2020.serving.pages.PageContentKeys;
10
import in.shop2020.serving.pages.PageEnum;
11
import in.shop2020.serving.pages.PageManager;
12
import in.shop2020.serving.utils.Utils;
13
import in.shop2020.thrift.clients.UserContextServiceClient;
14
 
15
import java.util.*;
16
 
17
import org.apache.juli.logging.Log;
18
import org.apache.juli.logging.LogFactory;
19
import org.apache.struts2.convention.annotation.Result;
20
import org.apache.struts2.convention.annotation.Results;
21
import org.apache.struts2.interceptor.ParameterAware;
22
import org.apache.struts2.rest.DefaultHttpHeaders;
23
import org.apache.struts2.rest.HttpHeaders;
24
import org.apache.thrift.TException;
25
 
26
public class ShippingController extends BaseController implements ParameterAware{
27
 
28
	private static final long serialVersionUID = 1L;
29
	private static Log log = LogFactory.getLog(ShippingController.class);
30
	Map<String, String[]> reqparams = null;
31
 
32
	private Map<String,String> htmlSnippets;
33
	private PageManager pageManager = null;
517 rajveer 34
	private long addressId = 0;
507 rajveer 35
 
36
	public ShippingController(){
37
		super();
38
		pageManager = PageManager.getPageManager();	
39
	}
40
 
41
	 // GET /shipping
42
	 public HttpHeaders index() {
555 chandransh 43
    	long userId = userinfo.getUserId();
44
    	boolean isLoggedIn = userinfo.isLoggedIn();
507 rajveer 45
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
46
 
47
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
555 chandransh 48
		params.put(PageContentKeys.IS_LOGGED_IN, isLoggedIn+"");
507 rajveer 49
		params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
50
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
555 chandransh 51
		params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
507 rajveer 52
 
53
		htmlSnippets = pageManager.getPageContents(PageEnum.SHIPPING_PAGE, params);
54
 
55
    	return new DefaultHttpHeaders("index").disableCaching();
56
	 }
57
 
58
	// POST /entity
59
	public HttpHeaders create(){
555 chandransh 60
		UserContextServiceClient userContextServiceClient = null;
61
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
62
 
507 rajveer 63
    	printParams();
64
 
517 rajveer 65
		String action = this.request.getParameter("action");
66
		if(action == null){
67
			return new DefaultHttpHeaders("failure");
68
		}
507 rajveer 69
 
555 chandransh 70
		try {
71
			userContextServiceClient = new UserContextServiceClient();
72
			userClient = userContextServiceClient.getClient();
73
		} catch (Exception e) {
74
			return new DefaultHttpHeaders("failure");
75
		}
76
 
517 rajveer 77
		if(action.equals("addnew")){
78
 
79
			Address address = new Address();
80
			address.setName(this.request.getParameter("customername"));
81
			address.setLine1(this.request.getParameter("line1"));
82
			address.setLine2(this.request.getParameter("line2"));
83
			address.setCity(this.request.getParameter("city"));
84
			address.setState(this.request.getParameter("state"));
85
			address.setPin(this.request.getParameter("pincode"));
86
			address.setPhone(this.request.getParameter("mobilenumber"));
87
			address.setCountry(this.request.getParameter("country"));
88
			address.setEnabled(true);
89
			address.setType(AddressType.HOME);
90
 
91
			if(userinfo.isLoggedIn()){
92
				try {
569 rajveer 93
					this.addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
517 rajveer 94
					// to set the address in cart
569 rajveer 95
					userClient.addAddressToCart(userinfo.getCartId(), this.addressId);
517 rajveer 96
				} catch (Exception e) {
97
					e.printStackTrace();
98
					return new DefaultHttpHeaders("failure");
99
				}
100
 
101
				return new DefaultHttpHeaders("success");
102
			}else{
103
				return new DefaultHttpHeaders("failure");
104
			}
105
		}
106
		if(action.equals("change")){
507 rajveer 107
			try {
517 rajveer 108
				long addressId = Long.parseLong(this.request.getParameter("addressid"));
555 chandransh 109
				userClient.addAddressToCart(userinfo.getCartId(), addressId);
517 rajveer 110
			} catch (TException e) {
111
				e.printStackTrace();
507 rajveer 112
			} catch (Exception e) {
113
				e.printStackTrace();
114
			}
115
			return new DefaultHttpHeaders("success");
116
		}
517 rajveer 117
		return new DefaultHttpHeaders("failure");
507 rajveer 118
	}
119
 
120
    public void printParams(){
121
    	for(String param : reqparams.keySet()) {
122
    		log.info("param name is " + param);
123
    		log.info("param first is " + reqparams.get(param)[0]);
124
    	}
125
    	log.info(this.reqparams);
126
    }
127
 
128
	@Override
129
	public void setParameters(Map<String, String[]> reqmap) {
130
		log.info("setParameters:" + reqmap);
131
 
132
		this.reqparams = reqmap;
133
	}
134
 
135
    public String getHeaderSnippet(){
136
		return htmlSnippets.get("HEADER");
137
	}
138
 
139
	public String getMainMenuSnippet(){
140
		return htmlSnippets.get("MAIN_MENU");
141
	}
142
 
143
	public String getSearchBarSnippet(){
144
		return htmlSnippets.get("SEARCH_BAR");
145
	}
146
 
147
 
148
	public String getCustomerServiceSnippet(){
149
		return htmlSnippets.get("CUSTOMER_SERVICE");
150
	}
151
 
152
	public String getShippingHeaderSnippet(){
153
		return htmlSnippets.get("SHIPPING_HEADER");
154
	}
155
 
156
	public String getShippingDetailsSnippet(){
157
		return htmlSnippets.get("SHIPPING_DETAILS");
158
	}
159
 
160
	public String getFooterSnippet(){
161
		return htmlSnippets.get("FOOTER");
162
	}
163
 
164
	public String getJsFileSnippet(){
165
		return htmlSnippets.get("JS_FILES");
166
	}
167
 
168
	public String getCssFileSnippet(){
169
		return htmlSnippets.get("CSS_FILES");
170
	}
517 rajveer 171
 
172
	public long getAddressId(){
173
		return this.addressId;
174
	}
507 rajveer 175
}