View Javadoc
1   package org.kuali.ole.utility.callnumber;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   import org.solrmarc.callnum.AbstractCallNumber;
7   import org.solrmarc.callnum.CallNumber;
8   
9   /**
10   * Created with IntelliJ IDEA.
11   * User: ?
12   * Date: 20/2/13
13   * Time: 7:32 PM
14   * To change this template use File | Settings | File Templates.
15   */
16  public class SuDocCallNumber extends AbstractCallNumber implements CallNumber {
17      private static final Logger Log = LoggerFactory.getLogger(SuDocCallNumber.class);
18      /**
19       * regular expression string for complete SuDoc classification
20       * Splits the based on continuous numbers and alphabets
21       * Ignore any special char and spaces.
22       */
23      public static final String SUDOC_REGEX = "[^A-Z0-9]+|(?<=[A-Z])(?=[0-9])|(?<=[0-9])(?=[A-Z])";
24      protected String shelfKey;
25  
26      private static SuDocCallNumber ourInstance = null;
27  
28      public static SuDocCallNumber getInstance() {
29          if (null == ourInstance) {
30              ourInstance = new SuDocCallNumber();
31          }
32          return ourInstance;
33      }
34  
35      @Override
36      public void parse(String call) {
37          try {
38              this.rawCallNum = call;
39              this.parse();
40          } catch (Exception e) {
41              Log.error("SuDoc Call Number Exception" + e);
42          }
43      }
44  
45      @Override
46      public String getShelfKey() {
47          return shelfKey;
48      }
49  
50      protected void parse() {
51          if (this.rawCallNum != null) {
52              this.buildShelfKey();
53          }
54  
55      }
56  
57      protected void buildShelfKey() {
58          String upcaseSuDoccallnum = rawCallNum.toUpperCase();
59          StringBuffer callNum = new StringBuffer();
60          //split the call number based on numbers and alphabets
61          String[] cNumSub = upcaseSuDoccallnum.split(SUDOC_REGEX);
62          for (String str : cNumSub) {
63              if (StringUtils.isNumeric(str)) {   // numbers
64                  // append zeros to sort Ordinal
65                  str = StringUtils.leftPad(str, 5, "0"); // constant length 5
66                  callNum.append(str);
67                  callNum.append(" ");
68              } else {                     // alphabets
69                  // append spaces to sort Lexicographic
70                  str = StringUtils.rightPad(str, 5);  // constant length 5
71                  callNum.append(str);
72                  callNum.append(" ");
73              }
74          }
75          shelfKey = callNum.toString().trim();
76      }
77  }