Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.model.v1.user.Address;
555 chandransh 7
import in.shop2020.model.v1.user.User;
507 rajveer 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;
620 rajveer 12
import in.shop2020.serving.services.PageLoaderHandler;
507 rajveer 13
import in.shop2020.thrift.clients.UserContextServiceClient;
14
 
15
import java.io.UnsupportedEncodingException;
16
import java.util.Date;
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;
22
import org.apache.struts2.convention.annotation.Result;
23
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
 
28
/**
29
 * @author rajveer
30
 *
31
 */
32
 
33
public class CompletedordersController extends BaseController 
34
	implements ParameterAware {
35
 
36
 
37
	/**
38
	 * 
39
	 */
40
	private static Log log = LogFactory.getLog(CompletedordersController.class);
41
 
620 rajveer 42
	private Map<String,String> htmlSnippets = new HashMap<String, String>();
507 rajveer 43
	/**
44
	 * 
45
	 */
46
 
47
 
48
	private Map<String, String[]> reqparams;
49
 
50
	private String id;
51
 
52
	private int action;
53
 
54
 
55
	public CompletedordersController() {
56
		super();
57
	}
58
 
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
82
    public HttpHeaders index() throws UnsupportedEncodingException {
83
    	log.info("this.request=" + this.request);
84
 
85
    	String userId = "";
86
 
87
    	if(this.userinfo.isLoggedIn()){
620 rajveer 88
    		PageLoaderHandler pageLoader = new PageLoaderHandler();
89
    		htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(this.userinfo.isLoggedIn(), this.userinfo.getNameOfUser()));
90
    		htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
91
    		htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 0));
92
 
93
    		htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
94
    		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", "");
507 rajveer 103
 
104
			return new DefaultHttpHeaders("index").disableCaching();
105
 
106
    	}else{
107
    		return new DefaultHttpHeaders("login").disableCaching();
108
    	}
109
 
110
    }
111
 
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(){
141
		return htmlSnippets.get("MYACCOUNT_HEADER");
142
	}
143
 
144
	public String getMyaccountDetailsSnippet(){
145
		return htmlSnippets.get("MYACCOUNT_DETAILS");
146
	}
147
 
148
	public String getMyResearchSnippet(){
149
		return htmlSnippets.get("MY_RESEARCH");
150
	}
151
 
620 rajveer 152
	public String getBrowseHistorySnippet(){
153
		return htmlSnippets.get("BROWSE_HISTORY");
154
	}
155
 
507 rajveer 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{
555 chandransh 204
				User user = new User();
205
				user.setPassword(password);
206
				user.setEmail(email);
207
 
208
				user = userClient.createUser(user);
209
				long userId = user.getUserId();
507 rajveer 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
 
569 rajveer 243
			if(userClient.addAddressForUser(userId, address, false)>0){
244
				return true;
245
			}else{
246
				return false;
247
			}
507 rajveer 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
 
286
}