]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Cat/Utils.pm
7ad231e3cda25c0d4807c64ca654e2bdb583cd9f
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Cat / Utils.pm
1 package OpenILS::Application::Cat::Utils;
2 use strict; use warnings;
3 use OpenILS::Application::AppUtils;
4 use OpenILS::Utils::Fieldmapper;
5 use XML::LibXML;
6 use XML::LibXSLT;
7 use OpenSRF::Utils::SettingsParser;
8 use OpenILS::Utils::FlatXML;
9 use OpenILS::Utils::ModsParser;
10
11 my $mods_utils = OpenILS::Utils::ModsParser->new();
12
13
14 my $parser              = XML::LibXML->new();
15 my $xslt                        = XML::LibXSLT->new();
16 my $xslt_doc    = $parser->parse_file( "/pines/cvs/ILS/Open-ILS/xsl/MARC21slim2MODS.xsl" );
17 my $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
18
19 sub new {
20         my($class) = @_;
21         $class = ref($class) || $class;
22         return bless( {}, $class );
23 }
24
25
26 # ---------------------------------------------------------------------------
27 # Converts an XML nodeset into a tree
28 # This method expects a blessed Fieldmapper::biblio::record_node object 
29 sub nodeset2tree {
30         my($class, $nodeset) = @_;
31
32         for my $child (@$nodeset) {
33                 next unless ($child and defined($child->parent_node));
34                 my $parent = $nodeset->[$child->parent_node];
35                 if( ! $parent ) {
36                         warn "No Parent For " . $child->intra_doc_id() . "\n";
37                 }
38                 $parent->children([]) unless defined($parent->children); 
39                 $child->isnew(0);
40                 $child->isdeleted(0);
41                 push( @{$parent->children}, $child );
42         }
43
44         return $nodeset->[0];
45 }
46
47
48 # ---------------------------------------------------------------------------
49 # Converts a tree into an xml nodeset
50 # This method expects a blessed Fieldmapper::biblio::record_node object 
51
52 sub tree2nodeset {
53
54         my($self, $node, $newnodes) = @_;
55         return $newnodes unless $node;
56
57         if(!$newnodes) { $newnodes = []; }
58         push( @$newnodes, $node );
59
60         if( $node->children() ) {
61
62                 for my $child (@{ $node->children() }) {
63
64                         new Fieldmapper::biblio::record_node ($child);
65                         if(!defined($child->parent_node)) {
66                                 $child->parent_node($node->intra_doc_id); 
67                                 $child->ischanged(1); #just to be sure
68                         }
69                         $self->tree2nodeset( $child, $newnodes );
70                 }
71         }
72
73         $node->children([]); #we don't need them hanging around
74         return $newnodes;
75 }
76
77
78 # Removes any deleted nodes from the tree
79 sub clean_nodeset {
80
81         my($self, $nodeset) = @_;
82         my @newnodes = ();
83         for my $node (@$nodeset) {
84                 if(!$node->isdeleted() ) {
85                         push @newnodes, $node;
86                 }
87         }
88
89         return \@newnodes;
90 }
91
92
93
94
95 1;