001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.statistics;
018:
019: import java.io.IOException;
020:
021: import javax.portlet.ActionRequest;
022: import javax.portlet.ActionResponse;
023: import javax.portlet.PortletConfig;
024: import javax.portlet.PortletContext;
025: import javax.portlet.PortletException;
026: import javax.portlet.PortletSession;
027: import javax.portlet.RenderRequest;
028: import javax.portlet.RenderResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.jetspeed.CommonPortletServices;
033: import org.apache.jetspeed.statistics.AggregateStatistics;
034: import org.apache.jetspeed.statistics.InvalidCriteriaException;
035: import org.apache.jetspeed.statistics.PortalStatistics;
036: import org.apache.jetspeed.statistics.StatisticsQueryCriteria;
037: import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
038: import org.apache.velocity.context.Context;
039:
040: /**
041: * Statistics Portlet
042: *
043: * @author <a href="mailto:chris@bluesunrise.com">Chris Schaefer </a>
044: * @version $Id: $
045: */
046: public class StatisticsPortlet extends GenericVelocityPortlet {
047:
048: private PortalStatistics statistics;
049:
050: private static final String SESSION_CRITERIA = "criteria";
051:
052: private static final String SESSION_RESULTS = "results";
053:
054: private static final String SESSION_TOTALSESSIONS = "totalsessions";
055:
056: /* CLF logger */
057: protected final Log logger = LogFactory.getLog(this .getClass());
058:
059: public void init(PortletConfig config) throws PortletException {
060: super .init(config);
061: PortletContext context = getPortletContext();
062: statistics = (PortalStatistics) context
063: .getAttribute(CommonPortletServices.CPS_PORTAL_STATISTICS);
064: if (statistics == null)
065: throw new PortletException(
066: "Could not get instance of portal statistics component");
067: }
068:
069: public void doView(RenderRequest request, RenderResponse response)
070: throws PortletException, IOException {
071: Context velocityContext = getContext(request);
072: PortletSession session = request.getPortletSession();
073:
074: StatisticsQueryCriteria sqc = (StatisticsQueryCriteria) session
075: .getAttribute(SESSION_CRITERIA);
076: AggregateStatistics stats = (AggregateStatistics) session
077: .getAttribute(SESSION_RESULTS);
078: if (stats == null) {
079: if (sqc == null) {
080: // if we get here, we're on the first startup.
081: sqc = statistics.createStatisticsQueryCriteria();
082: sqc.setQueryType(PortalStatistics.QUERY_TYPE_PORTLET);
083: sqc.setTimePeriod("1");
084: sqc.setListsize("5");
085: sqc.setSorttype("count");
086: sqc.setSortorder("desc");
087: session.setAttribute(SESSION_CRITERIA, sqc);
088:
089: try {
090: statistics.forceFlush();
091: stats = statistics.queryStatistics(sqc);
092: } catch (InvalidCriteriaException e) {
093: logger
094: .warn(
095: "unable to complete a statistics query ",
096: e);
097: }
098: session.setAttribute(SESSION_RESULTS, stats);
099:
100: }
101: }
102: velocityContext.put(SESSION_TOTALSESSIONS, ""
103: + statistics.getNumberOfCurrentUsers());
104: velocityContext.put(SESSION_RESULTS, stats);
105: velocityContext.put(SESSION_CRITERIA, sqc);
106: super .doView(request, response);
107: }
108:
109: public void processAction(ActionRequest request,
110: ActionResponse actionResponse) throws PortletException,
111: IOException {
112: PortletSession session = request.getPortletSession();
113: StatisticsQueryCriteria criteria = statistics
114: .createStatisticsQueryCriteria();
115:
116: String user = request.getParameter("user");
117: criteria.setUser(user);
118: String timeperiod = request.getParameter("timeperiod");
119: if (timeperiod == null) {
120: timeperiod = "all";
121: }
122:
123: String listsizeStr = request.getParameter("listsize");
124: if (listsizeStr == null) {
125: listsizeStr = "5";
126: } else {
127: try {
128: Integer.parseInt(listsizeStr);
129: } catch (NumberFormatException e) {
130: // if we can't parse it.. just make it 5
131: listsizeStr = "5";
132: }
133: }
134: criteria.setListsize(listsizeStr);
135: criteria.setSorttype("count");
136: criteria.setSortorder("desc");
137:
138: criteria.setTimePeriod(timeperiod);
139: String queryType = request.getParameter("queryType");
140:
141: criteria.setQueryType(queryType);
142: AggregateStatistics stats = statistics
143: .getDefaultEmptyAggregateStatistics();
144: try {
145: statistics.forceFlush();
146: stats = statistics.queryStatistics(criteria);
147: } catch (InvalidCriteriaException e) {
148: logger.warn("unable to complete a statistics query ", e);
149: }
150: // save this to session for later display/edit
151: session.setAttribute(SESSION_CRITERIA, criteria);
152: session.setAttribute(SESSION_RESULTS, stats);
153:
154: }
155:
156: }
|