1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.validation.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.ole.select.businessobject.BibInfoBean;
20 import org.kuali.ole.select.validation.StandardNumberValidation;
21
22 import java.util.regex.Pattern;
23
24
25 public class StandardNumberValidationImpl implements StandardNumberValidation {
26
27 public static final String ISBN_CONSTANT = "ISBN";
28 public static final String ISSN_CONSTANT = "ISSN";
29 public static final String OCLC_CONSTANT = "OCLC";
30 public static final String INVALID_MSG = " number is invalid";
31 public static final String INVALID_STANDARDNUMBERTYPE_MSG = "Standart Number type should be ISBN or ISSN";
32
33 public boolean validateISBN(String input) {
34
35 if (input != null && ((input.length() == 13 && Pattern.matches("\\A\\d{1}\\-\\d{3}\\-\\d{5}\\-[X\\d]\\z", input)) ||
36 (input.length() == 13 && Pattern.matches("\\A\\d{1}\\-\\d{4}\\-\\d{4}\\-[X\\d]\\z", input)) ||
37 (input.length() == 17 && Pattern.matches("\\A\\d{3}\\-\\d{1}\\-\\d{2}\\-\\d{6}\\-[X\\d]\\z", input)) ||
38 (input.length() == 17 && Pattern.matches("\\A\\d{3}\\-\\d{1}\\-\\d{3}\\-\\d{5}\\-[X\\d]\\z", input)))) {
39 int tot = 0;
40 int remainder = 0;
41 char compChkDigit;
42
43 input = input.replaceAll("-", "");
44
45 switch (input.length()) {
46 case 10:
47 int[] weightFactor = {10, 9, 8, 7, 6, 5, 4, 3, 2};
48 for (int i = 0; i <= 8; i++)
49 tot = tot + (Character.getNumericValue(input.charAt(i)) * weightFactor[i]);
50
51 remainder = (11 - (tot % 11)) % 11;
52
53 if (remainder < 10)
54 compChkDigit = Character.forDigit(remainder, 10);
55 else
56 compChkDigit = 'X';
57
58 if (compChkDigit == input.charAt(9))
59 return true;
60 else
61 return false;
62
63 case 13:
64 int weight = 0;
65 for (int i = 0; i <= 11; i++) {
66 if (i % 2 == 0) weight = 1;
67 else weight = 3;
68 tot = tot + (Character.getNumericValue(input.charAt(i)) * weight);
69 }
70
71 remainder = (10 - (tot % 10)) % 10;
72 if (remainder < 10)
73 compChkDigit = Character.forDigit(remainder, 10);
74 else
75 compChkDigit = 'X';
76
77 if (compChkDigit == input.charAt(12))
78 return true;
79 else
80 return false;
81 }
82 }
83 return false;
84 }
85
86 public boolean validateISSN(String input) {
87
88 if (input != null && (input.length() == 9 && Pattern.matches("\\A\\d{4}\\-\\d{3}[X\\d]\\z", input))) {
89 int tot = 0;
90 char compChkDigit;
91 int[] weightFactor = {8, 7, 6, 5, 4, 3, 2};
92
93 input = input.replaceAll("-", "");
94
95 for (int i = 0; i <= 6; i++)
96 tot = tot + (Character.getNumericValue(input.charAt(i)) * weightFactor[i]);
97
98
99 int remainder = (11 - (tot % 11)) % 11;
100
101 if (remainder < 10)
102 compChkDigit = Character.forDigit(remainder, 10);
103 else
104 compChkDigit = 'X';
105
106 if (compChkDigit == input.charAt(7))
107 return true;
108 else
109 return false;
110 }
111 return false;
112 }
113
114 public boolean validateOCLC(String input) {
115 return true;
116 }
117
118 public boolean validateStandardNumbers(String inputType, String inputValue) {
119 if (ISBN_CONSTANT.equalsIgnoreCase(inputType))
120 return validateISBN(inputValue);
121 else if (ISSN_CONSTANT.equalsIgnoreCase(inputType))
122 return validateISSN(inputValue);
123 else if (OCLC_CONSTANT.equalsIgnoreCase(inputType))
124 return validateOCLC(inputValue);
125 else
126 return false;
127 }
128
129 public String isValidStandardNumbers(BibInfoBean bibInfoBean) {
130 boolean ret = false;
131 String standardNumber = bibInfoBean.getStandardNumber();
132 String typeOfStandardNumber = bibInfoBean.getTypeOfStandardNumber();
133 if (!StringUtils.isEmpty(standardNumber) && !StringUtils.isEmpty(typeOfStandardNumber)) {
134 typeOfStandardNumber = bibInfoBean.getTypeOfStandardNumber().toUpperCase();
135 if (typeOfStandardNumber.equals(ISBN_CONSTANT) || typeOfStandardNumber.equals(ISSN_CONSTANT)) {
136 boolean validNumber = validateStandardNumbers(typeOfStandardNumber, standardNumber);
137 StringBuffer buffer = new StringBuffer();
138 if (!validNumber) {
139 if (!standardNumber.isEmpty())
140 buffer.append(typeOfStandardNumber + INVALID_MSG);
141 }
142 if (buffer.toString().isEmpty()) {
143 return "true";
144 } else {
145 return buffer.toString();
146 }
147 } else {
148 return INVALID_STANDARDNUMBERTYPE_MSG;
149 }
150 } else {
151 return "";
152 }
153 }
154
155 }