001/* 002 * Copyright 2007 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */ 016package org.kuali.ole.vnd; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.ole.sys.context.SpringContext; 020import org.kuali.rice.core.api.config.property.ConfigurationService; 021import org.kuali.rice.krad.util.ObjectUtils; 022 023/** 024 * Utility class with helper methods for Vendor processing 025 */ 026public class VendorUtils { 027 028 public static final char LEFT_COLLECTION_SEPERATOR = '['; 029 public static final char RIGHT_COLLECTION_SEPERATOR = ']'; 030 public static final char FIELD_SEPERATOR = '.'; 031 032 /** 033 * Builds up a string and a position like so abc, 1 becomes abc[1] it is used for fields that require operations on 034 * collections. 035 * 036 * @param full 037 * @param collections 038 * @param pos 039 * @return Newly formatted string 040 */ 041 public static String assembleWithPosition(String full, String[] collections, int[] positions) { 042 043 if (collections.length != positions.length) { 044 throw new IllegalArgumentException(); 045 } 046 047 String[] parts = StringUtils.split(full, FIELD_SEPERATOR); 048 049 for (int j = 0; j < parts.length; j++) { 050 for (int i = 0; i < collections.length; i++) { 051 if (StringUtils.equals(parts[j], collections[i])) { 052 parts[j] = collections[i] + LEFT_COLLECTION_SEPERATOR + positions[i] + 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, String collection, int position) { 072 String[] collections = { collection }; 073 int[] positions = { position }; 074 return assembleWithPosition(full, collections, positions); 075 } 076 077 /** 078 * Returns the headerId portion from a composite vendor number. 079 * 080 * @param vendorNumber - composite vendor number (detail and header) 081 * @return returns the headerId number 082 */ 083 public static Integer getVendorHeaderId(String vendorNumber) { 084 085 // validate the vendorNumber passed in 086 if (!VendorUtils.validVendorNumberFormat(vendorNumber)) { 087 return null; 088 } 089 090 // return the headerId, everything before the dash (-) 091 String[] vendorNumberParts = vendorNumber.split("-"); 092 return new Integer(Integer.parseInt(vendorNumberParts[0])); 093 } 094 095 /** 096 * Returns the detailId portion from a composite vendor number. 097 * 098 * @param vendorNumber - composite vendor number (detail and header) 099 * @return returns the detailId number 100 */ 101 public static Integer getVendorDetailId(String vendorNumber) { 102 103 if (!VendorUtils.validVendorNumberFormat(vendorNumber)) { 104 return null; 105 } 106 107 // return the headerId, everything before the dash (-) 108 String[] vendorNumberParts = vendorNumber.split("-"); 109 return new Integer(Integer.parseInt(vendorNumberParts[1])); 110 } 111 112 /** 113 * Accepts a vendorNumber string, and evaluates it to make sure it is of the correct format. This method does not 114 * test whether the given vendor number exists in the database, rather it just tests that the format is correct. 115 * 116 * @param vendorNumber - String representing the vendor number 117 * @return - returns an empty string on success, or an error message on a failure 118 */ 119 public static boolean validVendorNumberFormat(String vendorNumber) { 120 121 // disallow null string 122 if (vendorNumber == null) { 123 return false; 124 } 125 126 // validate the overall format: numbers - numbers 127 if (!vendorNumber.matches("\\d+-\\d+")) { 128 return false; 129 } 130 131 return true; 132 } 133 134 /** 135 * Composes the text for the note related to parent change to be added to the old parent vendor. 136 * 137 * @param messageKey 138 * @param parameters 139 * @return 140 */ 141 public static String buildMessageText(String messageKey, String... parameters) { 142 String result = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(messageKey); 143 if (ObjectUtils.isNotNull(parameters)) { 144 for (int i = 0; i < parameters.length; i++) { 145 result = StringUtils.replace(result, "{" + i + "}", parameters[i]); 146 } 147 } 148 return result; 149 } 150}