Coverage Report - org.kuali.rice.krms.impl.repository.AgendaBoServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AgendaBoServiceImpl
54%
76/140
41%
26/62
4.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.Collection;
 20  
 import java.util.Collections;
 21  
 import java.util.HashMap;
 22  
 import java.util.HashSet;
 23  
 import java.util.Map;
 24  
 import java.util.Map.Entry;
 25  
 import java.util.Set;
 26  
 
 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.krad.service.SequenceAccessorService;
 31  
 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
 32  
 import org.kuali.rice.krms.api.repository.agenda.AgendaItem;
 33  
 import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
 34  
 
 35  20
 public final class AgendaBoServiceImpl implements AgendaBoService {
 36  
         
 37  
         // TODO: deal with active flag
 38  
         
 39  
     private BusinessObjectService businessObjectService;
 40  
         private SequenceAccessorService sequenceAccessorService;
 41  
 
 42  
         /**
 43  
          * This overridden method creates a KRMS Agenda in the repository
 44  
          * 
 45  
          * @see org.kuali.rice.krms.impl.repository.AgendaBoService#createAgenda(org.kuali.rice.krms.api.repository.agenda.AgendaDefinition)
 46  
          */
 47  
         @Override
 48  
         public AgendaDefinition createAgenda(AgendaDefinition agenda) {
 49  3
                 if (agenda == null){
 50  1
                         throw new IllegalArgumentException("agenda is null");
 51  
                 }
 52  2
                 final String nameKey = agenda.getName();
 53  2
                 final String namespaceKey = agenda.getNamespaceCode();
 54  2
                 final AgendaDefinition existing = getAgendaByNameAndNamespace(nameKey, namespaceKey);
 55  2
                 if (existing != null){
 56  1
                         throw new IllegalStateException("the agenda to create already exists: " + agenda);                        
 57  
                 }        
 58  
                 
 59  1
                 AgendaBo agendaBo = from(agenda);
 60  1
                 businessObjectService.save(agendaBo);
 61  1
                 return to(agendaBo);
 62  
         }
 63  
 
 64  
         /**
 65  
          * This overridden method updates an existing Agenda in the repository
 66  
          * 
 67  
          * @see org.kuali.rice.krms.impl.repository.AgendaBoService#updateAgenda(org.kuali.rice.krms.api.repository.agenda.AgendaDefinition)
 68  
          */
 69  
         @Override
 70  
         public void updateAgenda(AgendaDefinition agenda) {
 71  3
                 if (agenda == null){
 72  1
                         throw new IllegalArgumentException("agenda is null");
 73  
                 }
 74  
                 
 75  
                 // must already exist to be able to update
 76  2
                 final String agendaIdKey = agenda.getId();
 77  2
                 final AgendaBo existing = businessObjectService.findBySinglePrimaryKey(AgendaBo.class, agendaIdKey);
 78  2
                 if (existing == null) {
 79  1
                         throw new IllegalStateException("the agenda does not exist: " + agenda);
 80  
                 }
 81  
                 final AgendaDefinition toUpdate;
 82  1
                 if (!existing.getId().equals(agenda.getId())){
 83  
                         // if passed in id does not match existing id, correct it
 84  0
                         final AgendaDefinition.Builder builder = AgendaDefinition.Builder.create(agenda);
 85  0
                         builder.setId(existing.getId());
 86  0
                         toUpdate = builder.build();
 87  0
                 } else {
 88  1
                         toUpdate = agenda;
 89  
                 }
 90  
 
 91  
                 // copy all updateable fields to bo
 92  1
                 AgendaBo boToUpdate = from(toUpdate);
 93  
                 
 94  
                 // delete any old, existing attributes
 95  1
                 Map<String,String> fields = new HashMap<String,String>(1);
 96  1
                 fields.put(KRMSPropertyConstants.Agenda.AGENDA_ID, toUpdate.getId());
 97  1
                 businessObjectService.deleteMatching(AgendaAttributeBo.class, fields);
 98  
                 
 99  
                 // update new agenda and create new attributes
 100  1
                 businessObjectService.save(boToUpdate);
 101  1
         }
 102  
 
 103  
         /**
 104  
          * This overridden method retrieves an Agenda from the repository
 105  
          * 
 106  
          * @see org.kuali.rice.krms.impl.repository.AgendaBoService#getAgendaByAgendaId(java.lang.String)
 107  
          */
 108  
         @Override
 109  
         public AgendaDefinition getAgendaByAgendaId(String agendaId) {
 110  4
                 if (StringUtils.isBlank(agendaId)){
 111  2
                         throw new IllegalArgumentException("agenda id is null");
 112  
                 }
 113  2
                 AgendaBo bo = businessObjectService.findBySinglePrimaryKey(AgendaBo.class, agendaId);
 114  2
                 return to(bo);
 115  
         }
 116  
 
 117  
         /**
 118  
          * This overridden method retrieves an agenda from the repository
 119  
          * 
 120  
          * @see org.kuali.rice.krms.impl.repository.AgendaBoService#getAgendaByNameAndNamespace(java.lang.String, java.lang.String)
 121  
          */
 122  
         @Override
 123  
         public AgendaDefinition getAgendaByNameAndNamespace(String name, String namespace) {
 124  8
         if (StringUtils.isBlank(name)) {
 125  2
             throw new IllegalArgumentException("name is blank");
 126  
         }
 127  6
         if (StringUtils.isBlank(namespace)) {
 128  2
             throw new IllegalArgumentException("namespace is blank");
 129  
         }
 130  
 
 131  4
         final Map<String, Object> map = new HashMap<String, Object>();
 132  4
         map.put("name", name);
 133  4
         map.put("namespace", namespace);
 134  
 
 135  4
         AgendaBo myAgenda = businessObjectService.findByPrimaryKey(AgendaBo.class, Collections.unmodifiableMap(map));
 136  4
                 return to(myAgenda);
 137  
         }
 138  
 
 139  
         /**
 140  
          * This overridden method retrieves a set of agendas from the repository
 141  
          * 
 142  
          * @see org.kuali.rice.krms.impl.repository.AgendaBoService#getAgendasByContextId(java.lang.String)
 143  
          */
 144  
         @Override
 145  
         public Set<AgendaDefinition> getAgendasByContextId(String contextId) {
 146  4
                 if (StringUtils.isBlank(contextId)){
 147  2
             throw new IllegalArgumentException("context ID is null or blank");                        
 148  
                 }
 149  2
         final Map<String, Object> map = new HashMap<String, Object>();
 150  2
         map.put("contextId", contextId);
 151  2
                 Set<AgendaBo> bos = (Set<AgendaBo>) businessObjectService.findMatching(AgendaBo.class, map);
 152  2
                 return convertListOfBosToImmutables(bos);
 153  
         }
 154  
 
 155  
         /**
 156  
          * This overridden method creates a new Agenda in the repository
 157  
          * 
 158  
          * @see org.kuali.rice.krms.impl.repository.AgendaBoService#createAgendaItem(org.kuali.rice.krms.api.repository.agenda.AgendaItem)
 159  
          */
 160  
         @Override
 161  
         public AgendaItem createAgendaItem(AgendaItem agendaItem) {
 162  0
                 if (agendaItem == null){
 163  0
                         throw new IllegalArgumentException("agendaItem is null");
 164  
                 }
 165  0
                 if (agendaItem.getId() != null){
 166  0
                         final AgendaDefinition existing = getAgendaByAgendaId(agendaItem.getId());
 167  0
                         if (existing != null){
 168  0
                                 throw new IllegalStateException("the agendaItem to create already exists: " + agendaItem);                        
 169  
                         }
 170  
                 }
 171  
                 
 172  0
                 AgendaItemBo bo = AgendaItemBo.from(agendaItem);
 173  0
                 businessObjectService.save(bo);
 174  0
                 return AgendaItemBo.to(bo);
 175  
         }
 176  
 
 177  
         /**
 178  
          * This overridden method updates an existing Agenda in the repository
 179  
          * 
 180  
          * @see org.kuali.rice.krms.impl.repository.AgendaBoService#updateAgendaItem(org.kuali.rice.krms.api.repository.agenda.AgendaItem)
 181  
          */
 182  
         @Override
 183  
         public void updateAgendaItem(AgendaItem agendaItem) {
 184  0
                 if (agendaItem == null){
 185  0
                         throw new IllegalArgumentException("agendaItem is null");
 186  
                 }
 187  0
                 final String agendaItemIdKey = agendaItem.getId();
 188  0
                 final AgendaItem existing = getAgendaItemById(agendaItemIdKey);
 189  0
                 if (existing == null) {
 190  0
                         throw new IllegalStateException("the agenda item does not exist: " + agendaItem);
 191  
                 }
 192  
                 final AgendaItem toUpdate;
 193  0
                 if (!existing.getId().equals(agendaItem.getId())){
 194  0
                         final AgendaItem.Builder builder = AgendaItem.Builder.create(agendaItem);
 195  0
                         builder.setId(existing.getId());
 196  0
                         toUpdate = builder.build();
 197  0
                 } else {
 198  0
                         toUpdate = agendaItem;
 199  
                 }
 200  
 
 201  0
                 businessObjectService.save(AgendaItemBo.from(toUpdate));
 202  0
         }
 203  
 
 204  
         /**
 205  
          * This overridden method adds a new AgendaItem to the repository
 206  
          * 
 207  
          * @see org.kuali.rice.krms.impl.repository.AgendaBoService#addAgendaItem(org.kuali.rice.krms.api.repository.agenda.AgendaItem, java.lang.String, java.lang.String)
 208  
          */
 209  
         @Override
 210  
         public void addAgendaItem(AgendaItem agendaItem, String parentId, Boolean position) {
 211  0
                 if (agendaItem == null){
 212  0
                         throw new IllegalArgumentException("agendaItem is null");
 213  
                 }
 214  0
                 AgendaItem parent = null;
 215  0
                 if (parentId != null){
 216  0
                         parent = getAgendaItemById(parentId);
 217  0
                         if (parent == null){
 218  0
                                 throw new IllegalStateException("parent agendaItem does not exist in repository. parentId = " + parentId);
 219  
                         }
 220  
                 }
 221  
                 // create new AgendaItem
 222  
                 final AgendaItem toCreate;
 223  0
                 if (agendaItem.getId() == null) {
 224  0
                         SequenceAccessorService sas = getSequenceAccessorService();
 225  0
                         final AgendaItem.Builder builder = AgendaItem.Builder.create(agendaItem);
 226  0
                         final String newId =sas.getNextAvailableSequenceNumber(
 227  
                                         "KRMS_AGENDA_ITM_S", AgendaItemBo.class).toString();
 228  0
                         builder.setId(newId);
 229  0
                         toCreate = builder.build();
 230  0
                 } else {
 231  0
                         toCreate = agendaItem;
 232  
                 }
 233  0
                 createAgendaItem(toCreate);
 234  
                 
 235  
                 // link it to it's parent (for whenTrue/whenFalse, sibling for always
 236  0
                 if (parentId != null){                        
 237  0
                         final AgendaItem.Builder builder = AgendaItem.Builder.create(parent);
 238  0
                         if (position == null){
 239  0
                                 builder.setAlwaysId( toCreate.getId() );
 240  0
                         } else if (position.booleanValue() == true){
 241  0
                                 builder.setWhenTrueId( toCreate.getId() );
 242  0
                         } else if (position.booleanValue() == false){
 243  0
                                 builder.setWhenFalseId( toCreate.getId() );
 244  
                         }
 245  0
                         final AgendaItem parentToUpdate = builder.build();
 246  0
                         updateAgendaItem( parentToUpdate );
 247  
                 }
 248  0
         }
 249  
 
 250  
         /**
 251  
          * This overridden method retrieves an AgendaItem from the repository
 252  
          * 
 253  
          * @see org.kuali.rice.krms.impl.repository.AgendaBoService#addAgendaItem(org.kuali.rice.krms.api.repository.agenda.AgendaItem, java.lang.String, java.lang.String)
 254  
          */
 255  
         @Override
 256  
         public AgendaItem getAgendaItemById(String id) {
 257  0
                 if (StringUtils.isBlank(id)){
 258  0
                         throw new IllegalArgumentException("agenda item id is null");
 259  
                 }
 260  0
                 AgendaItemBo bo = businessObjectService.findBySinglePrimaryKey(AgendaItemBo.class, id);
 261  0
                 return AgendaItemBo.to(bo);
 262  
         }
 263  
 
 264  
         /**
 265  
          * Sets the businessObjectService attribute value.
 266  
          *
 267  
          * @param businessObjectService The businessObjectService to set.
 268  
          */
 269  
         public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
 270  12
                 this.businessObjectService = businessObjectService;
 271  12
         }
 272  
 
 273  
     protected BusinessObjectService getBusinessObjectService() {
 274  0
                 if ( businessObjectService == null ) {
 275  0
                         businessObjectService = KRADServiceLocator.getBusinessObjectService();
 276  
                 }
 277  0
                 return businessObjectService;
 278  
         }
 279  
     
 280  
         protected SequenceAccessorService getSequenceAccessorService() {
 281  0
                 if ( sequenceAccessorService == null ) {
 282  0
                         sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
 283  
                 }
 284  0
                 return sequenceAccessorService;
 285  
         }
 286  
 
 287  
         /**
 288  
          * Converts a Set<AgendaBo> to an Unmodifiable Set<Agenda>
 289  
          *
 290  
          * @param AgendaBos a mutable Set<AgendaBo> to made completely immutable.
 291  
          * @return An unmodifiable Set<Agenda>
 292  
          */
 293  
         public Set<AgendaDefinition> convertListOfBosToImmutables(final Collection<AgendaBo> agendaBos) {
 294  2
                 Set<AgendaDefinition> agendas = new HashSet<AgendaDefinition>();
 295  2
                 if (agendaBos != null){
 296  1
                         for (AgendaBo bo : agendaBos) {
 297  1
                                 AgendaDefinition agenda = to(bo);
 298  1
                                 agendas.add(agenda);
 299  1
                         }
 300  
                 }
 301  2
                 return Collections.unmodifiableSet(agendas);
 302  
         }
 303  
 
 304  
         /**
 305  
          * Converts a mutable bo to it's immutable counterpart
 306  
          * @param bo the mutable business object
 307  
          * @return the immutable object
 308  
          */
 309  
         @Override
 310  
         public AgendaDefinition to(AgendaBo bo) {
 311  11
                 if (bo == null) { return null; }
 312  8
                 return org.kuali.rice.krms.api.repository.agenda.AgendaDefinition.Builder.create(bo).build();
 313  
         }
 314  
 
 315  
 
 316  
         /**
 317  
         * Converts a immutable object to it's mutable bo counterpart
 318  
         * @param im immutable object
 319  
         * @return the mutable bo
 320  
         */
 321  
         @Override
 322  
    public AgendaBo from(AgendaDefinition im) {
 323  2
            if (im == null) { return null; }
 324  
 
 325  2
            AgendaBo bo = new AgendaBo();
 326  2
            bo.setId( im.getId() );
 327  2
            bo.setNamespace( im.getNamespaceCode() );
 328  2
            bo.setName( im.getName() );
 329  2
            bo.setTypeId( im.getTypeId() );
 330  2
            bo.setContextId( im.getContextId() );
 331  2
            bo.setFirstItemId( im.getFirstItemId() );
 332  2
            bo.setVersionNumber( im.getVersionNumber() );
 333  
 
 334  
            // build the set of agenda attribute BOs
 335  2
            Set<AgendaAttributeBo> attributes = new HashSet<AgendaAttributeBo>();
 336  
 
 337  
            // for each converted pair, build an AgendaAttributeBo and add it to the set
 338  
            AgendaAttributeBo attributeBo;
 339  2
            for (Entry<String,String> entry  : im.getAttributes().entrySet()){
 340  4
                    KrmsAttributeDefinitionBo attrDefBo = KrmsRepositoryServiceLocator
 341  
                                    .getKrmsAttributeDefinitionService()
 342  
                                    .getKrmsAttributeBo(entry.getKey(), im.getNamespaceCode());
 343  4
                    attributeBo = new AgendaAttributeBo();
 344  4
                    attributeBo.setAgendaId( im.getId() );
 345  4
                    attributeBo.setAttributeDefinitionId( attrDefBo.getId() );
 346  4
                    attributeBo.setValue( entry.getValue() );
 347  4
                    attributeBo.setAttributeDefinition( attrDefBo );
 348  4
                    attributes.add( attributeBo );
 349  4
            }
 350  2
            bo.setAttributeBos(attributes);
 351  
            
 352  2
            return bo;
 353  
    }
 354  
 
 355  
 }