DataObj.pm 38.9 KB
Newer Older
Nigel Kukard's avatar
Nigel Kukard committed
1
# AWIT Data Object
2
# Copyright (C) 2014-2017, AllWorldIT
Nigel Kukard's avatar
Nigel Kukard committed
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
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

=encoding utf8

=head1 NAME

AWITPT::DataObj - AWITPT Database Data Object

=head1 SYNOPSIS

	#
	# Create a child class
	#
	package AWITPT::DataObj::myobject;

	use strict;
	use warnings;

33
34
35
	use AWITPT::DataObj 1.00;
	use parent, -norequire 'AWITPT::DataObj';

Nigel Kukard's avatar
Nigel Kukard committed
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
	our $VERSION = '1.000';

	# Return the configuration for this object
	sub config
	{
		my $config = {
			# Setup our data definition
			'properties' => {
				'ID' => {
					'options' => DATAOBJ_PROPERTY_ID
				},
				'Name' => {
					'validate' => { 'type' => 'text', 'length' => 2 }
				}
			}
		};
		return $config;
	}

=head1 DESCRIPTION

The AWITPT::DataObj class provides an abstraction layer between a data definition and the underlying data store, providing easy
access to data.

=cut


package AWITPT::DataObj;

use strict;
use warnings;

68
69
70
71
72
use AWITPT::Object 1.01;
use parent -norequire, 'AWITPT::Object';


our $VERSION = 3.01;
Nigel Kukard's avatar
Nigel Kukard committed
73
74
75
76
77
78
79
80

our (@EXPORT,@EXPORT_OK);
@EXPORT = qw(
	DATAOBJ_LOADONIDSET

	DATAOBJ_PROPERTY_READONLY
	DATAOBJ_PROPERTY_NOLOAD
	DATAOBJ_PROPERTY_NOSAVE
81
82
83
	DATAOBJ_PROPERTY_ID
	DATAOBJ_PROPERTY_REQUIRED
	DATAOBJ_PROPERTY_ALL
Nigel Kukard's avatar
Nigel Kukard committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

	DATAOBJ_RELATION_READONLY

	DATAOBJ_LOG_ERROR
	DATAOBJ_LOG_WARNING
	DATAOBJ_LOG_NOTICE
	DATAOBJ_LOG_INFO
	DATAOBJ_LOG_DEBUG
	DATAOBJ_LOG_DEBUG2
);
@EXPORT_OK = qw(
);

# Constant exports...
use constant {
	# Object must be loaded on property ID set
	'DATAOBJ_LOADONIDSET' => 1,

	# Property is read only
	'DATAOBJ_PROPERTY_READONLY' => 1,
	# Property is not loaded from DB
	'DATAOBJ_PROPERTY_NOLOAD' => 2,
	# Do not save this field to DB
	'DATAOBJ_PROPERTY_NOSAVE' => 4,
108
109

	# Combination of READONLY and NOSAVE
Nigel Kukard's avatar
Nigel Kukard committed
110
	'DATAOBJ_PROPERTY_ID' => 5,
111
112
113
114

	# This property must be set before doing a commit
	'DATAOBJ_PROPERTY_REQUIRED' => 8,

Nigel Kukard's avatar
Nigel Kukard committed
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
	# Match property
	'DATAOBJ_PROPERTY_ALL' => 255,

# FIXME - Needs implementing
	# Relation is read only, it will not create the sub-object
	'DATAOBJ_RELATION_READONLY' => 1,

	# Debug options
	'DATAOBJ_LOG_ERROR' => 1,
	'DATAOBJ_LOG_WARNING' => 2,
	'DATAOBJ_LOG_NOTICE' => 3,
	'DATAOBJ_LOG_INFO' => 4,
	'DATAOBJ_LOG_DEBUG' => 5,
	'DATAOBJ_LOG_DEBUG2' => 6
};

# Module debugging
our $DEBUG = 2;

use AWITPT::Util 2.00 qw(
	prettyUndef
	isUsername
	isNumber
	isVariable
	isEmail
	isBoolean
	isDomain
);
use Carp qw(longmess);
use Data::Dumper;



=head1 METHODS

150
C<AWITPT::DataObj> provides the below manipulation methods, together with those inherited from C<AWITPT::Object>.
Nigel Kukard's avatar
Nigel Kukard committed
151
152
153
154
155
156
157

