Commits

Toby Inkster committed 686924b

remove tarballs from repo; misc other stuff

Comments (0)

Files changed (15)

 password=perl
 
 [webservice]
-endpoint=http://acton-8.nct.org.uk:8022/CAREServices/NDataAccess.asmx
+endpoint=http://192.168.100.108:8022/CAREServices/NDataAccess.asmx
 database=NCT_live
 username=guest
 password=guest
-package CRM::CARE;
-
-use 5.008;
-use common::sense;
-use Moose;
+use 5.010;
+use MooseX::Declare;
 
 use CRM::CARE::Address;
 use CRM::CARE::Contact;
+use CRM::CARE::ContactCategory;
+use CRM::CARE::ContactSuppression;
 
-use namespace::clean -except => 'meta';
+class CRM::CARE
+	with CRM::CARE::Trait::Configurable
+	with CRM::CARE::Trait::Queryable
+	with CRM::CARE::Trait::WebServices
+{
+	has 'webservice_endpoint' => (is => 'rw', isa => 'Str', required => 1);
+	has 'webservice_database' => (is => 'rw', isa => 'Str', required => 1);
+	has 'webservice_username' => (is => 'rw', isa => 'Str', required => 1);
+	has 'webservice_password' => (is => 'rw', isa => 'Str', required => 1);
 
-our $VERSION = '0.001';
+	has 'sql_database' => (is => 'rw', isa => 'Str', required => 1);
+	has 'sql_username' => (is => 'rw', isa => 'Str', required => 1);
+	has 'sql_password' => (is => 'rw', isa => 'Str', required => 1);
+	has 'sql_host' => (is => 'rw', isa => 'Str', required => 1);
+	has 'sql_port' => (is => 'rw', isa => 'Int', default => 1433);
 
-has 'webservice_endpoint' => (is => 'rw', isa => 'Str', required => 1);
-has 'webservice_database' => (is => 'rw', isa => 'Str', required => 1);
-has 'webservice_username' => (is => 'rw', isa => 'Str', required => 1);
-has 'webservice_password' => (is => 'rw', isa => 'Str', required => 1);
-
-has 'sql_database' => (is => 'rw', isa => 'Str', required => 1);
-has 'sql_username' => (is => 'rw', isa => 'Str', required => 1);
-has 'sql_password' => (is => 'rw', isa => 'Str', required => 1);
-has 'sql_host' => (is => 'rw', isa => 'Str', required => 1);
-has 'sql_port' => (is => 'rw', isa => 'Int', default => 1433);
-
-with 'CRM::CARE::Trait::Configurable';
-with 'CRM::CARE::Trait::Queryable';
-with 'CRM::CARE::Trait::WebServices';
-
-sub configfile
-{
-	my ($class) = @_;
-	my $name = ref $class ? lc ref $class : lc $class;
-	$name =~ s/::/-/g;
-	$name .= '.ini';
-	
-	return (
-		$name,
-		sprintf("/etc/%s", $name),
-		sprintf("C:\\Windows\\%s", $name),
-		);
+	method configfile (Str $class:)
+	{
+		my $name = ref $class ? lc ref $class : lc $class;
+		$name =~ s/::/-/g;
+		$name .= '.ini';
+		
+		return (
+			$name,
+			sprintf("/etc/%s", $name),
+			sprintf("C:\\Windows\\%s", $name),
+			);
+	}
 }
-
-__PACKAGE__->meta->make_immutable;
 1;
 
 __END__

lib/CRM/CARE/Address.pm

-package CRM::CARE::Address;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose;
+class CRM::CARE::Address
+	is mutable
+	with CRM::CARE::Trait::WithDefaultConnection
+	with CRM::CARE::Trait::Amendable
+	with CRM::CARE::Trait::Validable
+	with CRM::CARE::Trait::Updater
+{
+	has 'AddressNumber'  => (is => 'ro', isa => 'Int', required => 1);
+	has 'ContactNumber'  => (is => 'rw', isa => 'Int', required => 1);
 
-use namespace::clean -except => 'meta';
-
-our $VERSION = '0.001';
-
-has 'AddressNumber'  => (is => 'ro', isa => 'Int', required => 1);
-has 'ContactNumber'  => (is => 'rw', isa => 'Int', required => 1);
-
-has [qw{
-	HouseName
-	Address
-	Town
-	County
-	Postcode
-	CountryCode
-	CountryDesc
-	SortCode
-	PafStatus
-	Branch
-	}] => (is => 'rw', isa => 'Str');
-
-with 'CRM::CARE::Trait::WithDefaultConnection';
-with 'CRM::CARE::Trait::Amendable';
-with 'CRM::CARE::Trait::Validable';
-with 'CRM::CARE::Trait::Updater';
-
-sub _update_method
-{
-	return UpdateAddress => qw(
+	has [qw{
 		HouseName
 		Address
 		Town
 		County
 		Postcode
+		CountryCode
+		CountryDesc
+		SortCode
 		PafStatus
 		Branch
-		ValidFrom
-		ValidTo
-		),
-		[CountryCode => 'Country'];
+		}] => (is => 'rw', isa => 'Maybe[Str]');
+
+	method _update_method ()
+	{
+		return UpdateAddress => qw(
+			HouseName
+			Address
+			Town
+			County
+			Postcode
+			PafStatus
+			Branch
+			ValidFrom
+			ValidTo
+			),
+			[CountryCode => 'Country'];
+	}
+
 }
-
 1;

lib/CRM/CARE/Contact.pm

 use MooseX::Declare;
 
 class CRM::CARE::Contact
+	is mutable
 	with CRM::CARE::Trait::WithDefaultConnection
 	with CRM::CARE::Trait::Loader
 	with CRM::CARE::Trait::Updater
 	use CRM::CARE::TypeLibrary ':all';
 
 	has 'ContactNumber'  => (is => 'ro', isa => 'Int', required => 1);
-	has 'AddressNumber'  => (is => 'rw', isa => 'Int');
+	has 'AddressNumber'  => (is => 'rw', isa => 'Maybe[Int]');
 
 	has [qw{
 		Title
 		PrincipalUserReason
 		VatCategory
 		VatNumber
-		}] => (is => 'rw', isa => 'Str');
+		}] => (is => 'rw', isa => 'Maybe[Str]');
 
 	has [qw{
 		SourceDate
 		StatusDate
-		}] => (is => 'rw', isa => 'CareDate', coerce => 1);
+		}] => (is => 'rw', isa => 'Maybe[CareDate]', coerce => 1);
 
