Coverage Report - org.kuali.rice.krad.util.ErrorContainer
 
Classes in this File Line Coverage Branch Coverage Complexity
ErrorContainer
0%
0/29
0%
0/16
3
 
 1  
 /*
 2  
  * Copyright 2006-2008 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.rice.krad.util;
 17  
 
 18  
 
 19  
 import java.io.Serializable;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import org.apache.struts.action.ActionMessage;
 25  
 import org.apache.struts.action.ActionMessages;
 26  
 import org.kuali.rice.core.util.RiceKeyConstants;
 27  
 import org.springframework.util.AutoPopulatingList;
 28  
 
 29  
 
 30  
 /**
 31  
  * Provides access to a copy of an ErrorMap and information derived from it. Necessary because ErrorMap implements the Map
 32  
  * interface, which for some reason makes JSTL unwilling to translate ErrorMap.errorCount into a call to the getErrorCount method of
 33  
  * that ErrorMap instance.
 34  
  * 
 35  
  * 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
 36  
  * error message lists of all properties in the ErrorMap), I also moved in the existing code which massaged the contents of the
 37  
  * ErrorMap for the purposes of export to the JSP.
 38  
  * 
 39  
  * 
 40  
  */
 41  
 public class ErrorContainer implements Serializable {
 42  
     private final MessageMap errorMap;
 43  
     private final int errorCount;
 44  
 
 45  
     /**
 46  
      * Constructs an ErrorContainer
 47  
      * 
 48  
      * @param errorMap
 49  
      */
 50  0
     public ErrorContainer(MessageMap errorMap) {
 51  0
         this.errorMap = errorMap;
 52  0
         this.errorCount = errorMap.getErrorCount();
 53  0
     }
 54  
 
 55  
     /**
 56  
      * @return number of errors in the ErrorMap used to initialize this container
 57  
      */
 58  
     public int getErrorCount() {
 59  0
             if (hasFormatterError()) {
 60  0
                     return 0;
 61  
             }
 62  0
         return errorCount;
 63  
     }
 64  
 
 65  
     /**
 66  
      * @return simple List of all properies for which errorMessages exist in the ErrorMap used to initialize this container
 67  
      */
 68  
     public List getErrorPropertyList() {
 69  0
         List properties = new ArrayList();
 70  
 
 71  0
         for (Iterator iter = errorMap.getAllPropertiesWithErrors().iterator(); iter.hasNext();) {
 72  0
             properties.add(iter.next());
 73  
         }
 74  
 
 75  0
         return properties;
 76  
     }
 77  
 
 78  
     /**
 79  
      * This method checks whether the errorMap contains at least a formatter error.
 80  
      * @return boolean true if the errorMap contains a formatter error and false otherwise
 81  
      */
 82  
     private boolean hasFormatterError() {
 83  0
             if (errorMap.getErrorCount()>0) {
 84  0
             for (String errorKey : errorMap.getAllPropertiesWithErrors()) {
 85  0
                     AutoPopulatingList errorValues = errorMap.getMessages(errorKey);
 86  0
                     for (ErrorMessage errorMessage : (List<ErrorMessage>)errorValues) {
 87  0
                     if (errorMessage.getErrorKey().equals(RiceKeyConstants.ERROR_DOCUMENT_MAINTENANCE_FORMATTING_ERROR)) {
 88  0
                         return true;
 89  
                     }
 90  
                     }
 91  0
             }
 92  
         }
 93  0
         return false;
 94  
     }
 95  
     
 96  
     /**
 97  
      * @return ActionMessages instance containing error messages constructed from the contents of the ErrorMap with which this
 98  
      *         container was initialized
 99  
      */
 100  
     public ActionMessages getRequestErrors() {
 101  0
         ActionMessages requestErrors = new ActionMessages();
 102  0
         for (Iterator iter = errorMap.getAllPropertiesWithErrors().iterator(); iter.hasNext();) {
 103  0
             String property = (String) iter.next();
 104  0
             List errorList = (List) errorMap.getErrorMessagesForProperty(property);
 105  
 
 106  0
             for (Iterator iterator = errorList.iterator(); iterator.hasNext();) {
 107  0
                 ErrorMessage errorMessage = (ErrorMessage) iterator.next();
 108  
 
 109  
                 // add ActionMessage with any parameters
 110  0
                 requestErrors.add(property, new ActionMessage(errorMessage.getErrorKey(), errorMessage.getMessageParameters()));
 111  0
             }
 112  0
         }
 113  0
         return requestErrors;
 114  
     }
 115  
 }