=cut



=head2 new

158
	my $obj = AWITPT::DataObj::myobject->new([$options]);
Nigel Kukard's avatar
Nigel Kukard committed
159
160
161
162
163
164
165

The C<new> method is used to instantiate the object.

Data object C<options> can also be specified to customize the objects behavior.

=head3 Options

166
Each object supports options being passed as a parameter to C<new> described below...
Nigel Kukard's avatar
Nigel Kukard committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

	'options' => OPTION1 | OPTION2

Below is a list of supported options:

=over

=item *
B<DATAOBJ_LOADONIDSET>

This property will cause the object to load when a DATAOBJ_PROPERTY_ID is set.

=back


=cut

184
# The new() method is inherited from AWITPT::Object.
Nigel Kukard's avatar
Nigel Kukard committed
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



=head2 config

	# Data object configuration
	sub config
	{
		retrun {
			'properties' => {
				'OwnerID' => { }, # No parameters as its pretty much handled by the relation
				'Description' => {
					'validate' => { 'type' => 'text', 'length' => 2 },
					# 'options' => ... property options can be specified here
				}
			},
			'relations' => {
				'Owner' => {
					'class' => "DataObj::User",
					'type' => "Direct",
					'associate' => { 'OwnerID' => 'ID' },
					'options' => DATAOBJ_RELATION_READONLY
				}
			}
		}
	}

The C<config> method is used to return configuration information for the current object, it must be overridden for each object
created and must return a hashref with the object configuration.

=head3 Property Options

Each property supports options described below...

	'options' => OPTION1 | OPTION2

Below is a list of supported options:

=over

225
226
227
228
229
=item *
B<DATAOBJ_PROPERTY_ID>

This is the unique ID property of the object, only ONE of these can be specified!

Nigel Kukard's avatar
Nigel Kukard committed
230
=item *
231
B<DATAOBJ_PROPERTY_NOLOAD>
Nigel Kukard's avatar
Nigel Kukard committed
232

233
This property will not be loaded.
Nigel Kukard's avatar
Nigel Kukard committed
234
235

=item *
236
B<DATAOBJ_PROPERTY_NOSAVE>
Nigel Kukard's avatar
Nigel Kukard committed
237

238
This property is not saved.
Nigel Kukard's avatar
Nigel Kukard committed
239
240

=item *
241
242
243
244
245
246
B<DATAOBJ_PROPERTY_READONLY>

Ensure this property cannot be set using ->setXXX().

=item *
B<DATAOBJ_PROPERTY_REQUIRED>
Nigel Kukard's avatar
Nigel Kukard committed
247

248
This property must be set before using ->commit().
Nigel Kukard's avatar
Nigel Kukard committed
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

=back


=head3 Property Validation

Each property supports optional validation criteria described below...

	'validate' => { 'type' => <VALIDATE_TYPE>, <VALIDATION_OPTIONS>... }

Below is a list of supported validation types:

=over

=item *
B<text>

Validate text.  The C<length> and C<regex> options are supported.

=over

=item *
B<length>

Optional minimum length.

=item *
B<regex>

Optional regex, eg. qr ( /^ABc/ ).

=back

=item *
B<username>

Validate username, additional parameters in C<params> can be passed for validation.

=over

=item *
B<params> (arrayref)

See L<AWITPT::Util> for options for C<isUsername>.

=back

=item *
B<email>

Validate an email address.

=item *
B<boolean>

Validate boolean.

=item *
B<domain>

Validate domain.

=item *
B<number>

Validate number, additional validation options can be specified using the C<params> option.

=over

=item *
B<params> (arrayref)

See L<AWITPT::Util> for options for C<isNumber>.

=back

=item *
B<regex>

Validate against a regex. The C<regex> option must be specified with a qr(/..../) regex.

=item *
B<relation>

Validate using the related object. This calls the related objects validate() method.

=item *
B<load>

Validate by attempting to load the property, this calls the load() method on the current object.

=back

=head3 Relation Options

Each relation defined supports a number of options described below...

	'options' => OPTION1 | OPTION2

Below is a list of supported options:

=over

=item *
B<DATAOBJ_RELATION_READONLY>

