1 package org.rice.krms.test;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.kuali.rice.krad.bo.BusinessObject;
10 import org.kuali.rice.krad.bo.PersistableBusinessObject;
11 import org.kuali.rice.krad.service.BusinessObjectService;
12 import org.kuali.rice.krms.impl.repository.ContextBo;
13 import org.kuali.rice.krms.impl.repository.KrmsTypeBo;
14 import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
15 import org.springframework.transaction.annotation.Transactional;
16 import org.springframework.util.CollectionUtils;
17
18 public class TestBoService implements BusinessObjectService {
19
20 private Map<Class<?>, String> idFieldNamesMap = new HashMap<Class<?>, String>();
21
22 {
23 idFieldNamesMap.put(KrmsTypeBo.class, "id");
24 idFieldNamesMap.put(ContextBo.class, "id");
25 idFieldNamesMap.put(TermSpecificationBo.class, "id");
26 }
27
28 private final GenericTestDao dao;
29
30 public TestBoService(GenericTestDao genericDao) {
31 this.dao = genericDao;
32 }
33
34 public void addIdFieldNameMapping(Class<?> clazz, String fieldName) {
35 idFieldNamesMap.put(clazz, fieldName);
36 }
37
38
39
40
41 @Override
42 public PersistableBusinessObject save(PersistableBusinessObject bo) {
43 return dao.save(bo);
44 }
45
46
47
48
49 @SuppressWarnings({ "unchecked", "rawtypes" })
50 @Override
51 public List<? extends PersistableBusinessObject> save(
52 List<? extends PersistableBusinessObject> businessObjects) {
53 List results = new ArrayList();
54 if (!CollectionUtils.isEmpty(businessObjects)) {
55 for (PersistableBusinessObject bo : businessObjects) {
56 results.add(save(bo));
57 }
58 }
59 return results;
60 }
61
62
63
64
65
66 @Override
67 public PersistableBusinessObject linkAndSave(
68 PersistableBusinessObject bo) {
69
70 throw new UnsupportedOperationException();
71 }
72
73
74
75
76 @Override
77 public List<? extends PersistableBusinessObject> linkAndSave(
78 List<? extends PersistableBusinessObject> businessObjects) {
79
80 throw new UnsupportedOperationException();
81 }
82
83
84
85
86 @Override
87 public <T extends BusinessObject> T findBySinglePrimaryKey(
88 Class<T> clazz, Object primaryKey) {
89 String idFieldName = "id";
90 if (idFieldNamesMap.containsKey(clazz)) idFieldName = idFieldNamesMap.get(clazz);
91 return (T) dao.findById((String)primaryKey, idFieldName, clazz);
92 }
93
94
95
96
97 @Override
98 public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz,
99 Map<String, ?> primaryKeys) {
100 Collection<T> results = dao.findMatching(clazz, primaryKeys);
101 if (!CollectionUtils.isEmpty(results)) {
102 if (results.size() > 1) throw new IllegalStateException("primary key fetch should only return one result!");
103 return results.iterator().next();
104 }
105 return null;
106 }
107
108
109
110
111 @Override
112 public PersistableBusinessObject retrieve(
113 PersistableBusinessObject object) {
114
115 throw new UnsupportedOperationException();
116 }
117
118
119
120
121 @Override
122 public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) {
123
124 throw new UnsupportedOperationException();
125 }
126
127
128
129
130 @Override
131 public <T extends BusinessObject> Collection<T> findAllOrderBy(
132 Class<T> clazz, String sortField, boolean sortAscending) {
133
134 throw new UnsupportedOperationException();
135 }
136
137
138
139
140 @Override
141 public <T extends BusinessObject> Collection<T> findMatching(
142 Class<T> clazz, Map<String, ?> fieldValues) {
143 return dao.findMatching(clazz, fieldValues);
144 }
145
146
147
148
149 @Override
150 public int countMatching(Class clazz, Map<String, ?> fieldValues) {
151 throw new UnsupportedOperationException();
152 }
153
154
155
156
157 @Override
158 public int countMatching(Class clazz,
159 Map<String, ?> positiveFieldValues,
160 Map<String, ?> negativeFieldValues) {
161 throw new UnsupportedOperationException();
162 }
163
164
165
166
167 @Override
168 public <T extends BusinessObject> Collection<T> findMatchingOrderBy(
169 Class<T> clazz, Map<String, ?> fieldValues, String sortField,
170 boolean sortAscending) {
171 throw new UnsupportedOperationException();
172 }
173
174
175
176
177 @Override
178 public void delete(PersistableBusinessObject bo) {
179 throw new UnsupportedOperationException();
180 }
181
182
183
184
185 @Override
186 public void delete(List<? extends PersistableBusinessObject> boList) {
187 throw new UnsupportedOperationException();
188 }
189
190
191
192
193 @Override
194 @Transactional
195 public void deleteMatching(Class clazz, Map<String, ?> fieldValues) {
196 dao.deleteMatching(clazz, fieldValues);
197 }
198
199
200
201
202 @Override
203 public BusinessObject getReferenceIfExists(BusinessObject bo,
204 String referenceName) {
205 throw new UnsupportedOperationException();
206 }
207
208
209
210
211 @Override
212 public void linkUserFields(PersistableBusinessObject bo) {
213 throw new UnsupportedOperationException();
214 }
215
216
217
218
219 @Override
220 public void linkUserFields(List<PersistableBusinessObject> bos) {
221 throw new UnsupportedOperationException(); }
222
223
224
225
226 @Override
227 public PersistableBusinessObject manageReadOnly(
228 PersistableBusinessObject bo) {
229 throw new UnsupportedOperationException();
230 }
231
232 }