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