| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kew.impl.repository; |
| 17 | |
|
| 18 | |
|
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.Collection; |
| 21 | |
import java.util.Collections; |
| 22 | |
import java.util.HashMap; |
| 23 | |
import java.util.List; |
| 24 | |
import java.util.Map; |
| 25 | |
|
| 26 | |
import org.apache.commons.lang.StringUtils; |
| 27 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
| 28 | |
import org.kuali.rice.core.api.exception.RiceIllegalStateException; |
| 29 | |
import org.kuali.rice.kew.api.repository.type.KewTypeAttribute; |
| 30 | |
import org.kuali.rice.kew.api.repository.type.KewTypeDefinition; |
| 31 | |
import org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService; |
| 32 | |
import org.kuali.rice.kew.impl.type.KewTypeAttributeBo; |
| 33 | |
import org.kuali.rice.kew.impl.type.KewTypeBo; |
| 34 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
| 35 | |
|
| 36 | 0 | public final class KewTypeBoServiceImpl implements KewTypeRepositoryService { |
| 37 | |
|
| 38 | |
private BusinessObjectService businessObjectService; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
@Override |
| 46 | |
public KewTypeDefinition createKewType(KewTypeDefinition kewType) { |
| 47 | 0 | if (kewType == null){ |
| 48 | 0 | throw new RiceIllegalArgumentException("kewType is null"); |
| 49 | |
} |
| 50 | 0 | final String nameKey = kewType.getName(); |
| 51 | 0 | final String namespaceKey = kewType.getNamespace(); |
| 52 | 0 | final KewTypeDefinition existing = getTypeByNameAndNamespace(nameKey, namespaceKey); |
| 53 | 0 | if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){ |
| 54 | 0 | throw new RiceIllegalStateException("The KEW Type to create already exists: " + kewType); |
| 55 | |
} |
| 56 | |
|
| 57 | 0 | KewTypeBo bo = (KewTypeBo)businessObjectService.save(KewTypeBo.from(kewType)); |
| 58 | |
|
| 59 | 0 | return KewTypeBo.to(bo); |
| 60 | |
} |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
@Override |
| 68 | |
public void updateKewType(KewTypeDefinition kewType) { |
| 69 | 0 | if (kewType == null) { |
| 70 | 0 | throw new RiceIllegalArgumentException("kewType is null"); |
| 71 | |
} |
| 72 | 0 | final String idKey = kewType.getId(); |
| 73 | 0 | final KewTypeBo existing = businessObjectService.findBySinglePrimaryKey(KewTypeBo.class, idKey); |
| 74 | 0 | if (existing == null) { |
| 75 | 0 | throw new RiceIllegalStateException("The KEW type does not exist: " + kewType); |
| 76 | |
} |
| 77 | |
final KewTypeDefinition toUpdate; |
| 78 | 0 | if (!existing.getId().equals(kewType.getId())){ |
| 79 | 0 | final KewTypeDefinition.Builder builder = KewTypeDefinition.Builder.create(kewType); |
| 80 | 0 | builder.setId(existing.getId()); |
| 81 | 0 | toUpdate = builder.build(); |
| 82 | 0 | } else { |
| 83 | 0 | toUpdate = kewType; |
| 84 | |
} |
| 85 | |
|
| 86 | 0 | businessObjectService.save(KewTypeBo.from(toUpdate)); |
| 87 | 0 | } |
| 88 | |
|
| 89 | |
@Override |
| 90 | |
public KewTypeDefinition getTypeById(final String id) { |
| 91 | 0 | if (StringUtils.isBlank(id)) { |
| 92 | 0 | throw new RiceIllegalArgumentException("id is blank"); |
| 93 | |
} |
| 94 | |
|
| 95 | 0 | KewTypeBo kewTypeBo = businessObjectService.findBySinglePrimaryKey(KewTypeBo.class, id); |
| 96 | |
|
| 97 | 0 | return KewTypeBo.to(kewTypeBo); |
| 98 | |
} |
| 99 | |
|
| 100 | |
@Override |
| 101 | |
public KewTypeDefinition getTypeByNameAndNamespace(final String name, final String namespace) { |
| 102 | 0 | if (StringUtils.isBlank(name)) { |
| 103 | 0 | throw new RiceIllegalArgumentException("name is blank"); |
| 104 | |
} |
| 105 | 0 | if (StringUtils.isBlank(namespace)) { |
| 106 | 0 | throw new RiceIllegalArgumentException("namespace is blank"); |
| 107 | |
} |
| 108 | |
|
| 109 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 110 | 0 | map.put("name", name); |
| 111 | 0 | map.put("namespace", namespace); |
| 112 | |
|
| 113 | 0 | KewTypeBo myType = businessObjectService.findByPrimaryKey(KewTypeBo.class, Collections.unmodifiableMap(map)); |
| 114 | 0 | return KewTypeBo.to(myType); |
| 115 | |
} |
| 116 | |
|
| 117 | |
@Override |
| 118 | |
public List<KewTypeDefinition> findAllTypesByNamespace(final String namespace) { |
| 119 | 0 | if (StringUtils.isBlank(namespace)) { |
| 120 | 0 | throw new RiceIllegalArgumentException("namespace is blank"); |
| 121 | |
} |
| 122 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 123 | 0 | map.put("namespace", namespace); |
| 124 | 0 | map.put("active", Boolean.TRUE); |
| 125 | |
|
| 126 | 0 | Collection<KewTypeBo> kewTypeBos = businessObjectService.findMatching(KewTypeBo.class, Collections.unmodifiableMap(map)); |
| 127 | |
|
| 128 | 0 | return convertListOfBosToImmutables(kewTypeBos); |
| 129 | |
} |
| 130 | |
|
| 131 | |
@Override |
| 132 | |
public List<KewTypeDefinition> findAllTypes() { |
| 133 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 134 | 0 | map.put("active", Boolean.TRUE); |
| 135 | |
|
| 136 | 0 | Collection<KewTypeBo> kewTypeBos = businessObjectService.findMatching(KewTypeBo.class, Collections.unmodifiableMap(map)); |
| 137 | 0 | return convertListOfBosToImmutables(kewTypeBos); |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public void setBusinessObjectService(final BusinessObjectService businessObjectService) { |
| 146 | 0 | this.businessObjectService = businessObjectService; |
| 147 | 0 | } |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
List<KewTypeDefinition> convertListOfBosToImmutables(final Collection<KewTypeBo> kewTypeBos) { |
| 156 | 0 | ArrayList<KewTypeDefinition> kewTypes = new ArrayList<KewTypeDefinition>(); |
| 157 | 0 | for (KewTypeBo bo : kewTypeBos) { |
| 158 | 0 | KewTypeDefinition kewType = KewTypeBo.to(bo); |
| 159 | 0 | kewTypes.add(kewType); |
| 160 | 0 | } |
| 161 | 0 | return Collections.unmodifiableList(kewTypes); |
| 162 | |
} |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
@Override |
| 170 | |
public void createKewTypeAttribute(KewTypeAttribute kewTypeAttribute) { |
| 171 | 0 | if (kewTypeAttribute == null){ |
| 172 | 0 | throw new RiceIllegalArgumentException("kewTypeAttribute is null"); |
| 173 | |
} |
| 174 | |
|
| 175 | 0 | final String typeIdKey = kewTypeAttribute.getTypeId(); |
| 176 | 0 | final String attrDefIdKey = kewTypeAttribute.getAttributeDefinitionId(); |
| 177 | |
|
| 178 | 0 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 179 | 0 | map.put("typeId", typeIdKey); |
| 180 | 0 | map.put("attributeDefinitionId", attrDefIdKey); |
| 181 | |
|
| 182 | 0 | KewTypeAttributeBo existing = businessObjectService.findByPrimaryKey(KewTypeAttributeBo.class, Collections.unmodifiableMap(map)); |
| 183 | |
|
| 184 | 0 | if (existing != null && existing.getTypeId().equals(typeIdKey) && existing.getAttributeDefinitionId().equals(attrDefIdKey)){ |
| 185 | 0 | throw new RiceIllegalStateException("The KEW Type Attribute to create already exists: " + kewTypeAttribute); |
| 186 | |
} |
| 187 | |
|
| 188 | 0 | KewTypeAttributeBo bo = (KewTypeAttributeBo)businessObjectService.save(KewTypeAttributeBo.from(kewTypeAttribute)); |
| 189 | 0 | } |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
@Override |
| 197 | |
public void updateKewTypeAttribute(KewTypeAttribute kewTypeAttribute) { |
| 198 | 0 | if (kewTypeAttribute == null) { |
| 199 | 0 | throw new RiceIllegalArgumentException("kewTypeAttribute is null"); |
| 200 | |
} |
| 201 | 0 | final String idKey = kewTypeAttribute.getId(); |
| 202 | 0 | final KewTypeAttributeBo existing = businessObjectService.findBySinglePrimaryKey(KewTypeAttributeBo.class, idKey); |
| 203 | 0 | if (existing == null) { |
| 204 | 0 | throw new RiceIllegalStateException("The KEW type Attribute does not exist: " + kewTypeAttribute); |
| 205 | |
} |
| 206 | |
final KewTypeAttribute toUpdate; |
| 207 | 0 | if (!existing.getId().equals(kewTypeAttribute.getId())){ |
| 208 | 0 | final KewTypeAttribute.Builder builder = KewTypeAttribute.Builder.create(kewTypeAttribute); |
| 209 | 0 | builder.setId(existing.getId()); |
| 210 | 0 | toUpdate = builder.build(); |
| 211 | 0 | } else { |
| 212 | 0 | toUpdate = kewTypeAttribute; |
| 213 | |
} |
| 214 | |
|
| 215 | 0 | businessObjectService.save(KewTypeAttributeBo.from(toUpdate)); |
| 216 | 0 | } |
| 217 | |
} |