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;
7
import in.shop2020.model.v1.user.UserContext;
8
import in.shop2020.model.v1.user.UserContextException;
9
import in.shop2020.model.v1.user.UserPrimaryInfo;
10
import in.shop2020.model.v1.user.UserState;
405 rajveer 11
import in.shop2020.serving.pages.PageContentKeys;
12
import in.shop2020.serving.pages.PageEnum;
13
import in.shop2020.serving.pages.PageManager;
413 rajveer 14
import in.shop2020.thrift.clients.ShoppingCartClient;
15
import in.shop2020.thrift.clients.UserContextServiceClient;
405 rajveer 16
 
17
import java.io.UnsupportedEncodingException;
18
import java.net.URLEncoder;
413 rajveer 19
import java.sql.Timestamp;
405 rajveer 20
import java.util.ArrayList;
21
import java.util.Arrays;
413 rajveer 22
import java.util.Date;
405 rajveer 23
import java.util.HashMap;
24
import java.util.LinkedHashMap;
25
import java.util.List;
26
import java.util.Map;
27
 
413 rajveer 28
import javax.servlet.http.Cookie;
405 rajveer 29
import javax.servlet.http.HttpServletRequest;
30
 
31
import org.apache.commons.lang.StringUtils;
32
import org.apache.juli.logging.Log;
33
import org.apache.juli.logging.LogFactory;
413 rajveer 34
import org.apache.struts2.convention.annotation.Result;
35
import org.apache.struts2.convention.annotation.Results;
36
import org.apache.struts2.interceptor.ParameterAware;
405 rajveer 37
import org.apache.struts2.interceptor.ServletRequestAware;
38
import org.apache.struts2.rest.DefaultHttpHeaders;
39
import org.apache.struts2.rest.HttpHeaders;
413 rajveer 40
import org.apache.thrift.TException;
405 rajveer 41
 
42
import com.opensymphony.xwork2.ModelDriven;
43
 
44
/**
413 rajveer 45
 * @author rajveer
405 rajveer 46
 *
47
 */
413 rajveer 48
@Results({
49
    @Result(name="success", type="redirectAction", 
50
    		params = {"actionName" , "myaccount"})
51
})
52
 
