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
 
424 rajveer 86
    // POST /myaccount
413 rajveer 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
 
116
    	if(this.userinfo.isLoggedIn()){
117
    		userId = (new Long(this.userinfo.getUserId())).toString();
118
 
119
			Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
120
			params.put(PageContentKeys.CUSTOMER_ID, userId);
121
			htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
122
 
123
			return new DefaultHttpHeaders("index").disableCaching();
124
 
125
    	}else{
126
    		return new DefaultHttpHeaders("login").disableCaching();
127
    	}
128
 
405 rajveer 129
    }
130
 
131
    public HttpHeaders show(){
132
    	log.info("this.id=" + this.id);
133
 
134
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
135
		params.put(PageContentKeys.CUSTOMER_ID, id);
136
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
137
 
419 rajveer 138
        return new DefaultHttpHeaders("index").disableCaching();
405 rajveer 139
    }
140
 
141
    /**
142
     * 
143
     * @param id
144
     */
145
    public void setId(String id) {
146
        this.id = id;
147
    }
148
 
419 rajveer 149
    public boolean logoutUser(int userId) throws Exception{
150
		UserContextServiceClient userContextServiceClient = null;
151
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
152
 
153
		userContextServiceClient = new UserContextServiceClient();
154
		userClient = userContextServiceClient.getClient();
155
 
156
		userClient.setUserAsLoggedOut(userId, (new Date()).getTime());
157
 
158
    	return true;
159
    }
160
 
161
    public boolean addUpdateUserDetails(int action) throws Exception{
413 rajveer 162
 
163
		UserContextServiceClient userContextServiceClient = null;
164
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
165
 
166
		userContextServiceClient = new UserContextServiceClient();
167
		userClient = userContextServiceClient.getClient();
168
 
169
		switch (action) {
170
		case 1:
171
			String email = this.reqparams.get("email")[0];
172
			String password = this.reqparams.get("password")[0];
173
 
174
			if(userClient.userExists(email)){
175
				return false;
176
			}else{
177
				UserContext context = new UserContext();
178
				UserPrimaryInfo primaryInfo = new UserPrimaryInfo();
179
				UserState userState = new UserState();
180
 
181
				primaryInfo.setPassword(password);
182
				primaryInfo.setEmail(email);
183
				userState.setIsLoggedIn(true);
184
 
185
				context.setPrimaryInfo(primaryInfo);
186
				context.setUserState(userState);
187
 
188
				context = userClient.createContext(context, false);
419 rajveer 189
 
190
				long userId = context.getId();
191
 
192
				userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
193
 
424 rajveer 194
				this.userinfo.setUserId(userId);
195
				this.userinfo.setEmail(email);
196
				this.userinfo.setLoggedIn(true);
419 rajveer 197
 
413 rajveer 198
				return true;
199
			}
200
 
201
			//add address
202
		case 2:
203
			long userId = Long.parseLong(this.reqparams.get("user_id")[0]);
424 rajveer 204
			String name = this.reqparams.get("name")[0];
413 rajveer 205
			String line1 = this.reqparams.get("line1")[0];
206
			String line2 = this.reqparams.get("line2")[0];
207
			String landmark = this.reqparams.get("landmark")[0];
208
			String city = this.reqparams.get("city")[0];
209
			String state = this.reqparams.get("state")[0];
210
			String pin = this.reqparams.get("pin")[0];
211
			String country = this.reqparams.get("country")[0];
424 rajveer 212
			String phone = this.reqparams.get("phone")[0];
413 rajveer 213
 
214
			Address address = new Address();
424 rajveer 215
			address.setName(name);
413 rajveer 216
			address.setLine1(line1);
424 rajveer 217
			address.setLine2(line2);
413 rajveer 218
			address.setLandmark(landmark);
219
			address.setCity(city);
220
			address.setState(state);
221
			address.setPin(pin);
222
			address.setCountry(country);
424 rajveer 223
			address.setPhone(phone);
413 rajveer 224
			long timestamp = (new  Date()).getTime();
225
 
226
			if(userClient.addAddressForUser(address, userId, timestamp)){
227
				return true;
228
			} else{
229
				return false;
230
			}
231
 
232
 
233
		case 3:
234
 
235
			break;
236
 
237
		default:
238
			break;
239
		}
240
		return false;
241
    }
242
 
243
 
405 rajveer 244
 
245
    /**
246
     * 
247
     */
419 rajveer 248
//    @Override
249
//	public void setServletRequest(HttpServletRequest request) {
250
//		this.request = request;
251
//	}
405 rajveer 252
 
253
 
413 rajveer 254
	@Override
255
	public void setParameters(Map<String, String[]> reqmap) {
256
		log.info("setParameters:" + reqmap);
257
 
258
		this.reqparams = reqmap;
259
	}
260
 
261
 
405 rajveer 262
    public String getMyAccountSnippets(){
263
    	return htmlSnippets.get("My_ACCOUNT");
264
    }
265
 
266
    public String getMyOrdersSnippets(){
267
    	return htmlSnippets.get("My_ORDERS");
268
    }
269
 
270
}