]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/backend/catalog/biblio_fingerprint.js
using try-catch and some methods instead of properties -- looking good so far
[Evergreen.git] / Open-ILS / src / javascript / backend / catalog / biblio_fingerprint.js
1 //var marcdoc = new XML(environment.marc);
2 //var marc_ns = new Namespace('http://www.loc.gov/MARC21/slim');
3
4 var modsdoc = new XML(environment.mods);
5 var mods_ns = new Namespace('http://www.loc.gov/mods/');
6
7 //var mods3_ns = new Namespace('http://www.loc.gov/mods/v3');
8
9 default xml namespace = mods_ns;
10
11 var t = null;
12 var a = null;
13
14 function extract_typed_title( ti ) {
15
16         try {
17                 var types = ['uniform','translated','alternative'];
18                 for ( var j in types ) {
19                         for ( var i in ti ) {
20                                 if (ti[i].attribute("type") == types[j])
21                                         return  ti[i];
22                         }
23                 }
24
25         } catch (e) {
26                 log_debug(e);
27                 return ti[0];
28         }
29 }
30
31 function extract_author( au ) {
32         log_debug(au.toString());
33
34         if ( au..role.length() > 0 ) au = au.(role.text == 'creator' || role.text == 'author');
35         
36         if ( au.(hasOwnProperty("@type")) ) {
37                 au = au.(@type == 'personal')[0] ||
38                         au.(@type == 'corporate')[0] ||
39                         au.(@type == 'conference')[0];
40         }
41
42         return au ? au.namePart[0] : '';
43 }
44
45 log_debug("typeOfResource is " + modsdoc.typeOfResource);
46
47 var quality = 0;
48
49 // Treat non-text differently
50 if (modsdoc.typeOfResource != 'text') {
51         quality = 10;
52
53         // Look in related items for a good title
54         for ( var index in modsdoc.relatedItem.( hasOwnProperty('@type') && @type != 'series' && @type != 'host' ) ) {
55                 var ri = modsdoc.relatedItem[index];
56                 if ( ri.(!hasOwnProperty("@type") )) {
57                         t = extract_typed_title( ri.titleInfo.(hasOwnProperty('@type')) );
58                         if (!t) {
59                                 t = ri.titleInfo[0];
60                                 quality += 10;
61                         } else {
62                                 quality += 15;
63                         }
64                 }
65
66                 if (t != null) {
67                         log_debug('Found ['+modsdoc.typeOfResource+'] related titleInfo node: ' + t.toXMLString());
68                         break;
69                 }
70         }
71
72         // Couldn't find a usable title in a related item
73         if (t == null) {
74                 t = extract_typed_title( modsdoc.titleInfo );
75                 if (!t) {
76                         t = modsdoc.titleInfo[0];
77                         quality += 5;
78                 } else {
79                         quality += 10;
80                 }
81         }
82
83         log_debug('Found ['+modsdoc.typeOfResource+'] main titleInfo node: ' + t.toXMLString());
84
85 } else {
86         quality = 20;
87
88         t = extract_typed_title( modsdoc.titleInfo );
89
90         if (t == null) {
91                 t = modsdoc.titleInfo[0];
92                 quality += 15;
93         } else {
94                 quality += 10;
95         }
96
97         log_debug('Found ['+modsdoc.typeOfResource+'] main titleInfo node: ' + t.toXMLString());
98 }
99
100 var title = t.title
101         .toLowerCase()
102         .replace(/\[.+?\]/,'')
103         .replace(/\bthe\b|\ban?d?\b|\W+/g,'');
104
105
106 var author = (
107         ( modsdoc.typeOfResource != 'text' ?
108                 extract_author(modsdoc.relatedItem.name) :
109                 extract_author(modsdoc.name) ) ||
110         ''
111 ).toLowerCase().replace(/^\s*(\w+).*?$/,"$1");
112
113 result.fingerprint = title + author;
114 result.quality = '' + quality;
115