Subversion Repositories SmartDukaan

Rev

Rev 637 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 637 Rev 650
Line 1... Line 1...
1
/**
1
/**
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.serving.controllers;
4
package in.shop2020.serving.controllers;
5
 
5
 
6
import in.shop2020.model.v1.user.Address;
-
 
7
import in.shop2020.model.v1.user.User;
-
 
8
import in.shop2020.model.v1.user.UserState;
-
 
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.thrift.clients.UserContextServiceClient;
-
 
13
 
-
 
14
import java.io.UnsupportedEncodingException;
-
 
15
import java.util.Date;
-
 
16
import java.util.GregorianCalendar;
-
 
17
import java.util.HashMap;
-
 
18
import java.util.Map;
-
 
19
 
6
 
20
import org.apache.juli.logging.Log;
7
import org.apache.juli.logging.Log;
21
import org.apache.juli.logging.LogFactory;
8
import org.apache.juli.logging.LogFactory;
22
import org.apache.struts2.convention.annotation.Result;
9
import org.apache.struts2.convention.annotation.Result;
23
import org.apache.struts2.convention.annotation.Results;
10
import org.apache.struts2.convention.annotation.Results;
24
import org.apache.struts2.interceptor.ParameterAware;
-
 
25
import org.apache.struts2.rest.DefaultHttpHeaders;
-
 
26
import org.apache.struts2.rest.HttpHeaders;
-
 
27
 
11
 
28
/**
12
/**
29
 * @author rajveer
13
 * @author rajveer
30
 *
14
 *
31
 */
15
 */
32
 
16
 
