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 AddressController extends BaseController implements ParameterAware{
28
 
29
	private static final long serialVersionUID = 1L;
30
	private static Log log = LogFactory.getLog(AddressController.class);
31
	Map<String, String[]> reqparams = null;
32
 
33
	private Map<String,String> htmlSnippets;
34
	private PageManager pageManager = null;
35
 
36
	public AddressController(){
37
		super();
38
		pageManager = PageManager.getPageManager();	
39
	}
40
 
41
	 // GET /address
42
	 public HttpHeaders index() {
43
    	long userId = 0;
44
    	boolean isSessionId = true;
45
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
46
 
47
		if(userinfo.isLoggedIn()){
48
			userId = userinfo.getUserId();
49
			isSessionId = false;
50
		}else {
51
			userId = userinfo.getSessionId();
52
			isSessionId = true;
53
		}
54
 
55
 
56
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
57
		params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
58
		params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
59
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
60
 
61
		htmlSnippets = pageManager.getPageContents(PageEnum.SHIPPING_ADDRESS_PAGE, params);
62
 
63
    	return new DefaultHttpHeaders("index").disableCaching();
64
	 }
65
 
66
	// POST /address
67
	public HttpHeaders create(){
68
    	printParams();
69
 
70
		if(userinfo.isLoggedIn()){
71
			UserContextServiceClient userContextServiceClient;
72
			try {
73
				userContextServiceClient = new UserContextServiceClient();
74
				Client userClient = userContextServiceClient.getClient();
75
 
517 rajveer 76
				String action = this.request.getParameter("action");
77
				String isDefault = this.request.getParameter("default");
78
 
79
				if(action != null){
80
					if(action.equals("add")){
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
						if(isDefault.equals("true")){
92
							userClient.addAddressForUser(address, userinfo.getUserId(), (new Date()).getTime(), true);
93
						}else{
94
							userClient.addAddressForUser(address, userinfo.getUserId(), (new Date()).getTime(), false);
95
						}
96
					}
507 rajveer 97
 
517 rajveer 98
					if(action.equals("delete")){
99
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
100
						userClient.removeAddressForUser(userinfo.getUserId(), addressId);
101
					}
102
 
103
					if(action.equals("setdefault")){
104
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
105
						userClient.setDefaultAddress(userinfo.getUserId(), addressId);
106
					}
107
 
507 rajveer 108
				}
109
				return new DefaultHttpHeaders("success");	
110
			} catch (Exception e) {
111
				e.printStackTrace();
112
				return new DefaultHttpHeaders("failure");
113
			}
114
 
115
 
116
		}else{
117
			return new DefaultHttpHeaders("failure");
118
		}
119
 
120
	}
121
 
122
    public void printParams(){
123
    	for(String param : reqparams.keySet()) {
124
    		log.info("param name is " + param);
125
    		log.info("param first is " + reqparams.get(param)[0]);
126
    	}
127
    	log.info(this.reqparams);
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 getShippingAddressHeaderSnippet(){
155
		return htmlSnippets.get("SHIPPING_ADDRESS_HEADER");
156
	}
157
 
158
	public String getShippingAddressDetailsSnippet(){
159
		return htmlSnippets.get("SHIPPING_ADDRESS_DETAILS");
160
	}
161
 
162
	public String getMyResearchSnippet(){
163
		return htmlSnippets.get("MY_RESEARCH");
164
	}
165
 
166
	public String getFooterSnippet(){
167
		return htmlSnippets.get("FOOTER");
168
	}
169
 
170
	public String getJsFileSnippet(){
171
		return htmlSnippets.get("JS_FILES");
172
	}
173
 
174
	public String getCssFileSnippet(){
175
		return htmlSnippets.get("CSS_FILES");
176
	}
177
}