1 package org.kuali.ole.select.businessobject.options;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.displaytag.model.DefaultComparator;
5 import org.kuali.ole.BibliographicRecordHandler;
6 import org.kuali.ole.describe.service.DocstoreHelperService;
7 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
8 import org.kuali.ole.docstore.common.document.Bib;
9 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
10 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
11 import org.kuali.ole.pojo.bib.BibliographicRecord;
12 import org.kuali.ole.sys.OLEConstants;
13 import org.kuali.ole.sys.context.SpringContext;
14
15 import java.util.Comparator;
16
17
18
19
20
21
22
23
24 public class OLEOHQComparator implements Comparator {
25
26
27 private DefaultComparator comparator = new DefaultComparator();
28 private DocstoreClientLocator docstoreClientLocator;
29 private BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
30
31 public DocstoreClientLocator getDocstoreClientLocator() {
32
33 if (docstoreClientLocator == null) {
34 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
35 }
36 return docstoreClientLocator;
37 }
38
39
40 @Override
41 public int compare(Object obj1, Object obj2) {
42 obj1 = compareUsingIndicator2(obj1);
43 obj2 = compareUsingIndicator2(obj2);
44 return comparator.compare(obj1,obj2);
45 }
46
47 public Object compareUsingIndicator2(Object obj) {
48 if(obj.toString().contains(OLEConstants.TITLE_DISPLAY)){
49 obj = obj.toString().replaceAll(OLEConstants.TITLE_DISPLAY, OLEConstants.TITLE_SORT);
50 if(obj.toString().contains(OLEConstants.UUID_FOR_TITLE_SORT + "=")) {
51 String uuid = obj.toString().split(OLEConstants.UUID_FOR_TITLE_SORT + "=")[1].split("['\"']")[0];
52
53
54 try {
55
56 Bib bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(uuid);
57 if(bib.getContent() != null) {
58
59 BibMarcRecord bibMarcRecord = bibMarcRecordProcessor.fromXML(bib.getContent()).getRecords().get(0);
60 String indicatorValueAsString = bibMarcRecord.getDataFields().get(0).getInd2();
61 String oldTitle = obj.toString().split("Title_sort=")[1].split("&")[0];
62 String newTitle = bibMarcRecord.getDataFields().get(0).getSubFields().get(0).getValue();
63
64 if(!StringUtils.isNumeric(newTitle)) {
65 obj = obj.toString().replace(oldTitle,newTitle);
66 }
67 if(!indicatorValueAsString.equalsIgnoreCase(" ")){
68 Integer indicatorValue = Integer.valueOf(indicatorValueAsString);
69 obj = obj.toString().split(OLEConstants.TITLE_SORT)[0] +OLEConstants.TITLE_SORT + "=" + obj.toString().split(OLEConstants.TITLE_SORT)[1].substring(indicatorValue+1);
70 }
71 }
72 } catch (Exception e) {
73 e.printStackTrace();
74 }
75 }
76 }
77 return obj;
78 }
79
80
81 }