1 package org.kuali.ole.docstore.document.rdbms;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.ole.BibliographicRecordHandler;
5 import org.kuali.ole.DocumentUniqueIDPrefix;
6 import org.kuali.ole.RepositoryBrowser;
7 import org.kuali.ole.docstore.OleDocStoreException;
8 import org.kuali.ole.docstore.model.rdbms.bo.*;
9 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
10 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.ControlField;
11 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecord;
12 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecords;
13 import org.kuali.ole.docstore.model.xstream.work.bib.marc.WorkBibMarcRecordProcessor;
14 import org.kuali.ole.docstore.validation.DocStoreValidationError;
15 import org.kuali.ole.docstore.validation.WorkBibMarcRecordValidator;
16 import org.kuali.ole.pojo.bib.BibliographicRecord;
17 import org.kuali.ole.pojo.bib.Collection;
18 import org.kuali.rice.krad.service.BusinessObjectService;
19
20 import java.io.InputStream;
21 import java.util.*;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25
26
27
28
29
30
31
32 public class RdbmsWorkBibMarcDocumentManager extends RdbmsWorkBibDocumentManager {
33
34 private static RdbmsWorkBibMarcDocumentManager ourInstance = null;
35
36 public static RdbmsWorkBibMarcDocumentManager getInstance() {
37 if (null == ourInstance) {
38 ourInstance = new RdbmsWorkBibMarcDocumentManager();
39 }
40 return ourInstance;
41 }
42
43
44 protected void modifyDocumentContent(RequestDocument doc, String identifier, BusinessObjectService businessObjectService) {
45 String content = doc.getContent().getContent();
46 if (content != null && content != "" && content.length() > 0) {
47 Pattern pattern = Pattern.compile("tag=\"001\">.*?</controlfield");
48 Pattern pattern2 = Pattern.compile("<controlfield.*?tag=\"001\"/>");
49 Matcher matcher = pattern.matcher(content);
50 Matcher matcher2 = pattern2.matcher(content);
51 if (matcher.find()) {
52 doc.getContent().setContent(matcher.replaceAll("tag=\"001\">" + identifier + "</controlfield"));
53 } else if (matcher2.find()) {
54 doc.getContent()
55 .setContent(matcher2.replaceAll("<controlfield tag=\"001\">" + identifier + "</controlfield>"));
56 } else {
57 int ind = content.indexOf("</leader>") + 9;
58 if (ind == 8) {
59 ind = content.indexOf("<leader/>") + 9;
60 if (ind == 8) {
61 ind = content.indexOf("record>") + 7;
62 }
63 }
64 StringBuilder sb = new StringBuilder();
65 sb.append(content.substring(0, ind));
66 sb.append("<controlfield tag=\"001\">");
67 sb.append(identifier);
68 sb.append("</controlfield>");
69 sb.append(content.substring(ind + 1));
70 doc.getContent().setContent(sb.toString());
71 }
72 Map bibMap = new HashMap();
73 bibMap.put("bibId", DocumentUniqueIDPrefix.getDocumentId(identifier));
74 List<BibRecord> bibRecords = (List<BibRecord>) businessObjectService
75 .findMatching(BibRecord.class, bibMap);
76 if (bibRecords != null && bibRecords.size() > 0) {
77 BibRecord bibRecord = bibRecords.get(0);
78 bibRecord.setContent(doc.getContent().getContent());
79 businessObjectService.save(bibRecord);
80 }
81 }
82 }
83
84 protected void validateRequestDocument(RequestDocument requestDocument) throws OleDocStoreException {
85 if (requestDocument != null && requestDocument.getContent() != null && StringUtils.isNotEmpty(requestDocument.getContent().getContent())) {
86 WorkBibMarcRecordProcessor workBibMarcRecordProcessor = new WorkBibMarcRecordProcessor();
87
88 WorkBibMarcRecords workBibMarcRecords = workBibMarcRecordProcessor.fromXML(requestDocument.getContent().getContent());
89
90 WorkBibMarcRecordValidator wbmRecordValidator = new WorkBibMarcRecordValidator();
91 List<List<DocStoreValidationError>> docStoreValidationErrors = new ArrayList<>();
92 if (workBibMarcRecords != null) {
93
94 for (WorkBibMarcRecord workBibMarcRecord : workBibMarcRecords.getRecords()) {
95 docStoreValidationErrors.add(wbmRecordValidator.validate(workBibMarcRecord));
96 }
97 }
98
99 InputStream in = RepositoryBrowser.class.getResourceAsStream("docstore-type.properties");
100 Properties categories = wbmRecordValidator.getPropertyValues(in);
101
102 StringBuilder validationErrors = new StringBuilder();
103 int i = 0;
104 for (List<DocStoreValidationError> docStoreValidationErrorslist : docStoreValidationErrors) {
105 for (DocStoreValidationError docStoreValidationError : docStoreValidationErrorslist) {
106 List<String> errorParams = docStoreValidationError.getErrorParams();
107 String param1 = "";
108 String param2 = "";
109 if (errorParams != null) {
110 if (errorParams.size() == 1) {
111 param1 = errorParams.get(0);
112 }
113 if (errorParams.size() == 2) {
114 param1 = errorParams.get(0);
115 param2 = errorParams.get(1);
116 }
117 i++;
118 validationErrors.append(i);
119 validationErrors.append("-");
120 validationErrors.append(categories.get(docStoreValidationError.getErrorId()));
121 }
122 }
123 }
124 for (List<DocStoreValidationError> docStoreValidationErrorslist : docStoreValidationErrors) {
125 if(docStoreValidationErrorslist!=null && docStoreValidationErrorslist.size()>0) {
126 throw new OleDocStoreException(validationErrors.toString());
127
128 }
129 }
130 }
131 }
132
133 protected boolean getBibIdFromBibXMLContent(BibRecord bibRecord) {
134
135 boolean isBibIdFlag = true ;
136 BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
137 Collection bibCollectionList = bibliographicRecordHandler.fromXML(bibRecord.getContent());
138 if (bibCollectionList != null && bibCollectionList.getRecords() != null && bibCollectionList.getRecords().size() > 0) {
139 BibliographicRecord bibliographicRecord = bibCollectionList.getRecords().get(0);
140 if(bibliographicRecord.getControlfields() != null) {
141 for (ControlField controlField : bibliographicRecord.getControlfields()) {
142 if ("001".equals(controlField.getTag()) && validateIdField(controlField.getValue())) {
143 bibRecord.setBibId(controlField.getValue());
144 isBibIdFlag = false;
145 break;
146 }
147 }
148 }
149 }
150 return isBibIdFlag;
151 }
152
153 }