/** * Copyright © 2001, 2002 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.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; import org.jasig.portal.ChannelUser; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jasig.portal.utils.IPortalDocument; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * Describes channel statistics. * @author rtwigg@uccs.edu */ public class ChannelStatistics implements IBasicEntity { private static final Log log = LogFactory.getLog(ChannelStatistics.class); private static long zeroSeconds = 0; private int chanId; private String name; private Hashtable channelUsers; private int chanTotUsers; private int chanTotInstantiated; private int chanTotTargeted; /** * Constructs channel statistics. * @param chanId the channel ID */ public ChannelStatistics(int chanId) { this.chanId = chanId; this.name = ""; this.chanTotUsers = 0; this.chanTotInstantiated = 0; this.chanTotTargeted = 0; this.channelUsers = new Hashtable(); } // Accessor methods public int getChanId() { return chanId; } public String getName() { return name; } public int getChanTotUsers () { return chanTotUsers; } public int getChanTotInstantiated () { return chanTotInstantiated; } public int getChanTotTargeted () { return chanTotTargeted; } public ChannelUser[] getChannelUsers() { return (ChannelUser[])channelUsers.values().toArray(new ChannelUser[0]); } // Modifier methods public void setChanTotals() { this.chanTotUsers=0; this.chanTotInstantiated=0; this.chanTotTargeted=0; if (channelUsers != null) { Iterator iter = channelUsers.values().iterator(); while (iter.hasNext()) { ChannelUser cu = (ChannelUser)iter.next(); this.chanTotInstantiated+=cu.getTotInstantiated(); this.chanTotTargeted+=cu.getTotTargeted(); chanTotUsers++; } } } public void setName (String name) { this.name = name; } public void incChanTotUsers (int totUsersIncrement) { this.chanTotUsers += totUsersIncrement; } public void incChanTotInstantiated (int totInstantiatedIncrement) { this.chanTotInstantiated += totInstantiatedIncrement; } public void incChanTotTargeted (int totTargetedIncrement) { this.chanTotTargeted += totTargetedIncrement; } public void clearUsers() { this.channelUsers.clear(); } /** * Implementation required by IBasicEntity interface. * @return EntityIdentifier */ public EntityIdentifier getEntityIdentifier() { return new EntityIdentifier(String.valueOf(chanId), ChannelStatistics.class); } /** * Adds a user to this channel statistics */ public void addUser(ChannelUser channelUser) { channelUsers.put(String.valueOf(channelUser.getUserName()), channelUser); } /** * Removes a user from this channel statistics * @param channelUser the channel user to remove */ public void removeUser(ChannelUser channelUser) { removeUser(String.valueOf(channelUser.getUserName())); } /** * Removes a user from this channel statistics * @param userId the user Id */ public void removeUser(String userName) { channelUsers.remove(userName); } /** * Return an XML representation of channel user statistics */ private final void addUserElements(Document doc, Element channelStatsE) { if (channelUsers != null) { Iterator iter = channelUsers.values().iterator(); while (iter.hasNext()) { ChannelUser cu = (ChannelUser)iter.next(); Element channelUserE = cu.getDocument(doc); channelUserE = (Element)doc.importNode(channelUserE, true); channelStatsE.appendChild(channelUserE); } } } /** * Return an xml representation of this channel statistics */ public Element getDocument(Document doc, String idTag) { Element channelStats = doc.createElement("channelStats"); if (doc instanceof IPortalDocument) { ((IPortalDocument)doc).putIdentifier(idTag, channelStats); } else { StringBuffer msg = new StringBuffer(128); msg.append("ChannelStatistics::getDocument() : "); msg.append("Document does not implement IPortalDocument, "); msg.append("so element caching cannot be performed."); log.error( msg.toString()); } setChanTotals(); channelStats.setAttribute("ID", idTag); channelStats.setAttribute("chanID", chanId + ""); channelStats.setAttribute("name", name + ""); channelStats.setAttribute("chanTotUsers", chanTotUsers + ""); channelStats.setAttribute("chanTotInstantiated", chanTotInstantiated + ""); channelStats.setAttribute("chanTotTargeted", chanTotTargeted + ""); addUserElements(doc, channelStats); return channelStats; } }