Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
405 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
413 rajveer 6
import in.shop2020.model.v1.user.Address;
555 chandransh 7
import in.shop2020.model.v1.user.User;
413 rajveer 8
import in.shop2020.model.v1.user.UserState;
405 rajveer 9
import in.shop2020.serving.pages.PageContentKeys;
10
import in.shop2020.serving.pages.PageEnum;
11
import in.shop2020.serving.pages.PageManager;
413 rajveer 12
import in.shop2020.thrift.clients.UserContextServiceClient;
405 rajveer 13
 
14
import java.io.UnsupportedEncodingException;
413 rajveer 15
import java.util.Date;
555 chandransh 16
import java.util.GregorianCalendar;
405 rajveer 17
import java.util.HashMap;
18
import java.util.Map;
19
 
20
import org.apache.juli.logging.Log;
21
import org.apache.juli.logging.LogFactory;
413 rajveer 22
import org.apache.struts2.convention.annotation.Result;
23
import org.apache.struts2.convention.annotation.Results;
24
import org.apache.struts2.interceptor.ParameterAware;
405 rajveer 25
import org.apache.struts2.rest.DefaultHttpHeaders;
26
import org.apache.struts2.rest.HttpHeaders;
27
 
28
/**
413 rajveer 29
 * @author rajveer
405 rajveer 30
 *
31
 */
413 rajveer 32
 
