Subversion Repositories SmartDukaan

Rev

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