Subversion Repositories SmartDukaan

Rev

Rev 7482 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.controllers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.StorePricing;
import in.shop2020.support.utils.ReportsUtils;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.utils.ModelUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.opensymphony.xwork2.ActionSupport;


@InterceptorRefs({
    @InterceptorRef("defaultStack"),
    @InterceptorRef("login")
})
@Results({
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
})
public class StoreAdminController extends ActionSupport implements ServletRequestAware, ServletResponseAware {

        private static final long serialVersionUID = 1L;
        private static Logger logger = LoggerFactory.getLogger(StoreAdminController.class);
        private HttpServletRequest request;
        private HttpServletResponse response;
        private String itemString;
        private String id;
        
        private long itemId;
        private double recommendedPrice;
        private double minPrice;
        private double maxPrice;
        private double minAdvancePrice;
        private double absoluteMinPrice;
        

        private long freebieItemId;
        private String bestDealText;
        private boolean activeonstore;
        private boolean allcolors;
        private HttpSession session;
        
        public String index()   {
                if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
                        return "authfail";
                return "index";
        }
        
        public String show()    {
                return "index";
        }

        public String create() {
                StorePricing sp = new StorePricing(itemId, recommendedPrice, minPrice, maxPrice, minAdvancePrice, absoluteMinPrice, freebieItemId, bestDealText);
                try     {
                        CatalogClient csc = new CatalogClient();
                        CatalogClient cscs = new CatalogClient("catalog_service_server_host_prod", "catalog_service_server_port");
                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= csc.getClient();
                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClientProd= cscs.getClient();
                        
                        catalogClient.updateStorePricing(sp, allcolors);
                        catalogClientProd.updateStorePricing(sp, allcolors);
                        Item item = getItem(itemId);
                        if(item.isActiveOnStore() != isActiveonstore()){
                                item.setActiveOnStore(isActiveonstore());
                                catalogClient.updateItem(item);
                                catalogClientProd.updateItem(item);
                        }
                        addActionError("Pricing successfully updated");
                } catch (TException e) {
                        logger.error("", e);
                } catch (CatalogServiceException e) {
                        e.printStackTrace();
                        addActionError("Unable to update pricing because: " + e.getMessage());
                }
                setId(Long.toString(itemId));
                return "index";
        }
        
        public Collection<String> getMessages(){
                return getActionErrors();
        }

        public List<Item> searchItem(){
                List<Item> items = null;
                if(itemString != null){
                        try{
                                CatalogClient csc = new CatalogClient();
                                in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= csc.getClient();
                                List<String> searchTerms = Arrays.asList(itemString.split(" "));
                                items = catalogClient.searchItemsInRange(searchTerms, 0, 50);
                        }catch (Exception e) {
                                logger.error("", e);
                        }
                }else if(id == null)
                        {
                                try{
                                        CatalogClient csc = new CatalogClient();
                                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= csc.getClient();
                                        items = catalogClient.getLatestArrivals();
                                }catch (Exception e) {
                                        logger.error("", e);
                                }
                        }
                return items;
        }
        

        public StorePricing getStorePricing(){
                StorePricing sp = null;
                if(id != null){
                        try{
                                long itemId = Long.parseLong(id);
                                CatalogClient csc = new CatalogClient();
                                in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= csc.getClient();
                                sp = catalogClient.getStorePricing(itemId);
                        }catch (Exception e) {
                                logger.error("", e);
                        }
                }
                return sp;
        }

        public List<StorePricing> getStorePricings(List<Item> items){
                List<Long> itemIds = new ArrayList<Long>();
                List<StorePricing> sps = null;
                try{
                        for(Item item: items){
                                itemIds.add(item.getId());
                        }
                        CatalogClient csc = new CatalogClient();
                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= csc.getClient();
                        sps = catalogClient.getStorePricings(itemIds);
                }catch (Exception e) {
                        logger.error("", e);
                }
                return sps;
        }

        public Item getItem(long itemId){
                Item item = null;
                try{
                        CatalogClient csc = new CatalogClient();
                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= csc.getClient();
                        item = catalogClient.getItem(itemId);
                }catch (Exception e) {
                        logger.error("", e);
                }
                return item;
        }

        
    public String getServletContextPath() {
        return request.getContextPath();
    }
 
        @Override
        public void setServletRequest(HttpServletRequest request) {
                this.request = request;
                this.session = request.getSession();
        }

        @Override
        public void setServletResponse(HttpServletResponse response) {
                this.response = response;
        }
                
        public String getProductNameFromItem(Item item) {
                return ModelUtils.extractProductNameFromItem(item);
        }
        
        public String getItemString(){
                return this.itemString; 
        }
        
        public void setItemString(String itemString){
                this.itemString = itemString;
        }
        
        public void setId(String id){
                this.id = id;
        }

        public String getId(){
                return this.id;
        }
        
        public void setItemId(long itemId) {
                this.itemId = itemId;
        }

        public long getItemId() {
                return itemId;
        }

        public void setRecommendedPrice(double recommendedPrice) {
                this.recommendedPrice = recommendedPrice;
        }

        public double getRecommendedPrice() {
                return recommendedPrice;
        }

        public void setMinPrice(double minPrice) {
                this.minPrice = minPrice;
        }

        public double getMinPrice() {
                return minPrice;
        }

        public void setMaxPrice(double maxPrice) {
                this.maxPrice = maxPrice;
        }

        public double getMaxPrice() {
                return maxPrice;
        }

        public void setMinAdvancePrice(double minAdvancePrice) {
                this.minAdvancePrice = minAdvancePrice;
        }

        public double getMinAdvancePrice() {
                return minAdvancePrice;
        }

        public void setActiveonstore(String activeonstore) {
                if(activeonstore.equals("on")){
                        this.activeonstore = true;
                }else{
                        this.activeonstore = false;
                }
        }

        public boolean isActiveonstore() {
                return activeonstore;
        }

        public void setFreebieItemId(long freebieItemId) {
                this.freebieItemId = freebieItemId;
        }

        public long getFreebieItemId() {
                return freebieItemId;
        }

        public void setBestDealText(String bestDealText) {
                this.bestDealText = bestDealText;
        }

        public String getBestDealText() {
                return bestDealText;
        }

        public double getAbsoluteMinPrice() {
                return absoluteMinPrice;
        }

        public void setAbsoluteMinPrice(double absoluteMinPrice) {
                this.absoluteMinPrice = absoluteMinPrice;
        }

        public void setAllcolors(String allcolors) {
                if(allcolors.equals("on")){
                        this.allcolors = true;
                }else{
                        this.allcolors = false;
                }
        }

        public boolean isAllcolors() {
                return allcolors;
        }
        
}