Nigel Kukard's avatar
Nigel Kukard committed
355
The child object will not be created if it does not exist. This only pertains to the 'Direct' relation.
Nigel Kukard's avatar
Nigel Kukard committed
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
427
428
429
430
431
432
433
434
435
436

=back


=cut

# Blank config method, this needs to be overridden for each data object
sub config
{
	my $self = shift;


	$self->_log(DATAOBJ_LOG_ERROR,"The 'records' method needs to be implemented");

	return;
}



# Autoload function to automagically handle some common things
our $AUTOLOAD;
sub AUTOLOAD
{
	my ($self,@params) = @_;
	my $function = $AUTOLOAD;


	# Don't mess with garbage collection
	return if ($function eq "DESTROY");

	# Cleanup name so we get the unqualified name
	$function =~ s/.*:://;

	$self->_log(DATAOBJ_LOG_DEBUG,"Function '$function'");


=head2 get[A-Z][A-Za-z0-9]+

	$dataObj->getName();

The C<get[A-Z][A-Za-z0-9]+> method returns the value of a the property matching the regex.

=cut

	# get[A-Z]*()
	if ($function =~ /^get([A-Z][A-Za-z0-9]+)/) {
		my $propertyName = $1;

		return $self->get($propertyName,@params);


=head2 set[A-Z][A-Za-z0-9]+

	$dataObj->setName("Joe Soap");

The C<set[A-Z][A-Za-z0-9]+> method sets the value of a the property matching the regex.

The same special cases apply as with C<set>.

=cut

	# set*()
	} elsif ($function =~ /^set([A-Z][A-Za-z0-9]+)/) {
		my $propertyName = $1;

		return $self->set($propertyName,@params);


=head2 validate[A-Z][A-Za-z0-9]+

	$dataObj->validateName("Joe Soap");

The C<validate[A-Z][A-Za-z0-9]+> method validates the value for the named property.

=cut

	# validate*()
	} elsif ($function =~ /^validate([A-Z][A-Za-z0-9]+)/) {
		my $propertyName = $1;

		return $self->validate($propertyName,@params);
Nigel Kukard's avatar
Nigel Kukard committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450


=head2 relation[A-Z][A-Za-z0-9]+

	$dataObj->relationDomainNames();

The C<relation[A-Z][A-Za-z0-9]+> method returns the relation management object for the relation property.

=cut

	# relation*()
	} elsif ($function =~ /^relation([A-Z][A-Za-z0-9]+)/) {
		my $relationName = $1;

451
		return $self->relation($relationName);
Nigel Kukard's avatar
Nigel Kukard committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465


=head2 [A-Z][A-Za-z0-9]+

	$dataObj->DomainNames();

The C<[A-Z][A-Za-z0-9]+> method is shorthand to refer to a relation.

=cut

	# *()
	} elsif ($function =~ /^([A-Z][A-Za-z0-9]+)/) {
		my $relationName = $1;

466
		return $self->relation($relationName);
Nigel Kukard's avatar
Nigel Kukard committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
	}

	die "No such method: $AUTOLOAD";
}



=head2 set

	$dataObj->set("Name","Joe Soap");

The C<set> method sets the value of a property. The method returns $self;

NOTE: There is a special case where if you are setting a relation property, and that property has DATAOBJ_LOADONIDSET and
DATAOBJ_PROPERTY_ID options set, that this will cause a $obj->load() to occur. In this case the return is the same as C<load>.

=cut

# Property setter
sub set
{
	my ($self,$propertyName,$value) = @_;


	# Grab property
	my $property = $self->_propertyByName($propertyName);
	if (!defined($property)) {
		$self->_log(DATAOBJ_LOG_ERROR,"Property '$propertyName' not found");
	}

	# Check if we should insted do a load if we're have DATAOBJ_LOADONIDSET and we're an ID property
	if ($self->{'_options'} & DATAOBJ_LOADONIDSET && $property->{'options'} & DATAOBJ_PROPERTY_ID == DATAOBJ_PROPERTY_ID) {
		# As this is a object set to load when set, and set as a ID
		if (!defined($self->load($property->{'name'} => $value))) {
			return;
		}

	# Check its not a protected property
	} elsif ($property->{'options'} & DATAOBJ_PROPERTY_READONLY) {
		$self->_log(DATAOBJ_LOG_ERROR,"Cannot set property '%s' as its read-only",$property->{'name'});
	}

	return $self->_set($property->{'name'},$value);
}



