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