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