001/** 002 * Copyright 2005-2018 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.rice.kns.util; 017 018 019import java.io.Serializable; 020import java.util.ArrayList; 021import java.util.Iterator; 022import java.util.List; 023 024import org.apache.struts.action.ActionMessage; 025import org.apache.struts.action.ActionMessages; 026import org.kuali.rice.core.api.util.RiceKeyConstants; 027import org.kuali.rice.krad.util.ErrorMessage; 028import org.kuali.rice.krad.util.MessageMap; 029 030 031/** 032 * Provides access to a copy of an ErrorMap and information derived from it. Necessary because ErrorMap implements the Map 033 * interface, which for some reason makes JSTL unwilling to translate ErrorMap.errorCount into a call to the getErrorCount method of 034 * that ErrorMap instance. 035 * 036 * Since I had to create this class to provide easy access to the error count (which must be computed as the sum of the sizes of the 037 * error message lists of all properties in the ErrorMap), I also moved in the existing code which massaged the contents of the 038 * ErrorMap for the purposes of export to the JSP. 039 * 040 * @deprecated Only used in KNS classes, use KRAD. 041 */ 042@Deprecated 043public class ErrorContainer implements Serializable { 044 private final MessageMap errorMap; 045 private final int errorCount; 046 047 /** 048 * Constructs an ErrorContainer 049 * 050 * @param errorMap 051 */ 052 public ErrorContainer(MessageMap errorMap) { 053 this.errorMap = errorMap; 054 this.errorCount = errorMap.getErrorCount(); 055 } 056 057 /** 058 * @return number of errors in the ErrorMap used to initialize this container 059 */ 060 public int getErrorCount() { 061 if (hasFormatterError()) { 062 return 0; 063 } 064 return errorCount; 065 } 066 067 /** 068 * @return simple List of all properies for which errorMessages exist in the ErrorMap used to initialize this container 069 */ 070 public List getErrorPropertyList() { 071 List properties = new ArrayList(); 072 073 for (Iterator iter = errorMap.getAllPropertiesWithErrors().iterator(); iter.hasNext();) { 074 properties.add(iter.next()); 075 } 076 077 return properties; 078 } 079 080 /** 081 * This method checks whether the errorMap contains at least a formatter error. 082 * @return boolean true if the errorMap contains a formatter error and false otherwise 083 */ 084 private boolean hasFormatterError() { 085 if (errorMap.getErrorCount()>0) { 086 for (String errorKey : errorMap.getAllPropertiesWithErrors()) { 087 List errorValues = errorMap.getMessages(errorKey); 088 for (ErrorMessage errorMessage : (List<ErrorMessage>)errorValues) { 089 if (errorMessage.getErrorKey().equals(RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_FORMATTING_ERROR)) { 090 return true; 091 } 092 } 093 } 094 } 095 return false; 096 } 097 098 /** 099 * @return ActionMessages instance containing error messages constructed from the contents of the ErrorMap with which this 100 * container was initialized 101 */ 102 public ActionMessages getRequestErrors() { 103 ActionMessages requestErrors = new ActionMessages(); 104 for (Iterator iter = errorMap.getAllPropertiesWithErrors().iterator(); iter.hasNext();) { 105 String property = (String) iter.next(); 106 List errorList = (List) errorMap.getErrorMessagesForProperty(property); 107 108 for (Iterator iterator = errorList.iterator(); iterator.hasNext();) { 109 ErrorMessage errorMessage = (ErrorMessage) iterator.next(); 110 111 // add ActionMessage with any parameters 112 requestErrors.add(property, new ActionMessage(errorMessage.getErrorKey(), errorMessage.getMessageParameters())); 113 } 114 } 115 return requestErrors; 116 } 117}