Multi-bug fix - SetMarcUnicodeFlag for records coming from Koha
[koha.git] / C4 / OAI / DC.pm
1 #  ---------------------------------------------------------------------
2 #   Dublin Core helper class
3 #    v1.0
4 #    January 2007
5 #  ------------------+--------------------------------------------------
6 #   Ph. Jaillon      | 
7 #  ------------------+----------------------+---------------------------
8 #   Department of Computer Science          |      
9 #  -----------------------------------------+-------------+-------------
10 #   Ecole Nationale Superieure des Mines de St-Etienne    |  www.emse.fr 
11 #  -------------------------------------------------------+-------------
12
13 =head1 OAI::DC Dublin Core formating helper
14          
15 OAI::DC is an helper class for Dublin Core metadata. As Dublin Core have a well known
16 set of fields, OAI::DC is a subclass of the OAI::DP class and it implements a default
17 behavior to build correct answers. The data references returned by Archive_GetRecord
18 and Archive_ListRecords must be instance providing the following method (they are used
19 to translate your own data to Dublin Core) : Title(), Identifier(), Subject(), Creator(),
20 Date(), Description(), Publisher(), Language() and Type(). The semantic of these methods is
21 the same as the corresponding Dublin Core field.
22
23 To return correct metadata, you must provide or overide theses methods:
24
25 =over 
26
27 =over
28
29 =item B<new>: initialization step,
30
31 =item B<dispose>: clean up step,
32
33 =item B<Archive_ListSets>: return list of defined sets,
34
35 =item B<Archive_GetRecord>: return a record,
36
37 =item B<Archive_ListRecords>: return a list of records,
38
39 =item B<Archive_ListIdentifiers>: return a list of record identifiers,
40
41 =back
42
43 =back
44
45 =head2 new
46
47 =over
48
49 Object of this method is to build a new instance of your OAI data provider. At this step
50 you can overide somme default information about the repository, you can also initiate
51 connexion to a database... Parameters to the new method are user defined.
52
53 =back
54
55 =head2 dispose
56
57 =over
58
59 It's time to disconnect from database (if required). Must explicitly call SUPER::dispose().
60
61 =back
62
63 =head2 Archive_ListSets
64
65 =over
66
67 Return a reference to an array of list set. Each list set is a reference to a two element array.
68 The first element is the set name of the set and the second is its short description.
69
70         sub Archive_ListSets {
71                 [
72                         [ 'SET1', 'Description of the SET1'],
73                         [ 'SET2', 'Description of the SET2'],
74                 ];
75         }
76
77 =back
78
79 =head2 Archive_GetRecord
80
81 =over
82
83 This method take a record identifier and metadata format as parameter. It must return a reference to
84 the data associated to identifier. Data are reference to a hash and must provide methodes describe
85 at the begining of DC section.
86
87 =back
88
89 =head2 Archive_ListRecords
90
91 =over
92
93 Object of this method is to return a list of records occording to the user query. Parameters of the method
94 are the set, the from date, the until date, the metadata type required and a resumption token if supported.
95
96 The method must return a reference to a list of records, the metadata type of the answer and reference to
97 token information. Token information must be undefined or a reference to a hash with the I<completeListSize>
98 and the I<cursor> keys set.
99
100 =back
101
102 =cut
103
104 package C4::OAI::DC;
105
106 use C4::OAI::DP;
107 use vars ('@ISA');
108 @ISA = ("C4::OAI::DP");
109
110 # format DC record
111 sub FormatDC
112 {
113    my ($self, $hashref) = @_;
114
115    return undef if( $hashref->Status() eq 'deleted' );
116
117    {
118       title       => $hashref->Title(),
119       identifier  => $hashref->Identifier(),
120       subject     => $hashref->Subject(),
121       creator     => $hashref->Creator(),
122       date        => $hashref->Date(),
123       description => $hashref->Description(),
124       publisher   => $hashref->Publisher(),
125       language    => $hashref->Language(),
126       type        => $hashref->Type(),
127       mdorder     => [ qw (title creator subject description contributor publisher date type format identifier source language relation coverage rights) ]
128    };
129 }
130
131 # format header for ListIdentifiers
132 sub Archive_FormatHeader
133 {
134    my ($self, $hashref, $metadataFormat) = @_;
135    
136    $self->FormatHeader ($hashref->Identifier()->[0] ,
137                         $hashref->DateStamp(),
138                         '',
139                         $hashref->Set()
140                        );
141 }
142
143 # retrieve records from the source archive as required
144 sub Archive_FormatRecord
145 {
146    my ($self, $hashref, $metadataFormat) = @_;
147    
148    if ($self->MetadataFormatisValid ($metadataFormat) == 0)
149    {
150       $self->AddError ('cannotDisseminateFormat', 'The value of metadataPrefix ('.$metadataFormat.') is not supported by the repository');
151       return '';
152    }
153
154    my $dc = $self->FormatDC ($hashref);
155    my $header = "<oaidc:dc xmlns=\"http://purl.org/dc/elements/1.1/\" ".
156                 "xmlns:oaidc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" ".
157                 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ".
158                 "xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ ".
159                 "http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">\n";
160    my $footer = "</oaidc:dc>\n";
161    my $metadata = '';
162
163    $metadata = $header . $self->{'utility'}->FormatXML($dc) . $footer if( $dc );
164
165    $self->FormatRecord ($hashref->Identifier()->[0] ,
166                         $hashref->DateStamp(),
167                         $hashref->Status(),
168                         $hashref->Set(),
169                         $metadata,
170                         '',
171                        );
172 }
173
174
175 # get full list of mdps or list for specific identifier
176 sub Archive_ListMetadataFormats
177 {
178    my ($self, $identifier) = @_;
179    
180    if ((! defined $identifier) || ($identifier eq '')) {
181       return ['oai_dc'];
182    }
183    else {
184       $self->AddError ('idDoesNotExist', 'The value of the identifier argument is unknown or illegal in this repository');
185    }
186    return [];
187 }
188
189
190 # get full list of sets from the archive
191 sub Archive_ListSets
192 {
193         [];
194 }
195                               
196
197 # get a single record from the archive
198 sub Archive_GetRecord
199 {
200    my ($self, $identifier, $metadataFormat) = @_;
201
202    $self->AddError ('idDoesNotExist', 'The value of the identifier argument is unknown or illegal in this repository');
203    undef;
204 }
205
206 # list metadata records from the archive
207 sub Archive_ListRecords
208 {
209    my ($self, $set, $from, $until, $metadataPrefix, $resumptionToken) = @_;
210    my $tokenInfo = undef;
211
212         $self->AddError ('noRecordsMatch', 'The combination of the values of arguments results in an empty set');
213         ( [], $resumptionToken, $metadataPrefix, $tokenInfo );
214 }
215
216
217 # list identifiers (headers) from the archive
218 sub Archive_ListIdentifiers
219 {
220    my ($self, $set, $from, $until, $metadataPrefix, $resumptionToken) = @_;
221
222    if (($metadataPrefix ne '') && ($self->MetadataFormatisValid ($metadataPrefix) == 0))
223    {
224       $self->AddError ('cannotDisseminateFormat', 'The value of metadataPrefix ('.$metadataPrefix.')is not supported by the repository');
225       return '';
226    }
227    
228    $self->Archive_ListRecords ($set, $from, $until, $metadataPrefix, $resumptionToken);
229 }
230
231 1;
232