=head2 get

	$dataObj->get("Name");

The C<get> method returns the value of a data object property.

=cut

# Get property value
sub get
{
	my ($self,$propertyName) = @_;


	# Grab property
	my $property = $self->_propertyByName($propertyName);
	if (!defined($property)) {
		$self->_log(DATAOBJ_LOG_ERROR,"Property '%s' not found",$property->{'name'});
	}

	return $self->_get($property->{'name'});
}



=head2 validate

	$dataObj->validate("Name",$value,...);

The C<validate> method returns the value of a data object property. The method returns the validated value on success, undef on
failure.

The optional parameters are type specific. Currently types 'username' and 'number' accept additional parameters which match up to
the C<isUsername> and C<isNumber> L<AWITPT::Util> functions.

=cut

# Validate property value
sub validate
{
	my ($self,$propertyName,$value,@params) = @_;


	# Grab property
	my $property = $self->_propertyByName($propertyName);
	if (!defined($property)) {
		$self->_log(DATAOBJ_LOG_ERROR,"Property '$propertyName' not found");
	}

Nigel Kukard's avatar
Nigel Kukard committed
563
	# Grab validation type
Nigel Kukard's avatar
Nigel Kukard committed
564
565
566
567
568
569
570
571
572
573
574
575
	my $validateType;
	if (defined($property->{'validate'})) {
		$validateType = $property->{'validate'}->{'type'};
	} else {
		# If we have relations, use the relation as validation by default
		if (defined($self->_relationNamesFromProperty($property))) {
			$validateType = 'relation';
		} else {
			return $value;
		}
	}

576
577
578
579
580
581
582
583
	# Grab validation parameters, and allow overriding
	my @validateParams;
	if (@params > 0) {
		@validateParams = @params;
	} elsif (defined($property->{'validate'}->{'params'})) {
		@validateParams = @{$property->{'validate'}->{'params'}};
	}

Nigel Kukard's avatar
Nigel Kukard committed
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
	# Check this is a normal variable
	if (!defined(isVariable($value))) {
		$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed variable",$property->{'name'});
		$self->_error("Validation of '".$property->{'name'}."' failed");
		return;
	}

	# Grab validate option
	my $validated = 0;


	# Validate 'regex' type
	if ($validateType eq "regex") {
		# Validate against a regex
		my $regex = $property->{'validate'}->{'regex'};
		if (!($value =~ $regex)) {
			$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed regex =~",$property->{'name'});
			$self->_error("Validation of '".$property->{'name'}."' failed");
			return;
		}
		# Validate length
		if (defined(my $length = $property->{'validate'}->{'length'})) {
			if (length($value) < $length) {
				$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed regex length",$property->{'name'});
				$self->_error("Validation of '".$property->{'name'}."' length failed");
				return;
			}
		}

		return $value;

	# Validate 'text' type
	} elsif ($validateType eq "text") {
		# Make sure this is text
		if (!($value =~ /^[[:print:]]*$/)) {
			$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed text printable characters",$property->{'name'});
			$self->_error("Validation of '".$property->{'name'}."' failed");
			return;
		}

		# Check if we have a regex to also match against
		if (defined(my $regex = $property->{'validate'}->{'regex'})) {
			# Validate against a regex
			if (!($value =~ $regex)) {
				$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed text regex =~",$property->{'name'});
				$self->_error("Validation of '".$property->{'name'}."' failed");
				return;
			}
		}

		# Validate length
		if (defined(my $length = $property->{'validate'}->{'length'})) {
			if (length($value) < $length) {
				$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed length",$property->{'name'});
				$self->_error("Validation of '".$property->{'name'}."' length failed");
				return;
			}
		}

		return $value;

	# Validate 'email' type
	} elsif ($validateType eq "email") {
		# Validate email address
648
		if (!isEmail($value,@validateParams)) {
Nigel Kukard's avatar
Nigel Kukard committed
649
650
651
652
653
654
655
656
657
658
			$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed email",$property->{'name'});
			$self->_error("Validation of '".$property->{'name'}."' failed");
			return;
		}

		return $value;

	# Validate 'username' type
	} elsif ($validateType eq "username") {
		# Validate username
659
		if (!isUsername($value,@validateParams)) {
Nigel Kukard's avatar
Nigel Kukard committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
			$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed username",$property->{'name'});
			$self->_error("Validation of '".$property->{'name'}."' failed");
			return;
		}

		# Validate length
		if (defined(my $length = $property->{'validate'}->{'length'})) {
			if (length($value) < $length) {
				$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed username length",$property->{'name'});
				$self->_error("Validation of '".$property->{'name'}."' length failed");
				return;
			}
		}

		return $value;

	# Validate 'boolean' type
	} elsif ($validateType eq "boolean") {
		# Validate boolean
		if (!defined($value = isBoolean($value))) {
			$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed boolean",$property->{'name'});
			$self->_error("Validation of '".$property->{'name'}."' failed");
			return;
		}

		return $value;

	# Validate 'domain' type
	} elsif ($validateType eq "domain") {
		# Validate domain
		if (!defined(isDomain($value))) {
			$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed domain",$property->{'name'});
			$self->_error("Validation of '".$property->{'name'}."' failed");
			return;
		}

		return $value;

	# Validate 'number' type
	} elsif ($validateType eq "number") {
		# Validate username
701
		if (!defined($value = isNumber($value,@validateParams))) {
Nigel Kukard's avatar
Nigel Kukard committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
			$self->_log(DATAOBJ_LOG_INFO,"Validation of '%s' failed number",$property->{'name'});
			$self->_error("Validation of '".$property->{'name'}."' failed");
			return;
		}

		return $value;

	# Validate 'relation' type
	} elsif ($validateType eq "relation") {

		# Loop with relations and validate
		my $mismatch = 0;
		foreach my $relationName ($self->_relationNamesFromProperty($property)) {
			my $relationPropertyName = $self->_relationPropertyName($property,$relationName);

			# Grab validated value and compare
			my $validateMethod = "validate$relationPropertyName";
719
			my $validatedValue = $self->_relation($relationName)->$validateMethod($value,@validateParams);
Nigel Kukard's avatar
Nigel Kukard committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
			if (!defined($validatedValue) || $validatedValue ne $value) {
				$mismatch = 1;
				last;
			}
		}

		# If nothing mismatched, return the value
		if (!$mismatch) {
			return $value;
		}

	# Validate 'load' type
	} elsif ($validateType eq "load") {
		# Load item from DB
		if (my $obj = $self->clone()->reset()->load($property->{'name'} => $value)) {
			my $methodName = "get".$property->{'name'};
			# If we didn't load anything, the method will return undef
			return $obj->$methodName();
		}

	# Fallthrough
	} else {
		$self->_log(DATAOBJ_LOG_ERROR,"Property '".$property->{'name'}."' has invalid validation type");
	}

	return;
}



