Subversion Repositories SmartDukaan

Rev

Rev 1703 | Rev 1853 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1703 Rev 1761
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.model.v1.user.UserContextService;
3
import in.shop2020.model.v1.user.Widget;
4
import in.shop2020.model.v1.user.WidgetException;
4
import in.shop2020.model.v1.user.WidgetException;
5
import in.shop2020.model.v1.user.UserContextService.Client;
5
import in.shop2020.model.v1.user.UserContextService.Client;
-
 
6
import in.shop2020.model.v1.user.WidgetItem;
6
import in.shop2020.serving.utils.FileUtils;
7
import in.shop2020.serving.utils.FileUtils;
7
import in.shop2020.serving.utils.Utils;
8
import in.shop2020.serving.utils.Utils;
8
import in.shop2020.thrift.clients.UserContextServiceClient;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
9
 
10
 
10
import java.io.File;
11
import java.io.File;
-
 
12
import java.io.FileNotFoundException;
11
import java.io.IOException;
13
import java.io.IOException;
12
import java.util.StringTokenizer;
14
import java.util.ArrayList;
-
 
15
import java.util.List;
13
 
16
 
14
import org.apache.log4j.Logger;
17
import org.apache.log4j.Logger;
15
import org.apache.struts2.convention.annotation.Action;
18
import org.apache.struts2.convention.annotation.Action;
16
import org.apache.struts2.convention.annotation.InterceptorRef;
19
import org.apache.struts2.convention.annotation.InterceptorRef;
17
import org.apache.struts2.convention.annotation.InterceptorRefs;
-
 
18
import org.apache.struts2.convention.annotation.Result;
20
import org.apache.struts2.convention.annotation.Result;
19
import org.apache.struts2.convention.annotation.Results;
21
import org.apache.struts2.convention.annotation.Results;
20
import org.apache.thrift.TException;
22
import org.apache.thrift.TException;
-
 
23
import org.json.JSONArray;
-
 
24
import org.json.JSONException;
21
 
25
 
