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
 
144
	public String getFooterSnippet(){
145
		return htmlSnippets.get("FOOTER");
146
	}
147
 
148
	public String getJsFileSnippet(){
149
		return htmlSnippets.get("JS_FILES");
150
	}
151
 
152
	public String getCssFileSnippet(){
153
		return htmlSnippets.get("CSS_FILES");
154
	}
155
 
405 rajveer 156
    /**
157
     * 
158
     * @param id
159
     */
160
    public void setId(String id) {
161
        this.id = id;
162
    }
163
 
419 rajveer 164
    public boolean logoutUser(int userId) throws Exception{
165
		UserContextServiceClient userContextServiceClient = null;
166
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
167
 
168
		userContextServiceClient = new UserContextServiceClient();
169
		userClient = userContextServiceClient.getClient();
170
 
171
		userClient.setUserAsLoggedOut(userId, (new Date()).getTime());
172
 
173
    	return true;
174
    }
175
 
176
    public boolean addUpdateUserDetails(int action) throws Exception{
413 rajveer 177
 
178
		UserContextServiceClient userContextServiceClient = null;
179
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
180
 
181
		userContextServiceClient = new UserContextServiceClient();
182
		userClient = userContextServiceClient.getClient();
183
 
184
		switch (action) {
185
		case 1:
186
			String email = this.reqparams.get("email")[0];
187
			String password = this.reqparams.get("password")[0];
188
 
189
			if(userClient.userExists(email)){
190
				return false;
191
			}else{
555 chandransh 192
				User user = new User();
193
				user.setPassword(password);
194
				user.setEmail(email);
195
 
196
				user = userClient.createUser(user);
197
				long userId = user.getUserId();
419 rajveer 198
				userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
199
 
424 rajveer 200
				this.userinfo.setUserId(userId);
201
				this.userinfo.setEmail(email);
202
				this.userinfo.setLoggedIn(true);
419 rajveer 203
 
413 rajveer 204
				return true;
205
			}
206
 
207
			//add address
208
		case 2:
209
			long userId = Long.parseLong(this.reqparams.get("user_id")[0]);
424 rajveer 210
			String name = this.reqparams.get("name")[0];
413 rajveer 211
			String line1 = this.reqparams.get("line1")[0];
212
			String line2 = this.reqparams.get("line2")[0];
213
			String landmark = this.reqparams.get("landmark")[0];
214
			String city = this.reqparams.get("city")[0];
215
			String state = this.reqparams.get("state")[0];
216
			String pin = this.reqparams.get("pin")[0];
217
			String country = this.reqparams.get("country")[0];
424 rajveer 218
			String phone = this.reqparams.get("phone")[0];
413 rajveer 219
 
220
			Address address = new Address();
424 rajveer 221
			address.setName(name);
413 rajveer 222
			address.setLine1(line1);
424 rajveer 223
			address.setLine2(line2);
413 rajveer 224
			address.setLandmark(landmark);
225
			address.setCity(city);
226
			address.setState(state);
227
			address.setPin(pin);
228
			address.setCountry(country);
424 rajveer 229
			address.setPhone(phone);
413 rajveer 230
 
569 rajveer 231
 
232
			if(userClient.addAddressForUser(userId, address, false)>0){
233
				return true;
234
			}else{
235
				return false;
236
			}
237
 
555 chandransh 238
 
239
		case 3:		
413 rajveer 240
			break;
241
 
242
		default:
243
			break;
244
		}
245
		return false;
246
    }
247
 
248
 
405 rajveer 249
 
250
    /**
251
     * 
252
     */
419 rajveer 253
//    @Override
254
//	public void setServletRequest(HttpServletRequest request) {
255
//		this.request = request;
256
//	}
405 rajveer 257
 
258
 
413 rajveer 259
	@Override
260
	public void setParameters(Map<String, String[]> reqmap) {
261
		log.info("setParameters:" + reqmap);
262
 
263
		this.reqparams = reqmap;
264
	}
265
 
266
 
405 rajveer 267
    public String getMyAccountSnippets(){
268
    	return htmlSnippets.get("My_ACCOUNT");
269
    }
270
 
271
    public String getMyOrdersSnippets(){
272
    	return htmlSnippets.get("My_ORDERS");
273
    }
274
 
275
}