Nigel Kukard's avatar
Nigel Kukard committed
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
=head2 relation

	$dataObj->relation("DomainNames");

The C<relation> method returns the relation management object for the relation property.

=cut

# Return relation object
sub relation
{
	my ($self,$relationName) = @_;


	# Grab relation object
	my $relation = $self->_relation($relationName);
	if (!defined($relation)) {
		$self->_log(DATAOBJ_LOG_ERROR,"Relation '%s' not found",$relationName);
	}

770
	return $relation;
Nigel Kukard's avatar
Nigel Kukard committed
771
772
773
774
}



Nigel Kukard's avatar
Nigel Kukard committed
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
=head2 changed

	my %changed = $dataObj->changed();

The C<changed> method returns a hash containing the properties and values of each item which was changed since the last C<load> or
C<commit>

=cut

# Return changed data
sub changed
{
	my $self = shift;


	return \%{ $self->{'_data.changed'} };
}



=head2 asHash

	my %data = $dataObj->asHash();

The C<asHash> method returns a hash containing the objects current public properties.

=cut

# Return public data for this object
sub asHash
{
	my $self = shift;


	# Build up reply
	my %data;
811
	foreach my $property ($self->_properties()) {
Nigel Kukard's avatar
Nigel Kukard committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
		# We allow retrieval of data if the get method has been overridden
		my $method = "get$property";
		$data{$property} = $self->$method($property);
	}

	return \%data;
}



