Subversion Repositories SmartDukaan

Rev

Go to most recent revision | 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 
413 rajveer 54
	implements ServletRequestAware, 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
	private HttpServletRequest request;
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 {
95
			if(addUpdateuserDetails(action)){   	
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 {
110
    	log.info("this.request=" + this.request);
111
 
112
        return new DefaultHttpHeaders("index")
113
            .disableCaching();
114
    }
115
 
116
    public HttpHeaders show(){
117
    	log.info("this.id=" + this.id);
118
 
119
		Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
120
		params.put(PageContentKeys.CUSTOMER_ID, id);
121
		htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
122
 
123
 
124
        return new DefaultHttpHeaders("index")
125
        .disableCaching();
126
    }
127
 
128
    /**
129
     * 
130
     * @param id
131
     */
132
    public void setId(String id) {
133
        this.id = id;
134
    }
135
 
413 rajveer 136
    public boolean addUpdateuserDetails(int action) throws Exception{
137
 
138
		UserContextServiceClient userContextServiceClient = null;
139
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
140
 
141
		userContextServiceClient = new UserContextServiceClient();
142
		userClient = userContextServiceClient.getClient();
143
 
144
		switch (action) {
145
		case 1:
146
			String email = this.reqparams.get("email")[0];
147
			String password = this.reqparams.get("password")[0];
148
 
149
			if(userClient.userExists(email)){
150
				return false;
151
			}else{
152
				UserContext context = new UserContext();
153
				UserPrimaryInfo primaryInfo = new UserPrimaryInfo();
154
				UserState userState = new UserState();
155
 
156
				primaryInfo.setPassword(password);
157
				primaryInfo.setEmail(email);
158
				userState.setIsLoggedIn(true);
159
 
160
				context.setPrimaryInfo(primaryInfo);
161
				context.setUserState(userState);
162
 
163
//				context.getPrimaryInfo().setPassword(email);
164
//				context.getPrimaryInfo().setPassword(password);
165
//				context.getUserState().setIsLoggedIn(true);
166
 
167
				context = userClient.createContext(context, false);
168
				return true;
169
			}
170
 
171
			//add address
172
		case 2:
173
			long userId = Long.parseLong(this.reqparams.get("user_id")[0]);
174
			String line1 = this.reqparams.get("line1")[0];
175
			String line2 = this.reqparams.get("line2")[0];
176
			String landmark = this.reqparams.get("landmark")[0];
177
			String city = this.reqparams.get("city")[0];
178
			String state = this.reqparams.get("state")[0];
179
			String pin = this.reqparams.get("pin")[0];
180
			String country = this.reqparams.get("country")[0];
181
 
182
 
183
			Address address = new Address();
184
			address.setLine1(line1);
185
			address.setLine1(line2);
186
			address.setLandmark(landmark);
187
			address.setCity(city);
188
			address.setState(state);
189
			address.setPin(pin);
190
			address.setCountry(country);
191
			long timestamp = (new  Date()).getTime();
192
 
193
			if(userClient.addAddressForUser(address, userId, timestamp)){
194
				return true;
195
			} else{
196
				return false;
197
			}
198
 
199
 
200
		case 3:
201
 
202
			break;
203
 
204
		default:
205
			break;
206
		}
207
		return false;
208
    }
209
 
210
 
405 rajveer 211
 
212
    /**
213
     * 
214
     */
215
    @Override
216
	public void setServletRequest(HttpServletRequest request) {
217
		this.request = request;
218
	}
219
 
220
 
413 rajveer 221
	@Override
222
	public void setParameters(Map<String, String[]> reqmap) {
223
		log.info("setParameters:" + reqmap);
224
 
225
		this.reqparams = reqmap;
226
	}
227
 
228
 
405 rajveer 229
    public String getMyAccountSnippets(){
230
    	return htmlSnippets.get("My_ACCOUNT");
231
    }
232
 
233
    public String getMyOrdersSnippets(){
234
    	return htmlSnippets.get("My_ORDERS");
235
    }
236
 
237
}