Coverage Report - org.kuali.rice.krms.impl.repository.PropositionBoServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PropositionBoServiceImpl
88%
63/71
88%
30/34
4.333
 
 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 java.util.HashMap;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.kuali.rice.krad.service.BusinessObjectService;
 26  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
 27  
 import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
 28  
 
 29  29
 public final class PropositionBoServiceImpl implements PropositionBoService {
 30  
 
 31  
     private BusinessObjectService businessObjectService;
 32  
 
 33  
         /**
 34  
          * This overridden method creates a Proposition if it does not 
 35  
          * already exist in the repository.
 36  
          * 
 37  
          * @see org.kuali.rice.krms.impl.repository.PropositionBoService#createProposition(org.kuali.rice.krms.api.repository.proposition.PropositionDefinition)
 38  
          */
 39  
         @Override
 40  
         public PropositionDefinition createProposition(PropositionDefinition prop) {
 41  4
                 if (prop == null){
 42  1
                 throw new IllegalArgumentException("proposition is null");
 43  
                 }
 44  3
                 if (null != prop.getId()) {
 45  1
                     throw new IllegalStateException("for creation, PropositionDefinition.id must be null");
 46  
             }
 47  
                 
 48  2
                 PropositionBo propositionBo = PropositionBo.from(prop);
 49  2
                 businessObjectService.save(propositionBo);
 50  2
                 return PropositionBo.to(propositionBo);
 51  
         }
 52  
 
 53  
         /**
 54  
          * This overridden method updates an existing proposition
 55  
          * 
 56  
          * @see org.kuali.rice.krms.impl.repository.PropositionBoService#updateProposition(org.kuali.rice.krms.api.repository.proposition.PropositionDefinition)
 57  
          */
 58  
         @Override
 59  
         public void updateProposition(PropositionDefinition prop) {
 60  3
         if (prop == null) {
 61  1
             throw new IllegalArgumentException("proposition is null");
 62  
         }
 63  2
                 final String propIdKey = prop.getId();
 64  2
                 final PropositionDefinition existing = getPropositionById(propIdKey);
 65  2
         if (existing == null) {
 66  1
             throw new IllegalStateException("the proposition does not exist: " + prop);
 67  
         }
 68  
         final PropositionDefinition toUpdate;
 69  1
         if (!existing.getId().equals(prop.getId())){
 70  0
                 final PropositionDefinition.Builder builder = PropositionDefinition.Builder.create(prop);
 71  0
                 builder.setId(existing.getId());
 72  0
                 toUpdate = builder.build();
 73  0
         } else {
 74  1
                 toUpdate = prop;
 75  
         }
 76  
         
 77  1
         businessObjectService.save(PropositionBo.from(toUpdate));
 78  1
         }
 79  
 
 80  
         /**
 81  
          * This overridden method retrieves a proposition by the give proposition id.
 82  
          * 
 83  
          * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getPropositionById(java.lang.String)
 84  
          */
 85  
         @Override
 86  
         public PropositionDefinition getPropositionById(String propId) {
 87  5
                 if (StringUtils.isBlank(propId)){
 88  1
             throw new IllegalArgumentException("propId is null or blank");                        
 89  
                 }
 90  4
                 PropositionBo bo = businessObjectService.findBySinglePrimaryKey(PropositionBo.class, propId);
 91  4
                 return PropositionBo.to(bo);
 92  
         }
 93  
 
 94  
         /**
 95  
          * This overridden method creates a PropositionParameter if it does not 
 96  
          * already exist in the repository.
 97  
          * 
 98  
          * @see org.kuali.rice.krms.impl.repository.PropositionBoService#createParameter(org.kuali.rice.krms.api.repository.proposition.PropositionParameter)
 99  
          */
 100  
         @Override
 101  
         public void createParameter(PropositionParameter parameter) {
 102  3
                 if (parameter == null){
 103  1
                 throw new IllegalArgumentException("parameter is null");
 104  
                 }
 105  2
                 final String propIdKey = parameter.getPropId();
 106  2
                 final Integer seqNoKey = parameter.getSequenceNumber();
 107  2
                 final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
 108  2
                 if (existing != null && existing.getPropId().equals(propIdKey) && existing.getSequenceNumber().equals(seqNoKey)){
 109  1
             throw new IllegalStateException("the parameter to create already exists: " + parameter);                        
 110  
                 }
 111  
                 
 112  1
                 businessObjectService.save(PropositionParameterBo.from(parameter));
 113  1
         }
 114  
 
 115  
         /**
 116  
          * This overridden method updates an existing proposition parameter
 117  
          * 
 118  
          * @see org.kuali.rice.krms.impl.repository.PropositionBoService#updateParameter(org.kuali.rice.krms.api.repository.proposition.PropositionParameter)
 119  
          */
 120  
         @Override
 121  
         public void updateParameter(PropositionParameter parameter) {
 122  3
         if (parameter == null) {
 123  1
             throw new IllegalArgumentException("parameter is null");
 124  
         }
 125  2
                 final String propIdKey = parameter.getPropId();
 126  2
                 final Integer seqNoKey = parameter.getSequenceNumber();
 127  2
                 final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
 128  2
         if (existing == null) {
 129  1
             throw new IllegalStateException("the parameter does not exist: " + parameter);
 130  
         }
 131  
         final PropositionParameter toUpdate;
 132  1
         if (!existing.getId().equals(parameter.getId())){
 133  0
                 final PropositionParameter.Builder builder = PropositionParameter.Builder.create(parameter);
 134  0
                 builder.setId(existing.getId());
 135  0
                 toUpdate = builder.build();
 136  0
         } else {
 137  1
                 toUpdate = parameter;
 138  
         }
 139  
         
 140  1
         businessObjectService.save(PropositionParameterBo.from(toUpdate));
 141  1
         }
 142  
 
 143  
         /**
 144  
          * This overridden method retrieves a list of parameters for a given proposition
 145  
          * 
 146  
          * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameters(java.lang.Long)
 147  
          */
 148  
         @Override
 149  
         public List<PropositionParameter> getParameters(String propId) {
 150  4
                 if (StringUtils.isBlank(propId)) {
 151  2
             throw new IllegalArgumentException("propId is null or blank");
 152  
                 }
 153  2
         final Map<String, Object> map = new HashMap<String, Object>();
 154  2
         map.put("propId", propId);
 155  2
                 List<PropositionParameterBo> bos = (List<PropositionParameterBo>) businessObjectService.findMatchingOrderBy(PropositionParameterBo.class, map, "sequenceNumber", true);
 156  2
                 return PropositionParameterBo.to(bos);
 157  
         }
 158  
 
 159  
         /**
 160  
          * This overridden method gets a parameter by the parameter id
 161  
          * 
 162  
          * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameterById(java.lang.String)
 163  
          */
 164  
         @Override
 165  
         public PropositionParameter getParameterById(String id) {
 166  4
                 if (StringUtils.isBlank(id)) {
 167  2
             throw new IllegalArgumentException("id is null or blank");
 168  
                 }
 169  2
                 PropositionParameterBo bo = businessObjectService.findBySinglePrimaryKey(PropositionParameterBo.class, id);
 170  2
                 return PropositionParameterBo.to(bo);
 171  
         }
 172  
 
 173  
         /**
 174  
          * This overridden method gets a parameter by the Proposition Id and Sequence Number
 175  
          * 
 176  
          * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameterByPropIdAndSequenceNumber(java.lang.String, java.lang.String)
 177  
          */
 178  
         @Override
 179  
         public PropositionParameter getParameterByPropIdAndSequenceNumber(
 180  
                         String propId, Integer sequenceNumber) {
 181  9
                 if (StringUtils.isBlank(propId)) {
 182  2
             throw new IllegalArgumentException("propId is null or blank");
 183  
                 }
 184  7
                 if (sequenceNumber == null) {
 185  1
             throw new IllegalArgumentException("sequenceNumber is null");
 186  
                 }
 187  6
         final Map<String, Object> map = new HashMap<String, Object>();
 188  6
         map.put("propId", propId);
 189  6
         map.put("sequenceNumber", sequenceNumber);
 190  6
                 PropositionParameterBo bo = businessObjectService.findByPrimaryKey(PropositionParameterBo.class, map);
 191  6
                 return PropositionParameterBo.to(bo);
 192  
         }
 193  
         
 194  
     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
 195  29
         this.businessObjectService = businessObjectService;
 196  29
     }
 197  
 
 198  
 }