| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 70 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 122 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
} |