/usr/share/otrs/Kernel/System/CommunicationLog.pm is in otrs2 6.0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | # --
# Copyright (C) 2001-2018 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --
package Kernel::System::CommunicationLog;
use strict;
use warnings;
use Kernel::System::VariableCheck qw(:all);
our %ObjectManagerFlags = (
NonSingleton => 1,
AllowConstructorFailure => 1,
);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::Log',
'Kernel::System::CommunicationLog::DB',
'Kernel::System::DB',
'Kernel::System::DateTime',
'Kernel::System::Main',
);
=head1 PUBLIC INTERFACE
=head2 new()
Creates a CommunicationLog object. Do not use new() directly, instead use the object manager.
This is a class which represents a complete communication. Therefore the created
instances must not be shared between processes of different communications.
Please use the object manager as follows for this class:
# Create an object, representing a new communication:
my $CommunicationLogObject = $Kernel::OM->Create(
'Kernel::System::CommunicationLog',
ObjectParams => {
Transport => 'Email',
Direction => 'Incoming',
}
);
# Create an object for an already existing communication:
my $CommunicationLogObject = $Kernel::OM->Create(
'Kernel::System::CommunicationLog',
ObjectParams => {
CommunicationID => 123,
}
);
=cut
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {};
bless( $Self, $Type );
if ( IsStringWithData( $Param{CommunicationID} ) || IsStringWithData( $Param{ObjectLogID} ) ) {
return $Self->_RecoverCommunicationObject(%Param);
}
return $Self->_CommunicationStart(%Param);
}
=head2 CommunicationStop()
Update the status of a communication entry.
my $Success = $CommunicationLogObject->CommunicationStop(
Status => 'Successful', # (required) Needs to be either 'Successful', 'Warning' or 'Failed'
);
Returns:
1 in case of success, 0 in case of errors
=cut
sub CommunicationStop {
my ( $Self, %Param ) = @_;
# close open object types before
for my $ObjectType ( sort keys %{ $Self->{Object} } ) {
return if !$Self->ObjectLogStop( ObjectType => $ObjectType );
}
my $CommunicationDBObject = $Kernel::OM->Get('Kernel::System::CommunicationLog::DB');
my $Result = $CommunicationDBObject->CommunicationUpdate(
CommunicationID => $Self->{CommunicationID},
Status => $Param{Status},
);
# Remember status
$Self->{Status} = $Param{Status};
return 1;
}
=head2 CommunicationIDGet()
Returns the communication id.
my $CommunicationID = $CommunicationLogObject->CommunicationIDGet();
Returns:
The communication id of the current communication represented by this object.
=cut
sub CommunicationIDGet {
my ( $Self, %Param ) = @_;
return $Self->{CommunicationID};
}
=head2 TransportGet()
Returns the used transport.
my $Transport = $CommunicationLogObject->TransportGet();
Returns:
The transport of the current communication represented by this object.
=cut
sub TransportGet {
my ( $Self, %Param ) = @_;
return $Self->{Transport};
}
=head2 DirectionGet()
Returns the used direction.
my $Direction = $CommunicationLogObject->DirectionGet();
Returns:
The direction of the current communication represented by this object.
=cut
sub DirectionGet {
my ( $Self, %Param ) = @_;
return $Self->{Direction};
}
=head2 StatusGet()
Returns the current Status.
my $Direction = $CommunicationLogObject->StatusGet();
Returns:
The status of the current communication represented by this object.
=cut
sub StatusGet {
my ( $Self, %Param ) = @_;
return $Self->{Status};
}
=head2 ObjectLogStart()
Starts a log object of a given object type.
my $ObjectID = $CommunicationLogObject->ObjectLogStart(
ObjectType => 'Connection' # (required) Can be 'Connection' or 'Message'
);
Returns:
1 in case of success, 0 in case of errors
=cut
sub ObjectLogStart {
my ( $Self, %Param ) = @_;
if ( $Self->{Current}->{ $Param{ObjectLogType} } ) {
return $Self->_LogError("Object already has an open Log for this type.");
}
my $CommunicationDBObject = $Kernel::OM->Get('Kernel::System::CommunicationLog::DB');
my $ObjectLogID = $CommunicationDBObject->ObjectLogCreate(
CommunicationID => $Self->{CommunicationID},
%Param
);
return if !$ObjectLogID;
$Self->{Current}->{ $Param{ObjectLogType} } = $ObjectLogID;
return $ObjectLogID;
}
=head2 ObjectLogStop()
Stops a log object of a given object type.
my $Success = $CommunicationLogObject->ObjectLogStop(
ObjectLogType => 'Connection' # (required) Can be 'Connection' or 'Message'
ObjectLogID => 123, # (required) The ObjectID of the started object type
);
Returns:
1 in case of success, 0 in case of errors
=cut
sub ObjectLogStop {
my ( $Self, %Param ) = @_;
if ( !$Param{ObjectLogType} ) {
return $Self->_LogError("Need ObjectLogType.");
}
if ( !$Self->{Current}->{ $Param{ObjectLogType} } ) {
return $Self->_LogError("Cannot stop a ObjectLog that is not open.");
}
my $CommunicationDBObject = $Kernel::OM->Get('Kernel::System::CommunicationLog::DB');
my $Result = $CommunicationDBObject->ObjectLogUpdate(
CommunicationID => $Self->{CommunicationID},
ObjectLogID => $Self->{Current}->{ $Param{ObjectLogType} },
ObjectLogType => $Param{ObjectLogType},
Status => $Param{Status},
);
if ( !$Result ) {
return $Self->_LogError("Could not stop object log.");
}
delete $Self->{Current}->{ $Param{ObjectLogType} };
return 1;
}
=head2 ObjectLog()
Adds a log entry for a certain log object.
my $Success = $CommunicationLogObject->ObjectLog(
ObjectLogType => '...' # (required) To be defined by the related LogObject
ObjectLogID => 123, # (required) The ObjectID of the started object type
);
Returns:
1 in case of success, 0 in case of errors
=cut
sub ObjectLog {
my ( $Self, %Param ) = @_;
if ( !$Param{ObjectLogType} ) {
return $Self->_LogError("Need ObjectLogType.");
}
my $ObjectLogID = $Self->{Current}->{ $Param{ObjectLogType} };
if ( !$ObjectLogID ) {
return $Self->_LogError("Object Log needs to have an open Log Type.");
}
$Param{Priority} //= 'Info';
# In case of error also add it to the system log.
if ( $Param{Priority} eq 'Error' ) {
my @Identification = (
'ID:' . $Self->CommunicationIDGet(),
'AccountType:' . ( $Self->{AccountType} || '-' ),
'AccountID:' . ( $Self->{AccountID} || '-' ),
'Direction:' . $Self->{Direction},
'Transport:' . $Self->{Transport},
'ObjectLogType:' . $Param{ObjectLogType},
'ObjectLogID:' . $ObjectLogID,
);
$Self->_LogError(
sprintf(
'CommunicationLog(%s)' . '::%s => %s',
join( ',', @Identification, ),
$Param{Key},
$Param{Value},
),
);
}
my $CommunicationDBObject = $Kernel::OM->Get('Kernel::System::CommunicationLog::DB');
return $CommunicationDBObject->ObjectLogEntryCreate(
CommunicationID => $Self->{CommunicationID},
ObjectLogID => $ObjectLogID,
Key => $Param{Key},
Value => $Param{Value},
Priority => $Param{Priority},
);
}
=head2 ObjectLookupSet()
Inserts or updates a lookup information.
my $Result = $CommunicationLogObject->ObjectLookupSet(
ObjectID => 123, # (required)
TargetObjectType => 'Article', # (required)
TargetObjectID => 123, # (required)
);
Returns:
<undef> - if any error occur
1 - in case of success
=cut
sub ObjectLookupSet {
my ( $Self, %Param ) = @_;
if ( !$Param{ObjectLogType} ) {
return $Self->_LogError("Need ObjectLogType.");
}
if ( !$Self->{Current}->{ $Param{ObjectLogType} } ) {
return $Self->_LogError("Cannot set a ObjectLog that is not open.");
}
my $CommunicationDBObject = $Kernel::OM->Get('Kernel::System::CommunicationLog::DB');
return $CommunicationDBObject->ObjectLookupSet(
ObjectLogID => $Self->{Current}->{ $Param{ObjectLogType} },
TargetObjectType => $Param{TargetObjectType},
TargetObjectID => $Param{TargetObjectID},
);
}
=head2 ObjectLookupGet()
Gets the object lookup information.
my $Result = $CommunicationLogObject->ObjectLookupGet(
TargetObjectID => '...',
TargetObjectType => '...',
);
Returns:
<undef> - if any error occur
An hashref with object lookup information - in case info exists
An empty hasref - in case info doesn't exists
=cut
sub ObjectLookupGet {
my ( $Self, %Param ) = @_;
my $CommunicationDBObject = $Kernel::OM->Get('Kernel::System::CommunicationLog::DB');
return $CommunicationDBObject->ObjectLookupGet(%Param);
}
=head2 IsObjectLogOpen()
Checks if a given ObjectLogType has an open Object or not.
my $Result = $CommunicationLogObject->IsObjectLogOpen(
ObjectLogType => '...', # Required
);
Returns:
The ObjectLogID or undef.
=cut
sub IsObjectLogOpen {
my ( $Self, %Param ) = @_;
if ( !$Param{ObjectLogType} ) {
return $Self->_LogError("Need ObjectLogType.");
}
return $Self->{Current}->{ $Param{ObjectLogType} };
}
=head2 PRIVATE INTERFACE
Private methods
=cut
=head2 _CommunicationStart()
Create a new communication entry.
my $Success = $CommunicationLogObject->CommunicationStart(
Status => 'Processing', # (optional) Needs to be either 'Successful', 'Processing', 'Warning' or 'Failed'
# In most of the cases, just 'Processing' will make sense at the very beginning
# of a communication (Default: 'Processing').
AccountType => # (optional) The used account type
AccountID => # (optional) The used account id
);
Returns:
1 in case of success, 0 in case of errors
=cut
sub _CommunicationStart {
my ( $Self, %Param ) = @_;
if ( $Self->{CommunicationID} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'debug',
Message => "Communication with id '$Self->{CommunicationID}' is already started!",
);
return $Self;
}
$Self->{Transport} = $Param{Transport};
$Self->{Direction} = $Param{Direction};
$Self->{Status} = $Param{Status} || 'Processing';
my $CommunicationDBObject = $Kernel::OM->Get('Kernel::System::CommunicationLog::DB');
my $CommunicationID = $CommunicationDBObject->CommunicationCreate(
Direction => $Self->{Direction},
Transport => $Self->{Transport},
Status => $Self->{Status},
AccountType => $Param{AccountType},
AccountID => $Param{AccountID},
);
# return if there is not article created
if ( !$CommunicationID ) {
return $Self->_LogError("Can't get CommunicationID from communication start!");
}
# remember the new communication id
$Self->{CommunicationID} = $CommunicationID;
return $Self;
}
=head2 _RecoverCommunciationObject()
Recover a Communication object given an CommunicationID or ObjectLogID.
=cut
sub _RecoverCommunicationObject {
my ( $Self, %Param ) = @_;
my $CommunicationDBObject = $Kernel::OM->Get('Kernel::System::CommunicationLog::DB');
my $CommunicationData = {};
my $ErrorMessage = "Could not restore the communication with %s '%s'!";
if ( $Param{CommunicationID} ) {
$ErrorMessage = sprintf $ErrorMessage, 'CommunicationID', $Param{CommunicationID};
$CommunicationData = $CommunicationDBObject->CommunicationGet(
CommunicationID => $Param{CommunicationID},
);
}
else {
$ErrorMessage = sprintf $ErrorMessage, 'ObjectLogID', $Param{ObjectLogID};
$CommunicationData = $CommunicationDBObject->CommunicationGetByObjectLogID(
ObjectLogID => $Param{ObjectLogID},
);
}
if ( !$CommunicationData || !%{$CommunicationData} ) {
return $Self->_LogError($ErrorMessage);
}
if ( $CommunicationData->{Status} ne 'Processing' ) {
return $Self->_LogError(
sprintf(
"The communication '%s' is already closed, can't be used.",
$CommunicationData->{CommunicationID},
),
);
}
$Self->{CommunicationID} = $CommunicationData->{CommunicationID};
$Self->{Transport} = $CommunicationData->{Transport};
$Self->{Direction} = $CommunicationData->{Direction};
$Self->{Status} = $CommunicationData->{Status};
# Recover open objects.
my $Objects = $CommunicationDBObject->ObjectLogList(
CommunicationID => $CommunicationData->{CommunicationID},
ObjectLogStatus => 'Processing',
);
if ( !$Objects ) {
return $Self->_LogError( $ErrorMessage, );
}
for my $Object ( @{$Objects} ) {
$Self->{Current}->{ $Object->{ObjectLogType} } = $Object->{ObjectLogID};
}
return $Self;
}
=head2 _LogError()
Helper Method for logging.
=cut
sub _LogError {
my ( $Self, $Message ) = @_;
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => $Message,
);
return;
}
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L<http://otrs.org/>).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (AGPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.
=cut
1;
|