-	method _retrieve_method ()
-		{ return FindContacts => qw(ContactNumber); }
+	sub _retrieve_method
+	{
+		return FindContacts => qw(ContactNumber);
+	}
 		
-	method _update_method ()
+	sub _update_method
 	{
 		return UpdateContact => qw(
 			ContactNumber
 
 		if (my $bless = delete $args{bless})
 		{
+			my %extra;
+			%extra = %{ delete $args{constants} }
+				if defined $args{constants};
 			return map
-				{ $bless->new(%$_) }
+				{ $bless->new(%$_, %extra) }
 				$care->SelectContactData({pSelectDataType => $type}, %args);
 		}
 		
 
 	method addresses ()
 	{
-		return $self->_SelectContactData('xcdtContactAddresses', bless=>'CRM::CARE::Address');
+		return $self->_SelectContactData('xcdtContactAddresses',
+			bless     => 'CRM::CARE::Address',
+			constants => {ContactNumber => $self->ContactNumber});
+	}
+
+	method default_address ()
+	{
+		my ($default) = grep
+			{ $self->AddressNumber == $_->AddressNumber }
+			$self->addressses;
+		return $default;
 	}
 
 	method activities ()
 	{
-		return $self->_SelectContactData('xcdtContactCategories');
+		return $self->_SelectContactData('xcdtContactCategories',
+			bless     => 'CRM::CARE::ContactCategory',
+			constants => {ContactNumber => $self->ContactNumber});
+	}
+	
+	method activity ($code)
+	{
+		return grep
+			{ $_->ActivityCode eq $code }
+			$self->activities;
 	}
 
 	method suppressions ()
 	{
-		return $self->_SelectContactData('xcdtContactSuppressions');
+		return $self->_SelectContactData('xcdtContactSuppressions',
+			bless     => 'CRM::CARE::ContactSuppression',
+			constants => {ContactNumber => $self->ContactNumber});
 	}
-	
+
+	method suppression ($code)
+	{
+		return grep
+			{ $_->SuppressionCode eq $code }
+			$self->suppressions;
+	}
+
 };
 1;
 
 
 =item * C<information>
 
+A hashref of contact information. Mostly just the simple stuff stored in the "contacts" databse table.
+
 =item * C<addresses>
 
+Returns a list of addresses. Each item is a L<CRM::CARE::Addresss>.
+
+=item * C<default_address>
+
+Returns a just the contact's default address, as a L<CRM::CARE::Addresss>.
+
 =item * C<activities>
 
+Returns a list of activities. Each item is a L<CRM::CARE::ContactActivity>.
+
+=item * C<< activity($code) >>
+
+Returns a list of activities with given activity code. Each item is a L<CRM::CARE::ContactActivity>.
+
 =item * C<suppressions>
 
+Returns a list of suppressions. Each item is a L<CRM::CARE::ContactSuppression>.
+
+=item * C<< suppression($code) >>
+
+Returns a list of suppressions with given suppression code. Each item is a L<CRM::CARE::ContactSuppression>.
+
 =back
 
 =head1 SEE ALSO

lib/CRM/CARE/ContactCategory.pm

-package CRM::CARE::ContactCategory;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose;
+class CRM::CARE::ContactCategory
+	is mutable
+	with CRM::CARE::Trait::WithDefaultConnection
+	with CRM::CARE::Trait::Amendable
+	with CRM::CARE::Trait::Validable
+{
+	use CRM::CARE::TypeLibrary ':all';
 
-use namespace::clean -except => 'meta';
+	has 'ContactNumber'     => (is => 'ro', isa => 'Int', required => 1);
+	has 'ActivityCode'      => (is => 'rw', isa => 'Str', required => 1);
+	has 'ActivityValueCode' => (is => 'rw', isa => 'Str', required => 1);
+	has 'Quantity'          => (is => 'rw', isa => 'Maybe[Num]');
+	has 'ActivityDate'      => (is => 'rw', isa => 'Maybe[CareDate]', coerce => 1);
 
-our $VERSION = '0.001';
-
-use CRM::CARE::TypeLibrary ':all';
-
-has 'ContactNumber'     => (is => 'ro', isa => 'Int', required => 1);
-has 'ActivityCode'      => (is => 'rw', isa => 'Str', required => 1);
-has 'ActivityValueCode' => (is => 'rw', isa => 'Str', required => 1);
-has 'Quantity'          => (is => 'rw', isa => 'Int');
-has 'ActivityDate'      => (is => 'rw', isa => 'CareDate', coerce => 1);
-
-has [qw{
-	ActivityDesc
-	ActivityValueDesc
-	SourceCode
-	SourceDesc
-	Notes
-	}] => (is => 'rw', isa => 'Str');
-
-with 'CRM::CARE::Trait::WithDefaultConnection';
-with 'CRM::CARE::Trait::Amendable';
-with 'CRM::CARE::Trait::Validable';
+	has [qw{
+		ActivityDesc
+		ActivityValueDesc
+		SourceCode
+		SourceDesc
+		Notes
+		}] => (is => 'rw', isa => 'Maybe[Str]');
+}
 
 1;

lib/CRM/CARE/ContactSuppression.pm

-package CRM::CARE::ContactSuppression;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose;
+class CRM::CARE::ContactSuppression
+	is mutable
+	with CRM::CARE::Trait::WithDefaultConnection
+	with CRM::CARE::Trait::Amendable
+	with CRM::CARE::Trait::Validable
+{
+	has 'ContactNumber'     => (is => 'ro', isa => 'Int', required => 1);
+	has 'SuppressionCode'   => (is => 'rw', isa => 'Str', required => 1);
 
-use namespace::clean -except => 'meta';
-
-our $VERSION = '0.001';
-
-has 'ContactNumber'     => (is => 'ro', isa => 'Int', required => 1);
-has 'SuppressionCode'   => (is => 'rw', isa => 'Str', required => 1);
-
-has [qw{
-	SuppressionDesc
-	SuppressionInformation
-	Notes
-	}] => (is => 'rw', isa => 'Str');
-
-with 'CRM::CARE::Trait::WithDefaultConnection';
-with 'CRM::CARE::Trait::Amendable';
-with 'CRM::CARE::Trait::Validable';
+	has [qw{
+		SuppressionDesc
+		SuppressionInformation
+		Notes
+		}] => (is => 'rw', isa => 'Maybe[Str]');
+}
 
 1;

lib/CRM/CARE/Exception/WebServices.pm

-package CRM::CARE::Exception::WebServices;
+use 5.010;
+use MooseX::Declare;
 
-use 5.008;
-use common::sense;
-use Moose;
+class CRM::CARE::Exception::WebServices
+	is mutable
+	with Throwable
+	with StackTrace::Auto
+{
+	use overload '""' => \&to_string;
 
-use namespace::clean -except => 'meta';
+	has 'ErrorNumber'  => (is => 'rw', isa => 'Int', required => 1);
+	has 'ErrorMessage' => (is => 'rw', isa => 'Str', required => 1);
+	has 'Method'       => (is => 'rw', isa => 'Str');
+	has 'Module'       => (is => 'rw', isa => 'Str');
+	has 'Source'       => (is => 'rw', isa => 'Str');
 
-our $VERSION = '0.001';
-
-use overload '""' => \&to_string;
-
-has 'ErrorNumber'  => (is => 'rw', isa => 'Int', required => 1);
-has 'ErrorMessage' => (is => 'rw', isa => 'Str', required => 1);
-has 'Method'       => (is => 'rw', isa => 'Str');
-has 'Module'       => (is => 'rw', isa => 'Str');
-has 'Source'       => (is => 'rw', isa => 'Str');
-
-with 'Throwable';
-with 'StackTrace::Auto';
-
-sub to_string
-{
-	my ($self) = @_;
-	return sprintf("Error %d: %s\n", $self->ErrorNumber, $self->ErrorMessage);
+	method to_string ()
+	{
+		return sprintf("Error %d: %s\n", $self->ErrorNumber, $self->ErrorMessage);
+	}
 }
-
-1;
+1;

lib/CRM/CARE/Trait/Amendable.pm

 {
 	use CRM::CARE::TypeLibrary ':all';
 
-	has 'AmendedBy'  => (is => 'rw', isa => 'Str');
-	has 'AmendedOn'  => (is => 'rw', isa => 'CareDate', coerce => 1);
+	has 'AmendedBy'  => (is => 'rw', isa => 'Maybe[Str]');
+	has 'AmendedOn'  => (is => 'rw', isa => 'Maybe[CareDate]', coerce => 1);
 };
 1;
 

lib/CRM/CARE/Trait/Loader.pm

 	with CRM::CARE::Trait::WithDefaultConnection
 {
 	requires '_retrieve_method';
-
-	method retrieve ($class: @x)
+	
+	sub retrieve
 	{
+		my ($class, @x) = @_;
 		my ($care,  @params) = $class->process_method_arguments(@x);
 		
 		my %s_args;
 		return unless @results;
 		return wantarray ? @results : $results[0];
 	}
+
+	method refresh ()
+	{
+		my $class  = ref $self;
+		
+		my @fields = $class->_retrieve_method;
+		shift @fields;
+		
+		my @values = map { $self->$_ } @fields;
+		my $clone  = $class->retrieve(@values);
+		
+		#foreach my $attr ($self->meta->get_all_attributes)
+		#{
+		#	next unless defined $attr->has_write_method;
+		#	say " --> SETTING ", $attr->name;
+		#	$attr->set_value($self, $attr->get_value($clone))
+		#}
+		
+		# The following violates encapulation, but it seems the
+		# only way of doing things.
+		foreach my $key (keys %$self)
+		{
+			delete $self->{$key};
+		}
+		while (my ($key, $value) = each %$clone)
+		{
+			$self->{$key} = $value;
+		}
+	}
 };
 1;
 

lib/CRM/CARE/Trait/Updater.pm

 			}
 		}
 		
