001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * Created on Dec 10, 2003
018: *
019: * To change the template for this generated file go to
020: * Window>Preferences>Java>Code Generation>Code and Comments
021: */
022: package org.kuali.module.pdp.utilities;
023:
024: import java.math.BigDecimal;
025: import java.util.Date;
026: import java.util.Enumeration;
027: import java.util.regex.Matcher;
028: import java.util.regex.Pattern;
029:
030: import javax.servlet.http.HttpServletRequest;
031:
032: import org.kuali.kfs.service.ParameterService;
033: import org.kuali.module.pdp.exception.ConfigurationError;
034:
035: /**
036: * @author HSTAPLET
037: */
038: public class GeneralUtilities {
039: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
040: .getLogger(GeneralUtilities.class);
041:
042: // GENERAL UTILITIES SECTION
043:
044: public static int getParameterInteger(
045: ParameterService parameterService, Class componentClass,
046: String parm) {
047: String srpp = parameterService.getParameterValue(
048: componentClass, parm);
049: if (srpp != null) {
050: try {
051: return Integer.parseInt(srpp);
052: } catch (NumberFormatException e) {
053: throw new ConfigurationError(parm + " is not a number");
054: }
055: } else {
056: throw new ConfigurationError("Unable to find " + parm);
057: }
058: }
059:
060: public static int getParameterInteger(
061: ParameterService parameterService, Class componentClass,
062: String parm, int defaultValue) {
063: String srpp = parameterService.getParameterValue(
064: componentClass, parm);
065: if (srpp != null) {
066: try {
067: return Integer.parseInt(srpp);
068: } catch (NumberFormatException e) {
069: return defaultValue;
070: }
071: } else {
072: return defaultValue;
073: }
074: }
075:
076: /**
077: * Method to return the name of the button that was pressed. Returns the full name.
078: */
079: public static String whichButtonWasPressed(
080: HttpServletRequest request) {
081: String paramName = new String();
082:
083: Enumeration enumer = request.getParameterNames();
084: while (enumer.hasMoreElements()) {
085: paramName = (String) enumer.nextElement();
086: if (paramName.startsWith("btn")) { // All the button names start with btn
087: if (paramName.indexOf(".") > -1) {
088: return paramName.substring(0, paramName
089: .indexOf("."));
090: } else {
091: return paramName;
092: }
093: }
094: }
095: return "";
096: }
097:
098: // FORM VALIDATION SECTION
099:
100: /**
101: * Method to check if a String field is null or an empty string. Return true if String is empty, otherwise return false.
102: */
103: public static boolean isStringEmpty(String field) {
104: if (field == null || field.equals("")) {
105: return true;
106: } else {
107: return false;
108: }
109: }
110:
111: /**
112: * Method to check how many times char x occurs in the given String. Return the number of occurences.
113: */
114: public static int numberOfOccurences(String string, char x) {
115: LOG.debug("numberOfOccurences() Enter method with string "
116: + string + " and char " + x);
117: int occurenceCtr = 0;
118: for (int i = 0; i < string.length(); i++) {
119: if (string.charAt(i) == x) {
120: ++occurenceCtr;
121: }
122: }
123: return occurenceCtr;
124: }
125:
126: /**
127: * formatStringWithoutGivenChar takes a String and removes the given char out of the string. (i.e. remove commas: ',')
128: *
129: * @param myString string to be formatted
130: * @param removeChar char to be removed from string
131: * @return String formatted string
132: */
133: public static String formatStringWithoutGivenChar(String myString,
134: char removeChar) {
135: if (!isStringEmpty(myString)) {
136: for (int i = 0; i < myString.length(); i++) {
137: if (myString.charAt(i) == removeChar) {
138: myString = myString.substring(0, i)
139: + myString.substring(i + 1);
140: i--; // repeat check on last position to keep from skipping the next char
141: }
142: }
143: return myString;
144: }
145: return "";
146: }
147:
148: /**
149: * Method to check if a String field is n chars long. Return true if String is n chars long, otherwise return false.
150: */
151: public static boolean isStringFieldNLength(String field, int nLength) {
152: LOG.debug("Entered isStringFieldNLength");
153: if (!isStringEmpty(field)) {
154: if (field.length() == nLength) {
155: return true;
156: }
157: }
158: return false;
159: }
160:
161: /**
162: * Method to check if a String field is n chars long. Return true if String is n chars long, otherwise return false.
163: */
164: public static boolean isStringFieldAtMostNLength(String field,
165: int nLength) {
166: LOG.debug("Entered isStringFieldNLength");
167: if (!isStringEmpty(field)) {
168: if (field.length() >= nLength) {
169: return true;
170: }
171: }
172: return false;
173: }
174:
175: /**
176: * Method to check if a String field is all numbers or letters. Return true if String is numeric/alphabetic, otherwise return
177: * false.
178: */
179: public static boolean isStringAllNumbersOrLetters(String field) {
180: if (!isStringEmpty(field)) {
181: field = field.trim();
182: LOG.debug("Entered isStringAllNumbersOrLetters: field is "
183: + field);
184: for (int x = 0; x < field.length(); x++) {
185: char c = field.charAt(x);
186:
187: if (!Character.isLetterOrDigit(c)) {
188: return false;
189: }
190: }
191: return true;
192: }
193: return false;
194: }
195:
196: /**
197: * Method to check if a String field is all numbers or a given character. Return true if String is numbers and the given
198: * character, otherwise return false.
199: */
200: public static boolean isStringAllNumbersOrACharacter(String string,
201: char c) {
202: if (!isStringEmpty(string)) {
203: string = string.trim();
204: LOG
205: .debug("isStringAllNumbersOrACharacter(): entered method with string "
206: + string + " , character " + c);
207: for (int x = 0; x < string.length(); x++) {
208: char ch = string.charAt(x);
209:
210: if (!Character.isDigit(ch) && !(ch == c)) {
211: return false;
212: }
213: }
214: return true;
215: }
216: return false;
217: }
218:
219: /**
220: * Method to check if a String field is all numbers or a given character. Return true if String is numbers and the given
221: * character, otherwise return false.
222: */
223: public static boolean isStringAllNumbersOrASingleCharacter(
224: String string, char c) {
225: if (!isStringEmpty(string)) {
226: string = string.trim();
227: LOG
228: .debug("isStringAllNumbersOrASingleCharacter(): entered method with string "
229: + string + " , character " + c);
230: int charIndex = string.indexOf(c);
231: if (charIndex < 0) {
232: return isStringAllNumbers(string);
233: } else {
234: for (int x = 0; x < string.length(); x++) {
235: char ch = string.charAt(x);
236:
237: if (!Character.isDigit(ch) && !(ch == c)) {
238: return false;
239: }
240: if ((ch == c) && (x != charIndex)) {
241: return false;
242: }
243: }
244: return true;
245: }
246: }
247: return false;
248: }
249:
250: /**
251: * Method to check if a String field is all numbers. Return true if String is numeric, otherwise return false.
252: *
253: * NOTE: This returns true if the string is all spaces which is probably bad but since it is used
254: * so many places, I'm not changing it now.
255: */
256: public static boolean isStringAllNumbers(String field) {
257: LOG.debug("Entered isStringAllNumbers().");
258: if (!isStringEmpty(field)) {
259: field = field.trim();
260: for (int x = 0; x < field.length(); x++) {
261: char c = field.charAt(x);
262:
263: if (!Character.isDigit(c)) {
264: return false;
265: }
266: }
267: return true;
268: }
269: return false;
270: }
271:
272: public static Integer convertStringToInteger(String s) {
273: Integer i = null;
274: try {
275: if (!isStringEmpty(s)) {
276: i = new Integer(Integer.parseInt(s));
277: }
278: } catch (Exception e) {
279: LOG
280: .error(
281: "convertStringToInteger() error occurred; return -1.",
282: e);
283: i = new Integer(-1);
284: }
285: return i;
286: }
287:
288: public static Long convertStringToLong(String s) {
289: Long l = null;
290: try {
291: if (!isStringEmpty(s)) {
292: l = new Long(Long.parseLong(s));
293: }
294: } catch (Exception e) {
295: LOG.error(
296: "convertStringToLong() error occurred; return -1.",
297: e);
298: l = new Long(-1);
299: }
300: return l;
301: }
302:
303: public static Date convertStringToDate(String s) {
304: Date d = null;
305: try {
306: if (!isStringEmpty(s)) {
307: d = DateHandler.makeStringSqlDate(s);
308: }
309: } catch (Exception e) {
310: LOG.error("convertStringToDate() error occurred.", e);
311: d = null;
312: }
313: return d;
314: }
315:
316: public static BigDecimal convertStringToBigDecimal(String s) {
317: BigDecimal bd = null;
318: try {
319: if (!isStringEmpty(s)) {
320: bd = new BigDecimal(s);
321: }
322: } catch (Exception e) {
323: LOG
324: .error(
325: "convertStringToBigDecimal() error occurred; return -1.",
326: e);
327: bd = new BigDecimal(-1);
328: }
329: return bd;
330: }
331:
332: public static String convertIntegerToString(Integer i) {
333: String s = "";
334: try {
335: if (i != null) {
336: s = i.toString();
337: }
338: } catch (Exception e) {
339: LOG
340: .error(
341: "convertIntegerToString() error occurred; return empty string.",
342: e);
343: s = "";
344: }
345: return s;
346: }
347:
348: public static String convertLongToString(Long l) {
349: String s = "";
350: try {
351: if (l != null) {
352: s = l.toString();
353: }
354: } catch (Exception e) {
355: LOG
356: .error(
357: "convertLongToString() error occurred; return empty string.",
358: e);
359: s = "";
360: }
361: return s;
362: }
363:
364: public static String convertDateToString(Date d) {
365: String s = "";
366: try {
367: if (d != null) {
368: s = DateHandler.makeDateString(d);
369: }
370: } catch (Exception e) {
371: LOG
372: .error(
373: "convertDateToString() error occurred; return empty string.",
374: e);
375: s = "";
376: }
377: return s;
378: }
379:
380: public static String convertBigDecimalToString(BigDecimal bd) {
381: String s = "";
382: try {
383: if (bd != null) {
384: s = bd.toString();
385: }
386: } catch (Exception e) {
387: LOG
388: .error(
389: "convertBigDecimalToString() error occurred; return empty string.",
390: e);
391: s = "";
392: }
393: return s;
394: }
395:
396: public static boolean isRegexMatch(String regex, String input) {
397: Pattern regexPattern = Pattern.compile(regex);
398: Matcher regexMatcher = regexPattern.matcher(input);
399:
400: return regexMatcher.find();
401: }
402:
403: public static boolean isUsZipCodeValid(String test) {
404: if (test == null) {
405: return false;
406: }
407: return isRegexMatch("(^\\d{5}$)|(^\\d{5}-\\d{4}$)", test);
408: }
409: }
|