View | Details | Raw Unified | Return to bug 38224
Collapse All | Expand All

(-)a/Koha/Exceptions/Object.pm (+10 lines)
Lines 46-51 use Exception::Class ( Link Here
46
        isa => 'Koha::Exceptions::Object',
46
        isa => 'Koha::Exceptions::Object',
47
        description => "Invalid property",
47
        description => "Invalid property",
48
    },
48
    },
49
    'Koha::Exceptions::Object::MissingMessage' => {
50
        isa         => 'Koha::Exceptions::Object',
51
        description => "Expected message missing",
52
        fields      => ['type']
53
    },
49
    'Koha::Exceptions::Object::ReadOnlyProperty' => {
54
    'Koha::Exceptions::Object::ReadOnlyProperty' => {
50
        isa => 'Koha::Exceptions::Object',
55
        isa => 'Koha::Exceptions::Object',
51
        description => "Change of readonly property attempted",
56
        description => "Change of readonly property attempted",
Lines 116-121 Exception to be used when a foreign key constraint is broken. Link Here
116
121
117
Exception to be used when an invalid class method has been invoked.
122
Exception to be used when an invalid class method has been invoked.
118
123
124
=head2 Koha::Exceptions::Object::MissingMessage
125
126
Exception to be used when a message is expected to be added to the object
127
but it is not present.
128
119
=head2 Koha::Exceptions::Object::PropertyNotFound
129
=head2 Koha::Exceptions::Object::PropertyNotFound
120
130
121
Exception to be used when an invalid object property has been requested.
131
Exception to be used when an invalid object property has been requested.
(-)a/Koha/REST/V1/RPC/Biblios.pm (-58 / +38 lines)
Lines 19-25 use Modern::Perl; Link Here
19
19
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
21
22
use Try::Tiny qw( catch try );
22
use Scalar::Util qw( blessed );
23
use Try::Tiny    qw( catch try );
24
25
use Koha::Exceptions::Object;
23
26
24
=head1 API
27
=head1 API
25
28
Lines 39-112 sub populate_empty_callnumbers { Link Here
39
    return $c->render_resource_not_found("Bibliographic record")
42
    return $c->render_resource_not_found("Bibliographic record")
40
        unless $biblio;
43
        unless $biblio;
41
44
42
    my $items = $biblio->items->search(
45
    my $filter = {
43
        {
46
        -or => [
44
            -or => [
47
            itemcallnumber => undef,
45
                itemcallnumber => undef,
48
            itemcallnumber => q{},
46
                itemcallnumber => q{},
49
        ]
47
            ]
50
    };
48
        }
49
    );
50
51
51
    $items = $items->search( { itemnumber => $c->param('item_id') } )
52
    $filter->{itemnumber} = $c->param('item_id')
52
        if $c->param('item_id');
53
        if $c->param('item_id');
53
54
54
    return try {
55
    return try {
55
56
56
        my $cn_fields = C4::Context->preference('itemcallnumber');
57
        $biblio->populate_item_callnumbers($filter);
57
        return $c->render(
58
            status  => 409,
59
            openapi => {
60
                error      => "Callnumber fields not found",
61
                error_code => 'missing_configuration',
62
            }
63
        ) unless $cn_fields;
64
65
        my $record = $biblio->record;
66
        my $callnumber;
67
58
68
        foreach my $callnumber_marc_field ( split( /,/, $cn_fields ) ) {
59
        my ($message) =
69
            my $callnumber_tag       = substr( $callnumber_marc_field, 0, 3 );
60
            grep { $_->message eq 'populate_item_callnumbers' } @{ $biblio->object_messages };
70
            my $callnumber_subfields = substr( $callnumber_marc_field, 3 );
71
61
72
            next unless $callnumber_tag && $callnumber_subfields;
62
        Koha::Exceptions::Object::MissingMessage->throw( type => 'populate_item_callnumbers' )
73
63
            unless $message;
74
            my $field = $record->field($callnumber_tag);
75
76
            next unless $field;
77
78
            $callnumber = $field->as_string( $callnumber_subfields, '' );
79
            last if $callnumber;
80
        }
81
82
        return $c->render(
83
            status  => 409,
84
            openapi => {
85
                error      => "Callnumber empty in bibliographic record",
86
                error_code => 'callnumber_empty',
87
            }
88
        ) unless $callnumber;
89
90
        return $c->render(
91
            status  => 200,
92
            openapi => {
93
                updated_items_count => 0,
94
                callnumber          => $callnumber
95
            },
96
        ) unless $items->count;
97
98
        my ($res) = $items->batch_update( { new_values => { itemcallnumber => $callnumber } } );
99
        my @modified_itemnumbers = @{ $res->{modified_itemnumbers} };
100
64
101
        return $c->render(
65
        return $c->render(
102
            status  => 200,
66
            status  => 200,
103
            openapi => {
67
            openapi => $message->payload,
104
                updated_items_count => scalar @modified_itemnumbers,
105
                callnumber          => $callnumber,
106
                modified_item_ids   => \@modified_itemnumbers,
107
            },
108
        );
68
        );
69
109
    } catch {
70
    } catch {
71
        if ( blessed $_ ) {
72
            if ( ref($_) eq 'Koha::Exceptions::SysPref::NotSet' ) {
73
                return $c->render(
74
                    status  => 409,
75
                    openapi => {
76
                        error      => "Callnumber fields not found",
77
                        error_code => 'missing_configuration',
78
                    }
79
                );
80
            } elsif ( ref($_) eq 'Koha::Exceptions::Biblio::MissingField' ) {
81
                return $c->render(
82
                    status  => 409,
83
                    openapi => {
84
                        error      => "Callnumber empty in bibliographic record",
85
                        error_code => 'callnumber_empty',
86
                    }
87
                );
88
            }
89
        }
90
110
        $c->unhandled_exception($_);
91
        $c->unhandled_exception($_);
111
    };
92
    };
112
}
93
}
113
- 

Return to bug 38224