Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.social.controllers;

import in.shop2020.datalogger.SocialEventType;
import in.shop2020.utils.DataLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.util.ServletContextAware;
import com.thoughtworks.xstream.core.util.Base64Encoder;

public class CommunityPromotionController implements ParameterAware, ServletContextAware {
        public static class DisplayItem {
                private String name;
                private String price;
                private long entityId;
                
                public DisplayItem(String name, String price, long entityId)    {
                        this.name = name;
                        this.price = price;
                        this.entityId = entityId;
                }
                
                public String getName() {
                        return this.name;
                }
                
                public String getPrice()        {
                        return this.price;
                }
                
                public long getEntityId()       {
                        return this.entityId;
                }
        }
    protected Map<String, String[]> parameters;
    protected ServletContext context;
    private String pid;
    private List<DisplayItem> displayItems = new ArrayList<DisplayItem>();

    public String index()  {
        if (pid != null && pid.equals("bb"))    {
                DataLogger.logSocialData(SocialEventType.BLACKBERRY_PROMOTION_VIEWED , "1000");
                return "bb-promo";
        } else  {
                DataLogger.logSocialData(SocialEventType.COMMUNITY_PROMOTION_VIEWED, "1000");
        
                return "success";
        }
    }
    
    public String create()  {
        
        if (pid != null && pid.equals("bb"))    {
                DataLogger.logSocialData(SocialEventType.BLACKBERRY_PROMOTION_VIEWED , "1000");
                return "bb-promo";
        } else  {
                DataLogger.logSocialData(SocialEventType.COMMUNITY_PROMOTION_VIEWED, "1000");
                
            return "success";
        }
    }
    
    public void setPid(String pid) {
                this.pid = pid;
        }

    @Override
    public void setParameters(Map<String, String[]> parameters) {
        this.parameters = parameters;
    }
    
    @Override
    public void setServletContext(ServletContext context) {
        this.context = context;
    }
    
    public String getParameter(String key)    {
        String str = "";
        for (int i = 0; i < this.parameters.get(key).length; i ++)  {
            str += this.parameters.get(key)[i];
        }
        return str;
    }
    
    public Map<String, String[]> getParameters()   {
        return this.parameters;
    }
    
    public String getFBPostedJSON()  {
        System.out.println(this.parameters);

        if (this.parameters.get("signed_request").length > 0)   {
            String str = this.parameters.get("signed_request")[0];
            System.out.println(str);
            Base64Encoder decoder = new Base64Encoder();
            try {
                System.out.println(str.split("\\."));
                byte[] decodedBytes = decoder.decode(str.split("\\.")[1]);
                String s = new String(decodedBytes).trim();
                String encodedString = decoder.encode(s.getBytes());
                System.out.println(encodedString);
                System.out.println(encodedString.equals(str));
                System.out.println(s);
                return s;
            }
            catch (ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
                return "{}";
            }
        }
        return "{}";
    }
    
    public String test()  {
        String[] test = new String[] {"str1", "str2", "str3"};
        String str = "";
        
        for (int i = 0; i < test.length; i ++)  {
            str += test[i];
        }
        return str;
    }
    
    public String getServletContextPath(){
        return context.getContextPath();
    }

    public void addDisplayItem(String name, String price, long entityId)        {
        this.displayItems.add(new DisplayItem(name, price, entityId));
    }
    
    public List<DisplayItem> getDisplayItems()  {
        for (DisplayItem item: this.displayItems)       {
                System.out.println(item.getName());
        }
        return this.displayItems;
    }
}