001 package org.rice.krms.test; 002 003 import java.util.ArrayList; 004 import java.util.Collection; 005 import java.util.HashMap; 006 import java.util.List; 007 import java.util.Map; 008 009 import org.kuali.rice.kns.bo.BusinessObject; 010 import org.kuali.rice.kns.bo.PersistableBusinessObject; 011 import org.kuali.rice.kns.service.BusinessObjectService; 012 import org.kuali.rice.krms.impl.repository.ContextBo; 013 import org.kuali.rice.krms.impl.repository.KrmsTypeBo; 014 import org.kuali.rice.krms.impl.repository.TermSpecificationBo; 015 import org.springframework.transaction.annotation.Transactional; 016 import org.springframework.util.CollectionUtils; 017 018 public class TestBoService implements BusinessObjectService { 019 020 private Map<Class<?>, String> idFieldNamesMap = new HashMap<Class<?>, String>(); 021 022 { 023 idFieldNamesMap.put(KrmsTypeBo.class, "id"); 024 idFieldNamesMap.put(ContextBo.class, "contextId"); 025 idFieldNamesMap.put(TermSpecificationBo.class, "id"); 026 } 027 028 private final GenericTestDao dao; 029 030 public TestBoService(GenericTestDao genericDao) { 031 this.dao = genericDao; 032 } 033 034 public void addIdFieldNameMapping(Class<?> clazz, String fieldName) { 035 idFieldNamesMap.put(clazz, fieldName); 036 } 037 038 /* (non-Javadoc) 039 * @see org.kuali.rice.kns.service.BusinessObjectService#save(org.kuali.rice.kns.bo.PersistableBusinessObject) 040 */ 041 @Override 042 public PersistableBusinessObject save(PersistableBusinessObject bo) { 043 return dao.save(bo); 044 } 045 046 /* (non-Javadoc) 047 * @see org.kuali.rice.kns.service.BusinessObjectService#save(java.util.List) 048 */ 049 @SuppressWarnings({ "unchecked", "rawtypes" }) 050 @Override 051 public List<? extends PersistableBusinessObject> save( 052 List<? extends PersistableBusinessObject> businessObjects) { 053 List results = new ArrayList(); 054 if (!CollectionUtils.isEmpty(businessObjects)) { 055 for (PersistableBusinessObject bo : businessObjects) { 056 results.add(save(bo)); 057 } 058 } 059 return results; 060 } 061 062 063 /* (non-Javadoc) 064 * @see org.kuali.rice.kns.service.BusinessObjectService#linkAndSave(org.kuali.rice.kns.bo.PersistableBusinessObject) 065 */ 066 @Override 067 public PersistableBusinessObject linkAndSave( 068 PersistableBusinessObject bo) { 069 // TODO Auto-generated method stub 070 throw new UnsupportedOperationException(); 071 } 072 073 /* (non-Javadoc) 074 * @see org.kuali.rice.kns.service.BusinessObjectService#linkAndSave(java.util.List) 075 */ 076 @Override 077 public List<? extends PersistableBusinessObject> linkAndSave( 078 List<? extends PersistableBusinessObject> businessObjects) { 079 // TODO Auto-generated method stub 080 throw new UnsupportedOperationException(); 081 } 082 083 /* (non-Javadoc) 084 * @see org.kuali.rice.kns.service.BusinessObjectService#findBySinglePrimaryKey(java.lang.Class, java.lang.Object) 085 */ 086 @Override 087 public <T extends BusinessObject> T findBySinglePrimaryKey( 088 Class<T> clazz, Object primaryKey) { 089 String idFieldName = "id"; 090 if (idFieldNamesMap.containsKey(clazz)) idFieldName = idFieldNamesMap.get(clazz); 091 return (T) dao.findById((String)primaryKey, idFieldName, clazz); 092 } 093 094 /* (non-Javadoc) 095 * @see org.kuali.rice.kns.service.BusinessObjectService#findByPrimaryKey(java.lang.Class, java.util.Map) 096 */ 097 @Override 098 public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, 099 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 /* (non-Javadoc) 109 * @see org.kuali.rice.kns.service.BusinessObjectService#retrieve(org.kuali.rice.kns.bo.PersistableBusinessObject) 110 */ 111 @Override 112 public PersistableBusinessObject retrieve( 113 PersistableBusinessObject object) { 114 // TODO Auto-generated method stub 115 throw new UnsupportedOperationException(); 116 } 117 118 /* (non-Javadoc) 119 * @see org.kuali.rice.kns.service.BusinessObjectService#findAll(java.lang.Class) 120 */ 121 @Override 122 public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz) { 123 // TODO Auto-generated method stub 124 throw new UnsupportedOperationException(); 125 } 126 127 /* (non-Javadoc) 128 * @see org.kuali.rice.kns.service.BusinessObjectService#findAllOrderBy(java.lang.Class, java.lang.String, boolean) 129 */ 130 @Override 131 public <T extends BusinessObject> Collection<T> findAllOrderBy( 132 Class<T> clazz, String sortField, boolean sortAscending) { 133 // TODO Auto-generated method stub 134 throw new UnsupportedOperationException(); 135 } 136 137 /* (non-Javadoc) 138 * @see org.kuali.rice.kns.service.BusinessObjectService#findMatching(java.lang.Class, java.util.Map) 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 /* (non-Javadoc) 147 * @see org.kuali.rice.kns.service.BusinessObjectService#countMatching(java.lang.Class, java.util.Map) 148 */ 149 @Override 150 public int countMatching(Class clazz, Map<String, ?> fieldValues) { 151 throw new UnsupportedOperationException(); 152 } 153 154 /* (non-Javadoc) 155 * @see org.kuali.rice.kns.service.BusinessObjectService#countMatching(java.lang.Class, java.util.Map, java.util.Map) 156 */ 157 @Override 158 public int countMatching(Class clazz, 159 Map<String, ?> positiveFieldValues, 160 Map<String, ?> negativeFieldValues) { 161 throw new UnsupportedOperationException(); 162 } 163 164 /* (non-Javadoc) 165 * @see org.kuali.rice.kns.service.BusinessObjectService#findMatchingOrderBy(java.lang.Class, java.util.Map, java.lang.String, boolean) 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 /* (non-Javadoc) 175 * @see org.kuali.rice.kns.service.BusinessObjectService#delete(org.kuali.rice.kns.bo.PersistableBusinessObject) 176 */ 177 @Override 178 public void delete(PersistableBusinessObject bo) { 179 throw new UnsupportedOperationException(); 180 } 181 182 /* (non-Javadoc) 183 * @see org.kuali.rice.kns.service.BusinessObjectService#delete(java.util.List) 184 */ 185 @Override 186 public void delete(List<? extends PersistableBusinessObject> boList) { 187 throw new UnsupportedOperationException(); 188 } 189 190 /* (non-Javadoc) 191 * @see org.kuali.rice.kns.service.BusinessObjectService#deleteMatching(java.lang.Class, java.util.Map) 192 */ 193 @Override 194 @Transactional 195 public void deleteMatching(Class clazz, Map<String, ?> fieldValues) { 196 dao.deleteMatching(clazz, fieldValues); 197 } 198 199 /* (non-Javadoc) 200 * @see org.kuali.rice.kns.service.BusinessObjectService#getReferenceIfExists(org.kuali.rice.kns.bo.BusinessObject, java.lang.String) 201 */ 202 @Override 203 public BusinessObject getReferenceIfExists(BusinessObject bo, 204 String referenceName) { 205 throw new UnsupportedOperationException(); 206 } 207 208 /* (non-Javadoc) 209 * @see org.kuali.rice.kns.service.BusinessObjectService#linkUserFields(org.kuali.rice.kns.bo.PersistableBusinessObject) 210 */ 211 @Override 212 public void linkUserFields(PersistableBusinessObject bo) { 213 throw new UnsupportedOperationException(); 214 } 215 216 /* (non-Javadoc) 217 * @see org.kuali.rice.kns.service.BusinessObjectService#linkUserFields(java.util.List) 218 */ 219 @Override 220 public void linkUserFields(List<PersistableBusinessObject> bos) { 221 throw new UnsupportedOperationException(); } 222 223 /* (non-Javadoc) 224 * @see org.kuali.rice.kns.service.BusinessObjectService#manageReadOnly(org.kuali.rice.kns.bo.PersistableBusinessObject) 225 */ 226 @Override 227 public PersistableBusinessObject manageReadOnly( 228 PersistableBusinessObject bo) { 229 throw new UnsupportedOperationException(); 230 } 231 232 }