Subversion Repositories SmartDukaan

Rev

Rev 9684 | Rev 9697 | 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();
9602 amit.gupta 76
                List<String> cartResponse  = userClient.validateCart(cartId, -1);
9570 anupam.sin 77
                errorMsg = cartResponse.get(0);
78
                if(StringUtils.isNotEmpty(cartResponse.get(1))) {
79
                    addActionMessage(cartResponse.get(1));
80
                }
81
                log.info("Cart Change/EMI Message rcvd from the service is:" + errorMsg);
9667 anupam.sin 82
 
9686 amit.gupta 83
                Cart cart = userClient.getCurrentCart(userinfo.getUserId());
9667 anupam.sin 84
                cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cart, errorMsg));
85
 
9570 anupam.sin 86
            } catch (Exception e) {
87
                // This exception can be ignored for showing the cart. Not so
88
                // innocent when this occurs at the time of checkout or when the
89
                // user is proceeding to pay.
90
                log.warn("Unable to validate the cart: ", e);
91
            }
92
        }
93
        return "index";
94
    }
95
 
96
    public String create() {
9676 anupam.sin 97
        try {
98
            UserClient userServiceClient = new UserClient();
99
            UserContextService.Client userClient = userServiceClient.getClient();
100
            cartMsg = userClient.addItemToCart(cartId, itemId, 1, -1);
9680 amit.gupta 101
            if (!("".equals(cartMsg))) {
9676 anupam.sin 102
                return "outofstock";
103
            }
104
        } catch (TException e) {
105
            log.error("Unable to create or add to cart because of: ", e);
106
        } catch (Exception e) {
107
            log.error("Unable to create or add to cart because of: ", e);
9570 anupam.sin 108
        }
109
 
9676 anupam.sin 110
    return index();
111
    }       
9570 anupam.sin 112
 
9676 anupam.sin 113
 
114
    // DELETE /entity
115
    public String destroy() {
116
        if(cartId > 0)  {
9570 anupam.sin 117
            try {
9676 anupam.sin 118
                UserClient userContextServiceClient = new UserClient();
119
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
120
 
121
                userClient.deleteItemFromCart(cartId, itemId);
122
                return index();
123
            } catch (ShoppingCartException e) {
124
                log.error("Unable to delete item from cart: ", e);
9570 anupam.sin 125
            } catch (TException e) {
9676 anupam.sin 126
                log.error("Unable to delete item from cart: ", e);
9570 anupam.sin 127
            } catch (Exception e) {
9676 anupam.sin 128
                log.error("Unable to delete item from cart: ", e);
9570 anupam.sin 129
            }
130
        }
9676 anupam.sin 131
        return index();
9570 anupam.sin 132
    }
9676 anupam.sin 133
 
134
    //PUT
9570 anupam.sin 135
    public String update() {
9676 anupam.sin 136
        if(quantity <= 0)   {
137
            log.info("Not valid item quantity. Unable to change item quantity.");
138
        } else  {
139
            if(updateItemQuantityInCart(cartId, itemId, quantity))    {
140
                return index();
141
            }
142
        }
143
        return index();
9570 anupam.sin 144
    }
145
 
146
    private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
147
        try {
148
            UserClient userContextServiceClient = new UserClient();
149
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
150
 
9602 amit.gupta 151
            userClient.addItemToCart(cartId, itemId, quantity, -1);
9570 anupam.sin 152
            return true;
153
        } catch (ShoppingCartException e) {
154
            log.error("Unable to update the item quantity in the cart: ", e);
155
        } catch (TException e) {
156
            log.error("Unable to update the item quantity in the cart: ", e);
157
        } catch (Exception e) {
158
            log.error("Unable to update the item quantity in the cart: ", e);
159
        }
160
        return false;
161
    }
9676 anupam.sin 162
 
9570 anupam.sin 163
    public String insureItem() {
9576 anupam.sin 164
//        //TODO : Call a method in userservice that insures the item.
165
//        insuranceResult = "";
166
//        try {
167
//            UserContextService.Client usc = new UserClient().getClient();
168
//            if(usc.insureItem(productId, userinfo.getCartId(), toInsure)) {
169
//                setInsuranceResult("SUCCESS");
170
//            } else {
171
//                setInsuranceResult("FAILURE");
172
//            }
173
//        } catch (Exception e) {
174
//            log.error("Unable to insure item : " + productId + " for cart : " + userinfo.getCartId(), e);
175
//            setInsuranceResult("FAILURE");
176
//        }
9570 anupam.sin 177
        return "insurance-result";
178
    }
