Subversion Repositories SmartDukaan

Rev

Rev 2988 | Blame | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.json.JSONObject;

/**
 * 
 * @author Varun Gupta
 *
 */

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

    private String id;

        private static Logger logger = Logger.getLogger(Class.class);
        
        private long entityId;
        
        private Map<String, String> slideNotes;
        
        public MyNotesController() {
                super();
        }

        @Action(value="my-notes",interceptorRefs={@InterceptorRef("myDefault")})
        public String show()    {
                String responseContentType = null;
                
        try {
                String[] idParts = this.id.split("-");
                
                this.entityId = Long.parseLong(idParts[0]);
                responseContentType = idParts[1];
                
                this.slideNotes = getSlideNames(this.entityId);

                if(userinfo.getUserId() != -1)  {

                        String gaeNotesJSON = MyNotesGAEClient.getNotes(userinfo.getUserId(), this.entityId);
                        JSONObject gaeNotes = new JSONObject(gaeNotesJSON);

                        for(String key: this.slideNotes.keySet())       {
                                String datastoreKey = key.replace(" ", "");
                                
                                if(gaeNotes.has(datastoreKey))  {
                                        this.slideNotes.put(key, gaeNotes.getString(datastoreKey));
                                }
                        }
                }
                } catch (NumberFormatException e) {
                        logger.error("Number Format Exception. String: " + this.id);
                } catch (Exception e) {
                        logger.error("Exception: " + e.getMessage());
                }

                return responseContentType.equals("json") ? "json" : "show";
        }

        // Handle POST /save-note/
        @Action(value="save-note",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
        public String create()  {
                try {
                this.entityId = Long.parseLong(request.getParameter("entity"));
                String slide = request.getParameter("slide");
                String note = request.getParameter("note");

                MyNotesGAEClient.putNote(userinfo.getUserId(), this.entityId, slide, note);
                        
                } catch (NumberFormatException e) {
                        logger.error("Number Format Exception: " + e.getMessage());
                } catch (Exception e) {
                        logger.error("Exception: " + e.getMessage());
                }
                return "success";
        }
        
        public Map<String, String> getSlideNotes()      {
                return this.slideNotes;
        }
        
    private Map<String, String> getSlideNames(long productId) {
        Map<String, String> slides = new LinkedHashMap<String, String>();
        
        File f = new File(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideNamesSnippet.html");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            
            String line = br.readLine();        // Ignoring first line, which contains Product name and not Slide Label
            br.readLine();      // Ignoring second line, which contains Product Title
            
            while((line = br.readLine()) != null){
                
                String[] parts = line.split("!!!");
                String slideName = parts[0];
                
                if(!slides.containsKey(slideName)){
                    slides.put(slideName, null);
                }
            }
        } catch (IOException e) {
            logger.error("Unable to get slide names", e);
        }
        return slides;
    }
    
    public long getEntityId()   {
        return this.entityId;
    }
        
    public String getId() {
        return this.id;
    }

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