Changeset 1945


Ignore:
Timestamp:
02/14/17 18:32:09 (7 years ago)
Author:
nanardon
Message:

Rework the triggering for employment: ensure data are populate, improve performance

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Model/AttrForms.pm

    r1865 r1945  
    401401    } else { 
    402402        $id ||= join('', map {('a'..'z')[rand(26)]}(0..8)); 
    403         $self->base->create_object($self->otype, $id, %fields) or do { 
     403        $self->base->create_c_object($self->otype, $id, %fields) or do { 
    404404            $self->{c}->stash->{page}{error} = 
    405405                LATMOS::Accounts::Log::lastmessage(LA_ERR); 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm

    r1922 r1945  
    4545 
    4646sub _has_extended_attributes { 1 } 
    47  
    48 sub _reported_atributes { qw(contratType endcircuit hosted company employer) } 
    4947 
    5048sub _get_attr_schema { 
     
    318316    my $user = $base->get_object('user', $data{user}); 
    319317    $user or return; 
    320     $class->SUPER::_create($base, $id, %data); 
     318    my $res = $class->SUPER::_create($base, $id, %data); 
     319    $user->applyCurrentEmployment or return; 
     320    $res; 
    321321} 
    322322 
     
    332332    if ($res) { 
    333333        my $ouser = $base->get_object('user', $user); 
    334         $ouser->computeEmploymentDate; 
     334        $ouser->applyCurrentEmployment or return; 
    335335    } 
    336336 
    337337    $res 
     338} 
     339 
     340sub set_fields { 
     341    my ($self, %data) = @_; 
     342 
     343    my $res = $self->SUPER::set_fields(%data); 
     344 
     345    $self->applyToUser or return; 
     346    return $res; 
    338347} 
    339348 
     
    354363    $user->base->log(LA_DEBUG, "Applying Employement %s to user %s", $self->id, $user->id); 
    355364 
    356     $user->computeEmploymentDate; 
    357  
    358365    my $currentemployment = $user->get_attributes('currentEmployment') || ''; 
    359366 
    360367    if (!$currentemployment) { 
    361         return _resetUser($user); 
    362     } elsif ($currentemployment ne $self->id) { 
     368        return $user->_resetEmployment 
     369    } elsif ($currentemployment eq $self->id) { 
    363370        # No sync to do if this employment is not currently applied 
    364         return; 
     371        return $user->applyCurrentEmployment; 
     372    } else { 
     373        return $user->computeEmploymentDate; 
    365374    } 
    366  
    367     my %attrsets = ( 
    368         appliedEmployement => $self->id, 
    369     ); 
    370     foreach my $attr (_reported_atributes(), qw(department managerContact)) { 
    371         my $uval = $user->get_attributes($attr) || ''; 
    372         my $cval = $self->get_attributes($attr) || ''; 
    373  
    374         for ($attr) { 
    375             if ($attr eq 'managerContact') { 
    376                 if (!$cval) { 
    377                     my $dpmt  = $self->get_attributes('department') or last; 
    378                     my $odmpt = $self->base->get_object('group', $dpmt) or last; 
    379                     $cval = $odmpt->get_attributes('managedBy'); 
    380                 } 
    381             } 
    382         } 
    383  
    384         if ($uval ne $cval) { 
    385             my $oattr = $self->base->attribute('user', $attr); 
    386             $attrsets{$oattr->iname} = $cval; 
    387         } 
    388     } 
    389  
    390     if (keys %attrsets) { 
    391         if (my $res = $user->set_fields(%attrsets)) { 
    392             $user->ReportChange('Update', 'Attr %s updated to match Employment %s', join(', ', sort keys %attrsets), $self->id); 
    393             return $res; 
    394         } 
    395     } else { 
    396         return 1; 
    397     } 
    398 } 
    399  
    400 sub _resetUser { 
    401     my ($ouser) = @_; 
    402  
    403     $ouser->computeEmploymentDate; 
    404  
    405     my %changes = ( 
    406         appliedEmployement => undef, 
    407     ); 
    408  
    409     my @attributesToReset = _reported_atributes; 
    410     if (!$ouser->_get_attributes('_startEmployment')) { 
    411         push(@attributesToReset, qw(department)); 
    412     } 
    413  
    414     foreach my $attr (@attributesToReset) { 
    415         my $default = $ouser->base->config("unemployment.$attr") || ''; 
    416         my $old = $ouser->_get_attributes($attr) || ''; 
    417         if ($old ne $default) { 
    418             $changes{$attr} = $default || undef; 
    419         } 
    420     } 
    421     if ($ouser->set_fields(%changes)) { 
    422         $ouser->base->log(LA_NOTICE, "Updating user %s to match unemployment", $ouser->id); 
    423         $ouser->ReportChange('Update', 'Update %s to match unemployment', join(', ', sort keys %changes)); 
    424         return 1; 
    425     } 
    426  
    427     return 0; 
    428375} 
    429376 
     
    474421} 
    475422 
    476 sub ReportChange { 
    477     my ($self, $changetype, $message, @args) = @_; 
    478  
    479     $self->applyToUser(); 
    480  
    481     $self->SUPER::ReportChange($changetype, $message, @args); 
    482 } 
    483  
    4844231; 
    485424 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm

    r1941 r1945  
    15191519} 
    15201520 
     1521sub _reported_atributes { qw(contratType endcircuit hosted company employer) } 
     1522 
     1523=head2 applyCurrentEmployment 
     1524 
     1525Search the current employment is any and apply paramter to user 
     1526 
     1527=cut 
     1528 
     1529sub applyCurrentEmployment { 
     1530    my ($self) = @_; 
     1531 
     1532    my $currentemployment = $self->get_attributes('currentEmployment') || ''; 
     1533 
     1534    $self->base->log( 
     1535        LA_DEBUG, 
     1536        "Applying Employement %s to user %s", 
     1537        $currentemployment ? $currentemployment->id : '(none)', 
     1538        $self->id 
     1539    ); 
     1540 
     1541    if (!$currentemployment) { 
     1542        return $self->_resetEmployment; 
     1543    } 
     1544 
     1545    $self->computeEmploymentDate; 
     1546 
     1547    my %attrsets = ( 
     1548        appliedEmployement => $currentemployment->id, 
     1549    ); 
     1550    foreach my $attr (_reported_atributes(), qw(department managerContact)) { 
     1551        my $uval = $self->get_attributes($attr) || ''; 
     1552        my $cval = $currentemployment->get_attributes($attr) || ''; 
     1553 
     1554        for ($attr) { 
     1555            if ($attr eq 'managerContact') { 
     1556                if (!$cval) { 
     1557                    my $dpmt  = $currentemployment->get_attributes('department') or last; 
     1558                    my $odmpt = $currentemployment->base->get_object('group', $dpmt) or last; 
     1559                    $cval = $odmpt->get_attributes('managedBy'); 
     1560                } 
     1561            } 
     1562        } 
     1563 
     1564        if ($uval ne $cval) { 
     1565            my $oattr = $currentemployment->base->attribute('self', $attr); 
     1566            $attrsets{$oattr->iname} = $cval; 
     1567        } 
     1568    } 
     1569 
     1570    if (keys %attrsets) { 
     1571        if (my $res = $self->set_fields(%attrsets)) { 
     1572            $self->ReportChange('Update', 'Attr %s updated to match Employment %s', join(', ', sort keys %attrsets), $currentemployment->id); 
     1573            return $res; 
     1574        } 
     1575    } else { 
     1576        return 1; 
     1577    } 
     1578} 
     1579 
     1580sub _resetEmployment { 
     1581    my ($self) = @_; 
     1582 
     1583    $self->computeEmploymentDate; 
     1584 
     1585    my %changes = ( 
     1586        appliedEmployement => undef, 
     1587    ); 
     1588 
     1589    my @attributesToReset = _reported_atributes; 
     1590    if (!$self->_get_attributes('_startEmployment')) { 
     1591        push(@attributesToReset, qw(department)); 
     1592    } 
     1593 
     1594    foreach my $attr (@attributesToReset) { 
     1595        my $default = $self->base->config("unemployment.$attr") || ''; 
     1596        my $old = $self->_get_attributes($attr) || ''; 
     1597        if ($old ne $default) { 
     1598            $changes{$attr} = $default || undef; 
     1599        } 
     1600    } 
     1601    if ($self->set_fields(%changes)) { 
     1602        $self->base->log(LA_NOTICE, "Updating user %s to match unemployment", $self->id); 
     1603        $self->ReportChange('Update', 'Update %s to match unemployment', join(', ', sort keys %changes)); 
     1604        return 1; 
     1605    } 
     1606 
     1607    return 0; 
     1608} 
     1609 
     1610 
    15211611=head2 computeEmploymentDate 
    15221612 
Note: See TracChangeset for help on using the changeset viewer.