add try-catch ReferencedObjectUndeletableException to all delete calls
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / ServiceBase.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.api.service;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.apache.log4j.Logger;
21 import org.hibernate.LockMode;
22 import org.hibernate.Session;
23 import org.springframework.context.ApplicationContext;
24 import org.springframework.context.ApplicationContextAware;
25 import org.springframework.transaction.annotation.Transactional;
26
27 import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
28 import eu.etaxonomy.cdm.api.service.pager.Pager;
29 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
32 import eu.etaxonomy.cdm.persistence.query.Grouping;
33 import eu.etaxonomy.cdm.persistence.query.OrderHint;
34
35 public abstract class ServiceBase<T extends CdmBase, DAO extends ICdmEntityDao<T>> implements IService<T>, ApplicationContextAware {
36 @SuppressWarnings("unused")
37 private static final Logger logger = Logger.getLogger(ServiceBase.class);
38
39 //flush after saving this number of objects
40 int flushAfterNo = 2000;
41 protected ApplicationContext appContext;
42
43 protected DAO dao;
44
45 @Transactional(readOnly = true)
46 public void lock(T t, LockMode lockMode) {
47 dao.lock(t, lockMode);
48 }
49
50 @Transactional(readOnly = true)
51 public void refresh(T t, LockMode lockMode, List<String> propertyPaths) {
52 dao.refresh(t, lockMode, propertyPaths);
53 }
54
55 @Transactional(readOnly = false)
56 public void clear() {
57 dao.clear();
58 }
59
60 @Transactional(readOnly = true)
61 public int count(Class<? extends T> clazz) {
62 return dao.count(clazz);
63 }
64
65 @Transactional(readOnly = false)
66 public UUID delete(T persistentObject) throws ReferencedObjectUndeletableException {
67 return dao.delete(persistentObject);
68 }
69
70 @Transactional(readOnly = true)
71 public boolean exists(UUID uuid) {
72 return dao.exists(uuid);
73 }
74
75 @Transactional(readOnly = true)
76 public List<T> find(Set<UUID> uuidSet) {
77 return dao.list(uuidSet, null, null, null, null);
78 }
79
80 @Transactional(readOnly = true)
81 public List<T> findById(Set<Integer> idSet) { //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
82 return dao.listByIds(idSet, null, null, null, null);
83 }
84
85 @Transactional(readOnly = true)
86 public T find(UUID uuid) {
87 return dao.findByUuid(uuid);
88 }
89
90 @Transactional(readOnly = true)
91 public T find(int id) {
92 return dao.findById(id);
93 }
94
95 @Transactional(readOnly = true)
96 public Session getSession() {
97 return dao.getSession();
98 }
99
100 @Transactional(readOnly = true)
101 public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
102 return dao.group(clazz, limit, start, groups, propertyPaths);
103 }
104
105 @Transactional(readOnly = true)
106 public List<T> list(Class<? extends T> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths){
107 return dao.list(type,limit, start, orderHints,propertyPaths);
108 }
109
110 @Transactional(readOnly = true)
111 public T load(UUID uuid) {
112 return dao.load(uuid);
113 }
114
115 @Transactional(readOnly = true)
116 public T load(UUID uuid, List<String> propertyPaths){
117 return dao.load(uuid, propertyPaths);
118 }
119
120 @Transactional(readOnly = false)
121 public T merge(T newInstance) {
122 return dao.merge(newInstance);
123 }
124
125 @Transactional(readOnly = true)
126 public Pager<T> page(Class<? extends T> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
127 Integer numberOfResults = dao.count(type);
128 List<T> results = new ArrayList<T>();
129 pageNumber = pageNumber == null ? 0 : pageNumber;
130 if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
131 Integer start = pageSize == null ? 0 : pageSize * pageNumber;
132 results = dao.list(type, pageSize, start, orderHints,propertyPaths);
133 }
134 return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
135 }
136
137 @Transactional(readOnly = true)
138 public UUID refresh(T persistentObject) {
139 return dao.refresh(persistentObject);
140 }
141
142 /**
143 * FIXME Candidate for harmonization
144 * is this method used, and if so, should it be exposed in the service layer?
145 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
146 * database, then expose a method that talks about "rows" . . .
147 */
148 @Transactional(readOnly = true)
149 public List<T> rows(String tableName, int limit, int start) {
150 return dao.rows(tableName, limit, start);
151 }
152
153 @Transactional(readOnly = false)
154 public Map<UUID, T> save(Collection<T> newInstances) {
155 return dao.saveAll(newInstances);
156 }
157
158 @Transactional(readOnly = false)
159 public UUID save(T newInstance) {
160 return dao.save(newInstance);
161 }
162
163 @Transactional(readOnly = false)
164 public UUID saveOrUpdate(T transientObject) {
165 return dao.saveOrUpdate(transientObject);
166 }
167
168 @Transactional(readOnly = false)
169 public Map<UUID, T> saveOrUpdate(Collection<T> transientInstances) {
170 return dao.saveOrUpdateAll(transientInstances);
171 }
172
173 /* (non-Javadoc)
174 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
175 */
176 public void setApplicationContext(ApplicationContext appContext){
177 this.appContext = appContext;
178 }
179
180
181 protected abstract void setDao(DAO dao);
182
183 @Transactional(readOnly = false)
184 public UUID update(T transientObject) {
185 return dao.update(transientObject);
186 }
187
188 @Transactional(readOnly = true)
189 public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
190 return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
191 }
192
193 }