001package org.kuali.ole.utility.callnumber;
002
003import org.apache.commons.lang.StringUtils;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006import org.solrmarc.callnum.AbstractCallNumber;
007import org.solrmarc.callnum.CallNumber;
008
009/**
010 * Created with IntelliJ IDEA.
011 * User: ?
012 * Date: 20/2/13
013 * Time: 7:32 PM
014 * To change this template use File | Settings | File Templates.
015 */
016public class SuDocCallNumber extends AbstractCallNumber implements CallNumber {
017    private static final Logger Log = LoggerFactory.getLogger(SuDocCallNumber.class);
018    /**
019     * regular expression string for complete SuDoc classification
020     * Splits the based on continuous numbers and alphabets
021     * Ignore any special char and spaces.
022     */
023    public static final String SUDOC_REGEX = "[^A-Z0-9]+|(?<=[A-Z])(?=[0-9])|(?<=[0-9])(?=[A-Z])";
024    protected String shelfKey;
025
026    private static SuDocCallNumber ourInstance = null;
027
028    public static SuDocCallNumber getInstance() {
029        if (null == ourInstance) {
030            ourInstance = new SuDocCallNumber();
031        }
032        return ourInstance;
033    }
034
035    @Override
036    public void parse(String call) {
037        try {
038            this.rawCallNum = call;
039            this.parse();
040        } catch (Exception e) {
041            Log.error("SuDoc Call Number Exception" + e);
042        }
043    }
044
045    @Override
046    public String getShelfKey() {
047        return shelfKey;
048    }
049
050    protected void parse() {
051        if (this.rawCallNum != null) {
052            this.buildShelfKey();
053        }
054
055    }
056
057    protected void buildShelfKey() {
058        String upcaseSuDoccallnum = rawCallNum.toUpperCase();
059        StringBuffer callNum = new StringBuffer();
060        //split the call number based on numbers and alphabets
061        String[] cNumSub = upcaseSuDoccallnum.split(SUDOC_REGEX);
062        for (String str : cNumSub) {
063            if (StringUtils.isNumeric(str)) {   // numbers
064                // append zeros to sort Ordinal
065                str = StringUtils.leftPad(str, 5, "0"); // constant length 5
066                callNum.append(str);
067                callNum.append(" ");
068            } else {                     // alphabets
069                // append spaces to sort Lexicographic
070                str = StringUtils.rightPad(str, 5);  // constant length 5
071                callNum.append(str);
072                callNum.append(" ");
073            }
074        }
075        shelfKey = callNum.toString().trim();
076    }
077}