Bug 17932: Add a TO_JSON method to Koha::Object(s)
[koha.git] / Koha / Objects.pm
1 package Koha::Objects;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25
26 =head1 NAME
27
28 Koha::Objects - Koha Object set base class
29
30 =head1 SYNOPSIS
31
32     use Koha::Objects;
33     my @objects = Koha::Objects->search({ borrowernumber => $borrowernumber});
34
35 =head1 DESCRIPTION
36
37 This class must be subclassed.
38
39 =head1 API
40
41 =head2 Class Methods
42
43 =cut
44
45 =head3 Koha::Objects->new();
46
47 my $object = Koha::Objects->new();
48
49 =cut
50
51 sub new {
52     my ($class) = @_;
53     my $self = {};
54
55     bless( $self, $class );
56 }
57
58 =head3 Koha::Objects->_new_from_dbic();
59
60 my $object = Koha::Objects->_new_from_dbic( $resultset );
61
62 =cut
63
64 sub _new_from_dbic {
65     my ( $class, $resultset ) = @_;
66     my $self = { _resultset => $resultset };
67
68     bless( $self, $class );
69 }
70
71 =head3 Koha::Objects->find();
72
73 my $object = Koha::Objects->find($id);
74 my $object = Koha::Objects->find( { keypart1 => $keypart1, keypart2 => $keypart2 } );
75
76 =cut
77
78 sub find {
79     my ( $self, $id ) = @_;
80
81     return unless defined($id);
82
83     my $result = $self->_resultset()->find($id);
84
85     return unless $result;
86
87     my $object = $self->object_class()->_new_from_dbic( $result );
88
89     return $object;
90 }
91
92 =head3 Koha::Objects->find_or_create();
93
94 my $object = Koha::Objects->find_or_create( $attrs );
95
96 =cut
97
98 sub find_or_create {
99     my ( $self, $params ) = @_;
100
101     my $result = $self->_resultset->find_or_create($params);
102
103     return unless $result;
104
105     my $object = $self->object_class->_new_from_dbic($result);
106
107     return $object;
108 }
109
110 =head3 Koha::Objects->search();
111
112 my @objects = Koha::Objects->search($params);
113
114 =cut
115
116 sub search {
117     my ( $self, $params, $attributes ) = @_;
118
119     if (wantarray) {
120         my @dbic_rows = $self->_resultset()->search($params, $attributes);
121
122         return $self->_wrap(@dbic_rows);
123
124     }
125     else {
126         my $class = ref($self) ? ref($self) : $self;
127         my $rs = $self->_resultset()->search($params, $attributes);
128
129         return $class->_new_from_dbic($rs);
130     }
131 }
132
133 =head3 search_related
134
135     my @objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
136     my $objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
137
138 Searches the specified relationship, optionally specifying a condition and attributes for matching records.
139
140 =cut
141
142 sub search_related {
143     my ( $self, $rel_name, @params ) = @_;
144
145     return if !$rel_name;
146     if (wantarray) {
147         my @dbic_rows = $self->_resultset()->search_related($rel_name, @params);
148         return if !@dbic_rows;
149         my $object_class = _get_objects_class( $dbic_rows[0]->result_class );
150
151         eval "require $object_class";
152         return _wrap( $object_class, @dbic_rows );
153
154     } else {
155         my $rs = $self->_resultset()->search_related($rel_name, @params);
156         return if !$rs;
157         my $object_class = _get_objects_class( $rs->result_class );
158
159         eval "require $object_class";
160         return _new_from_dbic( $object_class, $rs );
161     }
162 }
163
164 =head3 single
165
166 my $object = Koha::Objects->search({}, { rows => 1 })->single
167
168 Returns one and only one object that is part of this set.
169 Returns undef if there are no objects found.
170
171 This is optimal as it will grab the first returned result without instantiating
172 a cursor.
173
174 See:
175 http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod#Retrieve_one_and_only_one_row_from_a_resultset
176
177 =cut
178
179 sub single {
180     my ($self) = @_;
181
182     my $single = $self->_resultset()->single;
183     return unless $single;
184
185     return $self->object_class()->_new_from_dbic($single);
186 }
187
188 =head3 Koha::Objects->next();
189
190 my $object = Koha::Objects->next();
191
192 Returns the next object that is part of this set.
193 Returns undef if there are no more objects to return.
194
195 =cut
196
197 sub next {
198     my ( $self ) = @_;
199
200     my $result = $self->_resultset()->next();
201     return unless $result;
202
203     my $object = $self->object_class()->_new_from_dbic( $result );
204
205     return $object;
206 }
207
208 =head3 Koha::Objects->reset();
209
210 Koha::Objects->reset();
211
212 resets iteration so the next call to next() will start agein
213 with the first object in a set.
214
215 =cut
216
217 sub reset {
218     my ( $self ) = @_;
219
220     $self->_resultset()->reset();
221
222     return $self;
223 }
224
225 =head3 Koha::Objects->as_list();
226
227 Koha::Objects->as_list();
228
229 Returns an arrayref of the objects in this set.
230
231 =cut
232
233 sub as_list {
234     my ( $self ) = @_;
235
236     my @dbic_rows = $self->_resultset()->all();
237
238     my @objects = $self->_wrap(@dbic_rows);
239
240     return wantarray ? @objects : \@objects;
241 }
242
243 =head3 Koha::Objects->unblessed
244
245 Returns an unblessed representation of objects.
246
247 =cut
248
249 sub unblessed {
250     my ($self) = @_;
251
252     return [ map { $_->unblessed } $self->as_list ];
253 }
254
255 =head3 Koha::Objects->TO_JSON
256
257 Returns an unblessed representation of objects, suitable for JSON output.
258
259 =cut
260
261 sub TO_JSON {
262     my ($self) = @_;
263
264     return [ map { $_->TO_JSON } $self->as_list ];
265 }
266
267 =head3 Koha::Objects->_wrap
268
269 wraps the DBIC object in a corresponding Koha object
270
271 =cut
272
273 sub _wrap {
274     my ( $self, @dbic_rows ) = @_;
275
276     my @objects = map { $self->object_class->_new_from_dbic( $_ ) } @dbic_rows;
277
278     return @objects;
279 }
280
281 =head3 Koha::Objects->_resultset
282
283 Returns the internal resultset or creates it if undefined
284
285 =cut
286
287 sub _resultset {
288     my ($self) = @_;
289
290     if ( ref($self) ) {
291         $self->{_resultset} ||=
292           Koha::Database->new()->schema()->resultset( $self->_type() );
293
294         return $self->{_resultset};
295     }
296     else {
297         return Koha::Database->new()->schema()->resultset( $self->_type() );
298     }
299 }
300
301 sub _get_objects_class {
302     my ( $type ) = @_;
303     return unless $type;
304
305     if( $type->can('koha_objects_class') ) {
306         return $type->koha_objects_class;
307     }
308     $type =~ s|Schema::Result::||;
309     return "${type}s";
310 }
311
312 =head3 columns
313
314 my @columns = Koha::Objects->columns
315
316 Return the table columns
317
318 =cut
319
320 sub columns {
321     my ( $class ) = @_;
322     return Koha::Database->new->schema->resultset( $class->_type )->result_source->columns;
323 }
324
325 =head3 AUTOLOAD
326
327 The autoload method is used call DBIx::Class method on a resultset.
328
329 Important: If you plan to use one of the DBIx::Class methods you must provide
330 relevant tests in t/db_dependent/Koha/Objects.t
331 Currently count, pager, update and delete are covered.
332
333 =cut
334
335 sub AUTOLOAD {
336     my ( $self, @params ) = @_;
337
338     my @known_methods = qw( count pager update delete result_class single );
339     my $method = our $AUTOLOAD;
340     $method =~ s/.*:://;
341
342     carp "The method $method is not covered by tests" and return unless grep {/^$method$/} @known_methods;
343     my $r = eval { $self->_resultset->$method(@params) };
344     if ( $@ ) {
345         carp "No method $method found for " . ref($self) . " " . $@;
346         return
347     }
348     return $r;
349 }
350
351 =head3 _type
352
353 The _type method must be set for all child classes.
354 The value returned by it should be the DBIC resultset name.
355 For example, for holds, _type should return 'Reserve'.
356
357 =cut
358
359 sub _type { }
360
361 =head3 object_class
362
363 This method must be set for all child classes.
364 The value returned by it should be the name of the Koha
365 object class that is returned by this class.
366 For example, for holds, object_class should return 'Koha::Hold'.
367
368 =cut
369
370 sub object_class { }
371
372 sub DESTROY { }
373
374 =head1 AUTHOR
375
376 Kyle M Hall <kyle@bywatersolutions.com>
377
378 =cut
379
380 1;