--- bin/ldap.pl 2008/03/30 21:59:34 44 +++ bin/import-ldap.pl 2008/06/16 16:49:30 175 @@ -11,22 +11,79 @@ use Getopt::Long; BEGIN { Jifty->new; }; +Jifty->web->request(Jifty::Request->new); +Jifty->web->response(Jifty::Response->new); my $limit = 0; my @models; +my $strix; + GetOptions( 'limit=i', => \$limit, 'model=s', => \@models, -); - -@models = ( 'Organization' ) unless @models; + 'strix+', => \$strix, +) or die "$0: $!"; my $ldap = A3C::LDAP->new; -Jifty->log->info( 'syncing: ', join(',', @models) ); +sub sync_org { + my $org_uid = shift; + my $action = Jifty->web->new_action( + class => 'SyncOrganization', + moniker => 'sync', + arguments => { + org_uid => $org_uid, + } + ); + $action->run; + if ( $action->result->success ) { + Jifty->log->info( $action->result->message ); + } else { + Jifty->log->error( "Can't sync $org_uid" ); + } +} + +@models = 'hrEduOrg' if $strix; + +if ( @models ) { + + Jifty->log->info( 'syncing: ', join(',', @models) ); + + foreach my $model ( @models ) { + my $collection = $ldap->collection( $model, limit => $limit ); + Jifty->log->info( 'found ', $collection->count, ' entries for ', $model ); + } +} else { + + my $person_oc = $ldap->objectClass->{person}; + my $org_oc = $ldap->objectClass->{organization}; + + Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" ); + + my $orgs = $ldap->collection( $org_oc , limit => $limit ); + + my $o_nr = 1; + + my $value_from = $ldap->link->{value_from}; + + while ( my $o = $orgs->next ) { + Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name ); + if ( ! $o->can( $value_from ) ) { + warn "can't find $value_from in ",dump( $o->as_hash ); + next; + } + my $org_uid = $o->$value_from || warn "can't find org_uid in $value_from in ",dump( $o->as_hash ); + sync_org( $org_uid ); + } +} -foreach my $model ( @models ) { - my $collection = $ldap->collection( $model, $limit ); - Jifty->log->info( "found ", $collection->count, " entries for $model" ); +if ( $strix ) { + my $instances = A3C::Model::StrixInstanceCollection->new; + $instances->unlimit; + my $o_nr = 1; + while ( my $instance = $instances->next ) { + Jifty->log->info( 'sync organization ', $o_nr++, '/', $instances->count, ' ', $instance->instance ); + sync_org( $instance->instance ); + } }