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