| 1 | package PICA::SOAPServer; | ||
| 2 | { | ||
| 3 | $PICA::SOAPServer::VERSION = '0.585'; | ||
| 4 | } | ||
| 5 | #ABSTRACT: Provide a SOAP interface to a L<PICA::Store> | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use SOAP::Lite; | ||
| 10 | use PICA::Record; | ||
| 11 | |||
| 12 | our @ISA = qw(Exporter SOAP::Server::Parameters); | ||
| 13 | |||
| 14 | |||
| 15 | # private functions to wrap SOAP nightmare | ||
| 16 | |||
| 17 | # die with a SOAP fault | ||
| 18 | my $fault = sub { | ||
| 19 | my ($code, $string) = @_; | ||
| 20 | die SOAP::Fault->new( faultcode => $code, faultstring => $string ); | ||
| 21 | }; | ||
| 22 | |||
| 23 | # unpack a SOAP envelope with named parameters of type string | ||
| 24 | my $unpack = sub { | ||
| 25 | my ($envelope, $required, $optional) = @_; | ||
| 26 | my %result; | ||
| 27 | foreach my $name ((@$required,@$optional)) { | ||
| 28 | my $param = $envelope->dataof($name); | ||
| 29 | $result{$name} = $param->value if $param; | ||
| 30 | } | ||
| 31 | foreach my $name (@$required) { | ||
| 32 | $fault->("BADREQUEST", "Missing parameter $name") | ||
| 33 | unless defined $result{$name}; | ||
| 34 | } | ||
| 35 | |||
| 36 | return %result; | ||
| 37 | }; | ||
| 38 | |||
| 39 | # pack a SOAP response object | ||
| 40 | my $pack = sub { | ||
| 41 | my (%values) = @_; | ||
| 42 | return SOAP::Data->name( "response" => | ||
| 43 | \SOAP::Data->value( | ||
| 44 | SOAP::Data->name('dbsid' => $values{'dbsid'})->type('string'), | ||
| 45 | SOAP::Data->name('ppn' => $values{'ppn'})->type('string'), | ||
| 46 | SOAP::Data->name('record' => $values{'record'})->type('string'), | ||
| 47 | SOAP::Data->name('version' => $values{'version'})->type('string'), | ||
| 48 | SOAP::Data->name('format' => 'pp')->type('string'), | ||
| 49 | ) | ||
| 50 | ); | ||
| 51 | }; | ||
| 52 | |||
| 53 | |||
| 54 | sub new { | ||
| 55 | my ($class, $store) = @_; | ||
| 56 | my $self = bless { | ||
| 57 | store => $store | ||
| 58 | }, $class; | ||
| 59 | if (not UNIVERSAL::isa( $store, 'PICA::Store' ) ) { | ||
| 60 | $self->{error} = $store ? "$store" : 'No PICA::Store available'; | ||
| 61 | $self->{store} = undef; | ||
| 62 | } | ||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub get { | ||
| 68 | my $self = shift; | ||
| 69 | my $env = pop; | ||
| 70 | my %params = $unpack->($env, [qw(userkey password dbsid ppn)], [qw(language format)]); | ||
| 71 | $fault->(1, $self->{error}) unless $self->{store}; | ||
| 72 | |||
| 73 | my %r = $self->{store}->access( %params )->get( $params{ppn} ); | ||
| 74 | $fault->($r{errorcode}, $r{errormessage}) if defined $r{errorcode}; | ||
| 75 | |||
| 76 | return $pack->( | ||
| 77 | ppn => $r{id}, | ||
| 78 | record => $r{record}->string, | ||
| 79 | version => $r{version}, | ||
| 80 | dbsid => $params{dbsid} | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | sub create { | ||
| 86 | my $self = shift; | ||
| 87 | my %params = $unpack->(pop, [qw(userkey password dbsid record)], [qw(language format rectype)]); | ||
| 88 | $fault->(1, $self->{error}) unless $self->{store}; | ||
| 89 | |||
| 90 | my %r = $self->{store}->access( %params )->create( PICA::Record->new($params{record}) ); | ||
| 91 | $fault->($r{errorcode}, $r{errormessage}) unless defined $r{id}; | ||
| 92 | |||
| 93 | return $pack->( | ||
| 94 | ppn => $r{id}, | ||
| 95 | record => $r{record}->string, | ||
| 96 | version => $r{version}, | ||
| 97 | dbsid => $params{dbsid} | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub update { | ||
| 103 | my $self = shift; | ||
| 104 | my %params = $unpack->(pop, [qw(userkey password dbsid ppn record version)], [qw(language format)]); | ||
| 105 | $fault->(1, $self->{error}) unless $self->{store}; | ||
| 106 | |||
| 107 | my %r = $self->{store}->access( %params ) | ||
| 108 | -> update( $params{ppn}, PICA::Record->new($params{record}), $params{version} ); | ||
| 109 | $fault->($r{errorcode}, $r{errormessage}) unless defined $r{id}; | ||
| 110 | |||
| 111 | return $pack->( | ||
| 112 | ppn => $r{id}, | ||
| 113 | record => $r{record}->string, | ||
| 114 | version => $r{version}, | ||
| 115 | dbsid => $params{dbsid} | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub delete { | ||
| 121 | my $self = shift; | ||
| 122 | my %params = $unpack->(pop, [qw(userkey password dbsid ppn)], [qw(language)]); | ||
| 123 | $fault->(1, $self->{error}) unless $self->{store}; | ||
| 124 | |||
| 125 | # get the record before deleting | ||
| 126 | my %r = $self->{store}->access( %params )->get( $params{ppn} ); | ||
| 127 | $fault->($r{errorcode}, $r{errormessage}) if defined $r{errorcode}; | ||
| 128 | |||
| 129 | # actually delete it | ||
| 130 | my %r2 = $self->{store}->access( %params )->delete( $params{ppn} ); | ||
| 131 | $fault->( $r2{errorcode}, $r2{errormessage} ) unless defined $r2{id}; | ||
| 132 | |||
| 133 | return $pack->( | ||
| 134 | ppn => $r{id}, | ||
| 135 | record => $r{record}->string, | ||
| 136 | version => $r{version}, | ||
| 137 | dbsid => $params{dbsid} | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | |||
| 141 | 1; | ||
| 142 | |||
| 143 | __END__ |