405 rajveer 33
public class MyaccountController extends BaseController 
419 rajveer 34
	implements ParameterAware {
405 rajveer 35
 
36
 
37
	/**
38
	 * 
39
	 */
40
	private static Log log = LogFactory.getLog(MyaccountController.class);
41
 
42
	private Map<String,String> htmlSnippets;
43
	/**
44
	 * 
45
	 */
46
 
419 rajveer 47
 
413 rajveer 48
	private Map<String, String[]> reqparams;
49
 
405 rajveer 50
	private String id;
51
 
413 rajveer 52
	private int action;
53
 
54
 
405 rajveer 55
	public MyaccountController() {
56
		super();
57
	}
58
 
424 rajveer 59
    // POST /myaccount
413 rajveer 60
    public String create() {
61
    	log.info("MyAccountController.create");
62
    	log.info("action type is " + this.reqparams.get("action"));
63
 
64
    	action = Integer.parseInt(this.reqparams.get("action")[0]);
65
 
66
 
67
    	try {
419 rajveer 68
			if(addUpdateUserDetails(action)){   	
413 rajveer 69
				return "success";
70
			}else{
71
				return "failure";
72
			}
73
		} catch (Exception e) {
74
			// TODO Auto-generated catch block
75
			e.printStackTrace();
76
		}
77
		return "failure";
78
    }
79
 
80
 
405 rajveer 81
    // GET /test
82
    public HttpHeaders index() throws UnsupportedEncodingException {
507 rajveer 83
    	log.info("this.request=" + this.request);
419 rajveer 84
 
85
    	String userId = "";
86
 
87
    	if(this.userinfo.isLoggedIn()){
88
    		userId = (new Long(this.userinfo.getUserId())).toString();
89
			Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
90
			params.put(PageContentKeys.CUSTOMER_ID, userId);
507 rajveer 91
			params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
555 chandransh 92
			params.put(PageContentKeys.IS_LOGGED_IN, userinfo.isLoggedIn()+"");
93
			params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
419 rajveer 94
			htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
95
 
96
			return new DefaultHttpHeaders("index").disableCaching();
97
 
98
    	}else{
99
    		return new DefaultHttpHeaders("login").disableCaching();
100
    	}
101
 
405 rajveer 102
    }
103
 
104
    public HttpHeaders show(){
105
    	log.info("this.id=" + this.id);
507 rajveer 106
    	log.info("this.id=" + userinfo.getTotalItems());
405 rajveer 107
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
108
		params.put(PageContentKeys.CUSTOMER_ID, id);
507 rajveer 109
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
405 rajveer 110
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
111
 
419 rajveer 112
        return new DefaultHttpHeaders("index").disableCaching();
405 rajveer 113
    }
114
 
507 rajveer 115
    public String getHeaderSnippet(){
116
		return htmlSnippets.get("HEADER");
117
	}
118
 
119
	public String getMainMenuSnippet(){
120
		return htmlSnippets.get("MAIN_MENU");
121
	}
122
 
123
	public String getSearchBarSnippet(){
124
		return htmlSnippets.get("SEARCH_BAR");
125
	}
126
 
127
 
128
	public String getCustomerServiceSnippet(){
129
		return htmlSnippets.get("CUSTOMER_SERVICE");
130
	}
131
 
132
	public String getMyaccountHeaderSnippet(){
133
		return htmlSnippets.get("MYACCOUNT_HEADER");
134
	}
135
 
136
	public String getMyaccountDetailsSnippet(){
137
		return htmlSnippets.get("MYACCOUNT_DETAILS");
138
	}
139
 
140
	public String getMyResearchSnippet(){
141
		return htmlSnippets.get("MY_RESEARCH");
142
	}
143
 
620 rajveer 144
 
145
	public String getBrowseHistorySnippet(){
146
		return htmlSnippets.get("BROWSE_HISTORY");
147
	}
148
 
507 rajveer 149
	public String getFooterSnippet(){
150
		return htmlSnippets.get("FOOTER");
151
	}
152
 
153
	public String getJsFileSnippet(){
154
		return htmlSnippets.get("JS_FILES");
155
	}
156
 
157
	public String getCssFileSnippet(){
158
		return htmlSnippets.get("CSS_FILES");
159
	}
160
 
405 rajveer 161
    /**
162
     * 
163
     * @param id
164
     */
165
    public void setId(String id) {
166
        this.id = id;
167
    }
168
 
419 rajveer 169
    public boolean logoutUser(int userId) throws Exception{
170
		UserContextServiceClient userContextServiceClient = null;
171
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
172
 
173
		userContextServiceClient = new UserContextServiceClient();
174
		userClient = userContextServiceClient.getClient();
175
 
176
		userClient.setUserAsLoggedOut(userId, (new Date()).getTime());
177
 
178
    	return true;
179
    }
180
 
181
    public boolean addUpdateUserDetails(int action) throws Exception{
413 rajveer 182
 
183
		UserContextServiceClient userContextServiceClient = null;
184
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
185
 
186
		userContextServiceClient = new UserContextServiceClient();
187
		userClient = userContextServiceClient.getClient();
188
 
189
		switch (action) {
190
		case 1:
191
			String email = this.reqparams.get("email")[0];
192
			String password = this.reqparams.get("password")[0];
193
 
194
			if(userClient.userExists(email)){
195
				return false;
196
			}else{
555 chandransh 197
				User user = new User();
198
				user.setPassword(password);
199
				user.setEmail(email);
200
 
201
				user = userClient.createUser(user);
202
				long userId = user.getUserId();
419 rajveer 203
				userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
204
 
424 rajveer 205
				this.userinfo.setUserId(userId);
206
				this.userinfo.setEmail(email);
207
				this.userinfo.setLoggedIn(true);
419 rajveer 208
 
413 rajveer 209
				return true;
210
			}
211
 
212
			//add address
213
		case 2:
214
			long userId = Long.parseLong(this.reqparams.get("user_id")[0]);
424 rajveer 215
			String name = this.reqparams.get("name")[0];
413 rajveer 216
			String line1 = this.reqparams.get("line1")[0];
217
			String line2 = this.reqparams.get("line2")[0];
218
			String landmark = this.reqparams.get("landmark")[0];
219
			String city = this.reqparams.get("city")[0];
220
			String state = this.reqparams.get("state")[0];
221
			String pin = this.reqparams.get("pin")[0];
222
			String country = this.reqparams.get("country")[0];
424 rajveer 223
			String phone = this.reqparams.get("phone")[0];
413 rajveer 224
 
225
			Address address = new Address();
424 rajveer 226
			address.setName(name);
413 rajveer 227
			address.setLine1(line1);
424 rajveer 228
			address.setLine2(line2);
413 rajveer 229
			address.setLandmark(landmark);
230
			address.setCity(city);
231
			address.setState(state);
232
			address.setPin(pin);
233
			address.setCountry(country);
424 rajveer 234
			address.setPhone(phone);
413 rajveer 235
 
569 rajveer 236
 
237
			if(userClient.addAddressForUser(userId, address, false)>0){
238
				return true;
239
			}else{
240
				return false;
241
			}
242
 
555 chandransh 243
 
244
		case 3:		
413 rajveer 245
			break;
246
 
247
		default:
248
			break;
249
		}
250
		return false;
251
    }
252
 
253
 
405 rajveer 254
 
255
    /**
256
     * 
257
     */
419 rajveer 258
//    @Override
259
//	public void setServletRequest(HttpServletRequest request) {
260
//		this.request = request;
261
//	}
405 rajveer 262
 
263
 
413 rajveer 264
	@Override
265
	public void setParameters(Map<String, String[]> reqmap) {
266
		log.info("setParameters:" + reqmap);
267
 
268
		this.reqparams = reqmap;
269
	}
270
 
271
 
405 rajveer 272
    public String getMyAccountSnippets(){
273
    	return htmlSnippets.get("My_ACCOUNT");
274
    }
275
 
276
    public String getMyOrdersSnippets(){
277
    	return htmlSnippets.get("My_ORDERS");
278
    }
279
 
280
}