/** * Copyright © 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.services.stats; import org.jasig.portal.ChannelDefinition; import org.jasig.portal.UserProfile; import org.jasig.portal.layout.IUserLayoutChannelDescription; import org.jasig.portal.layout.IUserLayoutFolderDescription; import org.jasig.portal.security.IPerson; import org.jasig.portal.ChannelStatisticsStoreFactory; import org.jasig.portal.IChannelStatisticsStore; import org.jasig.portal.ChannelUser; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.Hashtable; import java.util.Enumeration; import java.sql.Date; import java.io.IOException; import java.lang.reflect.*; /** * Updates portal database with channel statistics. Adapted from MessageStatsRecorder by Ken Weiner * @author rtwigg@uccs.edu */ public class RDBMStatsRecorder implements IStatsRecorder { private static final Log log = LogFactory.getLog(RDBMStatsRecorder.class); protected static final IChannelStatisticsStore css = ChannelStatisticsStoreFactory.getChannelStatisticsStoreImpl(); private Hashtable personStats = new Hashtable(); public void recordLogin(IPerson person) {} public void recordLogout(IPerson person) {} public void recordSessionCreated(IPerson person) {} public void recordSessionDestroyed(IPerson person) {} public void recordChannelDefinitionPublished(IPerson person, ChannelDefinition channelDef) {} public void recordChannelDefinitionModified(IPerson person, ChannelDefinition channelDef) {} public void recordChannelDefinitionRemoved(IPerson person, ChannelDefinition channelDef) {} public void recordChannelAddedToLayout(IPerson person, UserProfile profile, IUserLayoutChannelDescription channelDesc) { String msg = "Channel [" + channelDesc.getName() + ", " + channelDesc.getChannelPublishId() + ", " + channelDesc.getChannelSubscribeId() + "] " + "was added to layout " + profile.getLayoutId() + " " + "by " + getDisplayName(person) + " " + "at " + new java.sql.Date(System.currentTimeMillis()); if (channelDesc.trackStats()) { // Get set of ChannelStats for this person Hashtable channelSet = (Hashtable)personStats.get(person); if(channelSet==null) { channelSet = new Hashtable(); } // Now get/set stats for this channel String cId = channelDesc.getChannelPublishId(); ChannelUser cUser = (ChannelUser)channelSet.get(cId); if(cUser==null) { cUser = new ChannelUser(Integer.parseInt(cId), (String)person.getAttribute(IPerson.USERNAME)); } cUser.setDateAdded(new java.sql.Date(System.currentTimeMillis())); channelSet.put(cId, cUser); personStats.put(person, channelSet); } } public void recordChannelUpdatedInLayout(IPerson person, UserProfile profile, IUserLayoutChannelDescription channelDesc) {} public void recordChannelMovedInLayout(IPerson person, UserProfile profile, IUserLayoutChannelDescription channelDesc) {} public void recordChannelRemovedFromLayout(IPerson person, UserProfile profile, IUserLayoutChannelDescription channelDesc) {} public void recordFolderAddedToLayout(IPerson person, UserProfile profile, IUserLayoutFolderDescription folderDesc) {} public void recordFolderUpdatedInLayout(IPerson person, UserProfile profile, IUserLayoutFolderDescription folderDesc) {} public void recordFolderMovedInLayout(IPerson person, UserProfile profile, IUserLayoutFolderDescription folderDesc) {} public void recordFolderRemovedFromLayout(IPerson person, UserProfile profile, IUserLayoutFolderDescription folderDesc) {} public void recordChannelInstantiated(IPerson person, UserProfile profile, IUserLayoutChannelDescription channelDesc) { String msg = "Channel [" + channelDesc.getName() + ", " + channelDesc.getChannelPublishId() + ", " + channelDesc.getChannelSubscribeId() + "] " + "was instantiated in layout " + profile.getLayoutId() + " " + "by " + getDisplayName(person) + " " + "at " + new java.sql.Date(System.currentTimeMillis()); if (channelDesc.trackStats()) { // Get set of ChannelStatistics for this person Hashtable channelSet = (Hashtable)personStats.get(person); if(channelSet==null) { channelSet = new Hashtable(); } // Now get/set stats for this channel String cId = channelDesc.getChannelPublishId(); ChannelUser cUser = (ChannelUser)channelSet.get(cId); if(cUser==null) { cUser = new ChannelUser(Integer.parseInt(cId), (String)person.getAttribute(IPerson.USERNAME)); } cUser.setDateInstantiated(new java.sql.Date(System.currentTimeMillis())); cUser.incTotInstantiated(); channelSet.put(cId, cUser); personStats.put(person, channelSet); } } public void recordChannelRendered(IPerson person, UserProfile profile, IUserLayoutChannelDescription channelDesc) { } public void recordChannelTargeted(IPerson person, UserProfile profile, IUserLayoutChannelDescription channelDesc) { String msg = "Channel [" + channelDesc.getName() + ", " + channelDesc.getChannelPublishId() + ", " + channelDesc.getChannelSubscribeId() + "] " + "was instantiated in layout " + profile.getLayoutId() + " " + "by " + getDisplayName(person) + " " + "at " + new java.sql.Date(System.currentTimeMillis()); if (channelDesc.trackStats()) { // Get set of ChannelStatistics for this person Hashtable channelSet = (Hashtable)personStats.get(person); if(channelSet==null) { channelSet = new Hashtable(); } // Now get/set stats for this channel String cId = channelDesc.getChannelPublishId(); ChannelUser cUser = (ChannelUser)channelSet.get(cId); if(cUser==null) { cUser = new ChannelUser(Integer.parseInt(cId), (String)person.getAttribute(IPerson.USERNAME)); } cUser.setDateTargeted(new java.sql.Date(System.currentTimeMillis())); cUser.incTotTargeted(); channelSet.put(cId, cUser); personStats.put(person, channelSet); } } /** * Outputs the message formulated according * to the stat being recorded. Subclasses * have the responsibility of implementing * this method. * @param message, the message to output */ /** * Creates a name suitable for displaying in a * stats message. Indicates if user is a "guest" user. * @param person, the person * @return name, the display name for the user */ private String getDisplayName(IPerson person) { String name = null; if (person != null) { String userName = (String)person.getAttribute(IPerson.USERNAME); if (person.isGuest()) { name = "GUEST_USER (" + fixNull(userName) + ")"; } else { String firstName = (String)person.getAttribute("givenName"); String lastName = (String)person.getAttribute("sn"); name = fixNull(firstName) + " " + fixNull(lastName) + " (" + fixNull(userName) + ")"; } } else { name = "NULL_PERSON"; } return name; } /** * Replaces null String with an empty String * @param s, the string to fix * @return s, the fixed string */ private String fixNull(String s) { return s == null ? "" : s; } /** * Prepends a string to each message to make it * clear that it came from the LoggingStatsRecorder * @param msg, the message to log * @return msg, the message prepended with a string that * identifies the message as having come from a stats recorder */ private String fixMsg(String msg) { return "STATS-RECORDER: " + msg; } public void saveStatistics(IPerson person) { // Get set of ChannelStatistics for this person Hashtable channelSet = (Hashtable)personStats.get(person); try { css.putChannelStatistics(person, channelSet); } catch (Exception e) { log.error( "RDBMStatsRecorder::saveStatistics() : unable to save channel statistics."+e); } } }