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 AddressController extends BaseController implements ParameterAware{
27
 
28
	private static final long serialVersionUID = 1L;
29
	private static Log log = LogFactory.getLog(AddressController.class);
30
	Map<String, String[]> reqparams = null;
31
 
32
	private Map<String,String> htmlSnippets;
33
	private PageManager pageManager = null;
34
 
35
	public AddressController(){
36
		super();
37
		pageManager = PageManager.getPageManager();	
38
	}
39
 
40
	 // GET /address
41
	 public HttpHeaders index() {
555 chandransh 42
    	long userId = userinfo.getUserId();
43
    	boolean isLoggedIn = !userinfo.isLoggedIn();
44
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();		
507 rajveer 45
 
46
		params.put(PageContentKeys.CUSTOMER_ID, userId+"");
555 chandransh 47
		params.put(PageContentKeys.IS_LOGGED_IN, isLoggedIn+"");
507 rajveer 48
		params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
49
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
50
 
51
		htmlSnippets = pageManager.getPageContents(PageEnum.SHIPPING_ADDRESS_PAGE, params);
52
 
53
    	return new DefaultHttpHeaders("index").disableCaching();
54
	 }
55
 
56
	// POST /address
57
	public HttpHeaders create(){
58
    	printParams();
59
 
60
		if(userinfo.isLoggedIn()){
61
			UserContextServiceClient userContextServiceClient;
62
			try {
63
				userContextServiceClient = new UserContextServiceClient();
64
				Client userClient = userContextServiceClient.getClient();
65
 
517 rajveer 66
				String action = this.request.getParameter("action");
67
				String isDefault = this.request.getParameter("default");
68
 
69
				if(action != null){
70
					if(action.equals("add")){
71
						Address address = new Address();
72
						address.setName(this.request.getParameter("customername"));
73
						address.setLine1(this.request.getParameter("line1"));
74
						address.setLine2(this.request.getParameter("line2"));
75
						address.setCity(this.request.getParameter("city"));
76
						address.setState(this.request.getParameter("state"));
77
						address.setPin(this.request.getParameter("pincode"));
78
						address.setPhone(this.request.getParameter("mobilenumber"));
79
						address.setCountry(this.request.getParameter("country"));
80
						address.setEnabled(true);
81
						if(isDefault.equals("true")){
569 rajveer 82
							userClient.addAddressForUser(userinfo.getUserId(), address, true);
517 rajveer 83
						}else{
569 rajveer 84
							userClient.addAddressForUser(userinfo.getUserId(), address, false);
517 rajveer 85
						}
86
					}
507 rajveer 87
 
517 rajveer 88
					if(action.equals("delete")){
89
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
90
						userClient.removeAddressForUser(userinfo.getUserId(), addressId);
91
					}
92
 
93
					if(action.equals("setdefault")){
94
						Long addressId = Long.parseLong(this.request.getParameter("addressid"));
95
						userClient.setDefaultAddress(userinfo.getUserId(), addressId);
96
					}
97
 
507 rajveer 98
				}
99
				return new DefaultHttpHeaders("success");	
100
			} catch (Exception e) {
101
				e.printStackTrace();
102
				return new DefaultHttpHeaders("failure");
103
			}
104
 
105
 
106
		}else{
107
			return new DefaultHttpHeaders("failure");
108
		}
109
 
110
	}
111
 
112
    public void printParams(){
113
    	for(String param : reqparams.keySet()) {
114
    		log.info("param name is " + param);
115
    		log.info("param first is " + reqparams.get(param)[0]);
116
    	}
117
    	log.info(this.reqparams);
118
    }
119
 
120
	@Override
121
	public void setParameters(Map<String, String[]> reqmap) {
122
		log.info("setParameters:" + reqmap);
123
 
124
		this.reqparams = reqmap;
125
	}
126
 
127
    public String getHeaderSnippet(){
128
		return htmlSnippets.get("HEADER");
129
	}
130
 
131
	public String getMainMenuSnippet(){
132
		return htmlSnippets.get("MAIN_MENU");
133
	}
134
 
135
	public String getSearchBarSnippet(){
136
		return htmlSnippets.get("SEARCH_BAR");
137
	}
138
 
139
 
140
	public String getCustomerServiceSnippet(){
141
		return htmlSnippets.get("CUSTOMER_SERVICE");
142
	}
143
 
144
	public String getShippingAddressHeaderSnippet(){
145
		return htmlSnippets.get("SHIPPING_ADDRESS_HEADER");
146
	}
147
 
148
	public String getShippingAddressDetailsSnippet(){
149
		return htmlSnippets.get("SHIPPING_ADDRESS_DETAILS");
150
	}
151
 
152
	public String getMyResearchSnippet(){
153
		return htmlSnippets.get("MY_RESEARCH");
154
	}
155
 
156
	public String getFooterSnippet(){
157
		return htmlSnippets.get("FOOTER");
158
	}
159
 
160
	public String getJsFileSnippet(){
161
		return htmlSnippets.get("JS_FILES");
162
	}
163
 
164
	public String getCssFileSnippet(){
165
		return htmlSnippets.get("CSS_FILES");
166
	}
167
}