179
 
180
    public long getItemId(){
181
        return this.itemId;
182
    }
183
 
9684 amit.gupta 184
    public void setItemId(long itemId){
185
    	this.itemId = itemId;
186
    }
187
 
9570 anupam.sin 188
    public String getTotalAmount() {
189
        return totalamount;
190
    }
191
 
192
    public String getPinCode() {
193
        return pincode;
194
    }
195
 
196
    public String getCouponCode()  {
197
        return couponCode;
198
    }
199
 
200
    public String getDiscountedAmount()   {
201
        return discountedAmount;
202
    }
203
 
204
    public String getErrorMsg()    {
205
        return errorMsg;
206
    }
207
 
9576 anupam.sin 208
//    public long getNumberOfItems(){
209
//        return userinfo.getTotalItems();
210
//    }
9570 anupam.sin 211
 
212
    public String getCartMsg(){
213
        if(cartMsg.equals("")){
214
            return null;
215
        }
216
        return cartMsg;
217
    }
218
 
219
    public String getSnippets(){
220
        String snippets = "";
221
        CatalogClient csc;
222
        try {
223
            csc = new CatalogClient();
224
            List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
225
            for(Long catalogId: similarItems){
9602 amit.gupta 226
                snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
9570 anupam.sin 227
            }
228
        } catch (Exception e) {
229
            log.error("Unable to initialise Catalogservice Client");
230
        }       
231
        return snippets;
232
    }
233
 
234
    @Override
235
    public void setParameters(Map<String, String[]> parameters) {
236
        this.reqparams = parameters;    
237
    }
238
 
9576 anupam.sin 239
//    @Override
240
//    public String getHeaderSnippet() {
241
//        String url = request.getQueryString();
242
//        if (url == null) {
243
//            url = "";
244
//        } else {
245
//            url = "?" + url;
246
//        }
247
//        url = request.getRequestURI() + url;
248
//        return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
249
//    }
9570 anupam.sin 250
 
9576 anupam.sin 251
//    public boolean isUserLoggedIn() {
252
//        return userinfo.isLoggedIn();
253
//    }
9570 anupam.sin 254
 
255
    public void setVariationId(String uri)  {
256
        if (uri.equals("/cart1"))   {
257
            this.variationId = 1;
258
        }
259
    }
260
 
261
    public int getVariationId() {
262
        return this.variationId;
263
    }
264
 
265
    public String getActionMessage(){
266
        if(cartMsg.contains("out of stock")){
267
            return "Notify me when this product is in stock.";
268
        }else {
269
            return "Notify me when this product is available.";
270
        }
271
    }
272
 
273
    public String getOfferNote(){
274
        String note = null;
275
        if(cartMsg.contains("out of stock")){
276
            return note;
277
        }
278
        else {
279
            try {
280
                CatalogClient catalogServiceClient = new CatalogClient();
281
                Client catalogClient = catalogServiceClient.getClient();
282
                Item it = catalogClient.getItem(itemId);
283
                note = it.getBestDealText();
284
            } catch (Exception e)  {
285
                log.error("Unable to get the offertext because of: ", e);
286
            }
287
        }
288
        return note;
289
    }
290
 
291
    public String getInsuranceResult() {
292
        return insuranceResult;
293
    }
294
 
295
    public void setInsuranceResult(String insuranceResult) {
296
        this.insuranceResult = insuranceResult;
297
    }
298
 
299
    public void setToInsure(boolean toInsure) {
300
        this.toInsure = toInsure;
301
    }
302
 
303
    public boolean getToInsure() {
304
        return toInsure;
305
    }
306
 
307
    public long getCartId() {
308
        return cartId;
309
    }
310
 
311
    public void setCartId(long cartId) {
312
        this.cartId = cartId;
313
    }
9667 anupam.sin 314
 
315
 
316
    public void setCartPojoJson(String cartPojoJson) {
317
        this.cartPojoJson = cartPojoJson;
318
    }
319
 
320
 
321
    public String getCartPojoJson() {
322
        return cartPojoJson;
323
    }
9676 anupam.sin 324
 
325
    public void setQuantity(long quantity) {
326
        this.quantity = quantity;
327
    }
328
 
329
    public long getQuantity() {
330
        return quantity;
331
    }
9570 anupam.sin 332
}