33
@Results({
17
@Results({
34
    @Result(name="redirect", type="redirectAction", 
18
    @Result(name="redirect", type="redirectAction", 
35
    		params = {"actionName" , "login"})
19
    		params = {"actionName" , "login"})
36
})
20
})
37
public class MyaccountController extends BaseController 
21
public class MyaccountController extends BaseController {
-
 
22
 
38
	implements ParameterAware {
23
	private static final long serialVersionUID = 1L;
39
 
24
 
40
	
-
 
41
	/**
-
 
42
	 * 
-
 
43
	 */
-
 
44
	private static Log log = LogFactory.getLog(MyaccountController.class);
25
	private static Log log = LogFactory.getLog(MyaccountController.class);
45
	
26
	
46
	private Map<String,String> htmlSnippets;
-
 
47
	/**
-
 
48
	 * 
-
 
49
	 */
-
 
50
	
-
 
51
	
-
 
52
	private Map<String, String[]> reqparams;
-
 
53
	
-
 
54
	private String id;
-
 
55
	
-
 
56
	private int action;
-
 
57
	
-
 
58
	
27
	
59
	public MyaccountController() {
28
	public MyaccountController() {
60
		super();
29
		super();
61
	}
30
	}
62
	
-
 
63
    // POST /myaccount
-
 
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 {
-
 
72
			if(addUpdateUserDetails(action)){   	
-
 
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
    
31
    
85
    // GET /test
-
 
-
 
32
 
86
    public String index(){
33
	public String index(){
87
    	log.info("this.request=" + this.request);
34
    	log.info("this.request=" + this.request);
88
    	
-
 
89
    	String userId = "";
-
 
90
    	
35
 
91
    	if(this.userinfo.isLoggedIn()){
36
		if(!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);
37
			setRedirectUrl();
95
			params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
-
 
96
			params.put(PageContentKeys.IS_LOGGED_IN, userinfo.isLoggedIn()+"");
38
			return "redirect";
-
 
39
		}
-
 
40
 
97
			params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
41
		htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
98
			htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
42
		htmlSnippets.put("MYACCOUNT_DETAILS", pageLoader.getMyaccountDetailsHtml(userinfo.getUserId()));
99
			return "index";
43
		return "index";
100
    	}else{
-
 
101
    		return "redirect";
-
 
102
    	}
-
 
103
    	
44
    	
104
    }
45
    }
105
    
46
    
106
    public HttpHeaders show(){
-
 
107
    	log.info("this.id=" + this.id);
-
 
108
    	log.info("this.id=" + userinfo.getTotalItems());
-
 
109
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
-
 
110
		params.put(PageContentKeys.CUSTOMER_ID, id);
-
 
111
		params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
-
 
112
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
-
 
113
		
-
 
114
        return new DefaultHttpHeaders("index").disableCaching();
-
 
115
    }
-
 
116
    
-
 
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
	
47
	
134
	public String getMyaccountHeaderSnippet(){
48
	public String getMyaccountHeaderSnippet(){
135
		return htmlSnippets.get("MYACCOUNT_HEADER");
49
		return htmlSnippets.get("MYACCOUNT_HEADER");
136
	}
50
	}
137
	
51
	
138
	public String getMyaccountDetailsSnippet(){
52
	public String getMyaccountDetailsSnippet(){
139
		return htmlSnippets.get("MYACCOUNT_DETAILS");
53
		return htmlSnippets.get("MYACCOUNT_DETAILS");
140
	}
54
	}
141
	
55
	
142
	public String getMyResearchSnippet(){
-
 
143
		return htmlSnippets.get("MY_RESEARCH");
-
 
144
	}
-
 
145
				
-
 
146
	
-
 
147
	public String getBrowseHistorySnippet(){
-
 
148
		return htmlSnippets.get("BROWSE_HISTORY");
-
 
149
	}
-
 
150
	
-
 
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
	
-
 
163
    /**
-
 
164
     * 
-
 
165
     * @param id
-
 
166
     */
-
 
167
    public void setId(String id) {
-
 
168
        this.id = id;
-
 
169
    }
-
 
170
 
-
 
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{
-
 
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{
-
 
199
				User user = new User();
-
 
200
				user.setPassword(password);
-
 
201
				user.setEmail(email);
-
 
202
							
-
 
203
				user = userClient.createUser(user);
-
 
204
				long userId = user.getUserId();
-
 
205
				userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
-
 
206
				
-
 
207
				this.userinfo.setUserId(userId);
-
 
208
				this.userinfo.setEmail(email);
-
 
209
				this.userinfo.setLoggedIn(true);
-
 
210
				
-
 
211
				return true;
-
 
212
			}
-
 
213
 
-
 
214
			//add address
-
 
215
		case 2:
-
 
216
			long userId = Long.parseLong(this.reqparams.get("user_id")[0]);
-
 
217
			String name = this.reqparams.get("name")[0];
-
 
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];
-
 
225
			String phone = this.reqparams.get("phone")[0];
-
 
226
	
-
 
227
			Address address = new Address();
-
 
228
			address.setName(name);
-
 
229
			address.setLine1(line1);
-
 
230
			address.setLine2(line2);
-
 
231
			address.setLandmark(landmark);
-
 
232
			address.setCity(city);
-
 
233
			address.setState(state);
-
 
234
			address.setPin(pin);
-
 
235
			address.setCountry(country);
-
 
236
			address.setPhone(phone);
-
 
237
			
-
 
238
			 
-
 
239
			if(userClient.addAddressForUser(userId, address, false)>0){
-
 
240
				return true;
-
 
241
			}else{
-
 
242
				return false;
-
 
243
			}
-
 
244
				
-
 
245
		
-
 
246
		case 3:		
-
 
247
			break;
-
 
248
	
-
 
249
		default:
-
 
250
			break;
-
 
251
		}
-
 
252
		return false;
-
 
253
    }
-
 
254
 
-
 
255
 
-
 
256
    
-
 
257
    /**
-
 
258
     * 
-
 
259
     */
-
 
260
//    @Override
-
 
261
//	public void setServletRequest(HttpServletRequest request) {
-
 
262
//		this.request = request;
-
 
263
//	}
-
 
264
 
-
 
265
    
-
 
266
	@Override
-
 
267
	public void setParameters(Map<String, String[]> reqmap) {
-
 
268
		log.info("setParameters:" + reqmap);
-
 
269
		
-
 
270
		this.reqparams = reqmap;
-
 
271
	}
-
 
272
	
-
 
273
	
-
 
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
}
56
}