Coverage Report - org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KrmsAttributeDefinitionServiceImpl
21%
21/100
9%
4/44
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  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.Collections;
 22  
 import java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.commons.collections.CollectionUtils;
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.kuali.rice.krad.service.BusinessObjectService;
 29  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 30  
 import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
 31  
 
 32  
 
 33  4
 public final class KrmsAttributeDefinitionServiceImpl implements KrmsAttributeDefinitionService {
 34  
 
 35  
     private BusinessObjectService businessObjectService;
 36  4
     private final Map<String,KrmsAttributeDefinitionBo> krmsAttributeDefinitionIdCache = Collections.synchronizedMap( new HashMap<String,KrmsAttributeDefinitionBo>() );
 37  
 
 38  
         @Override
 39  
         /**
 40  
         * @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#convertAttributeKeys()
 41  
         */
 42  
         public Map<String,String> convertAttributeKeys(Map<String,String> attributesByName, String namespace) {
 43  0
                 Map<String,String> attributesById = new HashMap<String,String>();
 44  0
                 if(attributesByName != null) {
 45  0
                         for(Map.Entry<String, String> attr : attributesByName.entrySet()) {
 46  0
                                 String newKey = getKrmsAttributeId(attr.getKey(), namespace);
 47  0
                                 if(StringUtils.isNotEmpty(newKey)) {
 48  0
                                         attributesById.put(newKey, attr.getValue());
 49  
                                 }
 50  0
                         }
 51  
                 }
 52  0
                 return attributesById;
 53  
         }
 54  
    
 55  
         @Override
 56  
         /**
 57  
         * @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getKrmsAttributeId()
 58  
         */
 59  
         public String getKrmsAttributeId( String attributeName, String namespace) {
 60  0
                 String returnId = null;
 61  0
                 KrmsAttributeDefinitionBo bo = getKrmsAttributeBo(attributeName, namespace);
 62  0
                 if (bo != null){
 63  0
                         returnId = bo.getId();
 64  
                 }
 65  0
                 return returnId;
 66  
         }
 67  
     
 68  
         @Override
 69  
         /**
 70  
         * @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getKrmsAttributeBo()
 71  
         */
 72  
         public KrmsAttributeDefinitionBo getKrmsAttributeBo( String attributeName, String namespace) {
 73  6
                 String key = createKey(namespace, attributeName);
 74  6
                 if (krmsAttributeDefinitionIdCache.containsKey(key))
 75  0
                         return krmsAttributeDefinitionIdCache.get(key);                
 76  
 
 77  6
                 KrmsAttributeDefinitionBo result = null;
 78  6
                 Map<String,Object> criteria = new HashMap<String,Object>( 3 );
 79  6
                 criteria.put( "name", attributeName );
 80  6
                 criteria.put( "namespace", namespace );
 81  6
         criteria.put( "active", Boolean.TRUE );
 82  6
                 Collection<KrmsAttributeDefinitionBo> defs = getBusinessObjectService().findMatching( KrmsAttributeDefinitionBo.class, criteria );
 83  6
                 if(CollectionUtils.isNotEmpty(defs)) {
 84  6
                         if (defs.size() > 1){
 85  0
                                 throw new IllegalStateException("Multiple KrmsAttributeDefinitions found with same name and namespace");
 86  
                         }
 87  6
                         result = defs.iterator().next();
 88  6
                         krmsAttributeDefinitionIdCache.put(key, result);
 89  
                 }
 90  6
                 return result;
 91  
         }
 92  
     
 93  
         @Override
 94  
         /**
 95  
         * @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#createAttributeDefinition()
 96  
         */
 97  
         public KrmsAttributeDefinition createAttributeDefinition(KrmsAttributeDefinition attributeDefinition) {
 98  0
                 if (attributeDefinition == null){
 99  0
                 throw new IllegalArgumentException("attributeDefinition is null");
 100  
                 }
 101  0
                 final String nameKey = attributeDefinition.getName();
 102  0
                 final String namespaceKey = attributeDefinition.getNamespace();
 103  0
                 final KrmsAttributeDefinition existing = getAttributeDefinitionByNameAndNamespace(nameKey, namespaceKey);
 104  0
                 if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
 105  0
             throw new IllegalStateException("the krms attribute definition to create already exists: " + attributeDefinition);                        
 106  
                 }
 107  
                 
 108  0
                 KrmsAttributeDefinitionBo bo = KrmsAttributeDefinitionBo.from(attributeDefinition);
 109  0
                 getBusinessObjectService().save(bo);
 110  0
                 updateCache(bo);
 111  0
                 return KrmsAttributeDefinitionBo.to(bo);
 112  
         }
 113  
 
 114  
         @Override
 115  
         /**
 116  
         * @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#updateAttributeDefinition()
 117  
         */
 118  
         public void updateAttributeDefinition(KrmsAttributeDefinition attributeDefinition) {
 119  0
                 if (attributeDefinition == null){
 120  0
                 throw new IllegalArgumentException("attributeDefinition is null");
 121  
                 }
 122  0
                 final String idKey = attributeDefinition.getId();
 123  0
                 final KrmsAttributeDefinitionBo existing = getBusinessObjectService().findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, idKey);
 124  0
                 if (existing == null){
 125  0
             throw new IllegalStateException("the krms attribute definition does not exist: " + attributeDefinition);                        
 126  
                 }
 127  
                 final KrmsAttributeDefinition toUpdate;
 128  0
                 if (!existing.getId().equals(attributeDefinition.getId())){
 129  0
                         final KrmsAttributeDefinition.Builder builder = KrmsAttributeDefinition.Builder.create(attributeDefinition);
 130  0
                         builder.setId(existing.getId());
 131  0
                         toUpdate = builder.build();
 132  0
                 } else {
 133  0
                         toUpdate = attributeDefinition;
 134  
                 }
 135  0
                 KrmsAttributeDefinitionBo bo = KrmsAttributeDefinitionBo.from(toUpdate);
 136  0
                 getBusinessObjectService().save(bo);
 137  0
                 updateCache(bo);
 138  0
         }
 139  
 
 140  
         @Override
 141  
         /**
 142  
         * @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getAttributeDefinitionById()
 143  
         */
 144  
     public KrmsAttributeDefinition getAttributeDefinitionById(final String id) {
 145  0
         if (StringUtils.isBlank(id)) {
 146  0
             throw new IllegalArgumentException("id is blank");
 147  
         }
 148  0
                 if (krmsAttributeDefinitionIdCache.containsKey(id)){
 149  0
                         return KrmsAttributeDefinitionBo.to(krmsAttributeDefinitionIdCache.get(id));        
 150  
                 } 
 151  0
         KrmsAttributeDefinitionBo bo = getBusinessObjectService().findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, id);
 152  0
         return KrmsAttributeDefinitionBo.to(bo);
 153  
     }
 154  
 
 155  
     @Override
 156  
         /**
 157  
         * @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#getAttributeDefinitionByNameAndNamespace()
 158  
         */
 159  
     public KrmsAttributeDefinition getAttributeDefinitionByNameAndNamespace(final String name, final String namespace) {
 160  0
         if (StringUtils.isBlank(name)) {
 161  0
             throw new IllegalArgumentException("name is blank");
 162  
         }
 163  0
         if (StringUtils.isBlank(namespace)) {
 164  0
             throw new IllegalArgumentException("namespace is blank");
 165  
         }
 166  0
         KrmsAttributeDefinitionBo bo = getKrmsAttributeBo(name, namespace);
 167  0
         if (bo == null) 
 168  0
                 return null;
 169  0
                return KrmsAttributeDefinitionBo.to(bo);
 170  
     }
 171  
 
 172  
     @Override
 173  
         /**
 174  
         * @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#findAttributeDefinitionsByNamespace()
 175  
         */
 176  
     public List<KrmsAttributeDefinition> findAttributeDefinitionsByNamespace(final String namespace) {
 177  0
         final Map<String, Object> map = new HashMap<String, Object>();
 178  0
         map.put("namespace", namespace);
 179  0
         map.put("active", Boolean.TRUE);
 180  0
         Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = getBusinessObjectService().findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
 181  0
         return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
 182  
     }
 183  
 
 184  
     @Override
 185  
         /**
 186  
         * @see org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService#findAllAttributeDefinitions()
 187  
         */
 188  
     public List<KrmsAttributeDefinition> findAllAttributeDefinitions() {
 189  0
         final Map<String, Object> map = new HashMap<String, Object>();
 190  0
         map.put("active", Boolean.TRUE);
 191  
         
 192  0
         Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = getBusinessObjectService().findMatching(KrmsAttributeDefinitionBo.class, Collections.unmodifiableMap(map));
 193  0
         return convertListOfBosToImmutables(krmsAttributeDefinitionBos);
 194  
     }
 195  
 
 196  
     /**
 197  
      * Sets the businessObjectService attribute value.
 198  
      *
 199  
      * @param businessObjectService The businessObjectService to set.
 200  
      */
 201  
     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
 202  4
         this.businessObjectService = businessObjectService;
 203  4
     }
 204  
 
 205  
     /**
 206  
      * This method returns a reference to the businessObjectService.
 207  
      * If the businessObjectService is not set, get it from the KRADServiceLocator.
 208  
      * @return businessObjectService
 209  
      */
 210  
     protected BusinessObjectService getBusinessObjectService() {
 211  6
                 if ( businessObjectService == null ) {
 212  0
                         businessObjectService = KRADServiceLocator.getBusinessObjectService();
 213  
                 }
 214  6
                 return businessObjectService;
 215  
         }
 216  
 
 217  
     /**
 218  
      * Converts a List<KrmsAttributeDefinitionBo> to an Unmodifiable List<KrmsAttributeDefinition>
 219  
      *
 220  
      * @param krmsAttributeDefinitionBos a mutable List<KrmsAttributeDefinitionBo> to made completely immutable.
 221  
      * @return An unmodifiable List<KrmsAttributeDefinition>
 222  
      */
 223  
     public List<KrmsAttributeDefinition> convertListOfBosToImmutables(final Collection<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos) {
 224  0
         ArrayList<KrmsAttributeDefinition> krmsAttributeDefinitions = new ArrayList<KrmsAttributeDefinition>();
 225  0
         for (KrmsAttributeDefinitionBo bo : krmsAttributeDefinitionBos) {
 226  0
             KrmsAttributeDefinition krmsAttributeDefinition = KrmsAttributeDefinitionBo.to(bo);
 227  0
             krmsAttributeDefinitions.add(krmsAttributeDefinition);
 228  0
         }
 229  0
         return Collections.unmodifiableList(krmsAttributeDefinitions);
 230  
     }
 231  
 
 232  
     // Cache related methods
 233  
     
 234  
     public void clearCache(){
 235  0
             krmsAttributeDefinitionIdCache.clear();
 236  0
     }
 237  
     
 238  
     private String createKey(String namespace, String name){
 239  6
             String key = namespace + ":" + name;
 240  6
             return key;
 241  
     }
 242  
     
 243  
     private void updateCache(KrmsAttributeDefinitionBo bo){
 244  0
                 String key = createKey(bo.getNamespace(), bo.getName());
 245  0
                 if (bo.isActive()){
 246  0
                         krmsAttributeDefinitionIdCache.put(key, bo);
 247  
                 } else {
 248  0
                         krmsAttributeDefinitionIdCache.remove(key);
 249  
                 }
 250  0
     }
 251  
 }