001package org.kuali.ole.select.businessobject.options;
002
003import org.apache.commons.lang3.StringUtils;
004import org.displaytag.model.DefaultComparator;
005import org.kuali.ole.BibliographicRecordHandler;
006import org.kuali.ole.describe.service.DocstoreHelperService;
007import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
008import org.kuali.ole.docstore.common.document.Bib;
009import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
010import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
011import org.kuali.ole.pojo.bib.BibliographicRecord;
012import org.kuali.ole.sys.OLEConstants;
013import org.kuali.ole.sys.context.SpringContext;
014
015import java.util.Comparator;
016
017/**
018 * Created with IntelliJ IDEA.
019 * User: syedk
020 * Date: 10/4/13
021 * Time: 7:01 PM
022 * To change this template use File | Settings | File Templates.
023 */
024public class OLEOHQComparator implements Comparator {
025
026
027    private DefaultComparator comparator = new DefaultComparator();
028    private DocstoreClientLocator docstoreClientLocator;
029    private BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
030
031    public DocstoreClientLocator getDocstoreClientLocator() {
032
033        if (docstoreClientLocator == null) {
034            docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
035        }
036        return docstoreClientLocator;
037    }
038
039
040    @Override
041    public int compare(Object obj1, Object obj2) {
042        obj1 = compareUsingIndicator2(obj1);
043        obj2 = compareUsingIndicator2(obj2);
044        return comparator.compare(obj1,obj2);
045    }
046
047    public Object compareUsingIndicator2(Object obj) {
048        if(obj.toString().contains(OLEConstants.TITLE_DISPLAY)){
049            obj = obj.toString().replaceAll(OLEConstants.TITLE_DISPLAY, OLEConstants.TITLE_SORT);
050            if(obj.toString().contains(OLEConstants.UUID_FOR_TITLE_SORT + "=")) {
051                String uuid = obj.toString().split(OLEConstants.UUID_FOR_TITLE_SORT + "=")[1].split("['\"']")[0];
052               /* DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
053                String docstoreData = null;*/
054                try {
055                  /*  docstoreData = docstoreHelperService.getDocstoreData(uuid);*/
056                    Bib bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(uuid);
057                    if(bib.getContent() != null) {
058                        //BibMarcRecord bibMarcRecord = new BibMarcRecordProcessor().fromXML(docstoreData).getRecords().get(0);
059                        BibMarcRecord bibMarcRecord = bibMarcRecordProcessor.fromXML(bib.getContent()).getRecords().get(0);
060                        String indicatorValueAsString = bibMarcRecord.getDataFields().get(0).getInd2();
061                        String oldTitle = obj.toString().split("Title_sort=")[1].split("&")[0];
062                        String newTitle = bibMarcRecord.getDataFields().get(0).getSubFields().get(0).getValue();
063                        // This condition executed for Title added through other than Order Import (Batch process).
064                        if(!StringUtils.isNumeric(newTitle)) {
065                            obj = obj.toString().replace(oldTitle,newTitle);
066                        }
067                        if(!indicatorValueAsString.equalsIgnoreCase(" ")){
068                            Integer indicatorValue = Integer.valueOf(indicatorValueAsString);
069                            obj = obj.toString().split(OLEConstants.TITLE_SORT)[0] +OLEConstants.TITLE_SORT + "=" + obj.toString().split(OLEConstants.TITLE_SORT)[1].substring(indicatorValue+1);
070                        }
071                    }
072                } catch (Exception e) {
073                    e.printStackTrace();
074                }
075            }
076        }
077        return obj;
078    }
079
080
081}