Coverage Report - org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo
 
Classes in this File Line Coverage Branch Coverage Complexity
KimAttributeDataBo
63%
7/11
50%
5/10
0
KimAttributeDataBo$_createFrom_closure2
0%
0/7
0%
0/6
0
KimAttributeDataBo$_toAttributes_closure1
85%
6/7
50%
3/6
0
 
 1  
 /*
 2  
  * Copyright 2006-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  
 
 17  
 package org.kuali.rice.kim.impl.common.attribute
 18  
 
 19  
 import javax.persistence.Transient
 20  
 import org.apache.commons.lang.StringUtils
 21  
 import org.kuali.rice.kim.api.common.attribute.KimAttributeDataContract
 22  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator
 23  
 import org.kuali.rice.kim.api.type.KimTypeAttribute
 24  
 import org.kuali.rice.kim.impl.type.KimTypeBo
 25  
 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase
 26  
 
 27  
 abstract class KimAttributeDataBo extends PersistableBusinessObjectBase implements KimAttributeDataContract {
 28  
     private static final long serialVersionUID = 1L;
 29  
 
 30  
     String id
 31  
     String attributeValue
 32  
     String kimAttributeId
 33  
     KimAttributeBo kimAttribute
 34  
     String kimTypeId
 35  
     @Transient
 36  
     KimTypeBo kimType
 37  
 
 38  
     abstract void setAssignedToId(String s);
 39  
 
 40  
     @Override
 41  
     KimAttributeBo getKimAttribute() {
 42  18
         return kimAttribute
 43  
     }
 44  
 
 45  
     @Override
 46  
     KimTypeBo getKimType() {
 47  34
         if (kimType == null && StringUtils.isNotEmpty(id)) {
 48  0
             kimType = KimTypeBo.from(KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId));
 49  
         }
 50  34
         return kimType;
 51  
     }
 52  
 
 53  
     static <T extends KimAttributeDataBo> Map<String, String> toAttributes(Collection<T> bos) {
 54  16
         def m = [:]
 55  16
         if(bos != null) {
 56  16
             bos.each {
 57  16
                 if (it != null) {
 58  16
                     KimTypeAttribute attribute = null;
 59  16
                     if ( it.kimType != null ) {
 60  16
                         attribute = KimTypeBo.to(it.kimType).getAttributeDefinitionById( it.getKimAttributeId() );
 61  
                     }
 62  16
                     if ( attribute != null ) {
 63  0
                         m[attribute.getKimAttribute().getAttributeName()] = it.getAttributeValue();
 64  
                     } else {
 65  16
                         m[it.getKimAttribute().getAttributeName()] = it.getAttributeValue();
 66  
                     }
 67  
                 }
 68  
             }
 69  
         }
 70  16
         return m;
 71  
     }
 72  
 
 73  
     /** creates a list of KimAttributeDataBos from attributes, kimTypeId, and assignedToId. */
 74  
     static <T extends KimAttributeDataBo> List<T> createFrom(Class<T> type, Map<String, String> attributes, String kimTypeId) {
 75  0
        if (attributes == null) {
 76  
            //purposely not using Collections.emptyList() b/c we do not want to return an unmodifiable list.
 77  0
            return new ArrayList<T>();
 78  
        }
 79  0
        return attributes.entrySet().collect {
 80  0
             KimTypeAttribute attr = KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId).getAttributeDefinitionByName(it.getKey());
 81  0
             if (attr != null && StringUtils.isNotBlank(it.getValue())) {
 82  0
                 T newDetail = type.newInstance();
 83  0
                 newDetail.setKimAttributeId(attr.getKimAttribute().getId());
 84  0
                 newDetail.setKimTypeId(kimTypeId);
 85  0
                 newDetail.setAttributeValue(it.getValue());
 86  0
                 return newDetail;
 87  
             }
 88  
         }
 89  
     }
 90  
 }