/** * 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; import java.util.Date; import java.util.Iterator; import org.apache.xpath.XPathAPI; import org.jasig.portal.groups.IEntity; import org.jasig.portal.groups.IEntityGroup; import org.jasig.portal.groups.IGroupMember; import org.jasig.portal.properties.PropertiesManager; import org.jasig.portal.security.IPerson; import org.jasig.portal.services.GroupService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jasig.portal.utils.CommonUtils; import org.jasig.portal.utils.DocumentFactory; import org.jasig.portal.utils.SmartCache; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * Provides a list of summarized channel statistics. * @author rtwigg@uccs.edu, adapted from ChannelRegistryManager */ public class ChannelStatisticsManager { private static final Log log = LogFactory.getLog(ChannelStatisticsManager.class); protected static final IChannelStatisticsStore css = ChannelStatisticsStoreFactory.getChannelStatisticsStoreImpl(); protected static final IChannelRegistryStore crs = ChannelRegistryStoreFactory.getChannelRegistryStoreImpl(); // Cache timeout properties protected static final int statisticsCacheTimeout = PropertiesManager.getPropertyAsInt("org.jasig.portal.ChannelStatisticsManager.channel_statistics_cache_timeout"); // I18n property protected static final boolean localeAware = PropertiesManager.getPropertyAsBoolean("org.jasig.portal.i18n.LocaleManager.locale_aware"); // Caches protected static final SmartCache channelStatisticsCache = new SmartCache(statisticsCacheTimeout); // Cache keys private static final String CHANNEL_STATISTICS_CACHE_KEY = "channelStatisticsCacheKey"; // Permission constants private static final String FRAMEWORK_OWNER = "UP_FRAMEWORK"; private static final String SUBSCRIBER_ACTIVITY = "SUBSCRIBE"; private static final String GRANT_PERMISSION_TYPE = "GRANT"; /** * Returns a copy of the channel statistics listing as a Document. * This document is not filtered according to a user's channel permissions. * For a filtered list, see getChannelSummaryStats(IPerson person) * @return a copy of the channel registry as a Document */ public static Document getChannelStatsListing() throws PortalException { Document channelStatsListing = (Document)channelStatisticsCache.get(CHANNEL_STATISTICS_CACHE_KEY); if (channelStatsListing == null) { // Channel stats listing has expired, so get it and cache it try { channelStatsListing = getChannelStatsListingXML(); log.info("RT: Got the channel stats listing in XML"); } catch (Exception e) { throw new PortalException(e); } if (channelStatsListing != null) { channelStatisticsCache.put(CHANNEL_STATISTICS_CACHE_KEY, channelStatsListing); log.info( "Caching channel stats listing."); } } return channelStatsListing; } /** * Returns an XML document which describes the channel statistics listing. * @return doc the channel statistics listing document * @throws java.lang.Exception */ public static Document getChannelStatsListingXML() throws Exception { Document doc = DocumentFactory.getNewDocument(); Element statsListing = doc.createElement("statsListing"); doc.appendChild(statsListing); IEntityGroup channelCategoriesGroup = GroupService.getDistinguishedGroup(GroupService.CHANNEL_CATEGORIES); processGroupsRecursively(channelCategoriesGroup, doc, statsListing); return doc; } private static void processGroupsRecursively(IEntityGroup group, Document owner, Element parentGroup) throws Exception { Date now = new Date(); Iterator iter = group.getMembers(); while (iter.hasNext()) { IGroupMember member = (IGroupMember)iter.next(); if (member.isGroup()) { IEntityGroup memberGroup = (IEntityGroup)member; String key = memberGroup.getKey(); String name = memberGroup.getName(); String description = memberGroup.getDescription(); // Create category element and append it to its parent Element categoryE = owner.createElement("category"); categoryE.setAttribute("ID", "cat" + key); categoryE.setAttribute("name", name); categoryE.setAttribute("description", description); parentGroup.appendChild(categoryE); processGroupsRecursively(memberGroup, owner, categoryE); } else { IEntity channelStatsMember = (IEntity)member; int channelPublishId = CommonUtils.parseInt(channelStatsMember.getKey()); if ( channelPublishId > 0 ) { ChannelStatistics channelStats = css.getChannelStatistics(channelPublishId); if (channelStats != null) { Element channelStatsE = channelStats.getDocument(owner, "chan" + channelPublishId); channelStatsE = (Element)owner.importNode(channelStatsE, true); parentGroup.appendChild(channelStatsE); } } } } } /** * Looks in channel statistics listing for a channel element matching the * given channel ID. * @param channelPublishId the channel publish id * @return the channel element matching specified channel publish id * @throws PortalException */ public static Element getChannelDetails (String channelPublishId) throws PortalException { Document channelStatsListing = getChannelStatsListing(); Element channelStatsE = null; try { // This is unfortunately dependent on Xalan 2. Is there a way to use a standard interface? channelStatsE = (Element)XPathAPI.selectSingleNode(channelStatsListing, "(//channelStats[@ID = '" + channelPublishId + "'])[1]"); } catch (javax.xml.transform.TransformerException te) { throw new GeneralRenderingException("Not able to find channel " + channelPublishId + " within channel stats listing: " + te.getMessageAndLocation()); } return channelStatsE; } /** * Looks in channel registry for a channel element matching the * given channel ID. * @param channelPublishId the channel publish ID * @return the channel element matching chanID * @throws org.jasig.portal.PortalException */ public static NodeList getCategories(String channelPublishId) throws PortalException { Document channelStatsListing = (Document)channelStatisticsCache.get(CHANNEL_STATISTICS_CACHE_KEY); NodeList categories = null; try { // This is unfortunately dependent on Xalan 2. Is there a way to use a standard interface? categories = (NodeList)XPathAPI.selectNodeList(channelStatsListing, "//category[channel/@ID = '" + channelPublishId + "']"); } catch (javax.xml.transform.TransformerException te) { throw new GeneralRenderingException("Not able to find channel " + channelPublishId + " within channel stats listing: " + te.getMessageAndLocation()); } return categories; } }