405 rajveer 53
public class MyaccountController extends BaseController 
419 rajveer 54
	implements ParameterAware {
405 rajveer 55
 
56
 
57
	/**
58
	 * 
59
	 */
60
	private static Log log = LogFactory.getLog(MyaccountController.class);
61
 
62
	private Map<String,String> htmlSnippets;
63
	/**
64
	 * 
65
	 */
66
 
419 rajveer 67
 
413 rajveer 68
	private Map<String, String[]> reqparams;
69
 
405 rajveer 70
	private String id;
71
 
413 rajveer 72
	private int action;
73
 
74
	enum actionType{
75
		LOGIN_PASSWORD,
76
		ADDRESS,
77
		LOGIN_PASSWORD_ADDRESS
78
	}
79
 
405 rajveer 80
	public MyaccountController() {
81
		// TODO Auto-generated constructor stub
82
		super();
83
 
84
	}
85
 
413 rajveer 86
    // POST /entity
87
    public String create() {
88
    	log.info("MyAccountController.create");
89
    	log.info("action type is " + this.reqparams.get("action"));
90
 
91
    	action = Integer.parseInt(this.reqparams.get("action")[0]);
92
 
93
 
94
    	try {
419 rajveer 95
			if(addUpdateUserDetails(action)){   	
413 rajveer 96
				return "success";
97
			}else{
98
				return "failure";
99
			}
100
		} catch (Exception e) {
101
			// TODO Auto-generated catch block
102
			e.printStackTrace();
103
		}
104
		return "failure";
105
    }
106
 
107
 
405 rajveer 108
    // GET /test
109
    public HttpHeaders index() throws UnsupportedEncodingException {
419 rajveer 110
    	//log.info("this.request=" + this.request);
111
 
112
 
113
    	String userId = "";
114
 
115
    	//if(this.session.getAttribute("loggedin").toString().equals("true")){
116
		//	userId = this.session.getAttribute("userid").toString();
117
 
118
    	if(this.userinfo.isLoggedIn()){
119
    		userId = (new Long(this.userinfo.getUserId())).toString();
120
 
121
			Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
122
			params.put(PageContentKeys.CUSTOMER_ID, userId);
123
			htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
124
 
125
			return new DefaultHttpHeaders("index").disableCaching();
126
 
127
    	}else{
128
    		return new DefaultHttpHeaders("login").disableCaching();
129
    	}
130
 
405 rajveer 131
    }
132
 
133
    public HttpHeaders show(){
134
    	log.info("this.id=" + this.id);
135
 
136
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
137
		params.put(PageContentKeys.CUSTOMER_ID, id);
138
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
139
 
140
 
419 rajveer 141
        return new DefaultHttpHeaders("index").disableCaching();
405 rajveer 142
    }
143
 
144
    /**
145
     * 
146
     * @param id
147
     */
148
    public void setId(String id) {
149
        this.id = id;
150
    }
151
 
419 rajveer 152
    public boolean logoutUser(int userId) throws Exception{
153
		UserContextServiceClient userContextServiceClient = null;
154
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
155
 
156
		userContextServiceClient = new UserContextServiceClient();
157
		userClient = userContextServiceClient.getClient();
158
 
159
		userClient.setUserAsLoggedOut(userId, (new Date()).getTime());
160
 
161
    	return true;
162
    }
163
 
164
    public boolean addUpdateUserDetails(int action) throws Exception{
413 rajveer 165
 
166
		UserContextServiceClient userContextServiceClient = null;
167
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
168
 
169
		userContextServiceClient = new UserContextServiceClient();
170
		userClient = userContextServiceClient.getClient();
171
 
172
		switch (action) {
173
		case 1:
174
			String email = this.reqparams.get("email")[0];
175
			String password = this.reqparams.get("password")[0];
176
 
177
			if(userClient.userExists(email)){
178
				return false;
179
			}else{
180
				UserContext context = new UserContext();
181
				UserPrimaryInfo primaryInfo = new UserPrimaryInfo();
182
				UserState userState = new UserState();
183
 
184
				primaryInfo.setPassword(password);
185
				primaryInfo.setEmail(email);
186
				userState.setIsLoggedIn(true);
187
 
188
				context.setPrimaryInfo(primaryInfo);
189
				context.setUserState(userState);
190
 
191
//				context.getPrimaryInfo().setPassword(email);
192
//				context.getPrimaryInfo().setPassword(password);
193
//				context.getUserState().setIsLoggedIn(true);
194
 
195
				context = userClient.createContext(context, false);
419 rajveer 196
 
197
				long userId = context.getId();
198
 
199
				userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
200
				this.session.removeAttribute("useid");
201
				this.session.removeAttribute("loggedin");
202
				this.session.removeAttribute("issessionid");
203
				this.session.removeAttribute("email");
204
 
205
				this.session.setAttribute("userid", userId+"");
206
				this.session.setAttribute("loggedin", "true");
207
				this.session.setAttribute("issessionid", "false");
208
				this.session.setAttribute("email",email);
209
 
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]);
217
			String line1 = this.reqparams.get("line1")[0];
218
			String line2 = this.reqparams.get("line2")[0];
219
			String landmark = this.reqparams.get("landmark")[0];
220
			String city = this.reqparams.get("city")[0];
221
			String state = this.reqparams.get("state")[0];
222
			String pin = this.reqparams.get("pin")[0];
223
			String country = this.reqparams.get("country")[0];
224
 
225
 
226
			Address address = new Address();
227
			address.setLine1(line1);
228
			address.setLine1(line2);
229
			address.setLandmark(landmark);
230
			address.setCity(city);
231
			address.setState(state);
232
			address.setPin(pin);
233
			address.setCountry(country);
234
			long timestamp = (new  Date()).getTime();
235
 
236
			if(userClient.addAddressForUser(address, userId, timestamp)){
237
				return true;
238
			} else{
239
				return false;
240
			}
241
 
242
 
243
		case 3:
244
 
245
			break;
246
 
247
		default:
248
			break;
249
		}
250
		return false;
251
    }
252
 
253
 
405 rajveer 254
 
255
    /**
256
     * 
257
     */
419 rajveer 258
//    @Override
259
//	public void setServletRequest(HttpServletRequest request) {
260
//		this.request = request;
261
//	}
405 rajveer 262
 
263
 
413 rajveer 264
	@Override
265
	public void setParameters(Map<String, String[]> reqmap) {
266
		log.info("setParameters:" + reqmap);
267
 
268
		this.reqparams = reqmap;
269
	}
270
 
271
 
405 rajveer 272
    public String getMyAccountSnippets(){
273
    	return htmlSnippets.get("My_ACCOUNT");
274
    }
275
 
276
    public String getMyOrdersSnippets(){
277
    	return htmlSnippets.get("My_ORDERS");
278
    }
279
 
280
}