View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.vnd;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.sys.context.SpringContext;
20  import org.kuali.rice.core.api.config.property.ConfigurationService;
21  import org.kuali.rice.krad.util.ObjectUtils;
22  
23  /**
24   * Utility class with helper methods for Vendor processing 
25   */
26  public class VendorUtils {
27  
28      public static final char LEFT_COLLECTION_SEPERATOR = '[';
29      public static final char RIGHT_COLLECTION_SEPERATOR = ']';
30      public static final char FIELD_SEPERATOR = '.';
31  
32      /**
33       * Builds up a string and a position like so abc, 1 becomes abc[1] it is used for fields that require operations on
34       * collections.
35       * 
36       * @param full
37       * @param collections
38       * @param pos
39       * @return Newly formatted string
40       */
41      public static String assembleWithPosition(String full, String[] collections, int[] positions) {
42  
43          if (collections.length != positions.length) {
44              throw new IllegalArgumentException();
45          }
46  
47          String[] parts = StringUtils.split(full, FIELD_SEPERATOR);
48  
49          for (int j = 0; j < parts.length; j++) {
50              for (int i = 0; i < collections.length; i++) {
51                  if (StringUtils.equals(parts[j], collections[i])) {
52                      parts[j] = collections[i] + LEFT_COLLECTION_SEPERATOR + positions[i] + RIGHT_COLLECTION_SEPERATOR;
53                      break;
54                  }
55  
56              }
57          }
58  
59          return StringUtils.join(parts, FIELD_SEPERATOR);
60      }
61  
62      /**
63       * A helper to call assembleWithPosition(String full, String[] collections, int[] positions) when only one
64       * collection
65       * 
66       * @param full
67       * @param collection
68       * @param position
69       * @return Newly formatted string
70       */
71      public static String assembleWithPosition(String full, String collection, int position) {
72          String[] collections = { collection };
73          int[] positions = { position };
74          return assembleWithPosition(full, collections, positions);
75      }
76  
77      /**
78       * Returns the headerId portion from a composite vendor number.
79       * 
80       * @param vendorNumber - composite vendor number (detail and header)
81       * @return returns the headerId number
82       */
83      public static Integer getVendorHeaderId(String vendorNumber) {
84  
85          // validate the vendorNumber passed in
86          if (!VendorUtils.validVendorNumberFormat(vendorNumber)) {
87              return null;
88          }
89  
90          // return the headerId, everything before the dash (-)
91          String[] vendorNumberParts = vendorNumber.split("-");
92          return new Integer(Integer.parseInt(vendorNumberParts[0]));
93      }
94  
95      /**
96       * Returns the detailId portion from a composite vendor number.
97       * 
98       * @param vendorNumber - composite vendor number (detail and header)
99       * @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 }