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