| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.krms.impl.repository; |
| 18 | |
|
| 19 | |
|
| 20 | |
import org.apache.commons.lang.StringUtils; |
| 21 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
| 22 | |
import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute; |
| 23 | |
import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition; |
| 24 | |
import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService; |
| 25 | |
|
| 26 | |
import java.util.*; |
| 27 | |
|
| 28 | 16 | public final class KrmsTypeBoServiceImpl implements KrmsTypeRepositoryService { |
| 29 | |
|
| 30 | |
private BusinessObjectService businessObjectService; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
@Override |
| 39 | |
public KrmsTypeDefinition createKrmsType(KrmsTypeDefinition krmsType) { |
| 40 | 3 | if (krmsType == null){ |
| 41 | 1 | throw new IllegalArgumentException("krmsType is null"); |
| 42 | |
} |
| 43 | 2 | final String nameKey = krmsType.getName(); |
| 44 | 2 | final String namespaceKey = krmsType.getNamespace(); |
| 45 | 2 | final KrmsTypeDefinition existing = getTypeByNameAndNamespace(nameKey, namespaceKey); |
| 46 | 2 | if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){ |
| 47 | 1 | throw new IllegalStateException("the KRMS Type to create already exists: " + krmsType); |
| 48 | |
} |
| 49 | |
|
| 50 | 1 | KrmsTypeBo bo = (KrmsTypeBo)businessObjectService.save(KrmsTypeBo.from(krmsType)); |
| 51 | |
|
| 52 | 1 | return KrmsTypeBo.to(bo); |
| 53 | |
} |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
@Override |
| 61 | |
public void updateKrmsType(KrmsTypeDefinition krmsType) { |
| 62 | 3 | if (krmsType == null) { |
| 63 | 1 | throw new IllegalArgumentException("krmsType is null"); |
| 64 | |
} |
| 65 | 2 | final String idKey = krmsType.getId(); |
| 66 | 2 | final KrmsTypeBo existing = businessObjectService.findBySinglePrimaryKey(KrmsTypeBo.class, idKey); |
| 67 | 2 | if (existing == null) { |
| 68 | 1 | throw new IllegalStateException("the KRMS type does not exist: " + krmsType); |
| 69 | |
} |
| 70 | |
final KrmsTypeDefinition toUpdate; |
| 71 | 1 | if (!existing.getId().equals(krmsType.getId())){ |
| 72 | 0 | final KrmsTypeDefinition.Builder builder = KrmsTypeDefinition.Builder.create(krmsType); |
| 73 | 0 | builder.setId(existing.getId()); |
| 74 | 0 | toUpdate = builder.build(); |
| 75 | 0 | } else { |
| 76 | 1 | toUpdate = krmsType; |
| 77 | |
} |
| 78 | |
|
| 79 | 1 | businessObjectService.save(KrmsTypeBo.from(toUpdate)); |
| 80 | 1 | } |
| 81 | |
|
| 82 | |
@Override |
| 83 | |
public KrmsTypeDefinition getTypeById(final String id) { |
| 84 | 4 | if (StringUtils.isBlank(id)) { |
| 85 | 2 | throw new IllegalArgumentException("id is blank"); |
| 86 | |
} |
| 87 | |
|
| 88 | 2 | KrmsTypeBo krmsTypeBo = businessObjectService.findBySinglePrimaryKey(KrmsTypeBo.class, id); |
| 89 | |
|
| 90 | 2 | return KrmsTypeBo.to(krmsTypeBo); |
| 91 | |
} |
| 92 | |
|
| 93 | |
@Override |
| 94 | |
public KrmsTypeDefinition getTypeByNameAndNamespace(final String name, final String namespace) { |
| 95 | 5 | if (StringUtils.isBlank(name)) { |
| 96 | 1 | throw new IllegalArgumentException("name is blank"); |
| 97 | |
} |
| 98 | 4 | if (StringUtils.isBlank(namespace)) { |
| 99 | 1 | throw new IllegalArgumentException("namespace is blank"); |
| 100 | |
} |
| 101 | |
|
| 102 | 3 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 103 | 3 | map.put("name", name); |
| 104 | 3 | map.put("namespace", namespace); |
| 105 | |
|
| 106 | 3 | KrmsTypeBo myType = businessObjectService.findByPrimaryKey(KrmsTypeBo.class, Collections.unmodifiableMap(map)); |
| 107 | 3 | return KrmsTypeBo.to(myType); |
| 108 | |
} |
| 109 | |
|
| 110 | |
@Override |
| 111 | |
public List<KrmsTypeDefinition> findAllTypesByNamespace(final String namespace) { |
| 112 | 2 | if (StringUtils.isBlank(namespace)) { |
| 113 | 1 | throw new IllegalArgumentException("namespace is blank"); |
| 114 | |
} |
| 115 | 1 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 116 | 1 | map.put("namespace", namespace); |
| 117 | 1 | map.put("active", Boolean.TRUE); |
| 118 | |
|
| 119 | 1 | Collection<KrmsTypeBo> krmsTypeBos = businessObjectService.findMatching(KrmsTypeBo.class, Collections.unmodifiableMap(map)); |
| 120 | |
|
| 121 | 1 | return convertListOfBosToImmutables(krmsTypeBos); |
| 122 | |
} |
| 123 | |
|
| 124 | |
@Override |
| 125 | |
public List<KrmsTypeDefinition> findAllTypes() { |
| 126 | 1 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 127 | 1 | map.put("active", Boolean.TRUE); |
| 128 | |
|
| 129 | 1 | Collection<KrmsTypeBo> krmsTypeBos = businessObjectService.findMatching(KrmsTypeBo.class, Collections.unmodifiableMap(map)); |
| 130 | 1 | return convertListOfBosToImmutables(krmsTypeBos); |
| 131 | |
} |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public void setBusinessObjectService(final BusinessObjectService businessObjectService) { |
| 139 | 11 | this.businessObjectService = businessObjectService; |
| 140 | 11 | } |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
List<KrmsTypeDefinition> convertListOfBosToImmutables(final Collection<KrmsTypeBo> krmsTypeBos) { |
| 149 | 2 | ArrayList<KrmsTypeDefinition> krmsTypes = new ArrayList<KrmsTypeDefinition>(); |
| 150 | 2 | for (KrmsTypeBo bo : krmsTypeBos) { |
| 151 | 5 | KrmsTypeDefinition krmsType = KrmsTypeBo.to(bo); |
| 152 | 5 | krmsTypes.add(krmsType); |
| 153 | 5 | } |
| 154 | 2 | return Collections.unmodifiableList(krmsTypes); |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
@Override |
| 163 | |
public void createKrmsTypeAttribute(KrmsTypeAttribute krmsTypeAttribute) { |
| 164 | |
|
| 165 | 0 | throw new UnsupportedOperationException(); |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
@Override |
| 174 | |
public void updateKrmsTypeAttribute(KrmsTypeAttribute krmsTypeAttribute) { |
| 175 | |
|
| 176 | 0 | throw new UnsupportedOperationException(); |
| 177 | |
} |
| 178 | |
} |