22
@Results({
26
@Results({
23
	@Result(name="failure", location="myresearch-failure.vm"),
-
 
24
	@Result(name="success", location="myresearch-success.vm")
27
	@Result(name="success", location="myresearch-success.vm")
25
})
28
})
26
public class MyresearchController extends BaseController{
29
public class MyresearchController extends BaseController {
27
 
30
 
28
	private UserContextServiceClient userServiceClient = null;
31
    private UserContextServiceClient userServiceClient = null;
29
	private Client client;
32
	private Client client;
-
 
33
    private static Logger log = Logger.getLogger(MyresearchController.class);
-
 
34
    private String htmlSnippet = "";
-
 
35
    private String snippets="";
-
 
36
    private List<String> items;
-
 
37
    private String itemsInJson;
-
 
38
    private int totalItems = 0;
30
	
39
 
31
	public MyresearchController(){
40
	public MyresearchController(){
32
		super();
41
		super();
33
		try {
42
		try {
34
			userServiceClient = new UserContextServiceClient();
43
			userServiceClient = new UserContextServiceClient();
35
			client = userServiceClient.getClient();
44
			client = userServiceClient.getClient();
36
		} catch (Exception e) {
45
		} catch (Exception e) {
37
			e.printStackTrace();
46
			e.printStackTrace();
38
		}
47
		}
39
	}
48
	}
40
 
49
 
41
		
-
 
42
	private static Logger log = Logger.getLogger(Class.class);
-
 
43
	
-
 
44
	private String htmlSnippet = "";
-
 
45
 
-
 
46
	@Action(value="myresearch",interceptorRefs={@InterceptorRef("myDefault")})
50
	@Action(value="myresearch",interceptorRefs={@InterceptorRef("myDefault")})
47
	public String index() {
51
	public String index() {
-
 
52
	    try {
-
 
53
	        JSONArray itemsJson =  new JSONArray();
-
 
54
            Widget resWidget = client.getMyResearch(userinfo.getUserId());
-
 
55
            List<WidgetItem> widgetItems = resWidget.getItems();
-
 
56
            for (WidgetItem wItem : widgetItems) {
-
 
57
                itemsJson.put(wItem.getItem_id());
-
 
58
            }
-
 
59
            itemsInJson = itemsJson.toString();
-
 
60
        } catch (WidgetException e) {
-
 
61
            e.printStackTrace();
-
 
62
        } catch (TException e) {
-
 
63
            e.printStackTrace();
-
 
64
        }
48
		return "index";
65
		return "index";
49
	}
66
	}
50
	
67
	
-
 
68
	//myresearch/[1,2,3]
-
 
69
	@Action(value="myresearch",interceptorRefs={@InterceptorRef("myDefault")})
-
 
70
    public String show() {
-
 
71
	    if(items != null){
-
 
72
            for(String itemId: items){
-
 
73
                try {
-
 
74
                    snippets = snippets + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
-
 
75
                    totalItems++;
-
 
76
                }
-
 
77
                catch (FileNotFoundException e) {
-
 
78
                    log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
-
 
79
                }
-
 
80
                catch (IOException e) {
-
 
81
                    log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
-
 
82
                }
-
 
83
            }
-
 
84
        }
-
 
85
        return "show";
-
 
86
    }
-
 
87
	
51
	//Handle /myresearch/{id}
88
	//Handle /addtoresearch/{id}
52
	@Action(value="addtoresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
89
	@Action(value="addtoresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
53
	public String create() throws WidgetException, TException, IOException {
90
	public String create() throws WidgetException, TException, IOException {
54
		log.info("list of item ids is " + this.request.getParameter("productid"));
-
 
55
		String itemIds = this.request.getParameter("productid");
-
 
56
		if(userinfo.getUserId() != -1){
91
	    if(userinfo.getUserId() != -1){
57
			StringTokenizer tokenizer = new StringTokenizer(itemIds,"_");
-
 
58
			while(tokenizer.hasMoreTokens()){
92
			for (String item : items){
59
				long entity_id = Long.parseLong(tokenizer.nextToken());
93
	            long itemId = Long.parseLong(item);
60
				boolean isNew = client.updateMyResearch(userinfo.getUserId(), entity_id);
94
	            boolean isNew = client.updateMyResearch(userinfo.getUserId(), itemId);
61
				if(isNew){
95
	            if(isNew){
-
 
96
	                try {
62
					htmlSnippet = htmlSnippet + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + entity_id + File.separator +"WidgetSnippet.html");
97
	                    htmlSnippet = htmlSnippet + FileUtils.read( Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
-
 
98
	                }
-
 
99
	                catch (FileNotFoundException e) {
-
 
100
	                    log.error("File not found : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
-
 
101
	                }
-
 
102
	                catch (IOException e) {
-
 
103
	                    log.error("IO exception : " + Utils.EXPORT_ENTITIES_PATH + itemId + File.separator +"WidgetSnippet.html");
63
				}
104
	                }
-
 
105
                }
64
			}
106
	        }
65
		}
107
		}
66
		return "success";
108
		return "success";
67
	}
109
	}
68
	
110
	
69
	@Action(value="deletefromresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
111
	@Action(value="deletefromresearch",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
70
	public String destroy(){
112
	public String destroy(){
71
		log.info("list of item ids is " + this.request.getParameter("productid"));
-
 
72
		String itemIds = this.request.getParameter("productid");
-
 
73
		if(itemIds == null){
-
 
74
			return "failure";
-
 
75
		}
-
 
76
		long itemId = 0;
113
		for (String item : items){
77
		StringTokenizer tokenizer = new StringTokenizer(itemIds,"_");
-
 
78
		while(tokenizer.hasMoreTokens()){
-
 
79
			itemId = Long.parseLong(tokenizer.nextToken());
114
			long itemId = Long.parseLong(item);
80
			deleteFromMyResearch(userinfo.getUserId(), itemId);	
115
			deleteFromMyResearch(userinfo.getUserId(), itemId);	
81
		}
116
		}
82
		return "success";
117
		return "success";
83
	}
118
	}
84
	
119
	
Line 95... Line 130...
95
		} catch (Exception e) {
130
		} catch (Exception e) {
96
			e.printStackTrace();
131
			e.printStackTrace();
97
		}
132
		}
98
	}
133
	}
99
	
134
	
100
	
-
 
101
	public String getHtmlSnippet(){
135
	public String getHtmlSnippet(){
102
		if(htmlSnippet == "")	{
136
		if(htmlSnippet == "")	{
103
			htmlSnippet = "0";
137
			htmlSnippet = "0";
104
		}
138
		}
105
		return htmlSnippet;
139
		return htmlSnippet;
106
	}	
140
	}
-
 
141
	
-
 
142
	public String getSnippets() {
-
 
143
        return snippets;
-
 
144
    }
-
 
145
	
-
 
146
	public int getTotalItems() {
-
 
147
        return totalItems;
-
 
148
    }
-
 
149
	
-
 
150
	public void setProductid(String itemsString) {
-
 
151
        setId(itemsString);
-
 
152
    }
-
 
153
	
-
 
154
	public void setId(String itemsString) {
-
 
155
        JSONArray itemJson = null;
-
 
156
        items = new ArrayList<String>();
-
 
157
        try {
-
 
158
            itemJson = new JSONArray(itemsString);
-
 
159
        } catch (JSONException e) {
-
 
160
            log.error("Bad json : " + itemsString);
-
 
161
            return;
-
 
162
        }
-
 
163
        for (int i=0; i<itemJson.length(); i++) {
-
 
164
            try {
-
 
165
                items.add(itemJson.getString(i));
-
 
166
            } catch (JSONException e) {
-
 
167
                log.error("Bad item at index : " + i);
-
 
168
            }
-
 
169
        }
-
 
170
        log.info("items are: " + items);
-
 
171
    }
-
 
172
    
-
 
173
    public String getItemsInJson() {
-
 
174
        return this.itemsInJson;
-
 
175
    }
107
}
176
}
108
177