Subversion Repositories SmartDukaan

Rev

Rev 9667 | Rev 9680 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9576 anupam.sin 1
package in.shop2020.mobileapi.serving.controllers;
9570 anupam.sin 2
 
3
import in.shop2020.datalogger.EventType;
9577 anupam.sin 4
import in.shop2020.mobileapi.serving.services.ContentServingService;
9667 anupam.sin 5
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
9577 anupam.sin 6
import in.shop2020.mobileapi.serving.utils.SnippetType;
9570 anupam.sin 7
import in.shop2020.model.v1.catalog.CatalogService.Client;
8
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.user.Cart;
10
import in.shop2020.model.v1.user.ShoppingCartException;
11
import in.shop2020.model.v1.user.UserContextService;
12
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.UserClient;
14
import in.shop2020.utils.DataLogger;
15
 
16
import java.util.List;
17
import java.util.Map;
18
import java.util.StringTokenizer;
19
 
20
import org.apache.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
22
import org.apache.struts2.convention.annotation.Action;
23
import org.apache.struts2.convention.annotation.Actions;
24
import org.apache.struts2.convention.annotation.InterceptorRef;
25
import org.apache.struts2.convention.annotation.Result;
26
import org.apache.struts2.convention.annotation.Results;
27
import org.apache.struts2.interceptor.ParameterAware;
28
import org.apache.thrift.TException;
29
 
9667 anupam.sin 30
import com.google.gson.Gson;
9570 anupam.sin 31
 
9667 anupam.sin 32
 
9570 anupam.sin 33
@Results({
34
    @Result(name = "index", location = "cart-index.vm"),
35
    @Result(name="redirect", type="redirectAction", params = {"actionName" , "cart"}),
36
    @Result(name="failure", location="cart-failure.vm"),
37
    @Result(name="success", location="cart-success.vm")
38
})
39
 
40
public class CartController extends BaseController implements ParameterAware{
41
 
42
    private static final long serialVersionUID = 1L;
43
    private static Logger log = Logger.getLogger(Class.class);
44
    Map<String, String[]> reqparams = null;
45
 
46
    private int variationId = 0;
47
    private String totalamount;
48
 
49
    private String errorMsg = "";
50
    private String cartMsg = "";
51
 
52
    private String pincode = "110001";
53
 
54
    private String couponCode = null;
55
 
56
    private String discountedAmount;
57
 
58
    private long itemId;
59
    private String insuranceResult;
60
 
61
    private boolean toInsure;
62
 
9676 anupam.sin 63
 
9570 anupam.sin 64
    private long cartId;
9667 anupam.sin 65
    private String cartPojoJson;
9676 anupam.sin 66
    private long quantity;
9570 anupam.sin 67
 
68
    public CartController(){
69
        super();
70
    }
71
 
72
    public String index()  {
73
        if(cartId != -1){
74
            try {
75
                UserContextService.Client userClient = (new UserClient()).getClient();
76
                Cart cart = userClient.getCurrentCart(userinfo.getUserId());
77
                cartId = cart.getId();
9602 amit.gupta 78
                List<String> cartResponse  = userClient.validateCart(cartId, -1);
9570 anupam.sin 79
                errorMsg = cartResponse.get(0);
80
                if(StringUtils.isNotEmpty(cartResponse.get(1))) {
81
                    addActionMessage(cartResponse.get(1));
82
                }
83
                log.info("Cart Change/EMI Message rcvd from the service is:" + errorMsg);
9667 anupam.sin 84
 
85
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cart, errorMsg));
86
 
9570 anupam.sin 87
            } catch (Exception e) {
88
                // This exception can be ignored for showing the cart. Not so
89
                // innocent when this occurs at the time of checkout or when the
90
                // user is proceeding to pay.
91
                log.warn("Unable to validate the cart: ", e);
92
            }
93
        }
94
        return "index";
95
    }
96
 
97
    public String create() {
98
        long userId = userinfo.getUserId();
99
 
9676 anupam.sin 100
        try {
101
            UserClient userServiceClient = new UserClient();
102
            UserContextService.Client userClient = userServiceClient.getClient();
103
            if (cartId == 0){
104
                cartId = userClient.getUserById(userId).getActiveCartId();
105
            }
106
            cartMsg = userClient.addItemToCart(cartId, itemId, 1, -1);
107
            if ("".equals(cartMsg)) {
108
                return "outofstock";
109
            }
110
        } catch (TException e) {
111
            log.error("Unable to create or add to cart because of: ", e);
112
        } catch (Exception e) {
113
            log.error("Unable to create or add to cart because of: ", e);
9570 anupam.sin 114
        }
115
 
9676 anupam.sin 116
    return index();
117
    }       
9570 anupam.sin 118
 
9676 anupam.sin 119
 
120
    // DELETE /entity
121
    public String destroy() {
122
        if(cartId > 0)  {
9570 anupam.sin 123
            try {
9676 anupam.sin 124
                UserClient userContextServiceClient = new UserClient();
125
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
126
 
127
                userClient.deleteItemFromCart(cartId, itemId);
128
                return index();
129
            } catch (ShoppingCartException e) {
130
                log.error("Unable to delete item from cart: ", e);
9570 anupam.sin 131
            } catch (TException e) {
9676 anupam.sin 132
                log.error("Unable to delete item from cart: ", e);
9570 anupam.sin 133
            } catch (Exception e) {
9676 anupam.sin 134
                log.error("Unable to delete item from cart: ", e);
9570 anupam.sin 135
            }
136
        }
9676 anupam.sin 137
        return index();
9570 anupam.sin 138
    }
9676 anupam.sin 139
 
140
    //PUT
