1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.Serializable;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.HashMap;
|
---|
7 |
|
---|
8 | import org.apache.log4j.Logger;
|
---|
9 | import org.greenstone.gsdl3.util.DerbyWrapper;
|
---|
10 | import org.greenstone.gsdl3.util.GSXML;
|
---|
11 | import org.greenstone.util.GlobalProperties;
|
---|
12 | import org.w3c.dom.Element;
|
---|
13 |
|
---|
14 | public class UserTracker extends ServiceRack
|
---|
15 | {
|
---|
16 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.DocXMLUtil.class.getName());
|
---|
17 |
|
---|
18 | /**************************************************
|
---|
19 | * The list of services the User Tracker supports *
|
---|
20 | *************************************************/
|
---|
21 | protected static final String RECORD_USER_ACTION = "RecordUserAction";
|
---|
22 | protected static final String GET_ACTIVITY_ON_PAGE = "GetActivityOnPage";
|
---|
23 | /*************************************************/
|
---|
24 |
|
---|
25 | String[] services = { RECORD_USER_ACTION, GET_ACTIVITY_ON_PAGE };
|
---|
26 |
|
---|
27 | /** configure this service */
|
---|
28 | public boolean configure(Element info, Element extra_info)
|
---|
29 | {
|
---|
30 | if (!super.configure(info, extra_info))
|
---|
31 | {
|
---|
32 | return false;
|
---|
33 | }
|
---|
34 |
|
---|
35 | logger.info("Configuring DocXMLUtil...");
|
---|
36 | this.config_info = info;
|
---|
37 |
|
---|
38 | for (int i = 0; i < services.length; i++)
|
---|
39 | {
|
---|
40 | Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
41 | service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
42 | service.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
43 | this.short_service_info.appendChild(service);
|
---|
44 | }
|
---|
45 |
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected Element getServiceDescription(String service_id, String lang, String subset)
|
---|
50 | {
|
---|
51 | for (int i = 0; i < services.length; i++)
|
---|
52 | {
|
---|
53 | if (service_id.equals(services[i]))
|
---|
54 | {
|
---|
55 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
56 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
57 | service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
58 | return service_elem;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | return null;
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected synchronized Element processRecordUserAction(Element request)
|
---|
66 | {
|
---|
67 | Element result = GSXML.createBasicResponse(this.doc, RECORD_USER_ACTION);
|
---|
68 |
|
---|
69 | Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
70 | if (paramList == null)
|
---|
71 | {
|
---|
72 | GSXML.addError(this.doc, result, "Request has no parameter list");
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 |
|
---|
76 | HashMap<String, Serializable> params = GSXML.extractParams(paramList, true);
|
---|
77 | String username = (String) params.get("username");
|
---|
78 | String collection = (String) params.get("collection");
|
---|
79 | String site = (String) params.get("site");
|
---|
80 | String oid = (String) params.get("oid");
|
---|
81 | String action = (String) params.get("action");
|
---|
82 |
|
---|
83 | DerbyWrapper database = new DerbyWrapper(GlobalProperties.getGSDL3Home() + File.separatorChar + "etc" + File.separatorChar + "usersDB");
|
---|
84 | database.addUserAction(username, site, collection, oid, action);
|
---|
85 | database.closeDatabase();
|
---|
86 |
|
---|
87 | return result;
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected synchronized Element processGetActivityOnPage(Element request)
|
---|
91 | {
|
---|
92 | System.err.println("CALLED");
|
---|
93 | Element result = GSXML.createBasicResponse(this.doc, GET_ACTIVITY_ON_PAGE);
|
---|
94 | try
|
---|
95 | {
|
---|
96 |
|
---|
97 | Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
98 | if (paramList == null)
|
---|
99 | {
|
---|
100 | System.err.println("WHA?");
|
---|
101 | GSXML.addError(this.doc, result, "Request has no parameter list");
|
---|
102 | return result;
|
---|
103 | }
|
---|
104 |
|
---|
105 | HashMap<String, Serializable> params = GSXML.extractParams(paramList, true);
|
---|
106 | String collection = (String) params.get("collection");
|
---|
107 | String site = (String) params.get("site");
|
---|
108 | String oid = (String) params.get("oid");
|
---|
109 |
|
---|
110 | DerbyWrapper database = new DerbyWrapper(GlobalProperties.getGSDL3Home() + File.separatorChar + "etc" + File.separatorChar + "usersDB");
|
---|
111 | ArrayList<HashMap<String, String>> userActions = database.getMostRecentUserActions(site, collection, oid);
|
---|
112 |
|
---|
113 | System.err.println(userActions.size());
|
---|
114 |
|
---|
115 | Element userList = this.doc.createElement("userList");
|
---|
116 | for (HashMap<String, String> userAction : userActions)
|
---|
117 | {
|
---|
118 | Element user = this.doc.createElement("user");
|
---|
119 | user.setAttribute("username", userAction.get("username"));
|
---|
120 | user.setAttribute("action", userAction.get("action"));
|
---|
121 | userList.appendChild(user);
|
---|
122 | }
|
---|
123 | result.appendChild(userList);
|
---|
124 | }
|
---|
125 | catch (Exception ex)
|
---|
126 | {
|
---|
127 | ex.printStackTrace();
|
---|
128 | }
|
---|
129 | return result;
|
---|
130 | }
|
---|
131 | } |
---|