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