Coverage Report - org.kuali.rice.krms.impl.repository.KrmsTypeBoServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KrmsTypeBoServiceImpl
89%
52/58
87%
21/24
3.2
 
 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.krad.service.BusinessObjectService;
 22  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
 23  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
 24  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
 25  
 
 26  
 import java.util.*;
 27  
 
 28  16
 public final class KrmsTypeBoServiceImpl implements KrmsTypeRepositoryService {
 29  
 
 30  
     private BusinessObjectService businessObjectService;
 31  
 
 32  
         /**
 33  
          * This overridden method creates a KrmsType if it does not 
 34  
          * already exist in the repository.
 35  
          * 
 36  
          * @see org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService#createKrmsType(org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition)
 37  
          */
 38  
         @Override
 39  
         public KrmsTypeDefinition createKrmsType(KrmsTypeDefinition krmsType) {
 40  3
                 if (krmsType == null){
 41  1
                 throw new IllegalArgumentException("krmsType is null");
 42  
                 }
 43  2
                 final String nameKey = krmsType.getName();
 44  2
                 final String namespaceKey = krmsType.getNamespace();
 45  2
                 final KrmsTypeDefinition existing = getTypeByNameAndNamespace(nameKey, namespaceKey);
 46  2
                 if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
 47  1
             throw new IllegalStateException("the KRMS Type to create already exists: " + krmsType);                        
 48  
                 }
 49  
                 
 50  1
                 KrmsTypeBo bo = (KrmsTypeBo)businessObjectService.save(KrmsTypeBo.from(krmsType));
 51  
                 
 52  1
                 return KrmsTypeBo.to(bo);
 53  
         }
 54  
 
 55  
         /**
 56  
          * This overridden method updates an existing KrmsType
 57  
          * 
 58  
          * @see org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService#updateKrmsType(org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition)
 59  
          */
 60  
         @Override
 61  
         public void updateKrmsType(KrmsTypeDefinition krmsType) {
 62  3
         if (krmsType == null) {
 63  1
             throw new IllegalArgumentException("krmsType is null");
 64  
         }
 65  2
                 final String idKey = krmsType.getId();
 66  2
                 final KrmsTypeBo existing = businessObjectService.findBySinglePrimaryKey(KrmsTypeBo.class, idKey);
 67  2
         if (existing == null) {
 68  1
             throw new IllegalStateException("the KRMS type does not exist: " + krmsType);
 69  
         }
 70  
         final KrmsTypeDefinition toUpdate;
 71  1
         if (!existing.getId().equals(krmsType.getId())){
 72  0
                 final KrmsTypeDefinition.Builder builder = KrmsTypeDefinition.Builder.create(krmsType);
 73  0
                 builder.setId(existing.getId());
 74  0
                 toUpdate = builder.build();
 75  0
         } else {
 76  1
                 toUpdate = krmsType;
 77  
         }
 78  
         
 79  1
         businessObjectService.save(KrmsTypeBo.from(toUpdate));
 80  1
         }
 81  
 
 82  
     @Override
 83  
     public KrmsTypeDefinition getTypeById(final String id) {
 84  4
         if (StringUtils.isBlank(id)) {
 85  2
             throw new IllegalArgumentException("id is blank");
 86  
         }
 87  
 
 88  2
         KrmsTypeBo krmsTypeBo = businessObjectService.findBySinglePrimaryKey(KrmsTypeBo.class, id);
 89  
 
 90  2
         return KrmsTypeBo.to(krmsTypeBo);
 91  
     }
 92  
 
 93  
     @Override
 94  
     public KrmsTypeDefinition getTypeByNameAndNamespace(final String name, final String namespace) {
 95  5
         if (StringUtils.isBlank(name)) {
 96  1
             throw new IllegalArgumentException("name is blank");
 97  
         }
 98  4
         if (StringUtils.isBlank(namespace)) {
 99  1
             throw new IllegalArgumentException("namespace is blank");
 100  
         }
 101  
 
 102  3
         final Map<String, Object> map = new HashMap<String, Object>();
 103  3
         map.put("name", name);
 104  3
         map.put("namespace", namespace);
 105  
 
 106  3
         KrmsTypeBo myType = businessObjectService.findByPrimaryKey(KrmsTypeBo.class, Collections.unmodifiableMap(map));
 107  3
         return KrmsTypeBo.to(myType);
 108  
     }
 109  
 
 110  
     @Override
 111  
     public List<KrmsTypeDefinition> findAllTypesByNamespace(final String namespace) {
 112  2
         if (StringUtils.isBlank(namespace)) {
 113  1
             throw new IllegalArgumentException("namespace is blank");
 114  
         }
 115  1
         final Map<String, Object> map = new HashMap<String, Object>();
 116  1
         map.put("namespace", namespace);
 117  1
         map.put("active", Boolean.TRUE);
 118  
 
 119  1
         Collection<KrmsTypeBo> krmsTypeBos = businessObjectService.findMatching(KrmsTypeBo.class, Collections.unmodifiableMap(map));
 120  
 
 121  1
         return convertListOfBosToImmutables(krmsTypeBos);
 122  
     }
 123  
 
 124  
     @Override
 125  
     public List<KrmsTypeDefinition> findAllTypes() {
 126  1
         final Map<String, Object> map = new HashMap<String, Object>();
 127  1
         map.put("active", Boolean.TRUE);
 128  
 
 129  1
         Collection<KrmsTypeBo> krmsTypeBos = businessObjectService.findMatching(KrmsTypeBo.class, Collections.unmodifiableMap(map));
 130  1
         return convertListOfBosToImmutables(krmsTypeBos);
 131  
     }
 132  
 
 133  
     /**
 134  
      * Sets the businessObjectService attribute value.
 135  
      *
 136  
      * @param businessObjectService The businessObjectService to set.
 137  
      */
 138  
     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
 139  11
         this.businessObjectService = businessObjectService;
 140  11
     }
 141  
 
 142  
     /**
 143  
      * Converts a List<KrmsTypeBo> to an Unmodifiable List<KrmsType>
 144  
      *
 145  
      * @param KrmsTypeBos a mutable List<KrmsTypeBo> to made completely immutable.
 146  
      * @return An unmodifiable List<KrmsType>
 147  
      */
 148  
     List<KrmsTypeDefinition> convertListOfBosToImmutables(final Collection<KrmsTypeBo> krmsTypeBos) {
 149  2
         ArrayList<KrmsTypeDefinition> krmsTypes = new ArrayList<KrmsTypeDefinition>();
 150  2
         for (KrmsTypeBo bo : krmsTypeBos) {
 151  5
             KrmsTypeDefinition krmsType = KrmsTypeBo.to(bo);
 152  5
             krmsTypes.add(krmsType);
 153  5
         }
 154  2
         return Collections.unmodifiableList(krmsTypes);
 155  
     }
 156  
 
 157  
         /**
 158  
          * This overridden method ...
 159  
          * 
 160  
          * @see org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService#createKrmsTypeAttribute(org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute)
 161  
          */
 162  
         @Override
 163  
         public void createKrmsTypeAttribute(KrmsTypeAttribute krmsTypeAttribute) {
 164  
                 // TODO dseibert - THIS METHOD NEEDS JAVADOCS
 165  0
                 throw new UnsupportedOperationException();
 166  
         }
 167  
 
 168  
         /**
 169  
          * This overridden method ...
 170  
          * 
 171  
          * @see org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService#updateKrmsTypeAttribute(org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute)
 172  
          */
 173  
         @Override
 174  
         public void updateKrmsTypeAttribute(KrmsTypeAttribute krmsTypeAttribute) {
 175  
                 // TODO dseibert - THIS METHOD NEEDS JAVADOCS
 176  0
                 throw new UnsupportedOperationException();
 177  
         }
 178  
 }