Ignore:
Timestamp:
12/21/16 15:07:28 (8 years ago)
Author:
nanardon
Message:

Merge branch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm

    r1843 r1865  
    147147} 
    148148 
     149=head2 ListInternalObjects($otype) 
     150 
     151List objects flags as internal for type C<$otype> 
     152 
     153=cut 
     154 
     155sub ListInternalObjects { 
     156    my ($self, $otype) = @_; 
     157 
     158    my $pclass = $self->_load_obj_class($otype) or return; 
     159 
     160    # Object Alias: checking if object is alias, then returning it: 
     161    my $sth = $self->db->prepare_cached( 
     162        sprintf(q{select %s as k from %s where and internobject = true}, 
     163            $self->db->quote_identifier($pclass->_key_field), 
     164            $self->db->quote_identifier($pclass->_object_table), 
     165        ), 
     166    ); 
     167    $sth->execute(); 
     168    my @list; 
     169    while (my $res = $sth->fetchrow_hashref) { 
     170       push(@list, $_->{k}); 
     171   } 
     172   return(@list); 
     173} 
     174 
     175=head2 GetRawObject($otype, $id) 
     176 
     177Return an object even it is internal, alias are not follow and even 
     178unexported object are returned 
     179 
     180This function must be used only for maintenance operation. 
     181 
     182=cut 
     183 
     184sub GetRawObject { 
     185    my ($self, $otype, $id) = @_; 
     186 
     187    my $pclass = $self->_load_obj_class($otype) or return; 
     188 
     189    return $self->SUPER::get_object($otype, $id); 
     190} 
     191 
     192sub _derefObject { 
     193    my ($self, $otype, $oalias) = @_; 
     194    if (my ($aliasotype, $aliasoname, $aliasattr) = $oalias =~ m/^([^\/]+)\.([^\.]+)\.(.*)$/) { 
     195        my $attribute = $self->attribute($aliasotype, $aliasattr) or do { 
     196            $self->log(LA_DEBUG, "Oalias %s (%s): can fetch attibute %s/%s", 
     197                $otype, $oalias, $aliasotype, $aliasattr); 
     198            return; 
     199        }; 
     200        my $refotype = $attribute->reference or do { 
     201            $self->log(LA_DEBUG, "Oalias %s (%s): Attribute does not reference an object", 
     202                $otype, $oalias); 
     203            return; 
     204        }; 
     205        my $robj = $self->get_object($aliasotype, $aliasoname) or do { 
     206            $self->log(LA_DEBUG, "Oalias %s (%s): can fetch object %s/%s", 
     207                $otype, $oalias, $aliasotype, $aliasoname); 
     208            return; 
     209        }; 
     210        my $rvalue = $robj->get_attributes($aliasattr) or do { 
     211            $self->log(LA_DEBUG, "Oalias %s (%s): attribute value is empty", 
     212                $otype, $oalias); 
     213            return; 
     214        }; 
     215        return $self->get_object($refotype, $rvalue); 
     216    } else { 
     217        return $self->get_object($otype, $oalias); 
     218    } 
     219} 
     220 
     221sub get_object { 
     222    my ($self, $otype, $id) = @_; 
     223 
     224    my $pclass = $self->_load_obj_class($otype) or return; 
     225 
     226    # Object Alias: checking if object is alias, then returning it: 
     227    my $sth = $self->db->prepare_cached( 
     228        sprintf(q{select oalias from %s where %s = ? and internobject = false %s}, 
     229            $self->db->quote_identifier($pclass->_object_table), 
     230            $self->db->quote_identifier($pclass->_key_field), 
     231            ($self->{wexported} ? '' : 'and exported = true'), 
     232        ), 
     233    ); 
     234    $sth->execute($id); 
     235    my $res = $sth->fetchrow_hashref; 
     236    $sth->finish; 
     237    if (my $oalias = $res->{oalias}) { 
     238        # Cross reference over object/attribute 
     239        $self->_derefObject($otype, $oalias); 
     240    } else { 
     241        return $self->SUPER::get_object($otype, $id); 
     242    } 
     243} 
     244 
    149245=head2 getObjectFromOKey ($okey) 
    150246 
     
    255351    my ($self, @otype) = @_; 
    256352    $self->SUPER::list_supported_objects(qw(site), @otype); 
     353} 
     354 
     355# For SQL listRealObjects != list_objects 
     356sub listRealObjects { 
     357    my ($self, $otype) = @_; 
     358    my $pclass = $self->_load_obj_class($otype) or return; 
     359    $pclass->listReal($self); 
    257360} 
    258361 
     
    287390 
    288391=head1 SPECIFICS FUNCTIONS 
     392 
     393=head2 GetAlias($base, $id) 
     394 
     395Return object having id C<$id> only if it is an object alias 
     396 
     397=cut 
     398 
     399sub GetAlias { 
     400    my ($self, $otype, $id) = @_; 
     401 
     402    my $pclass = $self->_load_obj_class($otype) or return; 
     403 
     404    # Object Alias: checking if object is alias, then returning it: 
     405    my $sth = $self->db->prepare_cached( 
     406        sprintf(q{select oalias from %s where %s = ? and oalias IS NOT NULL and internobject = false %s}, 
     407            $self->db->quote_identifier($pclass->_object_table), 
     408            $self->db->quote_identifier($pclass->_key_field), 
     409            ($self->{wexported} ? '' : 'and exported = true'), 
     410        ), 
     411    ); 
     412    $sth->execute($id); 
     413    my $res = $sth->fetchrow_hashref; 
     414    $sth->finish; 
     415    if ($res) { 
     416        return $self->SUPER::get_object($otype, $id); 
     417    } else { 
     418        return; 
     419    } 
     420} 
     421 
     422=head2 CreateAlias($otype, $name, $for) 
     423 
     424Create an object alias named C<$name> for ovbject C<$for> 
     425 
     426=cut 
     427 
     428sub CreateAlias { 
     429    my ($self, $otype, $name, $for) = @_; 
     430 
     431    my $pclass = $self->_load_obj_class($otype) or return; 
     432 
     433    $for or die "Cannot create alias without giving object to point"; 
     434 
     435    my $res = $pclass->CreateAlias($self, $name, $for); 
     436 
     437    if ($res) { 
     438        $self->ReportChange( 
     439            $otype, 
     440            $name, 
     441            $pclass->_get_ikey($self, $name), 
     442            'Create', "Alias %s %s => %s", $otype, $name, $for 
     443        ); 
     444        $self->log(LA_DEBUG, "Alias $otype $name => $for created"); 
     445        return 1; 
     446    } else { 
     447        $self->log(LA_ERR, "Error when creating alias $otype $name"); 
     448        return; 
     449    } 
     450} 
     451 
     452=head2 RemoveAlias($otype, $name, $for) 
     453 
     454Create an object alias named C<$name> for ovbject C<$for> 
     455 
     456=cut 
     457 
     458sub RemoveAlias { 
     459    my ($self, $otype, $name) = @_; 
     460 
     461    my $pclass = $self->_load_obj_class($otype) or return; 
     462 
     463    my $obj = $self->GetAlias($otype, $name) or do { 
     464        $self->log('Cannot get alias %s/%s for removal', $otype, $name); 
     465        return; 
     466    }; 
     467 
     468    if ($obj->_get_attributes('internobject')) { 
     469        # Cannot happend: internal are not fetchable 
     470        $self->log(LA_ERR,'Cannot delete %s/%s: is an internal object', $pclass->type, $name); 
     471        return; 
     472    } 
     473    if ($obj->_get_attributes('nodelete')) { 
     474        $self->log(LA_ERR,'Cannot delete %s/%s: is write protected', $pclass->type, $name); 
     475        return; 
     476    } 
     477 
     478    my $id = $obj->Iid; 
     479 
     480    my $sth = $self->db->prepare_cached(sprintf( 
     481        'DELETE FROM %s WHERE %s = ?', 
     482        $self->db->quote_identifier($pclass->_object_table), 
     483        $self->db->quote_identifier($pclass->_key_field), 
     484    )); 
     485 
     486    my $res = $sth->execute($name); 
     487 
     488    if ($res) { 
     489        $self->ReportChange( 
     490            $otype, 
     491            $name, 
     492            $id, 
     493            'Delete', "Alias %s %s deleted", $otype, $name 
     494        ); 
     495        $self->log(LA_DEBUG, "Alias $otype $name removed"); 
     496        return 1; 
     497    } else { 
     498        $self->log(LA_ERR, "Error when removing alias $otype $name"); 
     499        return; 
     500    } 
     501} 
    289502 
    290503=head2 get_global_value ($varname) 
     
    419632            and expire > now() 
    420633            and expire is not null 
     634            and internobject = false 
    421635            } . ($self->{wexported} ? '' : 'and exported = true') . q{ 
    422636            order by expire 
     
    444658            expire < now() - ?::interval 
    445659            and expire is not null 
     660            and internobject = false 
    446661        } . ($self->{wexported} ? '' : 'and exported = true') . q{ 
    447662            order by expire 
Note: See TracChangeset for help on using the changeset viewer.