Coverage Report - org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KrmsAttributeDefinitionServiceImpl
0%
0/58
0%
0/26
3.875
 
 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.krms.impl.repository;
 18  
 
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.kns.service.BusinessObjectService;
 22  
 import org.kuali.rice.krms.api.repository.KrmsAttributeDefinition;
 23  
 import org.kuali.rice.krms.api.repository.KrmsAttributeDefinitionService;
 24  
 
 25  
 import java.util.*;
 26  
 
 27  0
 public final class KrmsAttributeDefinitionServiceImpl implements KrmsAttributeDefinitionService {
 28  
 
 29  
     private BusinessObjectService businessObjectService;
 30  
 
 31  
         @Override
 32  
         public void createAttributeDefinition(KrmsAttributeDefinition attributeDefinition) {
 33  0
                 if (attributeDefinition == null){
 34  0
                 throw new IllegalArgumentException("attributeDefinition is null");
 35  
                 }
 36  0
                 final String idKey = attributeDefinition.getId();
 37  0
                 final KrmsAttributeDefinition existing = getAttributeDefinitionById(idKey);
 38  0
                 if (existing != null && existing.getId().equals(idKey)){
 39  0
             throw new IllegalStateException("the krms attribute definition to create already exists: " + attributeDefinition);                        
 40  
                 }
 41  0
                 businessObjectService.save(KrmsAttributeDefinitionBo.from(attributeDefinition));
 42  0
         }
 43  
 
 44  
         @Override
 45  
         public void updateAttributeDefinition(KrmsAttributeDefinition attributeDefinition) {
 46  0
                 if (attributeDefinition == null){
 47  0
                 throw new IllegalArgumentException("attributeDefinition is null");
 48  
                 }
 49  0
                 final String nameKey = attributeDefinition.getName();
 50  0
                 final String namespaceKey = attributeDefinition.getNamespace();
 51  0
                 final KrmsAttributeDefinition existing = getAttributeDefinitionByNameAndNamespace(nameKey, namespaceKey);
 52  0
                 if (existing == null){
 53  0
             throw new IllegalStateException("the krms attribute definition does not exist: " + attributeDefinition);                        
 54  
                 }
 55  
                 final KrmsAttributeDefinition toUpdate;
 56  0
                 if (!existing.getId().equals(attributeDefinition.getId())){
 57  0
                         final KrmsAttributeDefinition.Builder builder = KrmsAttributeDefinition.Builder.create(attributeDefinition);
 58  0
                         builder.setId(existing.getId());
 59  0
                         toUpdate = builder.build();
 60  0
                 } else {
 61  0
                         toUpdate = attributeDefinition;
 62  
                 }
 63  
                 
 64  0
                 businessObjectService.save(KrmsAttributeDefinitionBo.from(toUpdate));
 65  0
         }
 66  
 
 67  
         @Override
 68  
     public KrmsAttributeDefinition getAttributeDefinitionById(final String id) {
 69  0
         if (StringUtils.isBlank(id)) {
 70  0
             throw new IllegalArgumentException("id is blank");
 71  
         }
 72  0
         KrmsAttributeDefinitionBo bo = businessObjectService.findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, id);
 73  0
         return KrmsAttributeDefinitionBo.to(bo);
 74  
     }
 75  
 
 76  
     @Override
 77  
     public KrmsAttributeDefinition getAttributeDefinitionByNameAndNamespace(final String name, final String namespace) {
 78  0
         if (StringUtils.isBlank(name)) {
 79  0
             throw new IllegalArgumentException("name is blank");
 80  
         }
 81  0
         if (StringUtils.isBlank(namespace)) {
 82  0
             throw new IllegalArgumentException("namespace is blank");
 83  
         }
 84  0
         final Map<String, Object> map = new HashMap<String, Object>();
 85  0
         map.put("name", name);
 86  0
         map.put("namespace", namespace);
 87  0
         Collection<KrmsAttributeDefinitionBo> definitionList = businessObjectService.findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
 88  0
         if (definitionList == null || definitionList.isEmpty()) {
 89  0
             return null;
 90  0
         } else if (definitionList.size() == 1) {
 91  0
             return KrmsAttributeDefinitionBo.to(definitionList.iterator().next());
 92  0
         } else throw new IllegalStateException("Multiple KkrmsAttributeDefinitions found with same name and namespace");
 93  
     }
 94  
 
 95  
     @Override
 96  
     public List<KrmsAttributeDefinition> findAttributeDefinitionsByNamespace(final String namespace) {
 97  0
         final Map<String, Object> map = new HashMap<String, Object>();
 98  0
         map.put("namespace", namespace);
 99  0
         map.put("active", Boolean.TRUE);
 100  0
         Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = businessObjectService.findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
 101  0
         return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
 102  
     }
 103  
 
 104  
     @Override
 105  
     public List<KrmsAttributeDefinition> findAllAttributeDefinitions() {
 106  0
         final Map<String, Object> map = new HashMap<String, Object>();
 107  0
         map.put("active", Boolean.TRUE);
 108  
         
 109  0
         Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = businessObjectService.findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
 110  0
         return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
 111  
     }
 112  
 
 113  
     /**
 114  
      * Sets the businessObjectService attribute value.
 115  
      *
 116  
      * @param businessObjectService The businessObjectService to set.
 117  
      */
 118  
     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
 119  0
         this.businessObjectService = businessObjectService;
 120  0
     }
 121  
 
 122  
     /**
 123  
      * Converts a List<KrmsAttributeDefinitionBo> to an Unmodifiable List<KrmsAttributeDefinition>
 124  
      *
 125  
      * @param krmsAttributeDefinitionBos a mutable List<KrmsAttributeDefinitionBo> to made completely immutable.
 126  
      * @return An unmodifiable List<KrmsAttributeDefinition>
 127  
      */
 128  
     List<KrmsAttributeDefinition> convertListOfBosToImmutables(final Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos) {
 129  0
         ArrayList<KrmsAttributeDefinition> krmsAttributeDefinitions = new ArrayList<KrmsAttributeDefinition>();
 130  0
         for (KrmsAttributeDefinitionBo bo : krmsAttributeDefinitionBos) {
 131  0
             KrmsAttributeDefinition krmsAttributeDefinition = KrmsAttributeDefinitionBo.to(bo);
 132  0
             krmsAttributeDefinitions.add(krmsAttributeDefinition);
 133  0
         }
 134  0
         return Collections.unmodifiableList(krmsAttributeDefinitions);
 135  
     }
 136  
 
 137  
 }