[otrs-cvs] ITSMConfigurationManagement/Kernel/System ITSMConfigItem.pm, 1.37, 1.38

"CVS commits notifications of OTRS.org" <[email protected]>
Newsgroups gmane.comp.otrs.cvs
Message-ID <[email protected]>
Comments:
Update of /home/cvs/ITSMConfigurationManagement/Kernel/System
In directory lancelot:/tmp/cvs-serv32293/Kernel/System

Modified Files:
	ITSMConfigItem.pm 
Log Message:
Added new optional sysconfig option to check if config item names are unique.

Author: ub

Index: ITSMConfigItem.pm
===================================================================
RCS file: /home/cvs/ITSMConfigurationManagement/Kernel/System/ITSMConfigItem.pm,v
retrieving revision 1.37
retrieving revision 1.38
diff -2 -u -d -r1.37 -r1.38
--- ITSMConfigItem.pm	30 Nov 2012 17:10:41 -0000	1.37
+++ ITSMConfigItem.pm	30 Nov 2012 19:49:09 -0000	1.38
@@ -29,4 +29,5 @@
 use Kernel::System::XML;
 use Kernel::System::VirtualFS;
+use Kernel::System::VariableCheck qw(:all);
 
 use vars qw(@ISA $VERSION);
@@ -1537,4 +1538,159 @@
 }
 
+=item UniqueNameCheck()
+
+This method checks all already existing config items, whether the given name does already exist
+within the same config item class or amongst all classes, depending on the SysConfig value of
+UniqueCIName::UniquenessCheckScope (Class or Global).
+
+This method requires 3 parameters: ConfigItemID, Name and Class
+"ConfigItemID"  is the ID of the ConfigItem, which is to be checked for uniqueness
+"Name"          is the config item name to be checked for uniqueness
+"ClassID"       is the ID of the config item's class
+
+All parameters are mandatory.
+
+my $DuplicateNames = $ConfigItemObject->UniqueNameCheck(
+    ConfigItemID => '73'
+    Name         => 'PC#005',
+    ClassID      => '32',
+);
+
+The given name is not unique
+my $NameDuplicates = [ 5, 35, 48, ];    # IDs of ConfigItems with the same name
+
+The given name is unique
+my $NameDuplicates = [];
+
+=cut
+
+sub UniqueNameCheck {
+    my ( $Self, %Param ) = @_;
+
+    # check for needed stuff
+    for my $Needed (qw(ConfigItemID Name ClassID)) {
+        if ( !$Param{$Needed} ) {
+            $Self->{LogObject}->Log(
+                Priority => 'error',
+                Message  => "Missing parameter $Needed!",
+            );
+            return;
+        }
+    }
+
+    # check ConfigItemID param for valid format
+    if (
+        !IsInteger( $Param{ConfigItemID} )
+        && ( IsStringWithData( $Param{ConfigItemID} ) && $Param{ConfigItemID} ne 'NEW' )
+        )
+    {
+        $Self->{LogObject}->Log(
+            Priority => 'error',
+            Message  => "The ConfigItemID parameter needs to be an integer or 'NEW'",
+        );
+        return;
+    }
+
+    # check Name param for valid format
+    if ( !IsStringWithData( $Param{Name} ) ) {
+        $Self->{LogObject}->Log(
+            Priority => 'error',
+            Message  => "The Name parameter needs to be a string!",
+        );
+        return;
+    }
+
+    # check ClassID param for valid format
+    if ( !IsInteger( $Param{ClassID} ) ) {
+        $Self->{LogObject}->Log(
+            Priority => 'error',
+            Message  => "The ClassID parameter needs to be an integer",
+        );
+        return;
+    }
+
+    # get class list
+    my $ClassList = $Self->{GeneralCatalogObject}->ItemList(
+        Class => 'ITSM::ConfigItem::Class',
+    );
+
+    # check class list for validity
+    if ( !IsHashRefWithData($ClassList) ) {
+        $Self->{LogObject}->Log(
+            Priority => 'error',
+            Message  => "Unable to retrieve a valid class list!",
+        );
+        return;
+    }
+
+    # get the class name from the class list
+    my $Class = $ClassList->{ $Param{ClassID} };
+
+    # check class for validity
+    if ( !IsStringWithData($Class) ) {
+        $Self->{LogObject}->Log(
+            Priority => 'error',
+            Message  => "Unable to determine a config item class using the given ClassID!",
+        );
+        return;
+    }
+    elsif ( $Self->{ConfigObject}->{Debug} > 0 ) {
+        $Self->{LogObject}->Log(
+            Priority => 'debug',
+            Message  => "Resolved ClassID $Param{ClassID} to class $Class",
+        );
+    }
+
+    # get the uniqueness scope from SysConfig
+    my $Scope = $Self->{ConfigObject}->Get('UniqueCIName::UniquenessCheckScope');
+
+    # check scope for validity
+    if ( !IsStringWithData($Scope) ) {
+        $Self->{LogObject}->Log(
+            Priority => 'error',
+            Message  => "The configuration of UniqueCIName::UniquenessCheckScope is invalid!",
+        );
+        return;
+    }
+
+    if ( $Scope ne 'global' && $Scope ne 'class' ) {
+        $Self->{LogObject}->Log(
+            Priority => 'error',
+            Message  => "UniqueCIName::UniquenessCheckScope is $Scope, but must be either "
+                . "'global' or 'class'!",
+        );
+        return;
+    }
+
+    if ( $Self->{ConfigObject}->{Debug} > 0 ) {
+        $Self->{LogObject}->Log(
+            Priority => 'debug',
+            Message  => "The scope for checking the uniqueness is $Scope",
+        );
+    }
+
+    my %SearchCriteria;
+
+    # add the config item class to the search criteria if the uniqueness scope is not global
+    if ( $Scope ne 'global' ) {
+        $SearchCriteria{ClassIDs} = [ $Param{ClassID} ];
+    }
+
+    $SearchCriteria{Name} = $Param{Name};
+
+    # search for a config item matching the given name
+    my $ConfigItem = $Self->ConfigItemSearchExtended(%SearchCriteria);
+
+    # remove the provided ConfigItemID from the results, otherwise the duplicate check would fail
+    # because the ConfigItem itself is found as duplicate
+    my @Duplicates = map {$_} grep { $_ ne $Param{ConfigItemID} } @{$ConfigItem};
+
+    # if a config item was found, the given name is not unique
+    # if no config item was found, the given name is unique
+
+    # return the result of the config item search for duplicates
+    return \@Duplicates;
+}
+
 =begin Internal:
 
---------------------------------------------------------------------
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.