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