Added merge(T t, LockMode lockmode) and refresh(T t, LockMode lockmode) to allow...
[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.pager.Pager;
28 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30 import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
31 import eu.etaxonomy.cdm.persistence.query.Grouping;
32 import eu.etaxonomy.cdm.persistence.query.OrderHint;
33
34 public abstract class ServiceBase<T extends CdmBase, DAO extends ICdmEntityDao<T>> implements IService<T>, ApplicationContextAware {
35 private static final Logger logger = Logger.getLogger(ServiceBase.class);
36
37 //flush after saving this number of objects
38 int flushAfterNo = 2000;
39 protected ApplicationContext appContext;
40
41 protected DAO dao;
42
43 @Transactional(readOnly = true)
44 public void lock(T t, LockMode lockMode) {
45 dao.lock(t, lockMode);
46 }
47
48 @Transactional(readOnly = true)
49 public void refresh(T t, LockMode lockMode) {
50 dao.refresh(t, lockMode);
51 }
52
53 @Transactional(readOnly = false)
54 public void clear() {
55 dao.clear();
56 }
57
58 @Transactional(readOnly = true)
59 public int count(Class<? extends T> clazz) {
60 return dao.count(clazz);
61 }
62
63 @Transactional(readOnly = false)
64 public UUID delete(T persistentObject) {
65 return dao.delete(persistentObject);
66 }
67
68 @Transactional(readOnly = true)
69 public boolean exists(UUID uuid) {
70 return dao.exists(uuid);
71 }
72
73 @Transactional(readOnly = true)
74 public List<T> find(Set<UUID> uuidSet) {
75 return dao.findByUuid(uuidSet);
76 }
77
78 @Transactional(readOnly = true)
79 public T find(UUID uuid) {
80 return dao.findByUuid(uuid);
81 }
82
83 @Transactional(readOnly = true)
84 public Session getSession() {
85 return dao.getSession();
86 }
87
88 @Transactional(readOnly = true)
89 public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
90 return dao.group(clazz, limit, start, groups, propertyPaths);
91 }
92
93 @Transactional(readOnly = true)
94 public List<T> list(Class<? extends T> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths){
95 return dao.list(type,limit, start, orderHints,propertyPaths);
96 }
97
98 @Transactional(readOnly = true)
99 public T load(UUID uuid) {
100 return dao.load(uuid);
101 }
102
103 @Transactional(readOnly = true)
104 public T load(UUID uuid, List<String> propertyPaths){
105 return dao.load(uuid, propertyPaths);
106 }
107
108 @Transactional(readOnly = false)
109 public UUID merge(T newInstance) {
110 return dao.merge(newInstance);
111 }
112
113 @Transactional(readOnly = true)
114 public Pager<T> page(Class<? extends T> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
115 Integer numberOfResults = dao.count(type);
116 List<T> results = new ArrayList<T>();
117 pageNumber = pageNumber == null ? 0 : pageNumber;
118 if(numberOfResults > 0) { // no point checking again
119 Integer start = pageSize == null ? 0 : pageSize * pageNumber;
120 results = dao.list(type, pageSize, start, orderHints,propertyPaths);
121 }
122 return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
123 }
124
125 @Transactional(readOnly = true)
126 public UUID refresh(T persistentObject) {
127 return dao.refresh(persistentObject);
128 }
129
130 /**
131 * FIXME Candidate for harmonization
132 * is this method used, and if so, should it be exposed in the service layer?
133 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
134 * database, then expose a method that talks about "rows" . . .
135 */
136 @Transactional(readOnly = true)
137 public List<T> rows(String tableName, int limit, int start) {
138 return dao.rows(tableName, limit, start);
139 }
140
141 @Transactional(readOnly = false)
142 public Map<UUID, T> save(Collection<T> newInstances) {
143 return dao.saveAll(newInstances);
144 }
145
146 @Transactional(readOnly = false)
147 public UUID save(T newInstance) {
148 return dao.save(newInstance);
149 }
150
151 @Transactional(readOnly = false)
152 public UUID saveOrUpdate(T transientObject) {
153 return dao.saveOrUpdate(transientObject);
154 }
155
156 /* (non-Javadoc)
157 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
158 */
159 public void setApplicationContext(ApplicationContext appContext){
160 this.appContext = appContext;
161 }
162
163
164 protected abstract void setDao(DAO dao);
165
166 @Transactional(readOnly = false)
167 public UUID update(T transientObject) {
168 return dao.update(transientObject);
169 }
170
171 @Transactional(readOnly = true)
172 public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
173 return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
174 }
175 }