001 /* 002 * Copyright 2011 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 */ 016 package org.kuali.kfs.module.ar.batch.report; 017 018 import java.util.List; 019 import java.util.Map; 020 import java.util.Set; 021 import java.util.TreeMap; 022 import java.util.TreeSet; 023 024 import org.apache.commons.lang.StringUtils; 025 import org.kuali.rice.kns.util.TypedArrayList; 026 027 /** 028 * 029 * Contains a set of errors while attempting to run a batch of 030 * AR Customer Load entries. 031 * 032 */ 033 public class CustomerLoadBatchErrors { 034 035 // key = CustomerName 036 private Map<String, List<CustomerLoadBatchError>> batchErrors; 037 private int errorCount; 038 039 /** 040 * 041 * Creates a new CustomerLoadBatchErrors object, with empty internal content. 042 */ 043 public CustomerLoadBatchErrors() { 044 // initialize the internal storage 045 batchErrors = new TreeMap<String, List<CustomerLoadBatchError>>(); 046 errorCount = 0; 047 } 048 049 public CustomerLoadBatchErrors(CustomerLoadBatchError error) { 050 051 // initialize the internal storage 052 batchErrors = new TreeMap<String, List<CustomerLoadBatchError>>(); 053 errorCount = 0; 054 055 // short circuit if no content is passed in 056 if (error == null) return; 057 058 // stick the constructor-passed entry in 059 batchErrors.put(error.getCustomerName(), new TypedArrayList(CustomerLoadBatchError.class)); 060 batchErrors.get(error.getCustomerName()).add(error); 061 errorCount++; 062 } 063 064 /** 065 * 066 * Constructs a CustomerLoadBatchErrors with the specified List of CustomerLoadBatchError 067 * objects as the starting content. 068 * 069 * @param errors A List of CustomerLoadBatchError objects, which handles nulls and 070 */ 071 public CustomerLoadBatchErrors(List<CustomerLoadBatchError> errors) { 072 073 // initialize the internal storage 074 batchErrors = new TreeMap<String, List<CustomerLoadBatchError>>(); 075 076 // short circuit if no content is passed in 077 if (errors == null) return; 078 if (errors.isEmpty()) return; 079 080 addErrors(errors); 081 } 082 083 public void addAll(CustomerLoadBatchErrors otherErrors) { 084 if (otherErrors == null) { 085 throw new IllegalArgumentException("Parameter otherErrors passed in was null."); 086 } 087 for (String customerName : otherErrors.getCompanyNames()) { 088 List<CustomerLoadBatchError> customerErrors = otherErrors.getErrorsByCompany(customerName); 089 for (CustomerLoadBatchError customerLoadBatchError : customerErrors) { 090 addError(customerLoadBatchError); 091 } 092 } 093 } 094 095 public void addError(CustomerLoadBatchError error) { 096 if (error == null) { 097 throw new IllegalArgumentException("Parameter 'error' passed in was null."); 098 } 099 if (!batchErrors.containsKey(error.getCustomerName())) { 100 batchErrors.put(error.getCustomerName(), new TypedArrayList(CustomerLoadBatchError.class)); 101 } 102 batchErrors.get(error.getCustomerName()).add(error); 103 errorCount++; 104 } 105 106 public void addError(String customerName, String propertyName, Class<?> propertyClass, String value, String description) { 107 if (StringUtils.isBlank(customerName)) { 108 throw new IllegalArgumentException("Parameter customerName was empty or null."); 109 } 110 if (StringUtils.isBlank(propertyName)) { 111 throw new IllegalArgumentException("Parameter propertyName was empty or null."); 112 } 113 if (StringUtils.isBlank(description)) { 114 throw new IllegalArgumentException("Parameter description was empty or null."); 115 } 116 CustomerLoadBatchError error = new CustomerLoadBatchError(customerName, propertyName, propertyClass, value, description); 117 addError(error); 118 } 119 120 public void addErrors(List<CustomerLoadBatchError> errors) { 121 if (errors == null) { 122 throw new IllegalArgumentException("Parameter 'error' passed in was null."); 123 } 124 for (CustomerLoadBatchError error : errors) { 125 addError(error); 126 } 127 } 128 129 /** 130 * 131 * Returns the companyName's that have errors in this list. 132 * 133 * Note that the set of CompanyNames returned should be in ascending order 134 * for String ordering. 135 * 136 * Note that the underlying implementation's return is undefined where there 137 * are no elements in the storage yet, so test for null first. 138 * 139 * @return The set of companyName's that have errors. 140 */ 141 public Set<String> getCompanyNames() { 142 return batchErrors.keySet(); 143 } 144 145 /** 146 * 147 * Returns the list of CustomerLoadBatchError objects for the given 148 * companyName. 149 * 150 * Note that null will be returned if there are no errors for that companyName. 151 * 152 * Note that ordering of CustomerLoadBatchError objects for a given companyName is 153 * undefined. 154 * 155 * @param companyName The companyName you want errors for. 156 * @return The (possibly null or empty) List of CustomerLoadBatchError objects for the given companyName. 157 */ 158 public List<CustomerLoadBatchError> getErrorsByCompany(String companyName) { 159 if (!batchErrors.containsKey(companyName)) { 160 return null; 161 } 162 return batchErrors.get(companyName); 163 } 164 165 /** 166 * 167 * Returns true if there are no elements (errors) in this object. 168 * @return True if no errors have been added, False if any have been. 169 */ 170 public boolean isEmpty() { 171 return batchErrors.isEmpty(); 172 } 173 174 /** 175 * 176 * Returns a string error for each error, suitable to go into some other error container. 177 * 178 * The returned Set is sorted by natural order, and the String constructed should naturally order 179 * by companyName, then propertyName, though this is not absolutely guaranteed in all cases. 180 * 181 * @return A set of error messages. 182 */ 183 public Set<String> getErrorStrings() { 184 Set<String> errors = new TreeSet<String>(); 185 for (String companyName : batchErrors.keySet()) { 186 for (CustomerLoadBatchError error : batchErrors.get(companyName)) { 187 errors.add(error.toString()); 188 } 189 } 190 return errors; 191 } 192 193 public int getCompaniesWithErrors() { 194 return batchErrors.size(); 195 } 196 197 public int getTotalErrors() { 198 return errorCount; 199 } 200 }