1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.statistics;
18
19 import java.io.IOException;
20
21 import javax.portlet.ActionRequest;
22 import javax.portlet.ActionResponse;
23 import javax.portlet.PortletConfig;
24 import javax.portlet.PortletContext;
25 import javax.portlet.PortletException;
26 import javax.portlet.PortletSession;
27 import javax.portlet.RenderRequest;
28 import javax.portlet.RenderResponse;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.jetspeed.CommonPortletServices;
33 import org.apache.jetspeed.statistics.AggregateStatistics;
34 import org.apache.jetspeed.statistics.InvalidCriteriaException;
35 import org.apache.jetspeed.statistics.PortalStatistics;
36 import org.apache.jetspeed.statistics.StatisticsQueryCriteria;
37 import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
38 import org.apache.velocity.context.Context;
39
40 /***
41 * Statistics Portlet
42 *
43 * @author <a href="mailto:chris@bluesunrise.com">Chris Schaefer </a>
44 * @version $Id: $
45 */
46 public class StatisticsPortlet extends GenericVelocityPortlet
47 {
48
49 private PortalStatistics statistics;
50
51 private static final String SESSION_CRITERIA = "criteria";
52
53 private static final String SESSION_RESULTS = "results";
54
55 private static final String SESSION_TOTALSESSIONS = "totalsessions";
56
57
58 protected final Log logger = LogFactory.getLog(this.getClass());
59
60 public void init(PortletConfig config) throws PortletException
61 {
62 super.init(config);
63 PortletContext context = getPortletContext();
64 statistics = (PortalStatistics) context
65 .getAttribute(CommonPortletServices.CPS_PORTAL_STATISTICS);
66 if (statistics == null)
67 throw new PortletException(
68 "Could not get instance of portal statistics component");
69 }
70
71 public void doView(RenderRequest request, RenderResponse response)
72 throws PortletException, IOException
73 {
74 Context velocityContext = getContext(request);
75 PortletSession session = request.getPortletSession();
76
77 StatisticsQueryCriteria sqc = (StatisticsQueryCriteria) session
78 .getAttribute(SESSION_CRITERIA);
79 AggregateStatistics stats = (AggregateStatistics) session
80 .getAttribute(SESSION_RESULTS);
81 if (stats == null)
82 {
83 if (sqc == null)
84 {
85
86 sqc = statistics.createStatisticsQueryCriteria();
87 sqc.setQueryType(PortalStatistics.QUERY_TYPE_PORTLET);
88 sqc.setTimePeriod("1");
89 sqc.setListsize("5");
90 sqc.setSorttype("count");
91 sqc.setSortorder("desc");
92 session.setAttribute(SESSION_CRITERIA, sqc);
93
94 try
95 {
96 statistics.forceFlush();
97 stats = statistics.queryStatistics(sqc);
98 } catch (InvalidCriteriaException e)
99 {
100 logger.warn("unable to complete a statistics query ", e);
101 }
102 session.setAttribute(SESSION_RESULTS, stats);
103
104 }
105 }
106 velocityContext.put(SESSION_TOTALSESSIONS, ""
107 + statistics.getNumberOfCurrentUsers());
108 velocityContext.put(SESSION_RESULTS, stats);
109 velocityContext.put(SESSION_CRITERIA, sqc);
110 super.doView(request, response);
111 }
112
113 public void processAction(ActionRequest request,
114 ActionResponse actionResponse) throws PortletException, IOException
115 {
116 PortletSession session = request.getPortletSession();
117 StatisticsQueryCriteria criteria = statistics.createStatisticsQueryCriteria();
118
119 String user = request.getParameter("user");
120 criteria.setUser(user);
121 String timeperiod = request.getParameter("timeperiod");
122 if (timeperiod == null)
123 {
124 timeperiod = "all";
125 }
126
127 String listsizeStr = request.getParameter("listsize");
128 if(listsizeStr == null)
129 {
130 listsizeStr = "5";
131 } else
132 {
133 try
134 {
135 Integer.parseInt(listsizeStr);
136 } catch (NumberFormatException e)
137 {
138
139 listsizeStr = "5";
140 }
141 }
142 criteria.setListsize(listsizeStr);
143 criteria.setSorttype("count");
144 criteria.setSortorder("desc");
145
146 criteria.setTimePeriod(timeperiod);
147 String queryType = request.getParameter("queryType");
148
149 criteria.setQueryType(queryType);
150 AggregateStatistics stats = statistics.getDefaultEmptyAggregateStatistics();
151 try
152 {
153 statistics.forceFlush();
154 stats = statistics.queryStatistics(criteria);
155 } catch (InvalidCriteriaException e)
156 {
157 logger.warn("unable to complete a statistics query ", e);
158 }
159
160 session.setAttribute(SESSION_CRITERIA, criteria);
161 session.setAttribute(SESSION_RESULTS, stats);
162
163 }
164
165 }