9570 anupam.sin 141
    public String update() {
9676 anupam.sin 142
        if(quantity <= 0)   {
143
            log.info("Not valid item quantity. Unable to change item quantity.");
144
        } else  {
145
            if(updateItemQuantityInCart(cartId, itemId, quantity))    {
146
                return index();
147
            }
148
        }
149
        return index();
9570 anupam.sin 150
    }
151
 
152
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
153
        try {
154
            UserClient userContextServiceClient = new UserClient();
155
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
156
 
9602 amit.gupta 157
            userClient.addItemToCart(cartId, itemId, quantity, -1);
9570 anupam.sin 158
            return true;
159
        } catch (ShoppingCartException e) {
160
            log.error("Unable to update the item quantity in the cart: ", e);
161
        } catch (TException e) {
162
            log.error("Unable to update the item quantity in the cart: ", e);
163
        } catch (Exception e) {
164
            log.error("Unable to update the item quantity in the cart: ", e);
165
        }
166
        return false;
167
    }
9676 anupam.sin 168
 
9570 anupam.sin 169
    public String insureItem() {
9576 anupam.sin 170
//        //TODO : Call a method in userservice that insures the item.
171
//        insuranceResult = "";
172
//        try {
173
//            UserContextService.Client usc = new UserClient().getClient();
174
//            if(usc.insureItem(productId, userinfo.getCartId(), toInsure)) {
175
//                setInsuranceResult("SUCCESS");
176
//            } else {
177
//                setInsuranceResult("FAILURE");
178
//            }
179
//        } catch (Exception e) {
180
//            log.error("Unable to insure item : " + productId + " for cart : " + userinfo.getCartId(), e);
181
//            setInsuranceResult("FAILURE");
182
//        }
9570 anupam.sin 183
        return "insurance-result";
184
    }
185
 
186
    public long getItemId(){
187
        return this.itemId;
188
    }
189
 
190
    public String getTotalAmount() {
191
        return totalamount;
192
    }
193
 
194
    public String getPinCode() {
195
        return pincode;
196
    }
197
 
198
    public String getCouponCode()  {
199
        return couponCode;
200
    }
201
 
202
    public String getDiscountedAmount()   {
203
        return discountedAmount;
204
    }
205
 
206
    public String getErrorMsg()    {
207
        return errorMsg;
208
    }
209
 
9576 anupam.sin 210
//    public long getNumberOfItems(){
211
//        return userinfo.getTotalItems();
212
//    }
9570 anupam.sin 213
 
214
    public String getCartMsg(){
215
        if(cartMsg.equals("")){
216
            return null;
217
        }
218
        return cartMsg;
219
    }
220
 
221
    public String getSnippets(){
222
        String snippets = "";
223
        CatalogClient csc;
224
        try {
225
            csc = new CatalogClient();
226
            List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
227
            for(Long catalogId: similarItems){
9602 amit.gupta 228
                snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
9570 anupam.sin 229
            }
230
        } catch (Exception e) {
231
            log.error("Unable to initialise Catalogservice Client");
232
        }       
233
        return snippets;
234
    }
235
 
236
    @Override
237
    public void setParameters(Map<String, String[]> parameters) {
238
        this.reqparams = parameters;    
239
    }
240
 
9576 anupam.sin 241
//    @Override
242
//    public String getHeaderSnippet() {
243
//        String url = request.getQueryString();
244
//        if (url == null) {
245
//            url = "";
246
//        } else {
247
//            url = "?" + url;
248
//        }
249
//        url = request.getRequestURI() + url;
250
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
251
//    }
9570 anupam.sin 252
 
9576 anupam.sin 253
//    public boolean isUserLoggedIn() {
254
//        return userinfo.isLoggedIn();
255
//    }
9570 anupam.sin 256
 
257
    public void setVariationId(String uri)  {
258
        if (uri.equals("/cart1"))   {
259
            this.variationId = 1;
260
        }
261
    }
262
 
263
    public int getVariationId() {
264
        return this.variationId;
265
    }
266
 
267
    public String getActionMessage(){
268
        if(cartMsg.contains("out of stock")){
269
            return "Notify me when this product is in stock.";
270
        }else {
271
            return "Notify me when this product is available.";
272
        }
273
    }
274
 
275
    public String getOfferNote(){
276
        String note = null;
277
        if(cartMsg.contains("out of stock")){
278
            return note;
279
        }
280
        else {
281
            try {
282
                CatalogClient catalogServiceClient = new CatalogClient();
283
                Client catalogClient = catalogServiceClient.getClient();
284
                Item it = catalogClient.getItem(itemId);
285
                note = it.getBestDealText();
286
            } catch (Exception e)  {
287
                log.error("Unable to get the offertext because of: ", e);
288
            }
289
        }
290
        return note;
291
    }
292
 
293
    public String getInsuranceResult() {
294
        return insuranceResult;
295
    }
296
 
297
    public void setInsuranceResult(String insuranceResult) {
298
        this.insuranceResult = insuranceResult;
299
    }
300
 
301
    public void setToInsure(boolean toInsure) {
302
        this.toInsure = toInsure;
303
    }
304
 
305
    public boolean getToInsure() {
306
        return toInsure;
307
    }
308
 
309
    public long getCartId() {
310
        return cartId;
311
    }
312
 
313
    public void setCartId(long cartId) {
314
        this.cartId = cartId;
315
    }
9667 anupam.sin 316
 
317
 
318
    public void setCartPojoJson(String cartPojoJson) {
319
        this.cartPojoJson = cartPojoJson;
320
    }
321
 
322
 
323
    public String getCartPojoJson() {
324
        return cartPojoJson;
325
    }
9676 anupam.sin 326
 
327
    public void setQuantity(long quantity) {
328
        this.quantity = quantity;
329
    }
330
 
331
    public long getQuantity() {
332
        return quantity;
333
    }
9570 anupam.sin 334
}