Coverage Report - org.kuali.rice.kim.document.rule.AttributeValidationHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
AttributeValidationHelper
0%
0/75
0%
0/42
3.333
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.kim.document.rule;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.core.api.uif.RemotableAttributeError;
 21  
 import org.kuali.rice.kim.api.type.KimTypeAttribute;
 22  
 import org.kuali.rice.kim.bo.ui.KimDocumentAttributeDataBusinessObjectBase;
 23  
 import org.kuali.rice.kim.impl.common.attribute.KimAttributeBo;
 24  
 import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
 25  
 import org.kuali.rice.kim.util.KimConstants;
 26  
 import org.kuali.rice.krad.service.BusinessObjectService;
 27  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 28  
 import org.kuali.rice.krad.util.GlobalVariables;
 29  
 import org.kuali.rice.krad.util.KRADConstants;
 30  
 import org.kuali.rice.krad.util.KRADPropertyConstants;
 31  
 
 32  
 import java.util.ArrayList;
 33  
 import java.util.HashMap;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 36  
 
 37  
 /**
 38  
  * This is a description of what this class does - wliang don't forget to fill this in. 
 39  
  * 
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  *
 42  
  */
 43  0
 public class AttributeValidationHelper {
 44  0
         private static final Logger LOG = Logger.getLogger(AttributeValidationHelper.class);
 45  
         
 46  
         protected BusinessObjectService businessObjectService;
 47  0
     protected Map<String,KimAttributeBo> attributeDefinitionMap = new HashMap<String,KimAttributeBo>();
 48  
     
 49  
     protected KimAttributeBo getAttributeDefinition( String id ) {
 50  0
             KimAttributeBo attributeImpl = attributeDefinitionMap.get( id );
 51  
             
 52  0
             if ( attributeImpl == null ) {
 53  0
                         Map<String,String> criteria = new HashMap<String,String>();
 54  0
                         criteria.put( KimConstants.PrimaryKeyConstants.KIM_ATTRIBUTE_ID, id );
 55  0
                         attributeImpl = (KimAttributeBo)getBusinessObjectService().findByPrimaryKey( KimAttributeBo.class, criteria );
 56  0
                         attributeDefinitionMap.put( id, attributeImpl );
 57  
             }
 58  0
             return attributeImpl;
 59  
     }
 60  
     
 61  
         public Map<String, String> convertAttributesToMap(List<? extends KimAttributeDataBo> attributes) {
 62  0
                 Map<String, String> m = new HashMap<String, String>();
 63  0
                 for(KimAttributeDataBo data: attributes) {
 64  0
                         KimAttributeBo attrib = getAttributeDefinition(data.getKimAttributeId());
 65  0
                         if(attrib != null){
 66  0
                                 m.put(attrib.getAttributeName(), data.getAttributeValue());
 67  
                         } else {
 68  0
                                 LOG.error("Unable to get attribute name for ID:" + data.getKimAttributeId());
 69  
                         }
 70  0
                 }
 71  0
                 return m;
 72  
         }
 73  
 
 74  
         public Map<String, String> convertQualifiersToMap( List<? extends KimDocumentAttributeDataBusinessObjectBase> qualifiers ) {
 75  0
                 Map<String, String> m = new HashMap<String, String>();
 76  0
                 for ( KimDocumentAttributeDataBusinessObjectBase data : qualifiers ) {
 77  0
                         KimAttributeBo attrib = getAttributeDefinition( data.getKimAttrDefnId() );
 78  0
                         if ( attrib != null ) {
 79  0
                                 m.put( attrib.getAttributeName(), data.getAttrVal() );
 80  
                         } else {
 81  0
                                 LOG.error("Unable to get attribute name for ID:" + data.getKimAttrDefnId() );
 82  
                         }
 83  0
                 }
 84  0
                 return m;
 85  
         }
 86  
 
 87  
         public Map<String, String> getBlankValueQualifiersMap(List<KimTypeAttribute> attributes) {
 88  0
                 Map<String, String> m = new HashMap<String, String>();
 89  0
                 for(KimTypeAttribute attribute: attributes){
 90  0
                         KimAttributeBo attrib = getAttributeDefinition(attribute.getId());
 91  0
                         if ( attrib != null ) {
 92  0
                                 m.put( attrib.getAttributeName(), "" );
 93  
                         } else {
 94  0
                                 LOG.error("Unable to get attribute name for ID:" + attribute.getId());
 95  
                         }
 96  0
                 }
 97  0
                 return m;
 98  
         }
 99  
 
 100  
         public Map<String, String> convertQualifiersToAttrIdxMap( List<? extends KimDocumentAttributeDataBusinessObjectBase> qualifiers ) {
 101  0
                 Map<String, String> m = new HashMap<String, String>();
 102  0
                 int i = 0;
 103  0
                 for ( KimDocumentAttributeDataBusinessObjectBase data : qualifiers ) {
 104  0
                         KimAttributeBo attrib = getAttributeDefinition( data.getKimAttrDefnId() );
 105  0
                         if ( attrib != null ) {
 106  0
                                 m.put( attrib.getAttributeName(), Integer.toString(i) );
 107  
                         } else {
 108  0
                                 LOG.error("Unable to get attribute name for ID:" + data.getKimAttrDefnId() );
 109  
                         }
 110  0
                         i++;
 111  0
                 }
 112  0
                 return m;
 113  
         }
 114  
         
 115  
         public BusinessObjectService getBusinessObjectService() {
 116  0
                 if(businessObjectService == null){
 117  0
                         businessObjectService = KRADServiceLocator.getBusinessObjectService();
 118  
                 }
 119  0
                 return businessObjectService;
 120  
         }
 121  
         
 122  
     public void moveValidationErrorsToErrorMap(List<RemotableAttributeError> validationErrors) {
 123  
                 // FIXME: This does not use the correct error path yet - may need to be moved up so that the error path is known
 124  
                 // Also, the above code would overwrite messages on the same attributes (namespaceCode) but on different rows
 125  0
                 for ( RemotableAttributeError error : validationErrors) {
 126  0
                     for (String errMsg : error.getErrors()) {
 127  0
                 String[] splitMsg = StringUtils.split(errMsg, ":");
 128  0
                             GlobalVariables.getMessageMap().putError( error.getAttributeName(), splitMsg[0], splitMsg.length > 1 ? StringUtils.split(splitMsg[1], ";") : new String[] {} );
 129  0
             }
 130  
                 }
 131  0
     }
 132  
 
 133  
         public List<RemotableAttributeError> convertErrorsForMappedFields(String errorPath, List<RemotableAttributeError> localErrors) {
 134  0
                 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>();
 135  0
                 if (errorPath == null) {
 136  0
                         errorPath = KRADConstants.EMPTY_STRING;
 137  
                 }
 138  0
                 else if (StringUtils.isNotEmpty(errorPath)) {
 139  0
                         errorPath = errorPath + ".";
 140  
                 }
 141  0
                 for ( RemotableAttributeError error : localErrors) {
 142  0
                         Map<String,String> criteria = new HashMap<String,String>();
 143  0
                         criteria.put(KRADPropertyConstants.ATTRIBUTE_NAME, error.getAttributeName());
 144  0
                         KimAttributeBo attribute = getBusinessObjectService().findByPrimaryKey(KimAttributeBo.class, criteria);
 145  0
                         String attributeDefnId = attribute==null?"":attribute.getId();
 146  0
                         errors.add(RemotableAttributeError.Builder.create(errorPath+"qualifier("+attributeDefnId+").attrVal", error.getErrors()).build());
 147  0
                 }
 148  0
                 return errors;
 149  
         }
 150  
 
 151  
         public List<RemotableAttributeError> convertErrors(String errorPath, Map<String, String> attrIdxMap, List<RemotableAttributeError> localErrors) {
 152  0
                 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>();
 153  0
                 if (errorPath == null) {
 154  0
                         errorPath = KRADConstants.EMPTY_STRING;
 155  
                 }
 156  0
                 else if (StringUtils.isNotEmpty(errorPath)) {
 157  0
                         errorPath = errorPath + ".";
 158  
                 }
 159  0
                 for ( RemotableAttributeError error : localErrors ) {
 160  0
                         for (String errMsg : error.getErrors()) {
 161  0
                 errors.add(RemotableAttributeError.Builder.create(errorPath+"qualifiers["+attrIdxMap.get(error.getAttributeName())+"].attrVal", errMsg).build());
 162  
             }
 163  
                 }
 164  0
                 return errors;
 165  
         }
 166  
 }