"Fossies" - the Fresh Open Source Software Archive

Member "otrs-5.0.40/Kernel/System/ProcessManagement/DB/Activity.pm" (12 Dec 2019, 18380 Bytes) of package /linux/www/otrs-5.0.40.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Activity.pm" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: rel-6_0_25_vs_rel-6_0_26.

    1 # --
    2 # Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
    3 # --
    4 # This software comes with ABSOLUTELY NO WARRANTY. For details, see
    5 # the enclosed file COPYING for license information (GPL). If you
    6 # did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
    7 # --
    8 
    9 package Kernel::System::ProcessManagement::DB::Activity;
   10 
   11 use strict;
   12 use warnings;
   13 
   14 use Kernel::System::VariableCheck qw(:all);
   15 
   16 our @ObjectDependencies = (
   17     'Kernel::Config',
   18     'Kernel::System::Cache',
   19     'Kernel::System::DB',
   20     'Kernel::System::Log',
   21     'Kernel::System::ProcessManagement::DB::ActivityDialog',
   22     'Kernel::System::YAML',
   23 );
   24 
   25 =head1 NAME
   26 
   27 Kernel::System::ProcessManagement::DB::Activity
   28 
   29 =head1 SYNOPSIS
   30 
   31 Process Management DB Activity backend
   32 
   33 =head1 PUBLIC INTERFACE
   34 
   35 =over 4
   36 
   37 =cut
   38 
   39 =item new()
   40 
   41 create an object. Do not use it directly, instead use:
   42 
   43     use Kernel::System::ObjectManager;
   44     local $Kernel::OM = Kernel::System::ObjectManager->new();
   45     my $ActivityObject = $Kernel::OM->Get('Kernel::System::ProcessManagement::DB::Activity');
   46 
   47 =cut
   48 
   49 sub new {
   50     my ( $Type, %Param ) = @_;
   51 
   52     # allocate new hash for object
   53     my $Self = {};
   54     bless( $Self, $Type );
   55 
   56     # get the cache TTL (in seconds)
   57     $Self->{CacheTTL} = int( $Kernel::OM->Get('Kernel::Config')->Get('Process::CacheTTL') || 3600 );
   58 
   59     # set lower if database is case sensitive
   60     $Self->{Lower} = '';
   61     if ( $Kernel::OM->Get('Kernel::System::DB')->GetDatabaseFunction('CaseSensitive') ) {
   62         $Self->{Lower} = 'LOWER';
   63     }
   64 
   65     return $Self;
   66 }
   67 
   68 =item ActivityAdd()
   69 
   70 add new Activity
   71 
   72 returns the id of the created activity if success or undef otherwise
   73 
   74     my $ID = $ActivityObject->ActivityAdd(
   75         EntityID    => 'A1'              # mandatory, exportable unique identifier
   76         Name        => 'NameOfActivity', # mandatory
   77         Config      => $ConfigHashRef,   # mandatory, activity configuration to be stored in YAML
   78                                          #   format
   79         UserID      => 123,              # mandatory
   80     );
   81 
   82 Returns:
   83 
   84     $ID = 567;
   85 
   86 =cut
   87 
   88 sub ActivityAdd {
   89     my ( $Self, %Param ) = @_;
   90 
   91     # check needed stuff
   92     for my $Key (qw(EntityID Name Config UserID)) {
   93         if ( !$Param{$Key} ) {
   94             $Kernel::OM->Get('Kernel::System::Log')->Log(
   95                 Priority => 'error',
   96                 Message  => "Need $Key!",
   97             );
   98             return;
   99         }
  100     }
  101 
  102     # get database object
  103     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  104 
  105     # check if EntityID already exists
  106     return if !$DBObject->Prepare(
  107         SQL => "
  108             SELECT id
  109             FROM pm_activity
  110             WHERE $Self->{Lower}(entity_id) = $Self->{Lower}(?)",
  111         Bind  => [ \$Param{EntityID} ],
  112         Limit => 1,
  113     );
  114 
  115     my $EntityExists;
  116     while ( my @Data = $DBObject->FetchrowArray() ) {
  117         $EntityExists = 1;
  118     }
  119 
  120     if ($EntityExists) {
  121         $Kernel::OM->Get('Kernel::System::Log')->Log(
  122             Priority => 'error',
  123             Message  => "The EntityID:$Param{EntityID} already exists for an activity!"
  124         );
  125         return;
  126     }
  127 
  128     # check config valid format
  129     if ( ref $Param{Config} ne 'HASH' ) {
  130         $Kernel::OM->Get('Kernel::System::Log')->Log(
  131             Priority => 'error',
  132             Message  => "Config needs to be a valid Hash reference!",
  133         );
  134         return;
  135     }
  136 
  137     # dump config as string
  138     my $Config = $Kernel::OM->Get('Kernel::System::YAML')->Dump( Data => $Param{Config} );
  139 
  140     # Make sure the resulting string has the UTF-8 flag. YAML only sets it if
  141     #   part of the data already had it.
  142     utf8::upgrade($Config);
  143 
  144     # sql
  145     return if !$DBObject->Do(
  146         SQL => '
  147             INSERT INTO pm_activity (entity_id, name, config, create_time, create_by, change_time,
  148                 change_by)
  149             VALUES (?, ?, ?, current_timestamp, ?, current_timestamp, ?)',
  150         Bind => [
  151             \$Param{EntityID}, \$Param{Name}, \$Config, \$Param{UserID}, \$Param{UserID},
  152         ],
  153     );
  154 
  155     return if !$DBObject->Prepare(
  156         SQL  => 'SELECT id FROM pm_activity WHERE entity_id = ?',
  157         Bind => [ \$Param{EntityID} ],
  158     );
  159 
  160     my $ID;
  161     while ( my @Row = $DBObject->FetchrowArray() ) {
  162         $ID = $Row[0];
  163     }
  164 
  165     # delete cache
  166     $Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
  167         Type => 'ProcessManagement_Activity',
  168     );
  169 
  170     return if !$ID;
  171 
  172     return $ID;
  173 }
  174 
  175 =item ActivityDelete()
  176 
  177 delete an Activity
  178 
  179 returns 1 if success or undef otherwise
  180 
  181     my $Success = $ActivityObject->ActivityDelete(
  182         ID      => 123,
  183         UserID  => 123,
  184     );
  185 
  186 =cut
  187 
  188 sub ActivityDelete {
  189     my ( $Self, %Param ) = @_;
  190 
  191     # check needed stuff
  192     for my $Key (qw(ID UserID)) {
  193         if ( !$Param{$Key} ) {
  194             $Kernel::OM->Get('Kernel::System::Log')->Log(
  195                 Priority => 'error',
  196                 Message  => "Need $Key!"
  197             );
  198             return;
  199         }
  200     }
  201 
  202     # check if exists
  203     my $Activity = $Self->ActivityGet(
  204         ID     => $Param{ID},
  205         UserID => 1,
  206     );
  207     return if !IsHashRefWithData($Activity);
  208 
  209     # delete activity
  210     return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
  211         SQL  => 'DELETE FROM pm_activity WHERE id = ?',
  212         Bind => [ \$Param{ID} ],
  213     );
  214 
  215     # delete cache
  216     $Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
  217         Type => 'ProcessManagement_Activity',
  218     );
  219 
  220     return 1;
  221 }
  222 
  223 =item ActivityGet()
  224 
  225 get Activity attributes
  226 
  227     my $Activity = $ActivityObject->ActivityGet(
  228         ID                  => 123,      # ID or EntityID is needed
  229         EntityID            => 'A1',
  230         ActivityDialogNames => 1,        # default 0, 1 || 0, if 0 returns an ActivityDialogs array
  231                                          #     with the activity dialog entity IDs, if 1 returns an
  232                                          #     ActivitiDialogs hash with the activity entity IDs as
  233                                          #     keys and ActivityDialog Names as values
  234         UserID        => 123,            # mandatory
  235     );
  236 
  237 Returns:
  238 
  239     $Activity = {
  240         ID             => 123,
  241         EntityID       => 'A1',
  242         Name           => 'some name',
  243         Config         => $ConfigHashRef,
  244         ActiviyDialogs => ['AD1','AD2','AD3'],
  245         CreateTime     => '2012-07-04 15:08:00',
  246         ChangeTime     => '2012-07-04 15:08:00',
  247     };
  248 
  249     $Activity = {
  250         ID           => 123,
  251         EntityID     => 'P1',
  252         Name         => 'some name',
  253         Config       => $ConfigHashRef,
  254         ActivityDialogs => {
  255             'AD1' => 'ActivityDialog1',
  256             'AD2' => 'ActivityDialog2',
  257             'AD3' => 'ActivityDialog3',
  258         };
  259         CreateTime   => '2012-07-04 15:08:00',
  260         ChangeTime   => '2012-07-04 15:08:00',
  261     };
  262 
  263 =cut
  264 
  265 sub ActivityGet {
  266     my ( $Self, %Param ) = @_;
  267 
  268     # check needed stuff
  269     if ( !$Param{ID} && !$Param{EntityID} ) {
  270         $Kernel::OM->Get('Kernel::System::Log')->Log(
  271             Priority => 'error',
  272             Message  => 'Need ID or EntityID!'
  273         );
  274         return;
  275     }
  276 
  277     if ( !$Param{UserID} ) {
  278         $Kernel::OM->Get('Kernel::System::Log')->Log(
  279             Priority => 'error',
  280             Message  => 'Need UserID!',
  281         );
  282         return;
  283     }
  284 
  285     my $ActivityDialogNames = 0;
  286     if ( defined $Param{ActivityDialogNames} && $Param{ActivityDialogNames} == 1 ) {
  287         $ActivityDialogNames = 1;
  288     }
  289 
  290     # check cache
  291     my $CacheKey;
  292     if ( $Param{ID} ) {
  293         $CacheKey = 'ActivityGet::ID::' . $Param{ID} . '::ActivityDialogNames::'
  294             . $ActivityDialogNames;
  295     }
  296     else {
  297         $CacheKey = 'ActivityGet::EntityID::' . $Param{EntityID} . '::ActivityDialogNames::'
  298             . $ActivityDialogNames;
  299     }
  300 
  301     # get cache object
  302     my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
  303 
  304     my $Cache = $CacheObject->Get(
  305         Type => 'ProcessManagement_Activity',
  306         Key  => $CacheKey,
  307     );
  308     return $Cache if $Cache;
  309 
  310     # get database object
  311     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  312 
  313     # sql
  314     if ( $Param{ID} ) {
  315         return if !$DBObject->Prepare(
  316             SQL => '
  317                 SELECT id, entity_id, name, config, create_time, change_time
  318                 FROM pm_activity
  319                 WHERE id = ?',
  320             Bind  => [ \$Param{ID} ],
  321             Limit => 1,
  322         );
  323     }
  324     else {
  325         return if !$DBObject->Prepare(
  326             SQL => '
  327                 SELECT id, entity_id, name, config, create_time, change_time
  328                 FROM pm_activity
  329                 WHERE entity_id = ?',
  330             Bind  => [ \$Param{EntityID} ],
  331             Limit => 1,
  332         );
  333     }
  334 
  335     # get yaml object
  336     my $YAMLObject = $Kernel::OM->Get('Kernel::System::YAML');
  337 
  338     my %Data;
  339     while ( my @Data = $DBObject->FetchrowArray() ) {
  340 
  341         my $Config = $YAMLObject->Load( Data => $Data[3] );
  342 
  343         %Data = (
  344             ID         => $Data[0],
  345             EntityID   => $Data[1],
  346             Name       => $Data[2],
  347             Config     => $Config,
  348             CreateTime => $Data[4],
  349             ChangeTime => $Data[5],
  350         );
  351     }
  352 
  353     return if !$Data{ID};
  354 
  355     # create the ActivityDialogsList
  356     if ($ActivityDialogNames) {
  357 
  358         my %ActivityDialogs;
  359         if ( IsHashRefWithData( $Data{Config}->{ActivityDialog} ) ) {
  360 
  361             # get activity dialog object
  362             my $ActivityDialogObject = $Kernel::OM->Get('Kernel::System::ProcessManagement::DB::ActivityDialog');
  363 
  364             my $ActivityDialogList = $ActivityDialogObject->ActivityDialogList(
  365                 UseEntities => 1,
  366                 UserID      => 1,
  367             );
  368 
  369             for my $ActivityOrder ( sort { $a <=> $b } keys %{ $Data{Config}->{ActivityDialog} } ) {
  370                 $ActivityDialogs{ $Data{Config}->{ActivityDialog}->{$ActivityOrder} }
  371                     = $ActivityDialogList->{ $Data{Config}->{ActivityDialog}->{$ActivityOrder} };
  372             }
  373         }
  374         $Data{ActivityDialogs} = \%ActivityDialogs;
  375     }
  376     else {
  377         my @ActivityDialogList;
  378 
  379         if ( IsHashRefWithData( $Data{Config}->{ActivityDialog} ) ) {
  380             @ActivityDialogList = map { $Data{Config}->{ActivityDialog}->{$_} }
  381                 sort { $a <=> $b } keys %{ $Data{Config}->{ActivityDialog} };
  382         }
  383         $Data{ActivityDialogs} = \@ActivityDialogList;
  384     }
  385 
  386     # set cache
  387     $CacheObject->Set(
  388         Type  => 'ProcessManagement_Activity',
  389         Key   => $CacheKey,
  390         Value => \%Data,
  391         TTL   => $Self->{CacheTTL},
  392     );
  393 
  394     return \%Data;
  395 }
  396 
  397 =item ActivityUpdate()
  398 
  399 update Activity attributes
  400 
  401 returns 1 if success or undef otherwise
  402 
  403     my $Success = $ActivityObject->ActivityUpdate(
  404         ID          => 123,             # mandatory
  405         EntityID    => 'A1'             # mandatory, exportable unique identifier
  406         Name        => 'NameOfProcess', # mandatory
  407         Config      => $ConfigHashRef,  # mandatory, process configuration to be stored in YAML
  408                                         #   format
  409         UserID      => 123,             # mandatory
  410     );
  411 
  412 =cut
  413 
  414 sub ActivityUpdate {
  415     my ( $Self, %Param ) = @_;
  416 
  417     # check needed stuff
  418     for my $Key (qw(ID EntityID Name Config UserID)) {
  419         if ( !$Param{$Key} ) {
  420             $Kernel::OM->Get('Kernel::System::Log')->Log(
  421                 Priority => 'error',
  422                 Message  => "Need $Key!"
  423             );
  424             return;
  425         }
  426     }
  427 
  428     # get database object
  429     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  430 
  431     # check if EntityID already exists
  432     return if !$DBObject->Prepare(
  433         SQL => "
  434             SELECT id FROM pm_activity
  435             WHERE $Self->{Lower}(entity_id) = $Self->{Lower}(?)
  436             AND id != ?",
  437         Bind  => [ \$Param{EntityID}, \$Param{ID} ],
  438         LIMIT => 1,
  439     );
  440 
  441     my $EntityExists;
  442     while ( my @Data = $DBObject->FetchrowArray() ) {
  443         $EntityExists = 1;
  444     }
  445 
  446     if ($EntityExists) {
  447         $Kernel::OM->Get('Kernel::System::Log')->Log(
  448             Priority => 'error',
  449             Message  => "The EntityID:$Param{Name} already exists for a activity!",
  450         );
  451         return;
  452     }
  453 
  454     # check config valid format
  455     if ( ref $Param{Config} ne 'HASH' ) {
  456         $Kernel::OM->Get('Kernel::System::Log')->Log(
  457             Priority => 'error',
  458             Message  => "Config needs to be a valid Hash reference!",
  459         );
  460         return;
  461     }
  462 
  463     # dump config as string
  464     my $Config = $Kernel::OM->Get('Kernel::System::YAML')->Dump( Data => $Param{Config} );
  465 
  466     # Make sure the resulting string has the UTF-8 flag. YAML only sets it if
  467     #   part of the data already had it.
  468     utf8::upgrade($Config);
  469 
  470     # check if need to update db
  471     return if !$DBObject->Prepare(
  472         SQL => '
  473             SELECT entity_id, name, config
  474             FROM pm_activity
  475             WHERE id = ?',
  476         Bind  => [ \$Param{ID} ],
  477         Limit => 1,
  478     );
  479 
  480     my $CurrentEntityID;
  481     my $CurrentName;
  482     my $CurrentConfig;
  483     while ( my @Data = $DBObject->FetchrowArray() ) {
  484         $CurrentEntityID = $Data[0];
  485         $CurrentName     = $Data[1];
  486         $CurrentConfig   = $Data[2];
  487     }
  488 
  489     if ($CurrentEntityID) {
  490 
  491         return 1 if $CurrentEntityID eq $Param{EntityID}
  492             && $CurrentName eq $Param{Name}
  493             && $CurrentConfig eq $Config;
  494     }
  495 
  496     # sql
  497     return if !$DBObject->Do(
  498         SQL => '
  499             UPDATE pm_activity
  500             SET entity_id = ?, name = ?,  config = ?, change_time = current_timestamp, change_by = ?
  501             WHERE id = ?',
  502         Bind => [
  503             \$Param{EntityID}, \$Param{Name}, \$Config, \$Param{UserID},
  504             \$Param{ID},
  505         ],
  506     );
  507 
  508     # delete cache
  509     $Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
  510         Type => 'ProcessManagement_Activity',
  511     );
  512 
  513     return 1;
  514 }
  515 
  516 =item ActivityList()
  517 
  518 get an Activity list
  519 
  520     my $List = $ActivityObject->ActivityList(
  521         UseEntities => 0,                       # default 0, 1 || 0. if 0 the return hash keys are
  522                                                 #    the activity IDs otherwise keys are the
  523                                                 #    activity entity IDs
  524         UserID      => 1,
  525     );
  526 
  527     Returns:
  528 
  529     $List = {
  530         1 => 'Activity1',
  531     }
  532 
  533     or
  534 
  535     $List = {
  536         'A1' => 'Activity1',
  537     }
  538 
  539 =cut
  540 
  541 sub ActivityList {
  542     my ( $Self, %Param ) = @_;
  543 
  544     # check needed
  545     if ( !$Param{UserID} ) {
  546         $Kernel::OM->Get('Kernel::System::Log')->Log(
  547             Priority => 'error',
  548             Message  => "Need UserID!"
  549         );
  550         return;
  551     }
  552 
  553     # get cache object
  554     my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
  555 
  556     # check cache
  557     my $UseEntities = 0;
  558     if ( defined $Param{UseEntities} && $Param{UseEntities} ) {
  559         $UseEntities = 1;
  560     }
  561     my $CacheKey = 'ActivityList::UseEntities::' . $UseEntities;
  562     my $Cache    = $CacheObject->Get(
  563         Type => 'ProcessManagement_Activity',
  564         Key  => $CacheKey,
  565     );
  566     return $Cache if ref $Cache;
  567 
  568     # get database object
  569     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  570 
  571     return if !$DBObject->Prepare(
  572         SQL => '
  573             SELECT id, entity_id, name
  574             FROM pm_activity',
  575     );
  576 
  577     my %Data;
  578     while ( my @Row = $DBObject->FetchrowArray() ) {
  579         if ( !$UseEntities ) {
  580             $Data{ $Row[0] } = $Row[2];
  581         }
  582         else {
  583             $Data{ $Row[1] } = $Row[2];
  584         }
  585     }
  586 
  587     # set cache
  588     $CacheObject->Set(
  589         Type  => 'ProcessManagement_Activity',
  590         Key   => $CacheKey,
  591         Value => \%Data,
  592         TTL   => $Self->{CacheTTL},
  593     );
  594 
  595     return \%Data;
  596 }
  597 
  598 =item ActivityListGet()
  599 
  600 get an Activity list with all activity details
  601 
  602     my $List = $ActivityObject->ActivityListGet(
  603         UserID      => 1,
  604     );
  605 
  606 Returns:
  607 
  608     $List = [
  609         {
  610             ID             => 123,
  611             EntityID       => 'A1',
  612             Name           => 'some name',
  613             Config         => $ConfigHashRef,
  614             ActiviyDialogs => ['AD1','AD2','AD3'],
  615             CreateTime     => '2012-07-04 15:08:00',
  616             ChangeTime     => '2012-07-04 15:08:00',
  617         }
  618         {
  619             ID             => 456,
  620             EntityID       => 'A2',
  621             Name           => 'some name',
  622             Config         => $ConfigHashRef,
  623             ActiviyDialogs => ['AD3','AD4','AD5'],
  624             CreateTime     => '2012-07-04 15:09:00',
  625             ChangeTime     => '2012-07-04 15:09:00',
  626         }
  627     ];
  628 
  629 =cut
  630 
  631 sub ActivityListGet {
  632     my ( $Self, %Param ) = @_;
  633 
  634     # check needed stuff
  635     if ( !$Param{UserID} ) {
  636         $Kernel::OM->Get('Kernel::System::Log')->Log(
  637             Priority => 'error',
  638             Message  => 'Need UserID!',
  639         );
  640         return;
  641     }
  642 
  643     # get cache object
  644     my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
  645 
  646     # check cache
  647     my $CacheKey = 'ActivityListGet';
  648 
  649     my $Cache = $CacheObject->Get(
  650         Type => 'ProcessManagement_Activity',
  651         Key  => $CacheKey,
  652     );
  653     return $Cache if $Cache;
  654 
  655     # get database object
  656     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  657 
  658     # sql
  659     return if !$DBObject->Prepare(
  660         SQL => '
  661             SELECT id, entity_id
  662             FROM pm_activity
  663             ORDER BY id',
  664     );
  665 
  666     my @ActivityIDs;
  667     while ( my @Row = $DBObject->FetchrowArray() ) {
  668         push @ActivityIDs, $Row[0];
  669     }
  670 
  671     my @Data;
  672     for my $ItemID (@ActivityIDs) {
  673 
  674         my $ActivityData = $Self->ActivityGet(
  675             ID     => $ItemID,
  676             UserID => 1,
  677         );
  678         push @Data, $ActivityData;
  679     }
  680 
  681     # set cache
  682     $CacheObject->Set(
  683         Type  => 'ProcessManagement_Activity',
  684         Key   => $CacheKey,
  685         Value => \@Data,
  686         TTL   => $Self->{CacheTTL},
  687     );
  688 
  689     return \@Data;
  690 }
  691 
  692 1;
  693 
  694 =back
  695 
  696 =head1 TERMS AND CONDITIONS
  697 
  698 This software is part of the OTRS project (L<https://otrs.org/>).
  699 
  700 This software comes with ABSOLUTELY NO WARRANTY. For details, see
  701 the enclosed file COPYING for license information (GPL). If you
  702 did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
  703 
  704 =cut