=head2 recordsAsHash

	my @records = $dataObj->recordsAsHash();

The C<recordsAsHash> method returns an array of records, each record is a hashref.

=cut

# Get records as a hash
sub recordsAsHash
{
	my ($self,@params) = @_;


	# Grab records
	my $records = $self->records(@params);

	# Build result set
	my @res;
	foreach my $record (@{$records}) {
		push(@res,$record->asHash());
	}

	return \@res;
}



=head2 recordsAsFriendlyList

	my @records = $dataObj->recordsAsFriendlyList();

The C<recordsAsFriendlyList> method returns an array of arrayrefs, each containing arrayref has the first index as the friendly
name and the second as the record ID.

The result looks like this...

	@records = (
		[ "friendly name 1", 1 ],
		[ "freindly 2" , 2 ]
	);

=cut

# Get records as a friendly list
sub recordsAsFriendlyList
{
	my ($self,@params) = @_;


	# Abort if we don't have a friendlyName() method
	if (!$self->can('friendlyName')) {
		die "Using recordsAsFriendlyList() requires the friendlyName() method to be defined";
	}

	# Grab records
	my $records = $self->records(@params);

	# Build result set
	my @res;
	foreach my $record (@{$records}) {
		push(@res,[ $record->friendlyName() => $record->getID() ]);
	}

	return \@res;
}



=head2 records

	my @records = $dataObj->records();

The C<records> method returns an array of data object records.

NOTE: This method must be implemented by child classes.

=cut

# Get records as an array of objects
sub records
{
	my $self = shift;


	$self->_log(DATAOBJ_LOG_ERROR,"The 'records' method needs to be implemented");

	return;
}



=head2 load

	$dataObj->load($id);

	$dataObj->load('Name' => 'Joe Soap');

The C<load> method is used to load a single record from the database. It has 2 forms of invocation, either by specifying one
parameter which is assumed to be the value of the ID field, or by specifying a hash of key-value pairs.

Only the first matching record is returned, if multiple records exist the result can be any one of them being returned.

NOTE: This method must be implemented by child classes.

=cut

# Load Record
# - The load defaults to loading on ID, but a hash can be provided to load on various matches
sub load
{
	my ($self,@params) = @_;


	$self->_log(DATAOBJ_LOG_ERROR,"The 'load' method needs to be implemented");

	return;
}



=head2 loadHash

945
	$dataObj->loadHash($hashref);
Nigel Kukard's avatar
Nigel Kukard committed
946

947
948
949
950
	$dataObj->loadHash('a' => 1, 'b' => 2);

The C<loadHash> method loads properties from a hash or hashref. If the data was previously changed the changed flag is reset and
the loaded flag is applied to the property.
Nigel Kukard's avatar
Nigel Kukard committed
951
952
953

=cut

954
# Load record data
Nigel Kukard's avatar
Nigel Kukard committed
955
956
957
# NOTE: The _loadHash function uses _set()
sub loadHash
{
958
	my ($self,@data) = @_;
Nigel Kukard's avatar
Nigel Kukard committed
959
960
961


	# Blank data if there is no data
962
963
964
965
966
967
968
969
970
971
972
973
	if (!@data) {
		$self->_log(DATAOBJ_LOG_DEBUG,"No properties to load");
		return $self;
	}

	# If we have an odd number of elements, treat it as a hashref
	my %properties;
	if (@data % 2) {
		my $hashref = shift(@data);
		%properties = %{$hashref};
	} else {
		%properties = @data;
Nigel Kukard's avatar
Nigel Kukard committed
974
975
976
	}

	# Set properties
977
978
	foreach my $item (keys %properties) {
		$self->set($item,$properties{$item});
Nigel Kukard's avatar
Nigel Kukard committed
979
980
	}

981
	$self->_log(DATAOBJ_LOG_DEBUG,"Loaded %s properties from hash", scalar keys %properties);
Nigel Kukard's avatar
Nigel Kukard committed
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

	return $self;
}



=head2 dataLoaded

	$dataObj->dataLoaded();

The C<dataLoaded> method returns the properties previously loaded with C<load>.

=cut

# Return the property names previously loaded by the load() method
sub dataLoaded
{
	my ($self,$data) = @_;

For faster browsing, not all history is shown. View entire blame