Merging r13268 through r14040 from trunk/cdmlib into branches/cdmlib-unitils3
[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.access.prepost.PreAuthorize;
26 import org.springframework.security.core.Authentication;
27 import org.springframework.transaction.annotation.Transactional;
28
29 import eu.etaxonomy.cdm.api.service.pager.Pager;
30 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32 import eu.etaxonomy.cdm.permission.CdmPermission;
33 import eu.etaxonomy.cdm.permission.CdmPermissionEvaluator;
34 import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
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 T find(UUID uuid) {
85 return dao.findByUuid(uuid);
86 }
87
88 @Transactional(readOnly = true)
89 public Session getSession() {
90 return dao.getSession();
91 }
92
93 @Transactional(readOnly = true)
94 public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
95 return dao.group(clazz, limit, start, groups, propertyPaths);
96 }
97
98 @Transactional(readOnly = true)
99 public List<T> list(Class<? extends T> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths){
100 return dao.list(type,limit, start, orderHints,propertyPaths);
101 }
102
103 @Transactional(readOnly = true)
104 public T load(UUID uuid) {
105 return dao.load(uuid);
106 }
107
108 @Transactional(readOnly = true)
109 public T load(UUID uuid, List<String> propertyPaths){
110 return dao.load(uuid, propertyPaths);
111 }
112
113 @Transactional(readOnly = false)
114 public T merge(T newInstance) {
115 return dao.merge(newInstance);
116 }
117
118 @Transactional(readOnly = true)
119 public Pager<T> page(Class<? extends T> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
120 Integer numberOfResults = dao.count(type);
121 List<T> results = new ArrayList<T>();
122 pageNumber = pageNumber == null ? 0 : pageNumber;
123 if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
124 Integer start = pageSize == null ? 0 : pageSize * pageNumber;
125 results = dao.list(type, pageSize, start, orderHints,propertyPaths);
126 }
127 return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
128 }
129
130 @Transactional(readOnly = true)
131 public UUID refresh(T persistentObject) {
132 return dao.refresh(persistentObject);
133 }
134
135 /**
136 * FIXME Candidate for harmonization
137 * is this method used, and if so, should it be exposed in the service layer?
138 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
139 * database, then expose a method that talks about "rows" . . .
140 */
141 @Transactional(readOnly = true)
142 public List<T> rows(String tableName, int limit, int start) {
143 return dao.rows(tableName, limit, start);
144 }
145
146 @Transactional(readOnly = false)
147 public Map<UUID, T> save(Collection<T> newInstances) {
148 return dao.saveAll(newInstances);
149 }
150
151 @Transactional(readOnly = false)
152 public UUID save(T newInstance) {
153 return dao.save(newInstance);
154 }
155
156 @Transactional(readOnly = false)
157 public UUID saveOrUpdate(T transientObject) {
158 return dao.saveOrUpdate(transientObject);
159 }
160
161 @Transactional(readOnly = false)
162 public Map<UUID, T> saveOrUpdate(Collection<T> transientInstances) {
163 return dao.saveOrUpdateAll(transientInstances);
164 }
165
166 /* (non-Javadoc)
167 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
168 */
169 public void setApplicationContext(ApplicationContext appContext){
170 this.appContext = appContext;
171 }
172
173
174 protected abstract void setDao(DAO dao);
175
176 @Transactional(readOnly = false)
177 public UUID update(T transientObject) {
178 return dao.update(transientObject);
179 }
180
181 @Transactional(readOnly = true)
182 public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
183 return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
184 }
185
186 @Transactional(readOnly = true)
187 public boolean hasPermission(Authentication authentication, T target, CdmPermission permission) {
188 CdmPermissionEvaluator permissionEvaluator = new CdmPermissionEvaluator();
189 return permissionEvaluator.hasPermission(authentication, target, permission);
190
191 }
192 }