Subversion Repositories SmartDukaan

Rev

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

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