/** * Copyright © 2001 The JA-SIG Collaborative. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the JA-SIG Collaborative * (http://www.jasig.org/)." * * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * */ package org.jasig.portal.channels; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.jasig.portal.ChannelStatisticsManager; import org.jasig.portal.ChannelRuntimeData; import org.jasig.portal.ChannelStaticData; import org.jasig.portal.ChannelStatistics; import org.jasig.portal.GeneralRenderingException; import org.jasig.portal.IChannel; import org.jasig.portal.PortalException; import org.jasig.portal.security.IAuthorizationPrincipal; import org.jasig.portal.security.IPermissionManager; import org.jasig.portal.security.IPerson; import org.jasig.portal.services.AuthorizationService; import org.jasig.portal.services.EntityNameFinderService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jasig.portal.utils.DocumentFactory; import org.jasig.portal.utils.XSLT; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.xml.sax.ContentHandler; /** * CStatisticsManager is a channel used to show channel usage statistics. * * @author rtwigg@uccs.edu, adapted from CChannelManager */ public class CStatisticsManager extends BaseChannel { private static final Log log = LogFactory.getLog(CStatisticsManager.class); protected static final String sslLocation = "CStatisticsManager/CStatisticsManager.ssl"; protected static final Document emptyDoc = DocumentFactory.getNewDocument(); protected short state; protected static final short DEFAULT_STATE = 0; protected static final short REVIEW_DETAILS_STATE = 8; protected static final short ALL_CHANNELS_STATE = 9; protected String action; protected Document channelStatsManagerDoc; protected ChannelStatistics channelStats; protected ModifyChannelSettings modChanSettings = new ModifyChannelSettings(); protected IPerson person; protected String errorMsg; protected String currentChan; // Called after publishing so that you won't see any previous settings // on the next publish attempt protected void resetSettings () { modChanSettings = new ModifyChannelSettings(); errorMsg = null; } public void setStaticData (ChannelStaticData sd) throws PortalException { staticData = sd; person = sd.getPerson(); } public void setRuntimeData (ChannelRuntimeData rd) throws PortalException { runtimeData = rd; action = runtimeData.getParameter("uPSM_action"); // Prepare the appropriate XML documents for the destination screen doAction(); } public void renderXML (ContentHandler out) throws PortalException { XSLT xslt = XSLT.getTransformer(this, runtimeData.getLocales()); xslt.setXML(channelStatsManagerDoc); xslt.setXSL(sslLocation, runtimeData.getBrowserInfo()); xslt.setTarget(out); xslt.setStylesheetParameter("baseActionURL", runtimeData.getBaseActionURL()); String action = null; switch (state) { case DEFAULT_STATE: action = "none"; break; case ALL_CHANNELS_STATE: action = "viewAllChannels"; break; case REVIEW_DETAILS_STATE: action = "reviewDetails"; xslt.setStylesheetParameter("currentChannel", runtimeData.getParameter("channelID")); xslt.setStylesheetParameter("currentCategory", runtimeData.getParameter("categoryID")); break; default: action = "none"; break; } xslt.setStylesheetParameter("action", action); xslt.transform(); } /** * Controller method that reacts to the action parameter. * @exception PortalException */ protected void doAction () throws PortalException { if (action != null) { if (action.equals("viewByUser")) { } else if (action.equals("viewByChannel")) { state = ALL_CHANNELS_STATE; channelStatsManagerDoc = getChannelStatsManagerDoc(modChanSettings); } else if (action.equals("changePage")) { String newPage = runtimeData.getParameter("newPage"); if (newPage != null) { modChanSettings.setCurrentPage(newPage); channelStatsManagerDoc = getChannelStatsManagerDoc(modChanSettings); } } else if (action.equals("changeRecordsPerPage")) { String recordsPerPage = runtimeData.getParameter("recordsPerPage"); if (recordsPerPage != null) { // Figure out what page we should be on based on the change in records per page. try { int oldPage = Integer.parseInt(modChanSettings.getCurrentPage()); int oldRecordsPerPage = Integer.parseInt(modChanSettings.getRecordsPerPage()); int recsPerPage = Integer.parseInt(recordsPerPage); if (recsPerPage > 0 && recsPerPage != oldRecordsPerPage) { // Thanks to jweight@campuspipeline.com for the following formula: String newPage = String.valueOf(((((oldPage-1)*oldRecordsPerPage)+1)/(recsPerPage)+1)); modChanSettings.setCurrentPage(newPage); modChanSettings.setRecordsPerPage(recordsPerPage); channelStatsManagerDoc = getChannelStatsManagerDoc(modChanSettings); } } catch (NumberFormatException nfe) { // do nothing here, just leave the current page as is. } } } else if (action.equals("filterByCategory")) { String filterByID = runtimeData.getParameter("newCategory"); if (filterByID != null) { // User may be beyond the last page of this filtered set so put them back on page 1. modChanSettings.setCurrentPage("1"); modChanSettings.setFilterByID(filterByID); channelStatsManagerDoc = getChannelStatsManagerDoc(modChanSettings); } } else if (action.equals("viewStatDetails")) { state=REVIEW_DETAILS_STATE; } } if (action == null || action.equals("cancel")) { state = DEFAULT_STATE; channelStatsManagerDoc = emptyDoc; } } /** * Produces an XML document used as an input to * this channel's XSLT transformation. * @param modChanSettings * @return * @exception PortalException */ protected Document getChannelStatsManagerDoc (ModifyChannelSettings modChanSettings) throws PortalException { Document channelStatsManagerDoc = DocumentFactory.getNewDocument(); // Add the top level to the document Element channelStatsManager = channelStatsManagerDoc.createElement("manageChannelStats"); channelStatsManagerDoc.appendChild(channelStatsManager); // Get the channel statistics listing Document channelStatsListingDoc = ChannelStatisticsManager.getChannelStatsListing(); // Set the registry ID attribute to "-1" Element listing = channelStatsListingDoc.getDocumentElement(); listing.setAttribute("ID", "-1"); // Add the to Element channelListing = (Element)channelStatsManagerDoc.importNode(channelStatsListingDoc.getDocumentElement(),true); channelStatsManager.appendChild(channelListing); // Add a fragment to appendModifyChannelSettings(channelStatsManager, modChanSettings); return channelStatsManagerDoc; } protected static void appendModifyChannelSettings (Element channelStatsManager, ModifyChannelSettings modChanSettings) { Document doc = channelStatsManager.getOwnerDocument(); Element userSettingsE = doc.createElement("userSettings"); Element modifyView = doc.createElement("modifyView"); userSettingsE.appendChild(modifyView); Element recordsPerPageE = doc.createElement("recordsPerPage"); recordsPerPageE.appendChild(doc.createTextNode(modChanSettings.getRecordsPerPage())); modifyView.appendChild(recordsPerPageE); Element currentPageE = doc.createElement("currentPage"); currentPageE.appendChild(doc.createTextNode(modChanSettings.getCurrentPage())); modifyView.appendChild(currentPageE); Element filterByIDE = doc.createElement("filterByID"); filterByIDE.appendChild(doc.createTextNode(modChanSettings.getFilterByID())); modifyView.appendChild(filterByIDE); channelStatsManager.appendChild(userSettingsE); } /** * Keeps track of page settings for ALL_CHANNELS_STATE */ protected class ModifyChannelSettings { private String recordsPerPage; private String currentPage; private String filterByID; private String currentID; /** * put your documentation comment here */ protected ModifyChannelSettings () { recordsPerPage = "15"; currentPage = "1"; filterByID = "-1"; currentID="-1"; } // Accessor methods protected String getRecordsPerPage () { return recordsPerPage; } protected String getCurrentPage () { return currentPage; } protected String getFilterByID () { return filterByID; } protected String getCurrentID () { return currentID; } protected void setRecordsPerPage (String recordsPerPage) { this.recordsPerPage = recordsPerPage; } protected void setCurrentPage (String currentPage) { this.currentPage = currentPage; } protected void setFilterByID (String filterByID) { this.filterByID = filterByID; } protected void setCurrentID (String currentID) { this.currentID = currentID; } } }