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