1 package org.kuali.ole.docstore.common.util;
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.DataField;
6 import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import java.util.*;
11
12
13
14
15 public class BibMarcUtil {
16
17 private static final String SEPERATOR_DATA_FIELD = ", ";
18 private static final String SEPERATOR_SUB_FIELD = " ";
19 private static final String PATTERN_CHAR = "*";
20 private static final String SEPERATOR_HYPHEN = " - ";
21 private static final String SEPERATOR_DOUBLE_HYPHEN = " -- ";
22
23 public static DocumentSearchConfig documentSearchConfig = DocumentSearchConfig.getDocumentSearchConfig();
24
25 public static String TITLE_DISPLAY = "Title_display";
26 public static String AUTHOR_DISPLAY = "Author_display";
27 public static String PUBLISHER_DISPLAY = "Publisher_display";
28 public static String ISBN_DISPLAY = "ISBN_display";
29 public static String ISSN_DISPLAY = "ISSN_display";
30
31
32 static {
33 List<String> fieldsNames = new ArrayList<>();
34 fieldsNames.add(TITLE_DISPLAY);
35 fieldsNames.add(AUTHOR_DISPLAY);
36 fieldsNames.add(PUBLISHER_DISPLAY);
37 fieldsNames.add(ISBN_DISPLAY);
38 fieldsNames.add(ISSN_DISPLAY);
39
40 documentSearchConfig.buildIncludeAndExcludeMapping(fieldsNames);
41 }
42
43
44 private static final Logger LOG = LoggerFactory.getLogger(BibMarcUtil.class);
45
46
47
48
49
50
51
52
53
54
55
56
57 private String getDataFieldValue(String includeTags, String excludeTags, BibMarcRecord record,
58 boolean isHyphenSeperatorFirst, String fieldName) {
59 List<String> fieldValues = new ArrayList<String>();
60 StringTokenizer includeTagsTokenizer = new StringTokenizer(includeTags, ",");
61
62 while (includeTagsTokenizer.hasMoreElements()) {
63 String tag = includeTagsTokenizer.nextToken();
64 tag = tag.trim();
65 int subFieldIdx = tag.indexOf('-');
66 String tagNum = (subFieldIdx == -1) ? tag : tag.substring(0, subFieldIdx);
67
68 for (int i = 0; i < record.getDataFields().size(); i++) {
69 DataField dataField = record.getDataFields().get(i);
70 if (isValidTag(dataField.getTag(), tagNum)) {
71 StringBuilder fieldValue = new StringBuilder();
72 List<SubField> subFields = dataField.getSubFields();
73 if (subFieldIdx != -1) {
74 if (excludeTags != null && !excludeTags.contains(tag)) {
75 String subFieldCodes = tag.substring(subFieldIdx + 1, tag.length());
76 boolean isHyphenCodedOnce = false;
77 for (SubField subField : subFields) {
78 if (subFieldCodes.contains(subField.getCode())) {
79 if (fieldValue.length() != 0) {
80 if (!isHyphenSeperatorFirst || isHyphenCodedOnce || (
81 dataField.getTag().endsWith("00") || dataField.getTag().endsWith("10")
82 || dataField.getTag().endsWith("11"))) {
83 fieldValue.append(SEPERATOR_SUB_FIELD);
84 } else {
85 fieldValue.append(SEPERATOR_HYPHEN);
86 isHyphenCodedOnce = true;
87 }
88 }
89 fieldValue.append(subField.getValue());
90 }
91 }
92 }
93 } else {
94 boolean isHyphenCodedOnce = false;
95 boolean isFirstSubField = false;
96 for (SubField subField : subFields) {
97 if (excludeTags != null && !excludeTags.contains(dataField.getTag() + "-" + subField.getCode()) && !excludeTags
98 .contains(tagNum + "-" + subField.getCode())) {
99 if (fieldValue.length() != 0) {
100 if (!isHyphenSeperatorFirst || isHyphenCodedOnce || (
101 dataField.getTag().endsWith("00") || dataField.getTag().endsWith("10")
102 || dataField.getTag().endsWith("11"))) {
103 fieldValue.append(SEPERATOR_SUB_FIELD);
104 } else {
105
106 fieldValue.append(SEPERATOR_HYPHEN);
107 isHyphenCodedOnce = true;
108 }
109 }
110 fieldValue.append(subField.getValue());
111 }
112 }
113 }
114 if ((dataField.getTag().equalsIgnoreCase("650") || dataField.getTag().equalsIgnoreCase("651"))
115 && fieldValue != null && fieldValue.length() > 1 && fieldValue.toString().trim().length() > 1) {
116 String fieldVal = fieldValue.toString().trim();
117 String lastChar = String.valueOf(fieldVal.charAt(fieldVal.length() - 1));
118 if (!lastChar.equalsIgnoreCase(".")) {
119 fieldValue.append(".");
120 }
121 }
122 fieldValues.add(fieldValue.toString().trim());
123 }
124 }
125 }
126 if (fieldValues.size() == 1) {
127 return fieldValues.get(0);
128 } else if (fieldValues.size() > 0) {
129 return fieldValues.get(0);
130 } else {
131 return null;
132 }
133 }
134
135
136
137
138
139
140
141
142 private boolean isValidTag(String tag, String tagFormat) {
143 try {
144 if (!tagFormat.contains(PATTERN_CHAR)) {
145 return tagFormat.equals(tag);
146 } else {
147 int idx = tagFormat.lastIndexOf(PATTERN_CHAR);
148 return isValidTag(tag.substring(0, idx) + tag.substring(idx + PATTERN_CHAR.length(), tag.length()), tagFormat.substring(0, idx)
149 + tagFormat.substring(idx + PATTERN_CHAR.length(), tagFormat.length()));
150 }
151 } catch (Exception e) {
152 LOG.info("Exception :", e);
153 return false;
154 }
155 }
156
157
158 public Map<String, String> buildDataValuesForBibInfo(BibMarcRecord bibMarcRecord) {
159 Map<String, String> dataFields = new HashMap<>();
160
161
162 String titleInclude = documentSearchConfig.FIELDS_TO_TAGS_2_INCLUDE_MAP_SELECTED.get(TITLE_DISPLAY);
163 String titleExclude = documentSearchConfig.FIELDS_TO_TAGS_2_EXCLUDE_MAP_SELECTED.get(TITLE_DISPLAY);
164 String title = this.getDataFieldValue(titleInclude, titleExclude, bibMarcRecord, false, TITLE_DISPLAY);
165
166 String authorInclude = documentSearchConfig.FIELDS_TO_TAGS_2_INCLUDE_MAP_SELECTED.get(AUTHOR_DISPLAY);
167 String authorExclude = documentSearchConfig.FIELDS_TO_TAGS_2_EXCLUDE_MAP_SELECTED.get(AUTHOR_DISPLAY);
168 String author = this.getDataFieldValue(authorInclude, authorExclude, bibMarcRecord, false, AUTHOR_DISPLAY);
169
170 String publisherInclude = documentSearchConfig.FIELDS_TO_TAGS_2_INCLUDE_MAP_SELECTED.get(PUBLISHER_DISPLAY);
171 String publisherExclude = documentSearchConfig.FIELDS_TO_TAGS_2_EXCLUDE_MAP_SELECTED.get(PUBLISHER_DISPLAY);
172 String publisher = this.getDataFieldValue(publisherInclude, publisherExclude, bibMarcRecord, false, PUBLISHER_DISPLAY);
173
174 String isbnInclude = documentSearchConfig.FIELDS_TO_TAGS_2_INCLUDE_MAP_SELECTED.get(ISBN_DISPLAY);
175 String isbnExclude = documentSearchConfig.FIELDS_TO_TAGS_2_EXCLUDE_MAP_SELECTED.get(ISBN_DISPLAY);
176 String isbn = this.getDataFieldValue(isbnInclude, isbnExclude, bibMarcRecord, false, ISBN_DISPLAY);
177
178
179 String issnInclude = documentSearchConfig.FIELDS_TO_TAGS_2_INCLUDE_MAP_SELECTED.get(ISSN_DISPLAY);
180 String issnExclude = documentSearchConfig.FIELDS_TO_TAGS_2_EXCLUDE_MAP_SELECTED.get(ISSN_DISPLAY);
181 String issn = this.getDataFieldValue(issnInclude, issnExclude, bibMarcRecord, false, ISSN_DISPLAY);
182
183
184 dataFields.put(TITLE_DISPLAY, title);
185 dataFields.put(AUTHOR_DISPLAY, author);
186 dataFields.put(PUBLISHER_DISPLAY, publisher);
187 dataFields.put(ISBN_DISPLAY, isbn);
188 dataFields.put(ISSN_DISPLAY, issn);
189
190 return dataFields;
191 }
192
193
194 }