package JCRSession; # -------------------------------------------------------------------- # The copyright and all other proprietary rights in this Database # (including software - SEE LICENSING AGREEMENT) are the sole and # exclusive property of the Institute for Scientific Information # (ISI). This Database and source code, or any derivative works # thereof, are confidential and proprietary to ISI . Therefore, # duplication, reuse, redistribution, reverse assembling, reverse # compiling, or translation of the Database, source code, or any # portion thereof IS NOT PERMITTED. Consult Licensing Agreement. # -------------------------------------------------------------------- # $Id: JCRSession.pm,v 1.1 2006/08/01 15:09:49 pfoley Exp $ # $Name: JCR_4_5_BETA_5 $ use strict; use lib ( "./" ); use Data::Dumper; use ISIBench; use JCRErrorList; use Log::Log4perl qw(get_logger); use StThomas::Context; =head1 NAME JCRSession - Module to support Session Management. Internally it communicates with the StThomas Client. =head2 METHODS =over 4 =cut =item new('product_code' => $product, 'server_uri' => $server_uri); Method to create new Session object. It returns Session object if success or else returns a non-zero positive integer. =cut sub new { ISIBench::start(); my $logger = get_logger("Session"); my $class_or_reference = shift; my $args = (ref $_[0] eq 'HASH') ? shift : { @_ }; my $ret; # return error if insufficient params. if ((defined $args->{product_code}) || (defined $args->{server_uri})) { my $class = ($class_or_reference || ref($class_or_reference)); my $self = bless {}, $class; $logger->debug(qq|Creating new session object with: PRODUCT=$args->{product_code} SERVER_URI=$args->{server_uri}|); # create the StThomas Session Management object. my $stObj = StThomas::Context->new( 'Product' => $args->{product_code}, 'StThomas_URI' => $args->{server_uri}, ); if (defined $stObj) { # save into data member variables. $self->{product} = $args->{product_code}; $self->{SID} = undef; $self->{stObj} = $stObj; $ret = $self; } else { $logger->error("Could not create StThomas object (#1004)."); $ret = 1004; } } else { $logger->error("Insufficient input params to create StThomas object. (#1003) Product Code: $args->{product_code}; Server URI: $args->{server_uri}"); $ret = 1003; } ISIBench::stop(); return $ret; } =item open('sid' => $sid); This method loads the session and get all the permissions for the given SID. Internally it calls StThomas Client's login api. It returns 0 upon success or a positive non-zero integer upon failure. =cut sub open { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; my $args = (ref $_[0] eq 'HASH') ? shift : { @_ }; # call StThomas's login method based on input params. my $ret; if((defined $args->{sid}) && (defined $args->{product})) { $self->{SID} = $args->{sid}; $self->{stObj}->login( 'Context_id' => $args->{sid}, ); my $error_string = $self->{stObj}->getError(); if (defined $error_string ) { $error_string =~ /^(.*?):/; $error_string = $1; $error_string =~ tr/a-z/A-Z/; $logger->error("StThomas login() method failed: $error_string."); # We had an error somewhere, so throw an error page my $errorlist = JCRErrorList->new(); if (defined($errorlist) && (ref($errorlist) =~ /jcrerrorlist/i)) { $ret = $errorlist->get_code('error' => $error_string) || 1007; } else { $ret = 1007; } } else { $ret = undef; } } else { $logger->error("SID not passed as an argument (#1006)."); $ret = 1006; } ISIBench::stop(); return $ret; } =item validate_session(); This method validates the user's session ID which is set for this session object. Returns 0 upon success or a positive non-zero integer upon failure. =cut sub validate_session { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; my $args = (ref $_[0] eq 'HASH') ? shift : { @_ }; my $ret; # call StThomas's login method based on input params. if (defined $self->{SID}) { $ret = $self->{stObj}->isIdValid('SID' => $self->{SID}); if (defined $ret) { $ret = 0; } else { $logger->error("StThomas isIdValid() method failed (#1009)."); $ret = 1009; } } else { $logger->error("SID not defined (#1008)."); $ret = 1008; } ISIBench::stop(); return $ret; } =item validate_editions('edition' => $edition, 'year' => $year); This method validates the user's subscription with the year and edition that provided as input. Returns 0 upon success or a positive non-zero integer upon failure. =cut sub validate_editions { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; my $args = (ref $_[0] eq 'HASH') ? shift : { @_ }; my $edition = $args->{edition} || 0; my $year = $args->{year} || 0; my $ret; if ($edition && $year) { # get the editions from StThomas for this user. my $limits = $self->get_editions(); if (defined $limits && (ref($limits) =~ /hash/i)) { # here we have to verify 2 items. if (defined $limits->{$edition}) { my $yearhash = $limits->{$edition}; if (($yearhash->{start_year} <= $year) && ($yearhash->{end_year} >= $year)) { # everything is valid. session is good to use. $logger->debug("validate(): Valid SID, Edition and Year."); $ret = return 0; } else { $logger->error("Invalid Year Selected **Subscription Error** (#1012)."); $ret = 1012; } } else { $logger->error("Invalid Edition Selected **Subscription Error** (#1013)."); $ret = 1013; } } else { $logger->error("Failed to get editions from StThomas (#1011)."); $ret = 1011; } } else { $logger->error("Inavlid/Insufficient input params (#1010)."); $ret = 1010; } ISIBench::stop(); return $ret; } =item close() This method will terminate user's session within StThomas and JCRWeb. Returns 0 upon success or a non-zero positive integer upon failure. =cut sub close { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; # call StThomas api to close this session. $logger->debug(qq|Terminating Session $self->{SID}|); my $retval = $self->{stObj}->logout(); ISIBench::stop(); return $retval; } =item get_editions( 'year_min' => '1999', 'year_max' => '2003') This method will retrieve the user's subscription information via the StThomas API method. It will return a hash whose keys are the edition names and its values are the subscription start and end years. If the optional 'year_min' and 'year_max' values are provided, the years of the editions return will never cross those boundries. =cut sub get_editions { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; my $args = (ref $_[0] eq 'HASH') ? shift : { @_ }; my $year_min = $args->{year_min} || 0; my $year_max = $args->{year_max} || 10000; # Not Y10K complient. :o/ my ($ret, %permissions); # call StThomas to get editions for this user. my $user = $self->{stObj}->get_user(); if (defined $user) { my @editions = $user->editions(); if ($#editions >= 0) { foreach my $edition (@editions) { my $jcr_edition = ($edition =~ /social.*/i) ? 'social' : 'science'; my $years = $user->edition_permissions($edition); $permissions{$jcr_edition}{end_year} = ($years->{NEWYEAR} >= $year_max) ? $year_max : $years->{NEWYEAR}; $permissions{$jcr_edition}{start_year} = ($years->{OLDYEAR} <= $year_min) ? $year_min : $years->{OLDYEAR}; } $ret = \%permissions; $logger->debug("Retrived Editions: ", sub {Dumper($ret)}); } else { $logger->error("Failed to get editions from StThomas (#1011)."); $logger->debug("Retrived Editions: ", sub {Dumper(\@editions)}); $ret = 1011; } } else { $logger->error("Failed to get editions from StThomas (#1011)."); $ret = 1011; } ISIBench::stop(); return $ret; } =item get('value' => $value); Method to retrieve a value from StThomas Client persistent session store. It returns the desired value upon success, undef upon failure. =cut sub get { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; my $args = (ref $_[0] eq 'HASH') ? shift : { @_ }; my $ret = { "$args->{value}" => $self->{stObj}->get($args->{value}) }; ISIBench::stop(); return $ret; } =item set('name' => $name, 'value' => $value); This method will save a value into the session store. Note that this method will only change the session value within the object. Another method commit() of this class must be called in order to save the changes to StThomas. It Returns 0 upon success or a positive non-zero value upon failure. =cut sub set { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; my $args = (ref $_[0] eq 'HASH') ? shift : { @_ }; $self->{stObj}->set($args->{name}, $args->{value}); ISIBench::stop(); return 0; } =item remove('name' => $name); This method will remove a value from the session store. It Returns 0 upon success or a positive non-zero value upon failure. =cut sub remove { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; my $args = (ref $_[0] eq 'HASH') ? shift : { @_ }; $self->set( 'name' => $args->{name}, 'value' => undef, ); ISIBench::stop(); return 0; } =item commit() Method to save changed session variables to StThomas. This will allow for only one 'set' to take place within StThomas, thus increasing performance. The user's session must be committed just before the page is displayed. =cut sub commit { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; # call the StThomas flush method to write stuff on disk. $self->{stObj}->flush(); ISIBench::stop(); return 0; } =item log_event ('event' => $event, 'data' => $data); Logs the defined event with the defined data hash to StThomas =cut sub log_event { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; my $args = (ref $_[0] eq 'HASH') ? shift : { @_ }; my $ret; if (exists ($args->{event}) && exists ($args->{data})) { my $event = $args->{event}; my $data = $args->{data}; $logger->debug("Logging Event: $event with data:", sub{Dumper($data)}); $ret = $self->{stObj}->log_events($event, $data) } else { $logger->error("Insufficient parameters to Session::log_event()"); $ret = 1016; } ISIBench::stop(); return $ret; } =item getError() Method to retrieve StThomas server error messages. =cut sub getError { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; my $ret = $self->{stObj}->getError(); ISIBench::stop(); return $ret; } =item getOpacSites() Method to retrieve StThomas Services Opac Sitenames. =cut sub getOpacSites { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; # call the StThomas::User custom_app_permissions method to retrieve Opac Sitenames. my $user = $self->{stObj}->get_user(); my @ret = $user->custom_app_permissions('Opac sites'); my ($value, $info); my @infos; foreach (@ret) { ($value, $info) = @{$_}; push @infos, $info; } ISIBench::stop(); return join(",", @infos); } =item getOpenURLsites() Method to retrieve StThomas Services OpenURL Sitenames. =cut sub getOpenURLsites { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; # call the StThomas::User custom_app_permissions method to retrieve OpenURL Sitenames. my $user = $self->{stObj}->get_user(); my @ret = $user->custom_app_permissions('OpenURL sites'); ISIBench::stop(); return $ret[0][1]; } =item getPublisherLinks() Method to retrieve StThomas Services Publisher Links. =cut sub getPublisherLinks { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; # call the StThomas custom_app_permissions method to retrieve Publisher Links. my $user = $self->{stObj}->get_user(); my $ret = $user->publishers(); ISIBench::stop(); return $ret; } =item getPortalSID() Method to retrieve StThomas Services Portal SID. =cut sub getPortalSID { ISIBench::start(); my $logger = get_logger("Session"); my $self = shift; # call the StThomas custom_app_permissions method to retrieve Portal SID. my $user = $self->{stObj}->get_user(); my $ret = $user->portal_sid(); ISIBench::stop(); return $ret; } ####### ## private method DESTROY, to destroy stObj object at the end of session object. ####### sub DESTROY { my $logger = get_logger("Session"); my $self = shift; $self->{stObj} = undef; $self->{SID} = undef; return; } 1;