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: package org.kuali.module.vendor.util;
017:
018: import org.apache.commons.lang.StringUtils;
019:
020: /**
021: * Utility class with helper methods for Vendor processing
022: */
023: public class VendorUtils {
024:
025: public static final char LEFT_COLLECTION_SEPERATOR = '[';
026: public static final char RIGHT_COLLECTION_SEPERATOR = ']';
027: public static final char FIELD_SEPERATOR = '.';
028:
029: /**
030: * Builds up a string and a position like so abc, 1 becomes abc[1] it is used for fields that require operations on
031: * collections.
032: *
033: * @param full
034: * @param collections
035: * @param pos
036: * @return Newly formatted string
037: */
038: public static String assembleWithPosition(String full,
039: String[] collections, int[] positions) {
040:
041: if (collections.length != positions.length) {
042: throw new IllegalArgumentException();
043: }
044:
045: String[] parts = StringUtils.split(full, FIELD_SEPERATOR);
046:
047: for (int j = 0; j < parts.length; j++) {
048: for (int i = 0; i < collections.length; i++) {
049: if (StringUtils.equals(parts[j], collections[i])) {
050: parts[j] = collections[i]
051: + LEFT_COLLECTION_SEPERATOR + positions[i]
052: + RIGHT_COLLECTION_SEPERATOR;
053: break;
054: }
055:
056: }
057: }
058:
059: return StringUtils.join(parts, FIELD_SEPERATOR);
060: }
061:
062: /**
063: * A helper to call assembleWithPosition(String full, String[] collections, int[] positions) when only one
064: * collection
065: *
066: * @param full
067: * @param collection
068: * @param position
069: * @return Newly formatted string
070: */
071: public static String assembleWithPosition(String full,
072: String collection, int position) {
073: String[] collections = { collection };
074: int[] positions = { position };
075: return assembleWithPosition(full, collections, positions);
076: }
077:
078: /**
079: * Returns the headerId portion from a composite vendor number.
080: *
081: * @param vendorNumber - composite vendor number (detail and header)
082: * @return returns the headerId number
083: */
084: public static Integer getVendorHeaderId(String vendorNumber) {
085:
086: // validate the vendorNumber passed in
087: if (!VendorUtils.validVendorNumberFormat(vendorNumber)) {
088: return null;
089: }
090:
091: // return the headerId, everything before the dash (-)
092: String[] vendorNumberParts = vendorNumber.split("-");
093: return new Integer(Integer.parseInt(vendorNumberParts[0]));
094: }
095:
096: /**
097: * Returns the detailId portion from a composite vendor number.
098: *
099: * @param vendorNumber - composite vendor number (detail and header)
100: * @return returns the detailId number
101: */
102: public static Integer getVendorDetailId(String vendorNumber) {
103:
104: if (!VendorUtils.validVendorNumberFormat(vendorNumber)) {
105: return null;
106: }
107:
108: // return the headerId, everything before the dash (-)
109: String[] vendorNumberParts = vendorNumber.split("-");
110: return new Integer(Integer.parseInt(vendorNumberParts[1]));
111: }
112:
113: /**
114: * Accepts a vendorNumber string, and evaluates it to make sure it is of the correct format. This method does not
115: * test whether the given vendor number exists in the database, rather it just tests that the format is correct.
116: *
117: * @param vendorNumber - String representing the vendor number
118: * @return - returns an empty string on success, or an error message on a failure
119: */
120: public static boolean validVendorNumberFormat(String vendorNumber) {
121:
122: // disallow null string
123: if (vendorNumber == null) {
124: return false;
125: }
126:
127: // validate the overall format: numbers - numbers
128: if (!vendorNumber.matches("\\d+-\\d+")) {
129: return false;
130: }
131:
132: return true;
133: }
134: }
|