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