1 package OpenILS::Application::Cat::Utils;
2 use strict; use warnings;
3 use OpenILS::Utils::Fieldmapper;
6 use OpenSRF::Utils::SettingsParser;
10 my $parser = XML::LibXML->new();
11 my $xslt = XML::LibXSLT->new();
12 my $xslt_doc = $parser->parse_file( "/pines/cvs/ILS/Open-ILS/xsl/MARC21slim2MODS.xsl" );
13 my $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
15 # ---------------------------------------------------------------------------
16 # Converts an XML nodeset into a tree
18 my($class, $nodeset) = @_;
20 for my $child (@$nodeset) {
21 next unless ($child and defined($child->parent_node));
22 my $parent = $nodeset->[$child->parent_node];
23 $parent->children([]) unless defined($parent->children);
24 push( @{$parent->children}, $child );
30 # ---------------------------------------------------------------------------
31 # Converts a tree into an xml nodeset
35 my($self, $node) = @_;
37 if((ref($node) eq "ARRAY")) {
38 $node = Fieldmapper::biblio::record_node->new($node);
41 return \@_nodelist unless $node;
43 if(!defined($node->parent_node)) {
47 push( @_nodelist, $node );
49 if( $node->children() ) {
51 for my $child (@{ $node->children() }) {
54 Fieldmapper::biblio::record_node->new($child);
56 if(!defined($child->parent_node)) {
57 $child->parent_node($node->intra_doc_id);
58 $child->ischanged(1); #just to be sure
61 $self->tree2nodeset( $child );
65 $node->children(undef);
69 # ---------------------------------------------------------------------------
70 # Walks a nodeset and checks for insert, update, and delete and makes
71 # appropriate db calls
74 my($self, $nodeset) = @_;
79 for my $index (0..$size) {
81 my $pos = $index + $offset;
82 my $node = $nodeset->[$index];
85 if($node->isdeleted()) {
87 return 0 unless _deletenode($node);
92 $node->intra_doc_id($pos);
93 return 0 unless _addnode($node);
97 if( ($node->intra_doc_id()
98 and $node->intra_doc_id() != $pos) ||
99 $node->ischanged() ) {
101 $node->intra_doc_id($pos);
102 return 0 unless _updatenode($node);
109 # send deletes, updates, then adds
126 # ---------------------------------------------------------------------------
129 sub marcxml_doc_to_perl {
130 my( $self, $marcxml_doc ) = @_;
131 return undef unless $marcxml_doc;
132 return OpenSRF::Utils::SettingsParser::XML2perl( $marcxml_doc->documentElement );
136 sub marcxml_doc_to_mods_perl {
137 my( $self, $marcxml_doc ) = @_;
139 print "DOC:\n" . $marcxml_doc->toString(1) . "\n";
140 my $mods = $mods_sheet->transform($marcxml_doc);
141 print "MODS:\n " . $mods->toString(1). "\n";
142 print "-------------------------------------------\n";
144 my $perl = OpenSRF::Utils::SettingsParser::XML2perl( $mods->documentElement );
145 return $perl->{mods} if $perl;
149 sub marcxml_nodeset_to_doc {
150 my( $self, $nodes ) = @_;
151 my $u = OpenILS::Utils::FlatXML->new();
152 return $u->nodeset_to_xml( $nodes );
155 sub marcxml_doc_to_mods_nodeset {
156 my( $self, $marcxml_doc ) = @_;
157 my $mods = $mods_sheet->transform($marcxml_doc);
158 my $u = OpenILS::Utils::FlatXML->new();
159 my $nodeset = $u->xmldoc_to_nodeset( $mods );
160 return $nodeset->nodeset if $nodeset;