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