Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/MSEARCH-0' into MSEARCH-569
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtHov committed Aug 24, 2023
2 parents c0af622 + 9f59b20 commit a7af2b7
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class EffectiveShelvingOrderTermProcessor implements SearchTermProcessor

@Override
public String getSearchTerm(String inputTerm) {
return getValidShelfKey(new NlmCallNumber(inputTerm))
return getValidShelfKey(new SuDocCallNumber(inputTerm))
.or(() -> getValidShelfKey(new NlmCallNumber(inputTerm)))
.or(() -> getValidShelfKey(new LCCallNumber(inputTerm)))
.or(() -> getValidShelfKey(new DeweyCallNumber(inputTerm)))
.orElse(normalizeEffectiveShelvingOrder(inputTerm))
Expand Down
124 changes: 124 additions & 0 deletions src/main/java/org/folio/search/cql/SuDocCallNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.folio.search.cql;

import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.marc4j.callnum.AbstractCallNumber;
import org.marc4j.callnum.Utils;

public class SuDocCallNumber extends AbstractCallNumber {
private static final String GROUP1 = "([A-Za-z]+\\s*)";
private static final String GROUP2 = "(\\d+)";
private static final String GROUP3 = "(\\.(?:[A-Za-z]+\\d*|\\d+))";
private static final String GROUP4 = "(/(?:[A-Za-z]+(?:\\d+(?:-\\d+)?)?|\\d+(?:-\\d+)?))?";
private static final String GROUP5 = "(.*)";
public static final String SU_DOC_PATTERN = "^(?:" + GROUP1 + GROUP2 + GROUP3 + GROUP4 + ")?:?" + GROUP5;
protected static Pattern stemPattern = Pattern.compile(SU_DOC_PATTERN);
protected String authorSymbol;
protected String subordinateOffice;
protected String series;
protected String subSeries;

protected String suffix;
protected String shelfKey;


public SuDocCallNumber(String callNumber) {
this.parse(callNumber);
}

protected void init() {
this.rawCallNum = null;
this.authorSymbol = null;
this.subordinateOffice = null;
this.series = null;
this.subSeries = null;
this.suffix = null;
this.shelfKey = null;
}

@Override
public void parse(String s) {
this.init();
if (s == null) {
this.rawCallNum = null;
} else {
this.rawCallNum = s.trim();
}

this.parse();
}

protected void parse() {
if (this.rawCallNum != null) {
this.parseCallNumber();
}
}

protected void parseCallNumber() {
var stemMatcher = stemPattern.matcher(rawCallNum);
if (stemMatcher.matches()) {
authorSymbol = stemMatcher.group(1);
subordinateOffice = stemMatcher.group(2);
series = stemMatcher.group(3);
subSeries = stemMatcher.group(4);
suffix = stemMatcher.group(5);
} else {
suffix = rawCallNum;
}
}

@Override
public String getShelfKey() {
if (shelfKey == null) {
buildShelfKey();
}
return shelfKey;
}

@Override
public boolean isValid() {
return authorSymbol != null;
}

private void buildShelfKey() {
StringBuilder key = new StringBuilder();
if (authorSymbol != null) {
key.append(authorSymbol.trim());
}

appendWithSymbolIfNeeded(key, subordinateOffice);
appendWithSymbolIfNeeded(key, series);
appendWithSymbolIfNeeded(key, subSeries);
appendWithSymbolIfNeeded(key, suffix);

shelfKey = key.toString();
}

private void appendWithSymbolIfNeeded(StringBuilder key, String cnPart) {
if (StringUtils.isBlank(cnPart)) {
return;
}

if (cnPart.startsWith(".") || cnPart.startsWith("/") || cnPart.startsWith("-") || cnPart.startsWith(":")) {
cnPart = cnPart.substring(1);
}
var parts = cnPart.split("[./ -]");
for (String part : parts) {
if (key.length() > 0) {
key.append(' ');
}
part = part.trim();

if (StringUtils.isBlank(part)) {
continue;
}
if (Character.isAlphabetic(part.charAt(0))) {
key.append(" !");
} else if (part.length() >= 3) {
key.append("!");
}

Utils.appendNumericallySortable(key, part);
}
}
}

0 comments on commit a7af2b7

Please sign in to comment.