Coverage Report - org.kuali.rice.core.api.uif.RemotableAttributeError
 
Classes in this File Line Coverage Branch Coverage Complexity
RemotableAttributeError
0%
0/37
0%
0/14
2.444
RemotableAttributeError$1
N/A
N/A
2.444
RemotableAttributeError$Builder
0%
0/41
0%
0/16
2.444
RemotableAttributeError$Constants
0%
0/1
N/A
2.444
RemotableAttributeError$Elements
0%
0/1
N/A
2.444
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.core.api.uif;
 17  
 
 18  
 import java.io.Serializable;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Arrays;
 21  
 import java.util.Collection;
 22  
 import java.util.Collections;
 23  
 import java.util.HashMap;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 import javax.xml.bind.annotation.XmlAccessType;
 27  
 import javax.xml.bind.annotation.XmlAccessorType;
 28  
 import javax.xml.bind.annotation.XmlAnyElement;
 29  
 import javax.xml.bind.annotation.XmlElement;
 30  
 import javax.xml.bind.annotation.XmlElementWrapper;
 31  
 import javax.xml.bind.annotation.XmlRootElement;
 32  
 import javax.xml.bind.annotation.XmlType;
 33  
 
 34  
 import org.apache.commons.lang.StringUtils;
 35  
 import org.kuali.rice.core.api.CoreConstants;
 36  
 import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
 37  
 import org.kuali.rice.core.api.mo.ModelBuilder;
 38  
 import org.w3c.dom.Element;
 39  
 
 40  
 @XmlRootElement(name = RemotableAttributeError.Constants.ROOT_ELEMENT_NAME)
 41  
 @XmlAccessorType(XmlAccessType.NONE)
 42  
 @XmlType(name = RemotableAttributeError.Constants.TYPE_NAME,
 43  
         propOrder = {RemotableAttributeError.Elements.ATTRIBUTE_NAME, RemotableAttributeError.Elements.ERRORS,
 44  
                 CoreConstants.CommonElements.FUTURE_ELEMENTS})
 45  0
 public final class RemotableAttributeError extends AbstractDataTransferObject implements AttributeError {
 46  
 
 47  
     @XmlElement(name = Elements.ATTRIBUTE_NAME, required = false)
 48  
     private final String attributeName;
 49  
     @XmlElementWrapper(name = Elements.ERRORS, required = true )
 50  
     @XmlElement(name = Elements.ERROR, required = false)
 51  
     private final List<String> errors;
 52  0
     @SuppressWarnings("unused") @XmlAnyElement
 53  
     private final Collection<Element> _futureElements = null;
 54  
 
 55  
     /**
 56  
      * Private constructor used only by JAXB.
 57  
      */
 58  0
     private RemotableAttributeError() {
 59  0
         this.attributeName = null;
 60  0
         this.errors = null;
 61  0
     }
 62  
 
 63  0
     private RemotableAttributeError(Builder builder) {
 64  0
         this.attributeName = builder.getAttributeName();
 65  0
         this.errors = builder.getErrors();
 66  0
     }
 67  
 
 68  
     @Override
 69  
     public String getAttributeName() {
 70  0
         return this.attributeName;
 71  
     }
 72  
 
 73  
     @Override
 74  
     public List<String> getErrors() {
 75  0
         return this.errors;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Utility method to search a collection of attribute errors and check in the collection
 80  
      * contains a error for a give attribute name.
 81  
      *
 82  
      * @param attributeName the name of the attribute to search for.  Cannot be blank or null.
 83  
      * @param errors cannot be null.
 84  
      *
 85  
      * @return true if list contains an error class for attribute name.
 86  
      */
 87  
     public static boolean containsAttribute(String attributeName, Collection<RemotableAttributeError> errors) {
 88  0
         if (StringUtils.isBlank(attributeName)) {
 89  0
             throw new IllegalArgumentException("attributeName is blank");
 90  
         }
 91  
 
 92  0
         if (errors == null) {
 93  0
             throw new IllegalArgumentException("errors is null");
 94  
         }
 95  
 
 96  0
         for (AttributeError error : errors) {
 97  0
             if (attributeName.equals(error.getAttributeName())) {
 98  0
                 return true;
 99  
             }
 100  
         }
 101  0
         return false;
 102  
     }
 103  
 
 104  
     /**
 105  
      * Utility method that iterates over a collection of AttributeErrors searching for multiple
 106  
      * AttributeError instances for the same attributeName.  If found those instances are combined.
 107  
      *
 108  
      * @param errors the errors.  cannot be null
 109  
      * @return a normalized list
 110  
      */
 111  
     public static List<RemotableAttributeError> normalizeRemotableAttributes(List<RemotableAttributeError> errors) {
 112  0
         if (errors == null) {
 113  0
             throw new IllegalArgumentException("errors is null");
 114  
         }
 115  
 
 116  0
         final List<RemotableAttributeError> normalized = new ArrayList<RemotableAttributeError>();
 117  0
         final Map<String, Integer> map = new HashMap<String, Integer>();
 118  
 
 119  0
         for (final RemotableAttributeError error :  errors) {
 120  0
             final Integer prevIndex = map.get(error.getAttributeName());
 121  0
             if (prevIndex == null) {
 122  0
                 final int insertIdx = normalized.size();
 123  0
                 map.put(error.getAttributeName(), insertIdx);
 124  0
                 normalized.add(insertIdx, error);
 125  0
             } else {
 126  0
                 final RemotableAttributeError prevError = normalized.get(prevIndex.intValue());
 127  0
                 final RemotableAttributeError.Builder b  = RemotableAttributeError.Builder.create(error.getAttributeName(), error.getErrors());
 128  0
                 b.addErrors(prevError.getErrors());
 129  0
                 normalized.set(prevIndex.intValue(), b.build());
 130  
             }
 131  0
         }
 132  0
         return normalized;
 133  
     }
 134  
 
 135  
     /**
 136  
      * A builder which can be used to construct {@link RemotableAttributeError} instances.  Enforces the constraints of
 137  
      * the {@link AttributeError}.
 138  
      */
 139  0
     public static final class Builder implements Serializable, ModelBuilder, AttributeError {
 140  
 
 141  
         private String attributeName;
 142  0
         private List<String> errors = new ArrayList<String>();
 143  
 
 144  0
         private Builder(String attributeName) {
 145  0
             this.attributeName = attributeName;
 146  0
         }
 147  
 
 148  
         public static Builder create(String attributeName) {
 149  0
             return new Builder(attributeName);
 150  
         }
 151  
 
 152  
         public static Builder create(String attributeName, List<String> errors) {
 153  0
             Builder b = new Builder(attributeName);
 154  0
             b.setErrors(errors);
 155  0
             return b;
 156  
         }
 157  
 
 158  
         public static Builder create(String attributeName, String... errors) {
 159  0
             Builder b = new Builder(attributeName);
 160  0
             b.addErrors(errors);
 161  0
             return b;
 162  
         }
 163  
 
 164  
         public static Builder create(AttributeError contract) {
 165  0
             if (contract == null) {
 166  0
                 throw new IllegalArgumentException("contract was null");
 167  
             }
 168  0
             Builder builder = create(contract.getAttributeName());
 169  0
             builder.setErrors(contract.getErrors());
 170  0
             return builder;
 171  
         }
 172  
 
 173  
         public RemotableAttributeError build() {
 174  0
             if (errors.isEmpty()) {
 175  0
                 throw new IllegalStateException("must contain at least one error");
 176  
             }
 177  
 
 178  0
             for (String err : errors) {
 179  0
                 if (StringUtils.isBlank(err)) {
 180  0
                     throw new IllegalStateException("contains a blank error");
 181  
                 }
 182  
             }
 183  
 
 184  0
             return new RemotableAttributeError(this);
 185  
         }
 186  
 
 187  
         @Override
 188  
         public String getAttributeName() {
 189  0
             return this.attributeName;
 190  
         }
 191  
 
 192  
         @Override
 193  
         public List<String> getErrors() {
 194  0
             return Collections.unmodifiableList(this.errors);
 195  
         }
 196  
 
 197  
         public void setAttributeName(String attributeName) {
 198  0
             if (StringUtils.isBlank(attributeName)) {
 199  0
                 throw new IllegalArgumentException("attributeName is blank");
 200  
             }
 201  
 
 202  0
             this.attributeName = attributeName;
 203  0
         }
 204  
 
 205  
         public void setErrors(List<String> errors) {
 206  0
             if (errors == null) {
 207  0
                 throw new IllegalArgumentException("errors is null");
 208  
             }
 209  
 
 210  0
             this.errors = new ArrayList<String>(errors);
 211  0
         }
 212  
 
 213  
         /**
 214  
          * Adds errors to the AttributeError.  The passed in errors cannot be null.
 215  
          *
 216  
          * @param errors any subsequent errors to add
 217  
          */
 218  
         public void addErrors(String... errors) {
 219  0
             if (errors == null) {
 220  0
                 throw new IllegalArgumentException("errors is null");
 221  
             }
 222  
 
 223  0
             this.errors.addAll(Arrays.asList(errors));
 224  0
         }
 225  
 
 226  
         /**
 227  
          * Adds errors to the AttributeError.  The passed in errors cannot be null.
 228  
          *
 229  
          * @param errors any subsequent errors to add
 230  
          */
 231  
         public void addErrors(List<String> errors) {
 232  0
             if (errors == null) {
 233  0
                 throw new IllegalArgumentException("errors is null");
 234  
             }
 235  
 
 236  0
             this.errors.addAll(errors);
 237  0
         }
 238  
     }
 239  
 
 240  
     /**
 241  
      * Defines some internal constants used on this class.
 242  
      */
 243  0
     static class Constants {
 244  
 
 245  
         final static String ROOT_ELEMENT_NAME = "attributeError";
 246  
         final static String TYPE_NAME = "attributeErrorType";
 247  
 
 248  
     }
 249  
 
 250  
     /**
 251  
      * A private class which exposes constants which define the XML element names to use when this object is marshalled
 252  
      * to XML.
 253  
      */
 254  0
     static class Elements {
 255  
 
 256  
         final static String ATTRIBUTE_NAME = "attributeName";
 257  
         final static String ERROR = "error";
 258  
         final static String ERRORS = "errors";
 259  
 
 260  
     }
 261  
 
 262  
 }