1 package org.kuali.ole.docstore.common.document;
2
3 import org.kuali.ole.docstore.common.document.config.DocumentSearchConfig;
4 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
5 import org.kuali.ole.docstore.common.document.content.bib.marc.ControlField;
6 import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
7 import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
8
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.Map;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15
16
17
18
19
20
21
22
23
24 public class BibMarcMapper {
25
26 private static BibMarcMapper bibMarcMapper = null;
27 private static Map<String, String> FIELDS_TO_TAGS_2_INCLUDE_MAP = new HashMap<String, String>();
28 private static Map<String, String> FIELDS_TO_TAGS_2_EXCLUDE_MAP = new HashMap<String, String>();
29 private String publicationDateRegex = "[0-9]{4}";
30
31 private BibMarcMapper() {
32 DocumentSearchConfig.getDocumentSearchConfig();
33 FIELDS_TO_TAGS_2_INCLUDE_MAP = Collections.unmodifiableMap(DocumentSearchConfig.FIELDS_TO_TAGS_2_INCLUDE_MAP);
34 FIELDS_TO_TAGS_2_EXCLUDE_MAP = Collections.unmodifiableMap(DocumentSearchConfig.FIELDS_TO_TAGS_2_EXCLUDE_MAP);
35 }
36
37 public static BibMarcMapper getInstance() {
38 if (bibMarcMapper == null) {
39 bibMarcMapper = new BibMarcMapper();
40 }
41 return bibMarcMapper;
42
43 }
44
45 public void extractFields(BibMarcRecord bibMarc, Bib bib) {
46 for (String field : FIELDS_TO_TAGS_2_INCLUDE_MAP.keySet()) {
47 buildFields(field, bibMarc, bib);
48 }
49
50 }
51
52 public void buildFields(String field, BibMarcRecord bibMarc, Bib bib) {
53 DataField dataField;
54
55 if (field.equalsIgnoreCase("Title_display")) {
56 StringBuilder title = new StringBuilder();
57 dataField = bibMarc.getDataFieldForTag("245");
58 if(dataField!=null) {
59 for (SubField subField : dataField.getSubFields()) {
60 if (subField.getCode().equalsIgnoreCase("a")) {
61 title.append(subField.getValue());
62 } else if (subField.getCode().equalsIgnoreCase("b")) {
63 title.append(subField.getValue());
64 }
65 }
66 }
67 bib.setTitle(title.toString());
68 } else if (field.equalsIgnoreCase("Author_display")) {
69 StringBuilder author = new StringBuilder();
70 dataField = bibMarc.getDataFieldForTag("100");
71 if(dataField!=null) {
72 for (SubField subField : dataField.getSubFields()) {
73 if (subField.getCode().equalsIgnoreCase("a")) {
74 author.append(subField.getValue());
75 }
76 }
77 }
78 dataField = bibMarc.getDataFieldForTag("110");
79 if(dataField!=null) {
80 for (SubField subField : dataField.getSubFields()) {
81 if (subField.getCode().equalsIgnoreCase("a")) {
82 author.append(subField.getValue());
83 }
84 }
85 }
86
87 bib.setAuthor(author.toString());
88 }
89 else if (field.equalsIgnoreCase("Publisher_display")) {
90 StringBuilder publisher = new StringBuilder();
91 dataField = bibMarc.getDataFieldForTag("260");
92 if(dataField!=null) {
93 for (SubField subField : dataField.getSubFields()) {
94 if (subField.getCode().equalsIgnoreCase("a")) {
95 publisher.append(subField.getValue());
96 } else if (subField.getCode().equalsIgnoreCase("b")) {
97 publisher.append(subField.getValue());
98 }
99 }
100 }
101 bib.setPublisher(publisher.toString());
102 }
103 else if(field.equalsIgnoreCase("PublicationDate_display")) {
104 String publicationDate = "";
105 for (ControlField controlField : bibMarc.getControlFields()) {
106 if (controlField.getTag().equalsIgnoreCase("008")) {
107 String controlField008 = controlField.getValue();
108 if (controlField008 != null && controlField008.length() > 10) {
109 publicationDate = controlField008.substring(7, 11);
110 publicationDate = extractPublicationDateWithRegex(publicationDate);
111 }
112 }
113 }
114
115 if (publicationDate == null || publicationDate.trim().length() == 0) {
116 dataField = bibMarc.getDataFieldForTag("260");
117 if(dataField!=null) {
118 for (SubField subField : dataField.getSubFields()) {
119
120 if(subField.getCode().equalsIgnoreCase("c")){
121 publicationDate=subField.getValue();
122 }
123 }
124 }
125 publicationDate = extractPublicationDateWithRegex(publicationDate);
126 }
127
128 bib.setPublicationDate(publicationDate);
129 }else if (field.equalsIgnoreCase("ISBN_display")) {
130 StringBuilder isbn = new StringBuilder();
131 dataField = bibMarc.getDataFieldForTag("020");
132 if(dataField!=null) {
133 for (SubField subField : dataField.getSubFields()) {
134 if (subField.getCode().equalsIgnoreCase("a")) {
135 isbn.append(subField.getValue());
136 } else if (subField.getCode().equalsIgnoreCase("z")) {
137 isbn.append(subField.getValue());
138 }
139 }
140 }
141 bib.setIsbn(isbn.toString());
142 }
143 else if (field.equalsIgnoreCase("ISSN_display")) {
144 StringBuilder issn = new StringBuilder();
145 dataField = bibMarc.getDataFieldForTag("022");
146 if(dataField!=null) {
147 for (SubField subField : dataField.getSubFields()) {
148 if (subField.getCode().equalsIgnoreCase("a")) {
149 issn.append(subField.getValue());
150 } else if (subField.getCode().equalsIgnoreCase("z")) {
151 issn.append(subField.getValue());
152 }
153 }
154 }
155 bib.setIssn(issn.toString());
156 }
157
158
159 }
160 private String extractPublicationDateWithRegex(String publicationDate) {
161 Pattern pattern = Pattern.compile(publicationDateRegex);
162 Matcher matcher = pattern.matcher(publicationDate);
163 if (matcher.find()) {
164 if (matcher.group(0).equalsIgnoreCase("0000")) {
165 return "";
166 }
167 return matcher.group(0);
168 } else {
169 return "";
170 }
171
172
173 }
174
175 }