Subversion Repositories SmartDukaan

Rev

Rev 2145 | Rev 3126 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import java.io.IOException;
import java.lang.NumberFormatException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.model.v1.user.UserContextService;
import in.shop2020.model.v1.user.UserCommunicationType;
import in.shop2020.serving.controllers.BaseController;
import in.shop2020.thrift.clients.TransactionServiceClient;
import in.shop2020.thrift.clients.UserContextServiceClient;
import in.shop2020.serving.utils.UserMessage;

import org.apache.log4j.Logger;

/**
 * @author Varun Gupta
 */

@SuppressWarnings("serial")
public class ContactUsController extends BaseController {

    private String id;

    private static Logger log = Logger.getLogger(Class.class);

    private UserCommunicationType formType = null;

    List<Order> orders = null;
    List<LineItem> products = null;
    List<Long> order_ids = null;

    public ContactUsController() {
        super();
    }

    // GET /Show Form
    public String index() {
        try {
            TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
            in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();
            orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0, (new Date()).getTime(), null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (request.getParameter("type") != null) {
            String requestedFormType = request.getParameter("type").toString().trim();
            log.debug("Contact us requested for: " + requestedFormType);
            
            if (requestedFormType.equals("return")) {
                this.formType = UserCommunicationType.RETURN_FORM;
            } else if (requestedFormType.equals("cancel")) {
                this.formType = UserCommunicationType.ORDER_CANCELLATION;
            } else if (requestedFormType.equals("delivery")) {
                this.formType = UserCommunicationType.DELIVERY_PROBLEM;
            }
        }
        return "index";
    }

    public String show() throws SecurityException, IOException {
        try {
            TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
            in.shop2020.model.v1.order.TransactionService.Client orderClient = transactionServiceClient.getClient();

            if (id.equals("to_return")) {
                order_ids = orderClient.getReturnableOrdersForCustomer(userinfo.getUserId(), 0);
            } else if (id.equals("to_cancel")) {
                order_ids = orderClient.getCancellableOrdersForCustomer(userinfo.getUserId(), 0);
            } else if (id.equals("all_orders")) {
                orders = orderClient.getOrdersForCustomer(userinfo.getUserId(), 0, (new Date()).getTime(), null);
            } else {
                long orderId = Integer.parseInt(id);
                products = orderClient.getLineItemsForOrder(orderId);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ajax";
    }

    // POST /Save Communication
    public String create() {
        long communicationType = -1;
        long orderId = -1;

        try {
            boolean u = userinfo.isLoggedIn();
            long userId = u ? userinfo.getUserId() : 0;
            String email = request.getParameter("email");
            communicationType = Integer.parseInt(request.getParameter("communication_type"));

            if (request.getParameter("order_id") != null) {
                orderId = Integer.parseInt(request.getParameter("order_id"));
            }
            String awb = request.getParameter("awb");
            String product = request.getParameter("product");
            String subject = request.getParameter("subject");
            String message = request.getParameter("message");

            UserContextService.Client userClient = (new UserContextServiceClient()).getClient();

            if (userClient.saveUserCommunication(userId, email, communicationType, orderId, awb, product, subject,
                    message)) {
                System.out.println("User Communication saved successfully!");
            }

        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (UserContextException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return "success";
    }

    public String getId() {
        return this.id;
    }

    /**
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    public UserCommunicationType getFormType() {
        return this.formType;
    }

    public String getSuccessMsg() {
        return UserMessage.USER_COMMUNICATION_SUCCESS;
    }

    private String getOrderIdSelector(List<Long> order_ids) {
        String html = "";

        if (order_ids == null || order_ids.size() == 0) {
            html += "<option value='-1'>No Orders Found</option>";
        } else {
            html += "<option value='-1'>Select Order ID</option>";

            for (long order_id : order_ids) {
                html += "<option value='" + order_id + "'>" + order_id + "</option>";
            }
        }
        return html;
    }

    public String getProductsForOrder() {
        String html = "";

        if (products == null || products.size() == 0) {
            html += "<option value='-1'>No Products Found</option>";
        } else {
            for (LineItem product : products) {
                html += "<option value='" + product.getId() + "'>";

                if (product.getBrand() != null)
                    html += product.getBrand() + " ";
                if (product.getModel_name() != null)
                    html += product.getModel_name() + " ";
                if (product.getModel_number() != null)
                    html += product.getModel_number();

                html += "</option>";
            }
        }
        return html;
    }

    public String getIdsOfAllOrders() {
        List<Long> order_ids = new ArrayList<Long>();

        for (Order order : this.orders) {
            order_ids.add(order.getId());
        }
        return getOrderIdSelector(order_ids);
    }

    public String getIdsOfReturnableOrders() {
        return getOrderIdSelector(this.order_ids);
    }

    public String getIdsOfCancellableOrders() {
        return getOrderIdSelector(this.order_ids);
    }
}