Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;


import in.shop2020.serving.utils.FileUtils;
import in.shop2020.serving.utils.Utils;

import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;
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.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @author rajveer
 *
 */

@InterceptorRefs({
    @InterceptorRef("myDefault"),
    @InterceptorRef("login")
})

@Results({
    @Result(name = "show", location = "after-sales-show.vm"),
    @Result(name = "redirect", location = "my-purchases", type = "redirect")
})
public class AfterSalesController extends BaseController {
                
        private static final long serialVersionUID = 1L;

        private static Logger log = Logger.getLogger(AfterSalesController.class);
        private String id;
        private long productId;
        private boolean isMobile = false;
        
        public AfterSalesController(){
                super();
        }

    public String show() throws SecurityException, JSONException {

        String productName = "";
        String displayAccessories = "FALSE";
        try {
            JSONObject productPropertiesInJson = new JSONObject(pageLoader.getProductPropertiesHtml(productId).trim());
            productName = productPropertiesInJson.getString("name");
            displayAccessories = productPropertiesInJson.getString("displayAccessories");
        }
        catch (JSONException e) {
                log.error("Metadata doesnot exist for this product. Redirecting to my-purchases.",e);
                return "redirect";
       }
        
        htmlSnippets.put("PRODUCT_ID", productId + "");
        htmlSnippets.put("PRODUCT_NAME", productName);
                if(displayAccessories.equals("TRUE")){
                        setMobile(true);
                }
                return "show";
    }
    
    /**
     * 
     * @param id
     */
    public void setId(String id) {
        StringTokenizer tokenizer = new StringTokenizer(id,"-");
        while(tokenizer.hasMoreTokens()){
                this.id = tokenizer.nextToken();
        }
        this.productId = Long.parseLong(this.id);
    }
        public String getProductName()  {
                return htmlSnippets.get("PRODUCT_NAME");
        }

        public String getProductId()    {
                return htmlSnippets.get("PRODUCT_ID");
        }
        
        /**
         * @param isMobile the isMobile to set
         */
        public void setMobile(boolean isMobile) {
                this.isMobile = isMobile;
        }

        /**
         * @return the isMobile
         */
        public boolean isMobile() {
                return isMobile;
        }
        
        public String getAfterSalesSnippet(){
                try {
                        return FileUtils.read(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "AfterSales.html");
                } catch (IOException e) {
                        log.error("Not able to read after sales snippets", e);
                }
                return "";
        }
}