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