Coverage Report - org.kuali.rice.core.impl.component.ComponentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentServiceImpl
98%
93/94
92%
39/42
3.071
ComponentServiceImpl$1
100%
2/2
N/A
3.071
 
 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.core.impl.component;
 17  
 
 18  
 import org.apache.commons.collections.CollectionUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.commons.lang.builder.CompareToBuilder;
 21  
 import org.apache.log4j.Logger;
 22  
 import org.kuali.rice.core.api.component.Component;
 23  
 import org.kuali.rice.core.api.component.ComponentService;
 24  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 25  
 import org.kuali.rice.core.api.util.ChecksumUtils;
 26  
 import org.kuali.rice.krad.service.BusinessObjectService;
 27  
 import org.springframework.transaction.annotation.Transactional;
 28  
 
 29  
 import java.sql.Timestamp;
 30  
 import java.util.ArrayList;
 31  
 import java.util.Collection;
 32  
 import java.util.Collections;
 33  
 import java.util.Comparator;
 34  
 import java.util.HashMap;
 35  
 import java.util.List;
 36  
 import java.util.Map;
 37  
 
 38  
 /**
 39  
  * Reference implementation of the {@code ComponentService}.
 40  
  *
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  
 @Transactional
 44  38
 public class ComponentServiceImpl implements ComponentService {
 45  
 
 46  1
     private static final Logger LOG = Logger.getLogger(ComponentServiceImpl.class);
 47  
 
 48  
     private BusinessObjectService businessObjectService;
 49  
     private ComponentSetDao componentSetDao;
 50  
 
 51  
     @Override
 52  
     public Component getComponentByCode(String namespaceCode, String componentCode) {
 53  8
         if (StringUtils.isBlank(namespaceCode)) {
 54  3
             throw new RiceIllegalArgumentException("namespaceCode was a null or blank value");
 55  
         }
 56  5
         if (StringUtils.isBlank(componentCode)) {
 57  3
             throw new RiceIllegalArgumentException("componentCode was a null or blank value");
 58  
         }
 59  2
         Map<String, String> primaryKeys = new HashMap<String, String>();
 60  2
         primaryKeys.put("namespaceCode", namespaceCode);
 61  2
         primaryKeys.put("code", componentCode);
 62  2
         ComponentBo componentBo = getBusinessObjectService().findByPrimaryKey(ComponentBo.class, primaryKeys);
 63  2
         if (componentBo != null) {
 64  1
             return ComponentBo.to(componentBo);
 65  
         }
 66  1
         DerivedComponentBo derivedComponentBo = getBusinessObjectService().findByPrimaryKey(DerivedComponentBo.class, primaryKeys);
 67  1
         return derivedComponentBo == null ? null : DerivedComponentBo.to(derivedComponentBo);
 68  
     }
 69  
 
 70  
     @Override
 71  
     public List<Component> getAllComponentsByNamespaceCode(String namespaceCode) {
 72  6
         if (StringUtils.isBlank(namespaceCode)) {
 73  3
             throw new RiceIllegalArgumentException("namespaceCode was a null or blank value");
 74  
         }
 75  3
         Map<String, String> criteria = new HashMap<String, String>();
 76  3
         criteria.put("namespaceCode", namespaceCode);
 77  
 
 78  3
         List<Component> components = new ArrayList<Component>();
 79  
 
 80  3
         Collection<ComponentBo> componentBos =
 81  
                 getBusinessObjectService().findMatching(ComponentBo.class, criteria);
 82  3
         Collection<DerivedComponentBo> derivedComponentBos =
 83  
                 getBusinessObjectService().findMatching(DerivedComponentBo.class, criteria);
 84  3
         return translateCollections(componentBos, derivedComponentBos);
 85  
     }
 86  
 
 87  
     @Override
 88  
     public List<Component> getActiveComponentsByNamespaceCode(String namespaceCode) {
 89  6
         if (StringUtils.isBlank(namespaceCode)) {
 90  3
             throw new RiceIllegalArgumentException("namespaceCode was a null or blank value");
 91  
         }
 92  3
         Map<String, Object> criteria = new HashMap<String, Object>();
 93  3
         criteria.put("namespaceCode", namespaceCode);
 94  3
         criteria.put("active", Boolean.TRUE);
 95  3
         Collection<ComponentBo> componentBos =
 96  
                 getBusinessObjectService().findMatching(ComponentBo.class, criteria);
 97  3
         criteria.remove("active");
 98  3
         Collection<DerivedComponentBo> derivedComponentBos =
 99  
                 getBusinessObjectService().findMatching(DerivedComponentBo.class, criteria);
 100  3
         return translateCollections(componentBos, derivedComponentBos);
 101  
     }
 102  
 
 103  
     @Override
 104  
     public List<Component> getDerivedComponentSet(String componentSetId) {
 105  8
         if (StringUtils.isBlank(componentSetId)) {
 106  3
             throw new RiceIllegalArgumentException("componentSetId was a null or blank value");
 107  
         }
 108  5
         Map<String, Object> criteria = new HashMap<String, Object>();
 109  5
         criteria.put("componentSetId", componentSetId);
 110  5
         Collection<DerivedComponentBo> derivedComponentBos =
 111  
                 getBusinessObjectService().findMatching(DerivedComponentBo.class, criteria);
 112  5
         return translateCollections(null, derivedComponentBos);
 113  
     }
 114  
 
 115  
     @Override
 116  
     public void publishDerivedComponents(String componentSetId, List<Component> components) {
 117  7
         if (StringUtils.isBlank(componentSetId)) {
 118  3
             throw new RiceIllegalArgumentException("componentSetId was a null or blank value");
 119  
         }
 120  4
         components = validateAndNormalizeComponents(componentSetId, components);
 121  3
         LOG.info("Requesting to publish " + components.size() + " derived components for componentSetId=" + componentSetId);
 122  3
         ComponentSetBo componentSet = getComponentSetDao().getComponentSet(componentSetId);
 123  3
         if (componentSet == null) {
 124  2
             componentSet = new ComponentSetBo();
 125  2
             componentSet.setComponentSetId(componentSetId);
 126  
         }
 127  3
         String checksum = calculateChecksum(components);
 128  3
         if (!checksum.equals(componentSet.getChecksum())) {
 129  3
             LOG.info("Checksums were different, proceeding with update of derived components for componentSetId=" + componentSetId);
 130  3
             componentSet.setChecksum(checksum);
 131  3
             componentSet.setLastUpdateTimestamp(new Timestamp(System.currentTimeMillis()));
 132  3
             if (getComponentSetDao().saveIgnoreLockingFailure(componentSet)) {
 133  3
                 updateDerivedComponents(componentSetId, components);
 134  
             }
 135  
         } else {
 136  0
             LOG.info("Checksums were the same, no derived component update needed for componentSetId=" + componentSetId);
 137  
         }
 138  3
     }
 139  
 
 140  
     protected List<Component> validateAndNormalizeComponents(String componentSetId, List<Component> components) {
 141  4
         List<Component> processedComponents = new ArrayList<Component>();
 142  
 
 143  
         // normalize and copy component list, we will later sort this list possibly so don't want to hold onto the original
 144  4
         if (components == null) {
 145  1
             components = new ArrayList<Component>();
 146  
         } else {
 147  3
             components = new ArrayList<Component>(components);
 148  
         }
 149  
         // components must either have a null componentSetId or one which matches the componentSetId being published
 150  4
         for (Component component : components) {
 151  
             // if componentSetId is null, recreate the component with that value
 152  2
             if (component.getComponentSetId() == null) {
 153  1
                 Component.Builder builder = Component.Builder.create(component);
 154  1
                 builder.setComponentSetId(componentSetId);
 155  1
                 component = builder.build();
 156  
             }
 157  2
             String currentComponentSetId = component.getComponentSetId();
 158  2
             if (!componentSetId.equals(currentComponentSetId)) {
 159  1
                 throw new RiceIllegalArgumentException("Encountered a component with an invalid componentSetId of '" +
 160  
                         currentComponentSetId + "'.  Expected null or '" + componentSetId + "'.");
 161  
             }
 162  1
             processedComponents.add(component);
 163  1
         }
 164  3
         return processedComponents;
 165  
     }
 166  
 
 167  
     /**
 168  
      * Calculates the checksum for the list of components.  The list of components should be sorted in a
 169  
      * consistent way prior to generation of the checksum to ensure that the checksum value comes out the same regardless
 170  
      * of the ordering of components contained therein.  The checksum allows us to easily determine if the component set
 171  
      * has been updated or not.
 172  
      */
 173  
     protected String calculateChecksum(List<Component> components) {
 174  14
         Collections.sort(components, new Comparator<Component>() {
 175  
             @Override
 176  
             public int compare(Component component1, Component component2) {
 177  7
                 return CompareToBuilder.reflectionCompare(component1, component2);
 178  
             }
 179  
         });
 180  7
         return ChecksumUtils.calculateChecksum(components);
 181  
     }
 182  
 
 183  
     protected void updateDerivedComponents(String componentSetId, List<Component> components) {
 184  3
         Map<String, Object> deleteCriteria = new HashMap<String, Object>();
 185  3
         deleteCriteria.put("componentSetId", componentSetId);
 186  3
         businessObjectService.deleteMatching(DerivedComponentBo.class, deleteCriteria);
 187  3
         if (CollectionUtils.isNotEmpty(components)) {
 188  1
             List<DerivedComponentBo> derivedComponentBos = new ArrayList<DerivedComponentBo>();
 189  1
             for (Component component : components) {
 190  1
                 derivedComponentBos.add(DerivedComponentBo.from(component));
 191  
             }
 192  1
             businessObjectService.save(derivedComponentBos);
 193  
         }
 194  3
     }
 195  
 
 196  
     protected List<Component> translateCollections(Collection<ComponentBo> componentBos, Collection<DerivedComponentBo> derivedComponentBos) {
 197  16
         List<Component> components = new ArrayList<Component>();
 198  16
         if (CollectionUtils.isNotEmpty(componentBos)) {
 199  6
             for (ComponentBo componentBo : componentBos) {
 200  6
                 components.add(ComponentBo.to(componentBo));
 201  
             }
 202  
         }
 203  16
         if (CollectionUtils.isNotEmpty(derivedComponentBos)) {
 204  5
             for (DerivedComponentBo derivedComponentBo : derivedComponentBos) {
 205  5
                 components.add(DerivedComponentBo.to(derivedComponentBo));
 206  
             }
 207  
         }
 208  16
         return Collections.unmodifiableList(components);
 209  
     }
 210  
 
 211  
     public BusinessObjectService getBusinessObjectService() {
 212  20
         return businessObjectService;
 213  
     }
 214  
 
 215  
     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
 216  31
         this.businessObjectService = businessObjectService;
 217  31
     }
 218  
 
 219  
     public ComponentSetDao getComponentSetDao() {
 220  6
         return componentSetDao;
 221  
     }
 222  
 
 223  
     public void setComponentSetDao(ComponentSetDao componentSetDao) {
 224  3
         this.componentSetDao = componentSetDao;
 225  3
     }
 226  
     
 227  
 }