Coverage Report - org.kuali.rice.kew.impl.peopleflow.PeopleFlowBo
 
Classes in this File Line Coverage Branch Coverage Complexity
PeopleFlowBo
0%
0/63
0%
0/70
0
PeopleFlowBo$_fromAndUpdate_closure1
0%
0/4
0%
0/6
0
PeopleFlowBo$_handleMembersUpdate_closure2
0%
0/1
N/A
0
PeopleFlowBo$_handleMembersUpdate_closure3
0%
0/1
N/A
0
 
 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.kew.impl.peopleflow
 17  
 
 18  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException
 19  
 import org.kuali.rice.core.api.mo.common.active.MutableInactivatable
 20  
 import org.kuali.rice.kew.api.KEWPropertyConstants
 21  
 import org.kuali.rice.kew.api.KewApiServiceLocator
 22  
 import org.kuali.rice.kew.api.peopleflow.PeopleFlowContract
 23  
 import org.kuali.rice.kew.api.peopleflow.PeopleFlowDefinition
 24  
 import org.kuali.rice.kew.api.peopleflow.PeopleFlowMember
 25  
 import org.kuali.rice.kew.api.repository.type.KewAttributeDefinition
 26  
 import org.kuali.rice.kew.api.repository.type.KewTypeAttribute
 27  
 import org.kuali.rice.kew.api.repository.type.KewTypeDefinition
 28  
 import org.kuali.rice.kew.impl.type.KewTypeBo
 29  
 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase
 30  
 import org.kuali.rice.krad.util.BeanPropertyComparator
 31  
 
 32  
 /**
 33  
  * Mapped entity for PeopleFlows
 34  
  */
 35  
 class PeopleFlowBo extends PersistableBusinessObjectBase implements MutableInactivatable, PeopleFlowContract {
 36  
 
 37  
     String id
 38  
     String name
 39  
     String namespaceCode
 40  
     String typeId
 41  
     String description
 42  
     boolean active = true
 43  
 
 44  
     KewTypeBo typeBo;
 45  
 
 46  0
     List<PeopleFlowAttributeBo> attributeBos = new ArrayList<PeopleFlowAttributeBo>();
 47  0
     List<PeopleFlowMemberBo> members = new ArrayList<PeopleFlowMemberBo>();
 48  
 
 49  
     // non-persisted, used for maintenance
 50  0
     Map<String, String> attributeValues = new HashMap<String, String>();
 51  
 
 52  
     @Override
 53  
     public Map<String, String> getAttributes() {
 54  0
         Map<String, String> results = new HashMap<String, String>();
 55  
 
 56  0
         if (attributeBos != null)
 57  0
             for (PeopleFlowAttributeBo attr: attributeBos) {
 58  0
                 results.put(attr.attributeDefinition.name, attr.value);
 59  
             }
 60  
 
 61  0
         return results;
 62  
     }
 63  
 
 64  
     public static PeopleFlowBo from(PeopleFlowDefinition peopleFlow, KewTypeDefinition kewTypeDefinition) {
 65  0
         return PeopleFlowBo.fromAndUpdate(peopleFlow, kewTypeDefinition, null);
 66  
     }
 67  
 
 68  
     /**
 69  
      * Translates from the given PeopleFlowDefinition to a PeopleFlowBo, optionally updating the given "toUpdate" parameter
 70  
      * instead of creating a new PeopleFlowBo.  If it's not passed then a new PeopleFlowBo will be created.
 71  
      */
 72  
     public static PeopleFlowBo fromAndUpdate(PeopleFlowDefinition peopleFlow, KewTypeDefinition kewTypeDefinition, PeopleFlowBo toUpdate) {
 73  0
         PeopleFlowBo result = toUpdate;
 74  0
         if (toUpdate == null) {
 75  0
             result = new PeopleFlowBo();
 76  
         }
 77  
 
 78  0
         result.id = peopleFlow.getId();
 79  0
         result.name = peopleFlow.getName();
 80  0
         result.namespaceCode = peopleFlow.getNamespaceCode();
 81  0
         result.typeId = peopleFlow.getTypeId();
 82  0
         result.description = peopleFlow.getDescription();
 83  0
         result.active = peopleFlow.isActive();
 84  0
         result.versionNumber = peopleFlow.getVersionNumber();
 85  
 
 86  
         // we need to translate attributes over, this is a bit more work, first let's do some validation
 87  0
         if (peopleFlow.getTypeId() == null) {
 88  0
             if (!peopleFlow.getAttributes().isEmpty()) {
 89  0
                 throw new RiceIllegalArgumentException("Given PeopleFlow definition does not have a type, but does have attribute values");
 90  
             }
 91  0
             if (kewTypeDefinition != null) {
 92  0
                 throw new RiceIllegalArgumentException("PeopleFlow has no type id, but a KewTypeDefinition was supplied when it should not have been.");
 93  
             }
 94  
         }
 95  0
         if (peopleFlow.getTypeId() != null) {
 96  0
             if (kewTypeDefinition == null) {
 97  0
                 throw new RiceIllegalArgumentException("PeopleFlow has a type id of '" + peopleFlow.getTypeId() + "' but no KewTypeDefinition was supplied.");
 98  
             }
 99  0
             if (!kewTypeDefinition.getId().equals(peopleFlow.getTypeId())) {
 100  0
                 throw new RiceIllegalArgumentException("Type id of given KewTypeDefinition does not match PeopleFlow type id:  " + kewTypeDefinition.getId() + " != " + peopleFlow.getTypeId());
 101  
             }
 102  
         }
 103  
 
 104  
         // now we need to effectively do a diff with the given attributes, first let's add new entries and update existing ones
 105  0
         result.attributeBos = new ArrayList<PeopleFlowAttributeBo>();
 106  0
         peopleFlow.getAttributes().each { key, value ->
 107  0
             KewAttributeDefinition attributeDefinition = kewTypeDefinition.getAttributeDefinitionByName(key);
 108  0
             if (attributeDefinition == null) {
 109  0
                 throw new RiceIllegalArgumentException("There is no attribute definition for the given attribute name '" + key + "'");
 110  
             }
 111  
             // they have no way to pass us the id of the attribute from the given contract
 112  0
             result.attributeBos.add(PeopleFlowAttributeBo.from(attributeDefinition, null, peopleFlow.getId(), value));
 113  
         }
 114  
 
 115  0
         handleMembersUpdate(result, peopleFlow);
 116  
 
 117  0
         return result;
 118  
     }
 119  
 
 120  
     /**
 121  
      * Translate the members, if the members have changed at all, we want to clear so that the current set of members
 122  
      * are removed by OJB's removal aware list.
 123  
      */
 124  
     private static void handleMembersUpdate(PeopleFlowBo peopleFlowBo, PeopleFlowDefinition peopleFlow) {
 125  0
         Set<PeopleFlowMember> currentMembers = new HashSet<PeopleFlowMember>();
 126  0
         if (peopleFlowBo.getMembers() == null) {
 127  0
             peopleFlowBo.setMembers(new ArrayList<PeopleFlowMemberBo>());
 128  
         }
 129  0
         peopleFlowBo.getMembers().each {
 130  0
             currentMembers.add(PeopleFlowMember.Builder.create(it).build());
 131  
         }
 132  0
         if (!currentMembers.equals(new HashSet<PeopleFlowMember>(peopleFlow.getMembers()))) {
 133  
             // this means that the membership has been updated, we need to rebuild it
 134  0
             peopleFlowBo.getMembers().clear();
 135  0
             peopleFlow.getMembers().each {
 136  0
                 peopleFlowBo.getMembers().add(PeopleFlowMemberBo.from(it));
 137  
             }
 138  
         }
 139  
     }
 140  
 
 141  
     public static PeopleFlowDefinition to(PeopleFlowBo peopleFlowBo) {
 142  0
         if (peopleFlowBo == null) {
 143  0
             return null;
 144  
         }
 145  0
         PeopleFlowDefinition.Builder builder = PeopleFlowDefinition.Builder.create(peopleFlowBo);
 146  0
         return builder.build();
 147  
     }
 148  
 
 149  
     /**
 150  
      * Invoked to rebuild the type attribute bos and attributes value map based on the type id
 151  
      */
 152  
     public void rebuildTypeAttributes() {
 153  0
         attributeBos = new ArrayList<PeopleFlowAttributeBo>();
 154  0
         attributeValues = new HashMap<String, String>();
 155  
 
 156  0
         KewTypeDefinition typeDefinition = KewApiServiceLocator.getKewTypeRepositoryService().getTypeById(this.typeId);
 157  0
         if ((typeDefinition.getAttributes() != null) && !typeDefinition.getAttributes().isEmpty()) {
 158  0
             List<KewTypeAttribute> typeAttributes = new ArrayList<KewTypeAttribute>(typeDefinition.getAttributes());
 159  
 
 160  0
             List<String> sortAttributes = new ArrayList<String>();
 161  0
             sortAttributes.add(KEWPropertyConstants.SEQUENCE_NUMBER);
 162  0
             Collections.sort(typeAttributes, new BeanPropertyComparator(sortAttributes));
 163  
 
 164  0
             for (KewTypeAttribute typeAttribute: typeAttributes) {
 165  0
                 PeopleFlowAttributeBo attributeBo = PeopleFlowAttributeBo.from(typeAttribute.attributeDefinition, null,
 166  
                         this.id, null);
 167  0
                 attributeBos.add(attributeBo);
 168  
 
 169  0
                 attributeValues.put(typeAttribute.getAttributeDefinition().name, "");
 170  
             }
 171  
         }
 172  
     }
 173  
 
 174  
     /**
 175  
      * Updates the values in the attribute bos from the attribute values map
 176  
      */
 177  
     public void updateAttributeBoValues() {
 178  0
         for (PeopleFlowAttributeBo attributeBo: attributeBos) {
 179  0
             if (attributeValues.containsKey(attributeBo.getAttributeDefinition().getName())) {
 180  0
                 String attributeValue = attributeValues.get(attributeBo.getAttributeDefinition().getName());
 181  0
                 attributeBo.setValue(attributeValue);
 182  
             }
 183  
         }
 184  
     }
 185  
 
 186  
     @Override
 187  
     protected void postLoad() {
 188  0
         attributeValues = new HashMap<String, String>();
 189  0
         for (PeopleFlowAttributeBo attributeBo: attributeBos) {
 190  0
             attributeValues.put(attributeBo.attributeDefinition.name, attributeBo.value);
 191  
         }
 192  
     }
 193  
 
 194  
 }