| 1 | #! /bin/perl | ||
| 2 | ############################################################################# | ||
| 3 | # | ||
| 4 | # NOTE: This file under revision control using RCS | ||
| 5 | # Any changes made without RCS will be lost | ||
| 6 | # | ||
| 7 | # $Source: /usr/local/cvsroot/vbtk/VBTK/Objects/History.pm,v $ | ||
| 8 | # $Revision: 1.8 $ | ||
| 9 | # $Date: 2002/03/04 20:53:07 $ | ||
| 10 | # $Author: bhenry $ | ||
| 11 | # $Locker: $ | ||
| 12 | # $State: Exp $ | ||
| 13 | # | ||
| 14 | # Purpose: A perl library used to store VB Object history | ||
| 15 | # elements | ||
| 16 | # | ||
| 17 | # Copyright (C) 1996 - 2002 Brent Henry | ||
| 18 | # | ||
| 19 | # This program is free software; you can redistribute it and/or | ||
| 20 | # modify it under the terms of version 2 of the GNU General Public | ||
| 21 | # License as published by the Free Software Foundation available at: | ||
| 22 | # http://http://www.gnu.org/copyleft/gpl.html | ||
| 23 | # | ||
| 24 | # This program is distributed in the hope that it will be useful, | ||
| 25 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 26 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 27 | # GNU General Public License for more details. | ||
| 28 | # | ||
| 29 | ############################################################################# | ||
| 30 | # | ||
| 31 | # | ||
| 32 | # REVISION HISTORY: | ||
| 33 | # | ||
| 34 | # $Log: History.pm,v $ | ||
| 35 | # Revision 1.8 2002/03/04 20:53:07 bhenry | ||
| 36 | # *** empty log message *** | ||
| 37 | # | ||
| 38 | # Revision 1.7 2002/03/04 16:49:10 bhenry | ||
| 39 | # Changed requirement back to perl 5.6.0 | ||
| 40 | # | ||
| 41 | # Revision 1.6 2002/03/02 00:53:55 bhenry | ||
| 42 | # Documentation updates | ||
| 43 | # | ||
| 44 | # Revision 1.5 2002/02/20 19:25:18 bhenry | ||
| 45 | # *** empty log message *** | ||
| 46 | # | ||
| 47 | # Revision 1.4 2002/01/25 16:42:06 bhenry | ||
| 48 | # Changed to serialized filename to end in '.ser' | ||
| 49 | # | ||
| 50 | # Revision 1.3 2002/01/25 07:13:08 bhenry | ||
| 51 | # Changed to use Storable instead of VBTK::Serialize | ||
| 52 | # | ||
| 53 | # Revision 1.2 2002/01/21 17:07:50 bhenry | ||
| 54 | # Disabled 'uninitialized' warnings | ||
| 55 | # | ||
| 56 | # Revision 1.1.1.1 2002/01/17 18:05:57 bhenry | ||
| 57 | # VBTK Project | ||
| 58 | # | ||
| 59 | |||
| 60 | package VBTK::Objects::History; | ||
| 61 | |||
| 62 | use 5.6.0; | ||
| 63 | use strict; | ||
| 64 | use warnings; | ||
| 65 | # I like using undef as a value so I'm turning off the uninitialized warnings | ||
| 66 | no warnings qw(uninitialized); | ||
| 67 | |||
| 68 | use VBTK::Common; | ||
| 69 | use VBTK::File; | ||
| 70 | |||
| 71 | our $VERBOSE=$ENV{'VERBOSE'}; | ||
| 72 | our $FULL=3; | ||
| 73 | our $NOTEXT=2; | ||
| 74 | our $MINIMAL=1; | ||
| 75 | |||
| 76 | our %CACHE_LEVELS = ( | ||
| 77 | Text => $FULL, | ||
| 78 | HeaderMsg => $FULL, | ||
| 79 | FooterMsg => $FULL, | ||
| 80 | Status => $NOTEXT, | ||
| 81 | Timestamp => $NOTEXT, | ||
| 82 | Repeated => $NOTEXT, | ||
| 83 | RepeatStart => $NOTEXT, | ||
| 84 | RawStatus => $NOTEXT, | ||
| 85 | ExpireTime => $NOTEXT, | ||
| 86 | ); | ||
| 87 | |||
| 88 | #------------------------------------------------------------------------------- | ||
| 89 | # Function: new | ||
| 90 | # Description: Object constructor. Allocates memory for all class members | ||
| 91 | # Input Parms: Configuration filename | ||
| 92 | # Output Parms: Pointer to class | ||
| 93 | #------------------------------------------------------------------------------- | ||
| 94 | sub new | ||
| 95 | { | ||
| 96 | my $type = shift; | ||
| 97 | my $self = {}; | ||
| 98 | bless $self, $type; | ||
| 99 | |||
| 100 | $self->set(@_); | ||
| 101 | |||
| 102 | log("Setting up VB History Object for $self->{ObjName}, $self->{Timestamp}") | ||
| 103 | if ($VERBOSE > 3); | ||
| 104 | |||
| 105 | # Only run this part if we're in VERBOSE mode, it's not really necessary, but | ||
| 106 | # is just an extra check to make sure the right parameters are being passed. | ||
| 107 | if ($VERBOSE) | ||
| 108 | { | ||
| 109 | # Setup a hash of default parameters | ||
| 110 | my $defaultParms = { | ||
| 111 | ObjPath => $::REQUIRED, | ||
| 112 | ObjName => $::REQUIRED, | ||
| 113 | FileName => $::REQUIRED, | ||
| 114 | Timestamp => undef, | ||
| 115 | Status => undef, | ||
| 116 | Text => undef, | ||
| 117 | HeaderMsg => undef, | ||
| 118 | FooterMsg => undef, | ||
| 119 | Repeated => undef, | ||
| 120 | RepeatStart => undef, | ||
| 121 | RawStatus => undef, | ||
| 122 | ExpireTime => undef, | ||
| 123 | CacheMode => undef, | ||
| 124 | }; | ||
| 125 | |||
| 126 | # Run a validation on the passed parms, using the default parms | ||
| 127 | $self->validateParms($defaultParms) || return undef; | ||
| 128 | } | ||
| 129 | |||
| 130 | my $ObjPath = $self->{ObjPath}; | ||
| 131 | my $ObjName = $self->{ObjName}; | ||
| 132 | my $FileName = $self->{FileName}; | ||
| 133 | my $metaFileName = "$ObjPath/$ObjName/$FileName.ser"; | ||
| 134 | |||
| 135 | $self->{CacheMode} = $NOTEXT unless ($self->{CacheMode}); | ||
| 136 | $self->{metaFileObj} = new VBTK::File($metaFileName); | ||
| 137 | |||
| 138 | # Mark the object as only currently having the minimal data loaded. | ||
| 139 | $self->{currCacheMode} = $MINIMAL; | ||
| 140 | |||
| 141 | # If no status was passed, then try to load the object from the corresponding | ||
| 142 | # history file. Pass in the specified cacheMode. | ||
| 143 | if($self->{Status} eq undef) | ||
| 144 | { | ||
| 145 | $self->load($self->{CacheMode}) || return undef; | ||
| 146 | } | ||
| 147 | |||
| 148 | ($self); | ||
| 149 | } | ||
| 150 | |||
| 151 | #------------------------------------------------------------------------------- | ||
| 152 | # Function: load | ||
| 153 | # Description: Load the history object from it's file. | ||
| 154 | # Input Parms: None | ||
| 155 | # Output Parms: None | ||
| 156 | #------------------------------------------------------------------------------- | ||
| 157 | sub load | ||
| 158 | { | ||
| 159 | my $self = shift; | ||
| 160 | my $cacheMode = shift; | ||
| 161 | |||
| 162 | # If we're already loaded at a higher cache mode than is requested, then just | ||
| 163 | # return. | ||
| 164 | return 1 if ($self->{currCacheMode} >= $cacheMode); | ||
| 165 | |||
| 166 | my $ObjName = $self->{ObjName}; | ||
| 167 | my $FileName = $self->{FileName}; | ||
| 168 | my $metaFileObj = $self->{metaFileObj}; | ||
| 169 | my ($key); | ||
| 170 | |||
| 171 | &log("Loading history object for '$ObjName - $FileName', cacheMode $cacheMode") | ||
| 172 | if ($VERBOSE > 1); | ||
| 173 | my $struct = $metaFileObj->serGet(); | ||
| 174 | |||
| 175 | return undef if ($struct eq undef); | ||
| 176 | |||
| 177 | # Load retrieved values into the current object, but only those which | ||
| 178 | # correspond to the specified cache level. | ||
| 179 | foreach $key (keys %{$struct}) | ||
| 180 | { | ||
| 181 | if ($CACHE_LEVELS{$key} <= $cacheMode) | ||
| 182 | { | ||
| 183 | &log("Loading '$key'") if ($VERBOSE > 2); | ||
| 184 | $self->{$key} = $struct->{$key}; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | # Mark as loaded. | ||
| 189 | $self->{currCacheMode} = $cacheMode; | ||
| 190 | |||
| 191 | (1); | ||
| 192 | } | ||
| 193 | |||
| 194 | #------------------------------------------------------------------------------- | ||
| 195 | # Function: store | ||
| 196 | # Description: Write the history object out to a file | ||
| 197 | # Input Parms: | ||
| 198 | # Output Parms: | ||
| 199 | #------------------------------------------------------------------------------- | ||
| 200 | sub store | ||
| 201 | { | ||
| 202 | my $self = shift; | ||
| 203 | my $ObjName = $self->{ObjName}; | ||
| 204 | my $Timestamp = $self->{Timestamp}; | ||
| 205 | my $CacheMode = $self->{CacheMode}; | ||
| 206 | my $metaFileObj = $self->{metaFileObj}; | ||
| 207 | my ($key); | ||
| 208 | |||
| 209 | # Create a structure of the data elements we want to save | ||
| 210 | my $struct = { map { $_ => $self->{$_} } keys %CACHE_LEVELS }; | ||
| 211 | |||
| 212 | &log("Saving history object for '$ObjName - $Timestamp'") if ($VERBOSE > 1); | ||
| 213 | $metaFileObj->serPut($struct) || return undef; | ||
| 214 | |||
| 215 | # Now remove any entries from the object which should not be cached in memory. | ||
| 216 | foreach $key (keys %CACHE_LEVELS) | ||
| 217 | { | ||
| 218 | delete($self->{$key}) if ($CACHE_LEVELS{$key} > $CacheMode); | ||
| 219 | } | ||
| 220 | |||
| 221 | # Re-set the current cache mode value | ||
| 222 | $self->{currCacheMode} = $CacheMode; | ||
| 223 | |||
| 224 | (1); | ||
| 225 | } | ||
| 226 | |||
| 227 | #------------------------------------------------------------------------------- | ||
| 228 | # Function: delete | ||
| 229 | # Description: Delete the history object | ||
| 230 | # Input Parms: | ||
| 231 | # Output Parms: | ||
| 232 | #------------------------------------------------------------------------------- | ||
| 233 | sub delete | ||
| 234 | { | ||
| 235 | my $self = shift; | ||
| 236 | $self->{metaFileObj}->unlink; | ||
| 237 | |||
| 238 | (1); | ||
| 239 | } | ||
| 240 | |||
| 241 | #------------------------------------------------------------------------------- | ||
| 242 | # Function: loadAndGet | ||
| 243 | # Description: Load the specified parm from it's meta file if it's not already | ||
| 244 | # loaded, and then return it's value. | ||
| 245 | # Input Parms: Parm name. | ||
| 246 | # Output Parms: Value | ||
| 247 | #------------------------------------------------------------------------------- | ||
| 248 | sub loadAndGet | ||
| 249 | { | ||
| 250 | my $self = shift; | ||
| 251 | my $parm = shift; | ||
| 252 | |||
| 253 | # If the value is already loaded, then just return it. | ||
| 254 | return $self->{$parm} if ($self->{$parm}); | ||
| 255 | |||
| 256 | $self->load($CACHE_LEVELS{$parm}); | ||
| 257 | |||
| 258 | $self->{$parm}; | ||
| 259 | } | ||
| 260 | |||
| 261 | # Simple Get Methods | ||
| 262 | sub getTimestamp { $_[0]->loadAndGet('Timestamp'); } | ||
| 263 | sub getFileName { $_[0]->{FileName}; } | ||
| 264 | sub getStatus { $_[0]->loadAndGet('Status'); } | ||
| 265 | sub getHeaderMsg { $_[0]->loadAndGet('HeaderMsg'); } | ||
| 266 | sub getFooterMsg { $_[0]->loadAndGet('FooterMsg'); } | ||
| 267 | sub getRepeated { $_[0]->loadAndGet('Repeated'); } | ||
| 268 | sub getRepeatStart { $_[0]->loadAndGet('RepeatStart'); } | ||
| 269 | sub getRawStatus { $_[0]->loadAndGet('RawStatus'); } | ||
| 270 | sub getExpireTime { $_[0]->loadAndGet('ExpireTime'); } | ||
| 271 | sub getText { $_[0]->loadAndGet('Text'); } | ||
| 272 | |||
| 273 | # Simple Set and Comparison Methods | ||
| 274 | sub setRepeatStart { $_[0]->{RepeatStart} = $_[1]; } | ||
| 275 | sub setStatus { $_[0]->{Status} = $_[1]; } | ||
| 276 | |||
| 277 | sub addToRepeated { $_[0]->{Repeated} += $_[1]; } | ||
| 278 | sub addHeaderMsg { $_[0]->{HeaderMsg} = $_[1] . $_[0]->{HeaderMsg}; } | ||
| 279 | sub addFooterMsg { $_[0]->{FooterMsg} .= $_[1]; } | ||
| 280 | |||
| 281 | sub sameStatus { $_[0]->{Status} eq $_[1]->{Status}; } | ||
| 282 | |||
| 283 | 1; | ||
| 284 | __END__ |