Subversion Repositories SmartDukaan

Rev

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