1 package OpenILS::WWW::TemplateBatchBibUpdate;
7 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
8 use APR::Const -compile => qw(:error SUCCESS);
11 use Apache2::RequestRec ();
12 use Apache2::RequestIO ();
13 use Apache2::RequestUtil;
18 use OpenSRF::EX qw(:try);
19 use OpenSRF::Utils qw/:datetime/;
20 use OpenSRF::Utils::Cache;
22 use OpenSRF::AppSession;
27 use Unicode::Normalize;
28 use OpenILS::Utils::Fieldmapper;
29 use OpenSRF::Utils::Logger qw/$logger/;
32 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
34 use UNIVERSAL::require;
36 our @formats = qw/USMARC UNIMARC XML BRE/;
38 # set the bootstrap config and template include directory when
39 # this module is loaded
49 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
50 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
57 my $authid = $cgi->cookie('ses') || $cgi->param('ses');
58 my $usr = verify_login($authid);
59 return show_template($r) unless ($usr);
61 my $template = $cgi->param('template');
62 return show_template($r) unless ($template);
65 my $rsource = $cgi->param('recordSource');
69 if ($rsource eq 'r') {
70 @records = map { $_ ? ($_) : () } $cgi->param('recid');
73 if ($rsource eq 'c') { # try for a file
74 my $file = $cgi->param('idfile');
76 my $col = $cgi->param('idcolumn') || 0;
77 my $csv = new Text::CSV;
81 my @data = $csv->fields;
90 my $e = OpenSRF::AppSession->connect('open-ils.cstore');
91 $e->request('open-ils.cstore.transaction.begin')->gather(1);
92 $e->request('open-ils.cstore.set_audit_info', $authid, $usr->id, $usr->wsid)->gather(1);
94 # still no records ...
95 my $container = $cgi->param('containerid');
96 if ($rsource eq 'b') {
98 my $bucket = $e->request(
99 'open-ils.cstore.direct.container.biblio_record_entry_bucket.retrieve',
103 $e->request('open-ils.cstore.transaction.rollback')->gather(1);
105 $r->log->error("No such bucket $container");
106 $logger->error("No such bucket $container");
107 return Apache2::Const::NOT_FOUND;
109 my $recs = $e->request(
110 'open-ils.cstore.direct.container.biblio_record_entry_bucket_item.search.atomic',
111 { bucket => $container }
113 @records = map { ($_->target_biblio_record_entry) } @$recs;
118 $e->request('open-ils.cstore.transaction.rollback')->gather(1);
120 return show_template($r);
123 # we have a template and some record ids, so...
125 # insert the template record
126 my $min_id = $e->request(
127 'open-ils.cstore.json_query',
128 { select => { bre => [{ column => 'id', transform => 'min', aggregate => 1}] }, from => 'bre' }
129 )->gather(1)->{id} - 1;
131 warn "new template bib id = $min_id\n";
133 my $tmpl_rec = Fieldmapper::biblio::record_entry->new;
134 $tmpl_rec->id($min_id);
135 $tmpl_rec->deleted('t');
136 $tmpl_rec->active('f');
137 $tmpl_rec->marc($template);
138 $tmpl_rec->creator($usr->id);
139 $tmpl_rec->editor($usr->id);
141 warn "about to create bib $min_id\n";
142 $e->request('open-ils.cstore.direct.biblio.record_entry.create', $tmpl_rec )->gather(1);
144 # create the new container for the records and the template
145 my $bucket = Fieldmapper::container::biblio_record_entry_bucket->new;
146 $bucket->owner($usr->id);
147 $bucket->btype('template_merge');
149 my $bname = $cgi->param('bname') || 'Temporary Merge Bucket '. localtime() . ' ' . $usr->id;
150 $bucket->name($bname);
152 $bucket = $e->request('open-ils.cstore.direct.container.biblio_record_entry_bucket.create', $bucket )->gather(1);
154 # create items in the bucket
155 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
156 $item->bucket($bucket->id);
157 $item->target_biblio_record_entry($min_id);
159 $e->request('open-ils.cstore.direct.container.biblio_record_entry_bucket_item.create', $item )->gather(1);
162 for my $r (@records) {
164 $item->target_biblio_record_entry($r);
165 $e->request('open-ils.cstore.direct.container.biblio_record_entry_bucket_item.create', $item )->gather(1);
169 $e->request('open-ils.cstore.transaction.commit')->gather(1);
172 # fire the background bucket processor
173 my $cache_key = OpenSRF::AppSession
174 ->create('open-ils.cat')
175 ->request('open-ils.cat.container.template_overlay.background', $authid, $bucket->id)
178 return show_processing_template($r, $bucket->id, \@records, $cache_key);
182 my $auth_token = shift;
183 return undef unless $auth_token;
185 my $user = OpenSRF::AppSession
186 ->create("open-ils.auth")
187 ->request( "open-ils.auth.session.retrieve", $auth_token )
190 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
194 return $user if ref($user);
198 sub show_processing_template {
202 my $cache_key = shift;
204 my $rec_string = @$recs;
206 $r->content_type('text/html');
208 <html xmlns="http://www.w3.org/1999/xhtml">
211 <title>Merging records...</title>
212 <style type="text/css">
213 \@import '/js/dojo/dojo/resources/dojo.css';
214 \@import '/js/dojo/dijit/themes/tundra/tundra.css';
215 .hide_me { display: none; visibility: hidden; }
216 th { font-weight: bold; }
219 <script type="text/javascript">
223 AutoIDL: ['aou','aout','pgt','au','cbreb']
227 <script src='/js/dojo/dojo/dojo.js'></script>
228 <!-- <script src="/js/dojo/dojo/openils_dojo.js"></script> -->
230 <script type="text/javascript">
232 dojo.require('fieldmapper.AutoIDL');
233 dojo.require('fieldmapper.dojoData');
234 dojo.require('openils.User');
235 dojo.require('openils.CGI');
236 dojo.require('openils.widget.ProgressDialog');
238 var cgi = new openils.CGI();
239 var u = new openils.User({ authcookie : 'ses' });
241 dojo.addOnLoad(function () {
242 progress_dialog.show(true);
243 progress_dialog.update({maximum:$rec_string});
246 interval = setInterval( function() {
247 fieldmapper.standardRequest(
248 ['open-ils.actor','open-ils.actor.anon_cache.get_value'],
250 params: [ u.authtoken, 'res_list' ],
251 onerror : function (r) { progress_dialog.hide(); },
252 onresponse : function (r) {
253 var counter = { success : 0, fail : 0, total : 0 };
254 dojo.forEach( openils.Util.readResponse(r), function(x) {
256 clearInterval(interval);
257 progress_dialog.hide();
258 if (x.success == 't') dojo.byId('complete_msg').innerHTML = 'Overlay completed successfully';
259 else dojo.byId('complete_msg').innerHTML = 'Overlay did not complet successfully';
273 // update the progress dialog
274 progress_dialog.update({progress:counter.total});
275 dojo.byId('success_count').innerHTML = counter.success;
276 dojo.byId('fail_count').innerHTML = counter.fail;
277 dojo.byId('total_count').innerHTML = counter.total;
287 <body style="margin:10px;" class='tundra'>
288 <div class="hide_me"><div dojoType="openils.widget.ProgressDialog" jsId="progress_dialog"></div></div>
290 <table style="width:100%; margin-top:100px;">
293 <td>Record Count</td>
297 <td id='success_count'></td>
301 <td id='fail_count'></td>
305 <td id='total_count'></td>
309 <div id='complete_msg'></div>
315 return Apache2::Const::OK;
322 $r->content_type('text/html');
324 <html xmlns="http://www.w3.org/1999/xhtml">
327 <title>Merge Template Builder</title>
328 <style type="text/css">
329 @import '/js/dojo/dojo/resources/dojo.css';
330 @import '/js/dojo/dijit/themes/tundra/tundra.css';
331 .hide_me { display: none; visibility: hidden; }
332 table.ruleTable th { padding: 5px; border-collapse: collapse; border-bottom: solid 1px gray; font-weight: bold; }
333 table.ruleTable td { padding: 5px; border-collapse: collapse; border-bottom: solid 1px gray; }
336 <script type="text/javascript">
340 AutoIDL: ['aou','aout','pgt','au','cbreb']
344 <script src='/js/dojo/dojo/dojo.js'></script>
345 <!-- <script src="/js/dojo/dojo/openils_dojo.js"></script> -->
347 <script type="text/javascript">
349 dojo.require('dojo.data.ItemFileReadStore');
350 dojo.require('dijit.form.Form');
351 dojo.require('dijit.form.NumberSpinner');
352 dojo.require('dijit.form.FilteringSelect');
353 dojo.require('dijit.form.TextBox');
354 dojo.require('dijit.form.Textarea');
355 dojo.require('dijit.form.Button');
356 dojo.require('MARC.Batch');
357 dojo.require('fieldmapper.AutoIDL');
358 dojo.require('fieldmapper.dojoData');
359 dojo.require('openils.User');
360 dojo.require('openils.CGI');
362 var cgi = new openils.CGI();
363 var u = new openils.User({ authcookie : 'ses' });
365 var bucketStore = new dojo.data.ItemFileReadStore(
366 { data : cbreb.toStoreData(
367 fieldmapper.standardRequest(
368 ['open-ils.actor','open-ils.actor.container.retrieve_by_class.authoritative'],
369 [u.authtoken, u.user.id(), 'biblio', 'staff_client']
375 function render_preview () {
376 var rec = ruleset_to_record();
377 dojo.byId('marcPreview').innerHTML = rec.toBreaker();
380 function render_from_template () {
381 var kid_number = dojo.byId('ruleList').childNodes.length;
382 var clone = dojo.query('*[name=ruleTable]', dojo.byId('ruleTemplate'))[0].cloneNode(true);
384 var typeSelect = dojo.query('*[name=typeSelect]',clone).instantiate(dijit.form.FilteringSelect, {
385 onChange : function (val) {
389 dijit.byNode(dojo.query('*[name=marcDataContainer] .dijit',clone)[0]).attr('disabled',false);
392 dijit.byNode(dojo.query('*[name=marcDataContainer] .dijit',clone)[0]).attr('disabled',true);
398 var marcData = dojo.query('*[name=marcData]',clone).instantiate(dijit.form.TextBox, {
399 onChange : render_preview
403 var tag = dojo.query('*[name=tag]',clone).instantiate(dijit.form.TextBox, {
404 onChange : function (newtag) {
405 var md = dijit.byNode(dojo.query('*[name=marcDataContainer] .dijit',clone)[0]);
406 var current_marc = md.attr('value');
408 if (newtag.length == 3) {
409 if (current_marc.length == 0) newtag += ' \\\\';
410 if (current_marc.substr(0,3) != newtag) current_marc = newtag + current_marc.substr(3);
412 md.attr('value', current_marc);
417 var sf = dojo.query('*[name=sf]',clone).instantiate(dijit.form.TextBox, {
418 onChange : function (newsf) {
419 var md = dijit.byNode(dojo.query('*[name=marcDataContainer] .dijit',clone)[0]);
420 var current_marc = md.attr('value');
421 var sf_list = newsf.split('');
423 for (var i in sf_list) {
424 var re = '\\$' + sf_list[i];
425 if (current_marc.match(re)) continue;
426 current_marc += '$' + sf_list[i];
429 md.attr('value', current_marc);
434 var matchSF = dojo.query('*[name=matchSF]',clone).instantiate(dijit.form.TextBox, {
435 onChange : render_preview
438 var matchRE = dojo.query('*[name=matchRE]',clone).instantiate(dijit.form.TextBox, {
439 onChange : render_preview
442 var removeButton = dojo.query('*[name=removeButton]',clone).instantiate(dijit.form.Button, {
443 onClick : function() {
445 dojo.byId('ruleList').childNodes[kid_number],
452 dojo.place(clone,'ruleList');
455 function ruleset_to_record () {
456 var rec = new MARC.Record ({ delimiter : '$' });
459 dojo.query('#ruleList *[name=ruleTable]').filter( function (node) {
460 if (node.className.match(/hide_me/)) return false;
464 var rule_tag = new MARC.Field ({
469 var rule_txt = dijit.byNode(dojo.query('*[name=tagContainer] .dijit',tbl)[0]).attr('value');
470 rule_txt += dijit.byNode(dojo.query('*[name=sfContainer] .dijit',tbl)[0]).attr('value');
472 var reSF = dijit.byNode(dojo.query('*[name=matchSFContainer] .dijit',tbl)[0]).attr('value');
474 var reRE = dijit.byNode(dojo.query('*[name=matchREContainer] .dijit',tbl)[0]).attr('value');
475 rule_txt += '[' + reSF + '~' + reRE + ']';
478 var rtype = dijit.byNode(dojo.query('*[name=typeSelectContainer] .dijit',tbl)[0]).attr('value');
479 rule_tag.addSubfields( rtype, rule_txt )
480 rec.appendFields( rule_tag );
482 if (rtype == 'a' || rtype == 'r') {
486 marcbreaker : dijit.byNode(dojo.query('*[name=marcDataContainer] .dijit',tbl)[0]).attr('value')
498 <body style="margin:10px;" class='tundra'>
500 <div dojoType="dijit.form.Form" id="myForm" jsId="myForm" encType="multipart/form-data" action="" method="POST">
501 <script type='dojo/method' event='onSubmit'>
502 var rec = ruleset_to_record();
504 if (rec.subfield('905','r') == '') { // no-op to force replace mode
510 subfields : [['r','901c']]
515 dojo.byId('template_value').value = rec.toXmlString();
519 <input type='hidden' id='template_value' name='template'/>
521 <label for='inputTypeSelect'>Record source:</label>
522 <select name='recordSource' dojoType='dijit.form.FilteringSelect'>
523 <script type='dojo/method' event='onChange' args="val">
526 dojo.removeClass('bucketListContainer','hide_me');
527 dojo.addClass('csvContainer','hide_me');
528 dojo.addClass('recordContainer','hide_me');
531 dojo.addClass('bucketListContainer','hide_me');
532 dojo.removeClass('csvContainer','hide_me');
533 dojo.addClass('recordContainer','hide_me');
536 dojo.addClass('bucketListContainer','hide_me');
537 dojo.addClass('csvContainer','hide_me');
538 dojo.removeClass('recordContainer','hide_me');
542 <script type='dojo/method' event='postCreate'>
543 if (cgi.param('recordSource')) {
544 this.attr('value',cgi.param('recordSource'));
545 this.onChange(cgi.param('recordSource'));
548 <option value='b'>a Bucket</option>
549 <option value='c'>a CSV File</option>
550 <option value='r'>a specific record ID</option>
553 <table style='margin:10px; margin-bottom:20px;'>
556 <th>Merge template name (optional):</th>
557 <td><input id='bucketName' jsId='bucketName' type='text' dojoType='dijit.form.TextBox' name='bname' value=''/></td>
560 <tr class='' id='bucketListContainer'>
562 <div name='containerid' jsId='bucketList' dojoType='dijit.form.FilteringSelect' store='bucketStore' searchAttr='name' id='bucketList'>
563 <script type='dojo/method' event='postCreate'>
564 if (cgi.param('containerid')) this.attr('value',cgi.param('containerid'));
569 <tr class='hide_me' id='csvContainer'>
571 Column <input style='width:75px;' type='text' dojoType='dijit.form.NumberSpinner' name='idcolumn' value='0' constraints='{min:0,max:100,places:0}' /> of:
572 <input id='idfile' type="file" name="idfile"/>
575 Columns are numbered starting at 0. For instance, when looking at a CSV file in Excel, the column labeled A is the same as column 0, and the column labeled B is the same as column 1.
578 <tr class='hide_me' id='recordContainer'>
579 <td>Record ID: <input dojoType='dijit.form.TextBox' name='recid' style='width:75px;' type='text' value=''/></td>
583 <button type="submit" dojoType='dijit.form.Button'>GO!</button> (After setting up your template below.)
588 </div> <!-- end of the form -->
591 <table style='width: 100%'>
593 <td style='width: 50%'><div id='ruleList'></div></td>
594 <td valign='top'>Update Template Preview:<br/><pre id='marcPreview'></pre></td>
598 <button dojoType='dijit.form.Button'>Add Merge Rule
599 <script type='dojo/connect' event='onClick'>render_from_template()</script>
600 <script type='dojo/method' event='postCreate'>render_from_template()</script>
603 <div class='hide_me' id='ruleTemplate'>
604 <div name='ruleTable'>
605 <table class='ruleTable'>
608 <th style="text-align:center;">Rule Setup</th>
609 <th style="text-align:center;">Data</th>
610 <th style="text-align:center;">Help</th>
613 <th>Action (Rule Type)</th>
614 <td name='typeSelectContainer'>
615 <select name='typeSelect'>
616 <option value='r'>Replace</option>
617 <option value='a'>Add</option>
618 <option value='d'>Delete</option>
621 <td>How to change the existing records</td>
625 <td name='tagContainer'><input style='with: 2em;' name='tag' type='text'></input</td>
626 <td>Three characters, no spaces, no indicators, etc. eg: 245</td>
629 <th>Subfields (optional)</th>
630 <td name='sfContainer'><input name='sf' type='text'/></td>
631 <td>No spaces, no delimiters, eg: abcnp</td>
635 <td name='marcDataContainer'><input name='marcData' type='text'/></td>
636 <td>MARC-Breaker formatted data with indicators and subfield delimiters, eg:<br/>245 04$aThe End</td>
639 <th colspan='3' style='padding-top: 20px; text-align: center;'>Advanced Matching Restriction (Optional)</th>
643 <td name='matchSFContainer'><input style='with: 2em;' name='matchSF' type='text'></input</td>
644 <td>A single subfield code, no delimiters, eg: a</td>
646 <th>Regular Expression</th>
647 <td name='matchREContainer'><input name='matchRE' type='text'/></td>
648 <td>See <a href="http://perldoc.perl.org/perlre.html#Regular-Expressions" target="_blank">the Perl documentation</a>
649 for an explanation of Regular Expressions.
653 <td colspan='3' style='padding-top: 20px; text-align: center;'>
654 <button name='removeButton'>Remove this Template Rule</button>
667 return Apache2::Const::OK;