[otrs-cvs] ITSMChangeManagement/Kernel/System/ITSMChange/Number AutoIncrement.pm, NONE, 1.1

"CVS commits notifications of OTRS.org" <[email protected]> Tue, 9 Apr 2013 16:59:10 +0000
Newsgroups gmane.comp.otrs.cvs
Message-ID <[email protected]>
Comments:
Update of /home/cvs/ITSMChangeManagement/Kernel/System/ITSMChange/Number
In directory lancelot:/tmp/cvs-serv15267/Kernel/System/ITSMChange/Number

Added Files:
	AutoIncrement.pm 
Log Message:
Fixed bug# 8007 - Autoincrement number generator for change numbers.

Author: ub

--- NEW FILE: AutoIncrement.pm ---
# --
# ITSMChange/Number/AutoIncrement.pm - a change number auto increment generator
# Copyright (C) 2001-2013 OTRS AG, http://otrs.org/
# --
# $Id: AutoIncrement.pm,v 1.1 2013/04/09 16:59:05 ub Exp $
# --
# 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.
# --
# Note:
# available objects are: ConfigObject, LogObject and DBObject
#
# Generates change numbers like ID##### (e. g. 1000123)
#--

package Kernel::System::ITSMChange::Number::AutoIncrement;

use strict;
use warnings;

use vars qw($VERSION);
$VERSION = qw($Revision: 1.1 $) [1];

sub ChangeNumberCreate {
    my ( $Self, %Param ) = @_;

    # get needed config options
    my $CounterLog = $Self->{ConfigObject}->Get('ITSMChange::CounterLog');
    my $SystemID   = $Self->{ConfigObject}->Get('SystemID');
    my $MinSize
        = $Self->{ConfigObject}->Get('ITSMChange::NumberGenerator::AutoIncrement::MinCounterSize')
        || 5;

    # define number of maximum loops if created change number exists
    my $MaxRetryNumber        = 16000;
    my $LoopProtectionCounter = 0;

    # try to create a unique change number for up to $MaxRetryNumber times
    while ( $LoopProtectionCounter <= $MaxRetryNumber ) {

        # read count
        my $Count      = 0;
        my $LastModify = '';

        # try to read existing counter from file
        if ( -f $CounterLog ) {

            my $ContentSCALARRef = $Self->{MainObject}->FileRead(
                Location => $CounterLog,
            );

            if ( $ContentSCALARRef && ${$ContentSCALARRef} ) {

                ( $Count, $LastModify ) = split( /;/, ${$ContentSCALARRef} );

                # just debug
                if ( $Self->{Debug} > 0 ) {
                    $Self->{LogObject}->Log(
                        Priority => 'debug',
                        Message  => "Read counter from $CounterLog: $Count",
                    );
                }
            }
        }

        # count auto increment
        $Count++;

        my $Content = $Count;

        # write new count
        my $Write = $Self->{MainObject}->FileWrite(
            Location => $CounterLog,
            Content  => \$Content,
        );

        # log debug message
        if ( $Write && $Self->{Debug} ) {

            $Self->{LogObject}->Log(
                Priority => 'debug',
                Message  => "Write counter: $Count",
            );
        }

        # pad change number with leading '0' to length $MinSize (config option)
        $Count = sprintf "%.*u", $MinSize, $Count;

        # create new change number
        my $ChangeNumber = $SystemID . $Count;

        # lookup if change number exists already
        my $ChangeID = $Self->ChangeLookup(
            ChangeNumber => $ChangeNumber,
        );

        # now we have a new unused change number and return it
        return $ChangeNumber if !$ChangeID;

        # start loop protection mode
        $LoopProtectionCounter++;

        # create new change number again
        $Self->{LogObject}->Log(
            Priority => 'notice',
            Message  => "ChangeNumber ($ChangeNumber) exists! Creating a new one.",
        );
    }

    # loop was running too long
    $Self->{LogObject}->Log(
        Priority => 'error',
        Message  => "LoopProtectionCounter is now $LoopProtectionCounter!"
            . " Stopped ChangeNumberCreate()!",
    );

    return;
}

1;

=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>.

=head1 VERSION

$Revision: 1.1 $ $Date: 2013/04/09 16:59:05 $

=cut
---------------------------------------------------------------------
OTRS mailing list: cvs-log - Webpage: http://otrs.org/
Archive: http://lists.otrs.org/pipermail/cvs-log
To unsubscribe: http://lists.otrs.org/cgi-bin/listinfo/cvs-log