Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

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.List;
import java.util.Map;

import in.shop2020.model.v1.user.UserContextService;
import in.shop2020.model.v1.user.UserNote;
import in.shop2020.serving.utils.Utils;
import in.shop2020.thrift.clients.UserContextServiceClient;

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

/**
 * 
 * @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)  {
                        UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
                        List<UserNote> userNotes = userClient.getUserNotes(userinfo.getUserId(), this.entityId);

                        for (UserNote note: userNotes)  {
                                slideNotes.put(note.getSlide(), note.getNote());
                        }
                }
                } 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");

                UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
                        userClient.putUserNote(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
            
            while((line = br.readLine()) != null){
                
                String[] parts = line.split("!!!");
                String slideName = parts[0];
                
                if(!slides.containsKey(slideName)){
                    slides.put(slideName, null);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return slides;
    }
    
    public long getEntityId()   {
        return this.entityId;
    }
        
    public String getId() {
        return this.id;
    }

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