1 package SL::DB::Helper::ValidateAssembly;
 
   4 use parent qw(Exporter);
 
   5 our @EXPORT = qw(validate_assembly);
 
   7 use SL::Locale::String;
 
  11 sub validate_assembly {
 
  12   my ($new_part, $part) = @_;
 
  14   return t8("The assembly '#1' cannot be a part from itself.", $part->partnumber) if $new_part->id == $part->id;
 
  16   my @seen = ($part->id);
 
  18   return assembly_loop_exists(0, $new_part, @seen);
 
  21 sub assembly_loop_exists {
 
  22   my ($depth, $new_part, @seen) = @_;
 
  24   return t8("Too much recursions in assembly tree (>100)") if $depth > 100;
 
  26   # 1. check part is an assembly
 
  27   return unless $new_part->is_assembly;
 
  29   # 2. check assembly is still in list
 
  30   return t8("The assembly '#1' would make a loop in assembly tree.", $new_part->partnumber) if grep { $_ == $new_part->id } @seen;
 
  34   push @seen, $new_part->id;
 
  36   # 4. go into depth for each child
 
  38   foreach my $assembly ($new_part->assemblies) {
 
  39     my $retval = assembly_loop_exists($depth + 1, $assembly->part, @seen);
 
  40     return $retval if $retval;
 
  53 SL::DB::Helper::ValidateAssembly - Mixin to check loops in assemblies
 
  57 SL::DB::Helper::ValidateAssembly->validate_assembly($newpart,$assembly_part);
 
  60 =head1 HELPER FUNCTION
 
  64 =item C<validate_assembly new_part_object  part_object>
 
  66 A new part is added to an assembly. C<new_part_object> is the part which is want to added.
 
  68 First it was checked if the new part is equal the actual part.
 
  69 Then recursively all assemblies in the assemby are checked for a loop.
 
  71 The function returns an error string if a loop exists or the maximum of 100 iterations is reached
 
  78 Martin Helmling E<lt>martin.helmling@opendynamic.de>E<gt>