-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathReference.pm
427 lines (387 loc) · 17 KB
/
Reference.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
=head1 NAME
Tuba::Reference : Controller class for references.
=cut
package Tuba::Reference;
use Mojo::Base qw/Tuba::Controller/;
use Tuba::DB::Objects qw/-nicknames/;
use Rose::DB::Object::Util qw/:all/;
use Tuba::Log qw/logger/;;
use Tuba::Util qw[new_uuid];
use Data::Dumper;
no warnings qw(experimental::smartmatch);
sub show {
my $c = shift;
my $identifier = $c->stash('reference_identifier');
my $reference = Reference->new(identifier => $identifier);
$reference->load(speculative => 1) or return $c->render_not_found_or_redirect;
$c->stash( object => $reference);
$c->SUPER::show(@_);
}
sub show_for_publication {
my $c = shift;
my $obj = $c->stash('selected_object') || return $c->reply->not_found;
my $identifier = $c->stash('reference_identifier');
my $reference = Reference->new(identifier => $identifier);
$reference->load(speculative => 1) or return $c->render_not_found_or_redirect;
$c->stash( object => $reference);
$c->SUPER::show(@_);
}
sub normalize_form_parameter {
my $c = shift;
my %args = @_;
my ($column, $value) = @args{qw/column value/};
my $obj;
for ($column) {
/^publication_id$/ and $obj = $c->str_to_obj($value);
/^child_publication_id$/ and $obj = $c->str_to_obj($value);
}
if ($obj) {
my $pub = $obj->get_publication(autocreate => 1);
$pub->save(audit_user => $c->audit_user, audit_note => $c->audit_note) unless $pub->id;
return $pub->id;
}
return $value;
}
sub create {
my $c = shift;
if (my $json = $c->req->json) {
if ($json->{sub_publication_uris}) {
return $c->render(json => "error sub_publication_uris is deprecated, use publication_uris");
}
my $pubs = delete $json->{publication_uris} || [];
my $more = delete $json->{publication};
$more ||= delete $json->{publication_uri};
push @$pubs, $more if $more;
for my $uri (@$pubs) {
my $obj = $c->uri_to_obj($uri) or do {
return $c->render(status => 400, json => { error => "uri $uri not found" } );
};
$obj->meta->table eq 'report' or do {
return $c->render(status => 400, json => { error => 'only reports for now' } );
};
my $pub = $obj->get_publication(autocreate => 1);
$pub->save(audit_user => $c->audit_user, audit_note => $c->audit_note) unless $pub->id;
$c->stash(object_json => $json);
}
$c->stash(publication_uris => $pubs);
}
$c->SUPER::create(@_);
}
sub post_create {
my $c = shift;
my $reference = shift;
my $tree = $reference->as_tree; # no-op for rose bug
my $uris = $c->stash('publication_uris') or return 1;
for my $uri (@$uris) {
my $pub = $c->uri_to_pub($uri)
or do { $reference->error("$uri not found"); return 0; };
$reference->add_publications({id => $pub->id});
}
$reference->save(audit_user => $c->user, audit_note => $c->stash('audit_note'));
return 1;
}
sub update {
my $c = shift;
my $reference = $c->_this_object or return $c->reply->not_found;
if (my $json = $c->req->json) {
my $audit_note = delete($json->{audit_note});
$c->stash(audit_note => $audit_note);
# Turn uris into ids
if ($json->{sub_publication_uris}) {
return $c->render(status => 400, json => { error => "sub_publication_uris is deprecated, use publication_uris" } );
}
my $pubs = delete $json->{publication_uris} || [];
my $more = delete $json->{publication};
$more ||= delete $json->{publication_uri};
push @$pubs, $more if $more;
$c->stash('publication_uris' => $pubs);
# child pub: uri to id
my $child_publication = delete $json->{child_publication_uri} || delete $json->{child_publication};
if ($child_publication) {
my $obj = $c->uri_to_obj($child_publication) or return $c->render(status => 400, json => { error => "uri $child_publication not found" } );
my $pub = $obj->get_publication(autocreate => 1) or return $c->render(status => 400, json => { error => 'not a publication'});
$pub->save(audit_user => $c->user, audit_note => $audit_note) unless $pub->id;
$json->{child_publication_id} = $pub->id;
} else {
my $obj = $c->_this_object;
$json->{child_publication_id} = $obj->child_publication_id;
}
$c->stash(object_json => $json);
$c->stash(publication_update_category => delete $json->{publication_update_category});
}
if (my $params = $c->req->params->to_hash ) {
if ( exists $params->{new_attr_key} || /^attribute/ ~~ %$params || /^delete_pub_attr/ ~~ %$params ) {
if ( exists $params->{new_attr_key} || /^attribute/ ~~ %$params ) {
my $new_attributes = _collect_attributes($params);
$reference->set_attr( { new_attrs => $new_attributes, audit_user => $c->user, audit_note => "Setting attributes" });
}
if ( /^delete_pub_attr/ ~~ %$params ) {
_delete_pub_attrs($c, $reference, $params);
}
$c->redirect_without_error('update_form');
}
else {
$c->SUPER::update(@_);
}
}
}
sub _delete_pub_attrs {
my ($c, $reference, $params ) = @_;
foreach my $key ( keys %$params ) {
if ( $key =~ /^delete_pub_attr(.*)$/ ) {
my $attr_key = $1;
$reference->delete_attr( { del_attr => $attr_key, audit_user => $c->user, audit_note => "Deleting attribute $attr_key" });
}
}
}
sub _collect_attributes {
my ( $params ) = @_;
my $attrs;
# any newly entered key should overwrite existing of that name.
my $new_attr_flag = 0;
foreach my $key ( keys %$params ) {
next if $key eq 'new_attr_value';
if ( $key eq 'new_attr_key') {
$new_attr_flag = 1;
}
if ( $key =~ /^attribute_(.*)/ && $params->{ $key } ) {
$attrs->{"$1"} = $params->{$key};
}
}
if ( $new_attr_flag ) {
$attrs->{ $params->{'new_attr_key'} } = $params->{'new_attr_value'}
}
return $attrs;
}
sub post_update {
my $c = shift;
my $reference = shift;
my $tree = $reference->as_tree; # no-op for rose bug
my $uris = $c->stash('publication_uris') or return 1;
my %existing = map { $_->id => 1 } $reference->publications;
for my $uri (@$uris) {
my $pub = $c->uri_to_pub($uri) or do { $reference->error("$uri not found"); return 0; };
next if delete($existing{$pub->id});
$reference->add_publications({id => $pub->id});
}
$reference->save(audit_user => $c->user, audit_note => $c->stash('audit_note'));
# To remove all other pubs of a certain category.
my $cat = $c->stash('publication_update_category') or return 1;
for my $pub_id (keys %existing) {
my $s = PublicationReferenceMap->new(reference_identifier => $reference->identifier, publication_id => $pub_id);
next unless $s->publication->publication_type_identifier eq $cat;
$s->load(speculative => 1) or next;
$s->delete(audit_user => $c->audit_user, audit_note => $c->audit_note) or do { $reference->error($s->error); return 0; };
}
return 1;
}
sub smartmatch {
my $c = shift;
my $reference = $c->_this_object;
$c->app->log->debug("matching [".$reference->attr('reftype')."]");
my $audit_note;
if (my $json = $c->req->json) {
$audit_note = $json->{audit_note};
}
my @tables = map $_->table, @{PublicationTypes->get_objects(all => 1)};
# Match this reference to a child publication.
my @try = map $_->{obj}, values %{ $c->orm };
my $existing = $reference->child_publication;
if ($existing) {
$existing->load;
$existing = $existing->to_object(autoclean => 1) or logger->warn("removed pub orphan : ".$reference->child_publication->as_yaml);
}
logger->debug("matching : ".$reference->identifier);
unshift @try, $existing if $existing;
my $match;
for my $class (@try) {
$match = $class->new_from_reference($reference, audit_user => $c->user, audit_note => $audit_note) and last;
}
logger->debug("match : ".($match // '<none>'));
undef $match if $match && !is_in_db($match) && $c->param('updates_only');
my $status = 'no match';
if ($match) {
logger->debug("saving");
$match->save(audit_user => $c->user, audit_note => $audit_note) or return $c->redirect_with_error($match->error);
if (is_in_db($match)) {
$status = 'match';
} else {
$status = 'new';
}
my $pub = $match->get_publication(autocreate => 1);
$pub->save(audit_user => $c->user, audit_note => $audit_note) unless $pub->id;
$reference->child_publication_id($pub->id);
$reference->save(audit_user => $c->user, audit_note => $audit_note)
or return $c->redirect_with_error(update_rel_form => $reference->error);
}
$c->respond_to(
json => sub {
shift->render(
json => {
publication => ( $match ? $match->uri($c) : undef),
status => $status,
});
},
html => sub {
shift->redirect_to('update_rel_form');
}
);
}
sub update_rel_form {
my $c = shift;
my $reference = $c->_this_object;
my ( $report ) = map $_->to_object, grep $_->to_object->meta->table eq 'report', $reference->publications;
my @chapters;
if ($report) {
@chapters = ( [ 'add a chapter', ''], map [ $_->stringify, "".$_->uri($c) ], sort { $a->sortkey cmp $b->sortkey } $report->chapters );
}
$c->stash(chapters => \@chapters);
$c->SUPER::update_rel_form(@_);
}
sub update_rel {
my $c = shift;
my $reference = $c->_this_object or return $c->reply->not_found;
if (my $json = $c->req->json) {
logger->info("got json");
if (my $subpubref = $json->{add_subpubref_uri}) { # Deprecation
return $c->render(status => 400, json => { error => "add_subpubref_uri is deprecated, use add_publication_uri"} ); # Deprecation
}
if (my $pub_uri = $json->{add_publication_uri}) {
logger->info("got $pub_uri");
# Add the URI for a chapter, figure, finding -- "sub publications" of a report --
# to this references.
my $pub = $c->uri_to_pub($pub_uri) or do {
return $c->render(status => 400, json => { error => "$pub_uri not found" });
};
$reference->add_publications({id => $pub->id});
$reference->save(changes_only => 1, audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->reply->exception;
}
if (my $subpubref = $json->{delete_subpub}) { # Deprecation
return $c->render(status => 400, json => { error => "delete_subpub is deprecated, use delete_publication"} );
}
if (my $pub_uri = $json->{delete_publication}) {
my $pub = $c->uri_to_pub($pub_uri) or do {
return $c->render(status => 400, json => { error => "$pub_uri not found" });
};
my $sub = PublicationReferenceMap->new(reference_identifier => $reference->identifier, publication_id => $pub->id);
$sub->load(speculative => 1) or return $c->redirect_with_error(update_rel_form => "$pub not found");
$sub->delete(audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->reply->exception($sub->error);
}
return $c->render(json => { status => 'ok'});
}
if ($c->param('delete_child_publication_id')) {
$reference->child_publication_id(undef);
$reference->save(changes_only => 1, audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->redirect_with_error(update_rel_form => $reference->error);
}
if (my $child = $c->param('child_publication_id')) {
my $obj = $c->str_to_obj($child) or return $c->redirect_with_error(update_rel_form => "could not find $child");
my $child_publication = $obj->get_publication(autocreate => 1);
$child_publication->save(audit_user => $c->audit_user, audit_note => $c->audit_note) unless $child_publication->id;
$reference->child_publication_id($child_publication->id);
$reference->save(changes_only => 1, audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->redirect_with_error(update_rel_form => $reference->error);
}
if (my $chapter_uri = $c->param('chapter')) {
my $chapter = $c->uri_to_obj($chapter_uri);
my $chapter_pub = $chapter->get_publication(autocreate => 1);
$chapter_pub->save(audit_user => $c->audit_user, audit_note => $c->audit_note) unless $chapter_pub->id;
$reference->add_publications({ id => $chapter_pub->id });
$reference->save(changes_only => 1, audit_user => $c->audit_user, audit_note => $c->audit_note) or
return $c->redirect_with_error(update_rel_form => $reference->error);
}
if (my $other_pub = $c->param('other_pub')) {
my $obj = $c->str_to_obj($other_pub)
or return $c->redirect_without_error(update_rel_form => "not found : $other_pub");
my $pub = $obj->get_publication(autocreate => 1);
$pub->save(audit_user => $c->audit_user, audit_note => $c->audit_note) unless $pub->id;
$reference->add_publications({ id => $pub->id });
$reference->save(changes_only => 1, audit_user => $c->audit_user, audit_note => $c->audit_note) or
return $c->redirect_with_error(update_rel_form => $reference->error);
}
if (my $which = $c->param('delete_publication')) {
my $sub = PublicationReferenceMap->new(reference_identifier => $reference->identifier, publication_id => $which);
$sub->load(speculative => 1) or return $c->redirect_with_error(update_rel_form => "$sub not found");
$sub->delete(audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->redirect_with_error(update_rel_form => $sub->error);
$c->stash(message => "saved changes");
}
$c->redirect_without_error('update_rel_form');
}
# Look up a reference by record number.
sub lookup {
my $c = shift;
my $record_number = $c->stash('record_number');
$record_number =~ /^[0-9]+$/ or return $c->reply->not_found;
my $found = References->get_objects(query => [ \"attrs->'_record_number' = ${record_number}::varchar" ], limit => 10 );
unless ($found && @$found) {
return $c->reply->not_found;
}
if (@$found > 1) {
return $c->respond_to(
html => { text => "multiple matches : ".join ',', map $_->identifier, @$found },
json => { json => { error => 'multiple matches : ', matches => [ map $_->identifier, @$found ] } },
yaml => { json => { error => 'multiple matches : ', matches => [ map $_->identifier, @$found ] } }
);
}
my $ref = $found->[0];
$c->respond_to(
html => sub { my $d = shift; $d->redirect_to($ref->uri($d)) },
json => sub { my $d = shift; $d->redirect_to($ref->uri($d).".json") },
yaml => sub { my $d = shift; $d->redirect_to($ref->uri($d).".yaml") },
)
}
sub updates_report {
my $c = shift;
my $rows = $c->dbs->query(<<SQL)->hashes;
select
attrs->'_uuid' as reference, attrs->'DOI' as endnote_doi, a.doi as gcis_doi
from reference r
inner join publication p on r.child_publication_id = p.id
inner join article a on a.identifier = p.fk->'identifier'
where ( (a.doi != (attrs->'DOI')) or (a.doi is not null and (attrs->'DOI')::varchar is null))
SQL
$c->stash(rows => $rows);
return $c->render(template => "reference/report/updates");
}
sub set_title {
my $c = shift;
my %args = @_;
my $obj = $args{object} || $c->stash('selected_object');
if ($obj) {
$c->stash(title => sprintf("References in %s",$obj->stringify(long => 1)));
return;
}
return $c->SUPER::set_title(%args);
}
sub create_form {
my $c = shift;
$c->param(identifier => new_uuid());
return $c->SUPER::create_form(@_);
}
sub list_for_publication {
my $c = shift;
my $obj = $c->stash('selected_object') || return $c->reply->not_found;
my $pub = $obj->get_publication(autocreate => 1);
my $all = !!$c->param('all');
$c->stash(title => "References for ".$obj->stringify(short => 1));
my $refs = References->get_objects(
query => [publication_id => $pub->id],
require_objects => ['publications'],
( $all ? () : (page => $c->page, per_page => $c->per_page))
);
$c->set_pages(References->get_objects_count(
query => [ "publication_id" => $pub->id ],
require_objects => ['publications'])) unless $all;
$c->stash(objects => $refs);
$c->SUPER::list(@_);
}
sub make_tree_for_list {
my $c = shift;
my $obj = shift;
my $h = $c->SUPER::make_tree_for_list($obj);
$h->{child_publication} = undef;
if (my $id = $h->{child_publication_id}) {
$h->{child_publication} = Publication->new(id => $h->{child_publication_id})->load->to_object->uri($c);
}
delete $h->{child_publication_id};
return $h;
}
1;