Project

General

Profile

Download (5.79 KB) Statistics
| Branch: | Tag: | Revision:
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

    
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
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) {
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.findByUuid(uuidSet);
78
	}
79

    
80
	@Transactional(readOnly = true)
81
	public T find(UUID uuid) {
82
		return dao.findByUuid(uuid);
83
	}
84
	
85
	@Transactional(readOnly = true)
86
	public Session getSession() {
87
		return dao.getSession();
88
	}
89
	
90
	@Transactional(readOnly = true)
91
	public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
92
		return dao.group(clazz, limit, start, groups, propertyPaths);
93
	}
94
	
95
	@Transactional(readOnly = true)
96
	public  List<T> list(Class<? extends T> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths){
97
		return dao.list(type,limit, start, orderHints,propertyPaths);
98
	}
99
	
100
	@Transactional(readOnly = true)
101
	public T load(UUID uuid) {
102
		return dao.load(uuid);
103
	}
104
		
105
	@Transactional(readOnly = true)
106
	public T load(UUID uuid, List<String> propertyPaths){
107
		return dao.load(uuid, propertyPaths);
108
	}
109

    
110
	@Transactional(readOnly = false)
111
	public UUID merge(T newInstance) {
112
		return dao.merge(newInstance);
113
	}
114
	
115
	@Transactional(readOnly = true)
116
	public  Pager<T> page(Class<? extends T> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
117
		Integer numberOfResults = dao.count(type);
118
		List<T> results = new ArrayList<T>();
119
		pageNumber = pageNumber == null ? 0 : pageNumber;
120
		if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
121
			Integer start = pageSize == null ? 0 : pageSize * pageNumber;
122
			results = dao.list(type, pageSize, start, orderHints,propertyPaths);
123
		}
124
		return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
125
	}
126
	
127
	@Transactional(readOnly = true)
128
    public UUID refresh(T persistentObject) {
129
		return dao.refresh(persistentObject);
130
	}
131
	
132
	/**
133
	 * FIXME Candidate for harmonization
134
	 * is this method used, and if so, should it be exposed in the service layer?
135
	 * it seems a bit incongruous that we use an ORM to hide the fact that there is a 
136
	 * database, then expose a method that talks about "rows" . . .
137
	 */
138
	@Transactional(readOnly = true)
139
	public List<T> rows(String tableName, int limit, int start) {
140
		return dao.rows(tableName, limit, start);
141
	}
142
	
143
	@Transactional(readOnly = false)
144
	public Map<UUID, T> save(Collection<T> newInstances) {
145
		return dao.saveAll(newInstances);
146
	}
147

    
148
	@Transactional(readOnly = false)
149
	public UUID save(T newInstance) {
150
		return dao.save(newInstance);
151
	}
152

    
153
	//@PostFilter("hasPermission(filterObject, 'edit')")
154
	//@PreAuthorize("hasRole('ROLE_EDIT')")
155
	@Transactional(readOnly = false)
156
	public UUID saveOrUpdate(T transientObject) {
157
		return dao.saveOrUpdate(transientObject);
158
	}
159
	
160
	@Transactional(readOnly = false)
161
	public Map<UUID, T> saveOrUpdate(Collection<T> transientInstances) {
162
		return dao.saveOrUpdateAll(transientInstances);
163
	}
164

    
165
	/* (non-Javadoc)
166
	 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
167
	 */
168
	public void setApplicationContext(ApplicationContext appContext){
169
		this.appContext = appContext;
170
	}
171

    
172

    
173
	protected abstract void setDao(DAO dao);
174
	
175
	@Transactional(readOnly = false)
176
	public UUID update(T transientObject) {
177
		return dao.update(transientObject);
178
	}
179
	
180
	@Transactional(readOnly = true)
181
	public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
182
		return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
183
	}
184
}
(70-70/79)