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