-		return $care->call_service($_save_method, %s_args);
+		$care->call_service($_save_method, %s_args);
+		$self->refresh if $self->DOES('CRM::CARE::Trait::Loader');
+		return $self;
 	}
 
 };

lib/CRM/CARE/Trait/Validable.pm

 	use DateTime;
 	use CRM::CARE::TypeLibrary ':all';
 
-	has 'ValidFrom'  => (is => 'rw', isa => 'CareDate', coerce => 1);
-	has 'ValidTo'    => (is => 'rw', isa => 'CareDate', coerce => 1);
+	has 'ValidFrom'  => (is => 'rw', isa => 'Maybe[CareDate]', coerce => 1);
+	has 'ValidTo'    => (is => 'rw', isa => 'Maybe[CareDate]', coerce => 1);
 
 	method is_current (DateTime $when)
 	{

lib/CRM/CARE/Trait/WebServices.pm

+# MooseX::Declare doesn't seem to work well with AUTOLOAD
+
 package CRM::CARE::Trait::WebServices;
 
-use 5.008;
-use common::sense;
+use 5.010;
 use Moose::Role;
 
 use CRM::CARE::Exception::WebServices;

lib/CRM/CARE/Trait/WithDefaultConnection.pm

 {
 	our $default;
 
-	method set_default_connection ($conn)
+	sub set_default_connection
 	{
-		$default = $conn;
+		$default = shift;
 	}
 
-	method get_default_connection ()
+	sub get_default_connection ()
 	{
 		unless ($default)
 		{
 		return $default;
 	}
 
-	method process_method_arguments (@args)
+	sub process_method_arguments
 	{
+		my ($self, @args) = @_;
+		
 		if (blessed($args[0]) and $args[0]->isa('CRM::CARE'))
 		{
 			return @args;
 use Data::Dumper;
 
 my $care = CRM::CARE->new_from_config;
-$care->database;
-
-print Dumper( $care->FindOrganisations(500_000) );
+my $org  = $care->FindOrganisations(500_000);
+print Dumper($org);
 use Try::Tiny;
 
 my $me = CRM::CARE::Contact->retrieve(500_002);
-$me->information;
-print Dumper( $me );
-#$me->Salutation('Darling Toby');
-#$me->update;
-#print Dumper( CRM::CARE::Contact->retrieve(500_002) );
+
+# print Dumper( $me, [$me->addresses], [$me->activities], [$me->suppressions] );
+
+print Dumper($me);
+$me->Salutation('Dear Toby');
+$me->update;
+print Dumper($me);
+$me->Salutation('Darling Toby');
+$me->update;
+print Dumper($me);