Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2644 varun.gupt 1
package in.shop2020.serving.controllers;
2
 
2718 varun.gupt 3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.IOException;
7
import java.io.InputStreamReader;
8
import java.util.LinkedHashMap;
2644 varun.gupt 9
import java.util.List;
2718 varun.gupt 10
import java.util.Map;
2644 varun.gupt 11
 
12
import in.shop2020.model.v1.user.UserContextService;
13
import in.shop2020.model.v1.user.UserNote;
2718 varun.gupt 14
import in.shop2020.serving.utils.Utils;
2644 varun.gupt 15
import in.shop2020.thrift.clients.UserContextServiceClient;
16
 
17
import org.apache.log4j.Logger;
18
import org.apache.struts2.convention.annotation.Action;
19
import org.apache.struts2.convention.annotation.InterceptorRef;
20
 
21
/**
22
 * 
23
 * @author Varun Gupta
24
 *
25
 */
26
 
27
@SuppressWarnings("serial")
28
public class MyNotesController extends BaseController {
29
 
30
    private String id;
31
 
32
	private static Logger logger = Logger.getLogger(Class.class);
33
 
2731 varun.gupt 34
	private long entityId;
35
 
2718 varun.gupt 36
	private Map<String, String> slideNotes;
2644 varun.gupt 37
 
38
	public MyNotesController() {
39
		super();
40
	}
41
 
42
	@Action(value="my-notes",interceptorRefs={@InterceptorRef("myDefault")})
43
	public String show()	{
2718 varun.gupt 44
		String responseContentType = null;
45
 
2866 varun.gupt 46
        try {
47
        	String[] idParts = this.id.split("-");
48
 
49
        	this.entityId = Long.parseLong(idParts[0]);
50
        	responseContentType = idParts[1];
51
 
52
        	this.slideNotes = getSlideNames(this.entityId);
53
 
54
    		if(userinfo.getUserId() != -1)	{
55
    			UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
56
    			List<UserNote> userNotes = userClient.getUserNotes(userinfo.getUserId(), this.entityId);
57
 
58
    			for (UserNote note: userNotes)	{
59
    				slideNotes.put(note.getSlide(), note.getNote());
60
    			}
61
    		}
62
		} catch (NumberFormatException e) {
63
			logger.error("Number Format Exception. String: " + this.id);
64
		} catch (Exception e) {
65
			logger.error("Exception: " + e.getMessage());
2644 varun.gupt 66
		}
67
 
2718 varun.gupt 68
		return responseContentType.equals("json") ? "json" : "show";
2644 varun.gupt 69
	}
70
 
2672 varun.gupt 71
	// Handle POST /save-note/
2644 varun.gupt 72
	@Action(value="save-note",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
73
	public String create()	{
74
 
75
		try {
2731 varun.gupt 76
        	this.entityId = Long.parseLong(request.getParameter("entity"));
2718 varun.gupt 77
        	String slide = request.getParameter("slide");
2644 varun.gupt 78
        	String note = request.getParameter("note");
79
 
80
        	UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
2731 varun.gupt 81
			userClient.putUserNote(userinfo.getUserId(), this.entityId, slide, note);
2644 varun.gupt 82
 
83
		} catch (NumberFormatException e) {
84
			logger.error("Number Format Exception: " + e.getMessage());
85
		} catch (Exception e) {
86
			logger.error("Exception: " + e.getMessage());
87
		}
88
		return "success";
89
	}
90
 
2718 varun.gupt 91
	public Map<String, String> getSlideNotes()	{
92
		return this.slideNotes;
2644 varun.gupt 93
	}
94
 
2718 varun.gupt 95
    private Map<String, String> getSlideNames(long productId) {
96
    	Map<String, String> slides = new LinkedHashMap<String, String>();
97
 
98
        File f = new File(Utils.EXPORT_ENTITIES_PATH + productId + File.separator + "SlideNamesSnippet.html");
99
        FileInputStream fis = null;
100
        try {
101
            fis = new FileInputStream(f);
102
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
103
 
104
            String line = br.readLine();	// Ignoring first line, which contains Product name and not Slide Label
105
 
106
            while((line = br.readLine()) != null){
107
 
108
                String[] parts = line.split("!!!");
109
                String slideName = parts[0];
110
 
111
                if(!slides.containsKey(slideName)){
112
                    slides.put(slideName, null);
113
                }
114
            }
115
        } catch (IOException e) {
116
            e.printStackTrace();
117
        }
118
        return slides;
119
    }
2731 varun.gupt 120
 
121
    public long getEntityId()	{
122
    	return this.entityId;
123
    }
2718 varun.gupt 124
 
2644 varun.gupt 125
    public String getId() {
126
        return this.id;
127
    }
128
 
129
    /**
130
     * @param id
131
     */
132
    public void setId(String id) {
133
        this.id = id;
134
    }
135
}