Changeset 24071

Show
Ignore:
Timestamp:
20.05.2011 16:34:22 (8 years ago)
Author:
davidb
Message:

Introduction of actions that take an array of items (e.g. an array of OIDs or filenames). In adding in this ability, we have started to make use of JSON.

Another action added in is the ability to control building using a manifest file (its fields passed in using JSON). Also the ability to delete files in the archives directory (i.e. when a collection is beeing used in an 'onlyadd' way). Still needs to the more general case to be implemented.

/DB/

Location:
main/trunk/greenstone2/perllib/cgiactions
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/cgiactions/buildaction.pm

    r23469 r24071  
    2828use strict; 
    2929 
     30use JSON; 
     31 
    3032use cgiactions::baseaction; 
    3133 
     
    3335use ghtml; 
    3436 
     37use util; 
    3538 
    3639BEGIN { 
     
    6568     
    6669    "incremental-rebuild" => { 'compulsory-args' => [], 
    67                    'optional-args'   => [] } 
     70                   'optional-args'   => [] }, 
     71                    
     72    "build-by-manifest" => { 'compulsory-args' => [], 
     73                   'optional-args'   => ["index-files", "reindex-files", "delete-OIDs"] } 
     74                    
    6875}; 
    6976 
     
    206213    # Make sure the collection isn't locked by someone else 
    207214    $self->lock_collection($username, $collect); 
    208  
    209  
     215     
    210216    my $bin_script = &util::filename_cat($gsdl_home,"bin","script"); 
    211217    my $cmd = "perl -S full-rebuild.pl \"$collect\""; 
     
    342348} 
    343349 
    344  
     350sub build_by_manifest 
     351{ 
     352    my $self = shift @_; 
     353 
     354    my $username  = $self->{'username'}; 
     355    my $collect   = $self->{'collect'}; 
     356    my $gsdl_cgi  = $self->{'gsdl_cgi'}; 
     357    my $gsdl_home  = $self->{'gsdlhome'}; 
     358 
     359    # Authenticate user if it is enabled 
     360    if ($baseaction::authentication_enabled) { 
     361    # Ensure the user is allowed to edit this collection 
     362    &authenticate_user($gsdl_cgi, $username, $collect); 
     363    } 
     364     
     365    # Obtain the collect dir 
     366    my $collect_dir = &util::filename_cat($gsdl_home, "collect"); 
     367 
     368    # Make sure the collection isn't locked by someone else 
     369    $self->lock_collection($username, $collect); 
     370 
     371    my $if_json_str   = $self->{'index-files'}; 
     372    my $rf_json_str   = $self->{'reindex-files'}; 
     373    my $df_json_str   = $self->{'delete-OIDs'}; 
     374     
     375         
     376    my $index_files   = (defined $if_json_str) ? decode_json $if_json_str : []; 
     377    my $reindex_files = (defined $rf_json_str) ? decode_json $rf_json_str : []; 
     378    my $delete_files  = (defined $df_json_str) ? decode_json $df_json_str : []; 
     379     
     380    my $index_files_xml   = join("\n", map { "    <Filename>$_</Filename>" } @$index_files); 
     381    my $reindex_files_xml = join("\n", map { "    <Filename>$_</Filename>" } @$reindex_files); 
     382    my $delete_files_xml  = join("\n", map { "    <OID>$_</OID>" } @$delete_files); 
     383     
     384    my $manifest_filename = &util::get_tmp_filename(".xml"); 
     385    my ($status,$report); 
     386             
     387    if (open(MOUT,">$manifest_filename")) { 
     388        binmode(MOUT,":utf8"); 
     389        print MOUT <<MOUTRAW; 
     390<Manifest> 
     391  <Index> 
     392$index_files_xml 
     393  </Index> 
     394  <Reindex> 
     395$reindex_files_xml 
     396  </Reindex> 
     397  <Delete> 
     398$delete_files_xml 
     399  </Delete> 
     400</Manifest> 
     401MOUTRAW 
     402        close(MOUT); 
     403         
     404        ## my $bin_script = &util::filename_cat($gsdl_home,"bin","script"); 
     405        my $cmd = "perl -S incremental-rebuild.pl -manifest \"$manifest_filename\" \"$collect\""; 
     406 
     407        ($status,$report) = $self->run_build_cmd($cmd); 
     408         
     409        if ($status==0) { 
     410            &util::rm($manifest_filename); 
     411        } 
     412    } 
     413    else { 
     414        $status = -1; 
     415        $report = "Failed to open '$manifest_filename' for output\n$!\n"; 
     416    } 
     417     
     418    
     419    # Release the lock once it is done 
     420    $self->unlock_collection($username, $collect); 
     421 
     422    if ($status==0) { 
     423    $gsdl_cgi->generate_ok_message($report); 
     424    } 
     425    else { 
     426    $gsdl_cgi->generate_error($report); 
     427    } 
     428     
     429    # incremental-rebuild.pl -manifest manifest.xml \"" + _greenstoneCollectionName  + "\""; 
     430 } 
    345431 
    346432 
  • main/trunk/greenstone2/perllib/cgiactions/explodeaction.pm

    r23768 r24071  
    3434use util; 
    3535 
     36use JSON; 
     37 
    3638use File::Basename; 
    3739 
     
    5052my $action_table = 
    5153{  
    52     "explode"          => { 'compulsory-args' => ["d"], 
    53                 'optional-args'   => [] } 
     54    "explode-document"          => { 'compulsory-args' => ["d"], 
     55                'optional-args'   => [] }, 
     56    "delete-document"           => { 'compulsory-args' => ["d"], 
     57                'optional-args'   => [ "onlyadd" ] }, 
     58    "delete-document-array"     => { 'compulsory-args' => ["json"], 
     59                'optional-args'   => [ "onlyadd" ] } 
     60                 
     61                 
    5462}; 
    5563 
     
    257265 
    258266 
    259 sub explode  
     267sub remove_docoids 
     268{ 
     269    my $self = shift @_; 
     270    my ($docids) = @_; 
     271 
     272    my $collect   = $self->{'collect'}; 
     273    my $gsdl_cgi  = $self->{'gsdl_cgi'}; 
     274    my $infodb_type = $self->{'infodbtype'}; 
     275 
     276    # Derive the archives and import directories 
     277    my $site = $self->{'site'}; 
     278    my $collect_dir = $gsdl_cgi->get_collection_dir($site); 
     279     
     280    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives"); 
     281 
     282    # Obtain the doc.xml path for the specified docID 
     283    my $arcinfo_doc_filename  
     284    = &dbutil::get_infodb_file_path($infodb_type, "archiveinf-doc",  
     285                    $archive_dir); 
     286 
     287    foreach my $docid (@$docids) { 
     288 
     289        my $doc_rec 
     290            = &dbutil::read_infodb_entry($infodb_type, $arcinfo_doc_filename,  
     291                     $docid); 
     292 
     293        my $doc_xml_file = $doc_rec->{'doc-file'}->[0]; 
     294 
     295        # The $doc_xml_file is relative to the archives, so need to do 
     296        # a bit more work to make sure the right folder containing this 
     297        # is moved to the right place in the import folder 
     298 
     299        my $assoc_path = dirname($doc_xml_file); 
     300        my $archive_assoc_dir = &util::filename_cat($archive_dir,$assoc_path); 
     301 
     302        &util::rm_r($archive_assoc_dir) 
     303    } 
     304} 
     305 
     306 
     307sub explode_document  
    260308{ 
    261309    my $self = shift @_; 
     
    309357 
    310358 
     359sub delete_document_entry 
     360{ 
     361    my $self = shift @_; 
     362    my ($docid_root,$opt_onlyadd) = @_; 
     363     
     364    my $docid_keys = []; 
     365    if ((defined $opt_onlyadd) && ($opt_onlyadd==1)) { 
     366        # delete docoid archive folder 
     367        push(@$docid_keys,$docid_root); 
     368    } 
     369    else { 
     370        print STDERR "**** Not currently implemented for the general case!!\nDeleting 'archive' version only."; 
     371         
     372        push(@$docid_keys,$docid_root); 
     373         
     374        #my $orig_import_filenames = $self->docid_to_import_filenames($docid_root); 
     375        #$docid_keys = $self->import_filenames_to_docids($orig_import_filenames); 
     376        #my $expanded_import_filenames = $self->docid_to_import_filenames(@$docid_keys); 
     377         
     378        # need to remove only the files that are not  
     379         
     380        #$self->remove_import_filenames($expanded_import_filenames); 
     381    } 
     382    
     383    $self->remove_docoids($docid_keys); 
     384} 
     385     
     386 
     387sub delete_document 
     388{ 
     389    my $self = shift @_; 
     390 
     391    my $username  = $self->{'username'}; 
     392    my $collect   = $self->{'collect'}; 
     393    my $gsdl_cgi  = $self->{'gsdl_cgi'}; 
     394    my $gsdl_home  = $self->{'gsdlhome'}; 
     395 
     396    # Authenticate user if it is enabled 
     397    if ($baseaction::authentication_enabled) { 
     398    # Ensure the user is allowed to edit this collection 
     399    &authenticate_user($gsdl_cgi, $username, $collect); 
     400    } 
     401     
     402    # Derive the archives dir     
     403    my $site = $self->{'site'}; 
     404    my $collect_dir = $gsdl_cgi->get_collection_dir($site); 
     405     
     406    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives"); 
     407    ##my $archive_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"archives"); 
     408 
     409    # Make sure the collection isn't locked by someone else 
     410    $self->lock_collection($username, $collect); 
     411 
     412    # look up additional args 
     413    my $docid  = $self->{'d'}; 
     414    if ((!defined $docid) || ($docid =~ m/^\s*$/)) { 
     415    $self->unlock_collection($username, $collect); 
     416    $gsdl_cgi->generate_error("No docid (d=...) specified."); 
     417    } 
     418 
     419    my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/); 
     420 
     421    my $onlyadd = $self->{'onlyadd'}; 
     422 
     423    my $status = $self->delete_document_entry($docid_root,$onlyadd);     
     424 
     425    # Release the lock once it is done 
     426    $self->unlock_collection($username, $collect); 
     427 
     428    my $mess = "delete-document successful: Key[$docid_root]\n"; 
     429    $gsdl_cgi->generate_ok_message($mess); 
     430 
     431} 
     432 
     433 
     434sub delete_document_array 
     435{ 
     436    my $self = shift @_; 
     437 
     438    my $username  = $self->{'username'}; 
     439    my $collect   = $self->{'collect'}; 
     440    my $gsdl_cgi  = $self->{'gsdl_cgi'}; 
     441    my $gsdlhome  = $self->{'gsdlhome'}; 
     442 
     443    if ($baseaction::authentication_enabled) { 
     444    # Ensure the user is allowed to edit this collection 
     445    &authenticate_user($gsdl_cgi, $username, $collect); 
     446    } 
     447 
     448    my $site = $self->{'site'}; 
     449    my $collect_dir = $gsdl_cgi->get_collection_dir($site); 
     450     
     451    $gsdl_cgi->checked_chdir($collect_dir); 
     452 
     453    # Obtain the collect dir 
     454    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
     455 
     456    # Make sure the collection isn't locked by someone else 
     457    $self->lock_collection($username, $collect); 
     458 
     459    # look up additional args 
     460     
     461    my $json_str      = $self->{'json'}; 
     462    my $doc_array = decode_json $json_str; 
     463 
     464    my $onlyadd = $self->{'onlyadd'}; 
     465     
     466     
     467    my $global_status = 0; 
     468    my $global_mess = ""; 
     469     
     470    my @all_docids = (); 
     471     
     472    foreach my $doc_array_rec ( @$doc_array ) { 
     473         
     474        my $docid     = $doc_array_rec->{'docid'}; 
     475         
     476        push(@all_docids,$docid); 
     477         
     478        my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/); 
     479           
     480        my $status = $self->delete_document_entry($docid_root,$onlyadd);     
     481         
     482        if ($status != 0) { 
     483            # Catch error if set infodb entry failed 
     484            $global_status = $status; 
     485            $global_mess .= "Failed to delete document key: $docid\n"; 
     486            $global_mess .= "Exit status: $status\n"; 
     487            $global_mess .= "System Error Message: $!\n"; 
     488            $global_mess .= "-" x 20; 
     489        } 
     490    } 
     491 
     492    if ($global_status != 0) { 
     493        $global_mess .= "PATH: $ENV{'PATH'}\n"; 
     494        $gsdl_cgi->generate_error($global_mess); 
     495    } 
     496    else { 
     497        my $mess = "delete-document-array successful: Keys[ ".join(", ",@all_docids)."]\n"; 
     498        $gsdl_cgi->generate_ok_message($mess); 
     499    } 
     500     
     501    # Release the lock once it is done 
     502    $self->unlock_collection($username, $collect); 
     503} 
     504 
     505 
    311506 
    3125071; 
  • main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm

    r23766 r24071  
    3333use ghtml; 
    3434 
     35use JSON; 
     36 
    3537 
    3638BEGIN { 
     
    5759                     'optional-args'   => [ "metapos" ] }, 
    5860 
     61    "set-metadata-array"     => { 'compulsory-args' => [ "json" ], 
     62                     'optional-args'   => [ ] }, 
     63                      
    5964    "set-archives-metadata" => { 'compulsory-args' => [ "d", "metaname", "metavalue" ], 
    6065                     'optional-args'   => [ "metapos", "metamode" ] 
     
    6267                }, 
    6368 
     69    "set-archives-metadata-array" => { 'compulsory-args' => [ "json" ], 
     70                     'optional-args'   => [ "metamode" ] 
     71                }, 
     72                 
    6473    "set-import-metadata"   => { 'compulsory-args' => [ "metaname", "metavalue" ], 
    6574                     'optional-args'   => [ "d", "f", "metamode" ] 
     
    292301} 
    293302 
     303sub set_metadata_entry 
     304{ 
     305    my $self = shift @_; 
     306    my ($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue) = @_; 
     307     
     308    # To people who know $collect_tail please add some comments 
     309    # Obtain path to the database 
     310    my $collect_tail = $collect; 
     311    $collect_tail =~ s/^.*[\/\\]//; 
     312    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text"); 
     313    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory); 
     314     
     315#   print STDERR "**** infodb file path = $infodb_file_path\n"; 
     316#   print STDERR "***** infodb type = $infodbtype\n"; 
     317     
     318    # Read the docid entry 
     319    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid); 
     320     
     321    # Set the metadata value 
     322    if (defined $metapos) { 
     323        $doc_rec->{$metaname}->[$metapos] = $metavalue; 
     324    } 
     325    else { 
     326        $doc_rec->{$metaname} = [ $metavalue ]; 
     327    } 
     328  
     329    my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path,$docid,$doc_rec); 
     330     
     331    return $status; 
     332     
     333} 
    294334 
    295335sub set_metadata 
     
    325365    my $infodbtype = $self->{'infodbtype'}; 
    326366     
    327     # To people who know $collect_tail please add some comments 
    328     # Obtain path to the database 
    329     my $collect_tail = $collect; 
    330     $collect_tail =~ s/^.*[\/\\]//; 
    331     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text"); 
    332     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory); 
    333      
    334     print STDERR "**** infodb file path = $infodb_file_path\n"; 
    335     print STDERR "***** infodb type = $infodbtype\n"; 
    336      
    337     # Read the docid entry 
    338     my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid); 
    339      
    340     # Set the metadata value 
    341     if (defined $metapos) { 
    342     $doc_rec->{$metaname}->[$metapos] = $metavalue; 
    343     } 
    344     else { 
    345     $doc_rec->{$metaname} = [ $metavalue ]; 
    346     } 
    347   
    348     my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path,$docid,$doc_rec); 
     367    my $status = $self->set_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue); 
     368     
    349369    if ($status != 0) { 
    350370        # Catch error if set infodb entry failed 
     
    358378    } 
    359379    else { 
    360     my $mess = "set-document-metadata successful: Key[$docid]\n"; 
     380    my $mess = "set-metadata successful: Key[$docid]\n"; 
    361381    $mess .= "  $metaname"; 
    362382    $mess .= "->[$metapos]" if (defined $metapos); 
     
    369389    $self->unlock_collection($username, $collect); 
    370390} 
     391 
     392 
     393 
     394 
     395sub set_metadata_array 
     396{ 
     397    my $self = shift @_; 
     398 
     399    my $username  = $self->{'username'}; 
     400    my $collect   = $self->{'collect'}; 
     401    my $gsdl_cgi  = $self->{'gsdl_cgi'}; 
     402    my $gsdlhome  = $self->{'gsdlhome'}; 
     403 
     404    if ($baseaction::authentication_enabled) { 
     405    # Ensure the user is allowed to edit this collection 
     406    &authenticate_user($gsdl_cgi, $username, $collect); 
     407    } 
     408 
     409    my $site = $self->{'site'}; 
     410    my $collect_dir = $gsdl_cgi->get_collection_dir($site); 
     411     
     412    $gsdl_cgi->checked_chdir($collect_dir); 
     413 
     414    # Obtain the collect dir 
     415    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
     416 
     417    # Make sure the collection isn't locked by someone else 
     418    $self->lock_collection($username, $collect); 
     419 
     420    # look up additional args 
     421     
     422    my $infodbtype = $self->{'infodbtype'}; 
     423     
     424    my $json_str      = $self->{'json'}; 
     425    my $doc_array = decode_json $json_str; 
     426     
     427     
     428    my $global_status = 0; 
     429    my $global_mess = ""; 
     430     
     431    my @all_docids = (); 
     432     
     433    foreach my $doc_array_rec ( @$doc_array ) { 
     434         
     435        my $docid     = $doc_array_rec->{'docid'}; 
     436        my $metaname  = $doc_array_rec->{'metaname'}; 
     437        my $metapos   = $doc_array_rec->{'metapos'}; 
     438        my $metavalue = $doc_array_rec->{'metavalue'}; 
     439         
     440        push(@all_docids,$docid); 
     441         
     442        my $status = $self->set_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue);  
     443         
     444        if ($status != 0) { 
     445            # Catch error if set infodb entry failed 
     446            $global_status = $status; 
     447            $global_mess .= "Failed to set metadata key: $docid\n"; 
     448            $global_mess .= "Exit status: $status\n"; 
     449            $global_mess .= "System Error Message: $!\n"; 
     450            $global_mess .= "-" x 20; 
     451        } 
     452    } 
     453 
     454    if ($global_status != 0) { 
     455        $global_mess .= "PATH: $ENV{'PATH'}\n"; 
     456        $gsdl_cgi->generate_error($global_mess); 
     457    } 
     458    else { 
     459        my $mess = "set-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n"; 
     460        $gsdl_cgi->generate_ok_message($mess); 
     461    } 
     462     
     463    # Release the lock once it is done 
     464    $self->unlock_collection($username, $collect); 
     465} 
     466 
    371467 
    372468 
     
    554650} 
    555651 
     652sub set_archives_metadata_entry 
     653{ 
     654    my $self = shift @_; 
     655    my ($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode) = @_; 
     656     
     657    # Obtain the doc.xml path for the specified docID 
     658    my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/); 
     659 
     660    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir); 
     661    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid_root); 
     662    my $doc_xml_file = $doc_rec->{'doc-file'}->[0]; 
     663     
     664    # The $doc_xml_file is relative to the archives, and now let's get the full path 
     665    my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");     
     666    my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file); 
     667     
     668    # Edit the doc.xml file with the specified metadata name, value and position. 
     669    # TODO: there is a potential problem here as this edit_doc_xml function  
     670    # is assuming the simple doc.xml situation where there is only one Section and no SubSections. 
     671    # Running import.pl -groupsize will cause this to have multiple sections in one doc.xml 
     672     
     673    print STDERR "** away to call edit_doc_xml\n"; 
     674     
     675    $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename, 
     676            $metaname,$metavalue,$metapos,$metamode,$docid_secnum); 
     677             
     678    print STDERR "*** finished edit_doc_xml\n"; 
     679     
     680    return 0; # return 0 for now to indicate no error 
     681             
     682} 
     683 
    556684 
    557685sub set_archives_metadata 
     
    565693    my $infodbtype = $self->{'infodbtype'}; 
    566694     
     695 
     696     
    567697    if ($baseaction::authentication_enabled) { 
    568     # Ensure the user is allowed to edit this collection 
    569     $self->authenticate_user($username, $collect); 
    570     } 
    571  
     698        # Ensure the user is allowed to edit this collection 
     699        $self->authenticate_user($username, $collect); 
     700    } 
     701 
     702    my $site = $self->{'site'}; 
     703         
    572704    # Obtain the collect and archive dir    
    573     my $site = $self->{'site'}; 
    574705    my $collect_dir = $gsdl_cgi->get_collection_dir($site);  
    575     ## my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
    576706     
    577707    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives"); 
     
    593723    # delete any existing values) 
    594724    $metamode = "accumulate"; 
    595     } 
    596      
    597     # Obtain the doc.xml path for the specified docID 
    598     my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/); 
    599  
    600     my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir); 
    601     my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid_root); 
    602     my $doc_xml_file = $doc_rec->{'doc-file'}->[0]; 
    603      
    604     # The $doc_xml_file is relative to the archives, and now let's get the full path 
    605     my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");     
    606     my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file); 
    607      
    608     # Edit the doc.xml file with the specified metadata name, value and position. 
    609     # TODO: there is a potential problem here as this edit_doc_xml function  
    610     # is assuming the simple doc.xml situation where there is only one Section and no SubSections. 
    611     # Running import.pl -groupsize will cause this to have multiple sections in one doc.xml 
    612     $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename, 
    613             $metaname,$metavalue,$metapos,$metamode,$docid_secnum); 
    614      
     725    }  
     726     
     727    my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid, 
     728                $metaname,$metapos,$metavalue,$metamode); 
     729    
    615730    # Release the lock once it is done 
    616731    $self->unlock_collection($username, $collect); 
    617732 
    618     my $mess = "set-archives-metadata successful: Key[$docid]\n"; 
    619     $mess .= "  $metaname"; 
    620     $mess .= "->[$metapos]" if (defined $metapos); 
    621     $mess .= " = $metavalue"; 
    622     $mess .= " ($metamode)\n"; 
    623      
    624     $gsdl_cgi->generate_ok_message($mess);   
     733    if ($status == 0) { 
     734        my $mess = "set-archives-metadata successful: Key[$docid]\n"; 
     735        $mess .= "  $metaname"; 
     736        $mess .= "->[$metapos]" if (defined $metapos); 
     737        $mess .= " = $metavalue"; 
     738        $mess .= " ($metamode)\n"; 
     739     
     740        $gsdl_cgi->generate_ok_message($mess);   
     741    } 
     742    else { 
     743        my $mess .= "Failed to set archives metadata key: $docid\n"; 
     744        $mess .= "Exit status: $status\n"; 
     745        $mess .= "System Error Message: $!\n"; 
     746        $mess .= "-" x 20 . "\n"; 
     747         
     748        $gsdl_cgi->generate_error($mess); 
     749    } 
     750} 
     751 
     752 
     753sub set_archives_metadata_array 
     754{ 
     755    my $self = shift @_; 
     756 
     757    my $username  = $self->{'username'}; 
     758    my $collect   = $self->{'collect'}; 
     759    my $gsdl_cgi  = $self->{'gsdl_cgi'}; 
     760    my $gsdlhome  = $self->{'gsdlhome'}; 
     761 
     762    if ($baseaction::authentication_enabled) { 
     763    # Ensure the user is allowed to edit this collection 
     764    &authenticate_user($gsdl_cgi, $username, $collect); 
     765    } 
     766 
     767    my $site = $self->{'site'}; 
     768    my $collect_dir = $gsdl_cgi->get_collection_dir($site); 
     769     
     770    $gsdl_cgi->checked_chdir($collect_dir); 
     771 
     772    # Obtain the collect dir 
     773    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
     774 
     775    # Make sure the collection isn't locked by someone else 
     776    $self->lock_collection($username, $collect); 
     777 
     778    # look up additional args 
     779     
     780    my $infodbtype = $self->{'infodbtype'}; 
     781 
     782   my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives"); 
     783     
     784    my $json_str      = $self->{'json'}; 
     785    my $doc_array = decode_json $json_str; 
     786     
     787     
     788    my $global_status = 0; 
     789    my $global_mess = ""; 
     790     
     791    my @all_docids = (); 
     792     
     793    foreach my $doc_array_rec ( @$doc_array ) { 
     794         
     795        my $docid     = $doc_array_rec->{'docid'}; 
     796        my $metaname  = $doc_array_rec->{'metaname'}; 
     797        my $metapos   = $doc_array_rec->{'metapos'}; 
     798        my $metamode   = $self->{'metamode'}; 
     799        my $metavalue = $doc_array_rec->{'metavalue'}; 
     800         
     801        # Some sanity checks 
     802        $metapos = 0 if (!defined $metapos); 
     803             
     804        if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) { 
     805            # make "accumulate" the default (less destructive, as won't actually  
     806            # delete any existing values) 
     807            $metamode = "accumulate"; 
     808        }  
     809     
     810        push(@all_docids,$docid); 
     811         
     812        my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid, 
     813                $metaname,$metapos,$metavalue,$metamode); 
     814         
     815        if ($status != 0) { 
     816            # Catch error if set infodb entry failed 
     817            $global_status = $status; 
     818            $global_mess .= "Failed to set metadata key: $docid\n"; 
     819            $global_mess .= "Exit status: $status\n"; 
     820            $global_mess .= "System Error Message: $!\n"; 
     821            $global_mess .= "-" x 20 . "\n"; 
     822        } 
     823    } 
     824 
     825    if ($global_status != 0) { 
     826        $global_mess .= "PATH: $ENV{'PATH'}\n"; 
     827        $gsdl_cgi->generate_error($global_mess); 
     828    } 
     829    else { 
     830        my $mess = "set-archives-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n"; 
     831        $gsdl_cgi->generate_ok_message($mess); 
     832    } 
     833     
     834    # Release the lock once it is done 
     835    $self->unlock_collection($username, $collect); 
    625836} 
    626837