Project

General

Profile

Download (6.46 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
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
    @SuppressWarnings("unused")
36
    private static final Logger logger = Logger.getLogger(ServiceBase.class);
37

    
38
    //flush after saving this number of objects
39
    int flushAfterNo = 2000;
40
    protected ApplicationContext appContext;
41

    
42
    protected DAO dao;
43

    
44
    @Transactional(readOnly = true)
45
    public void lock(T t, LockMode lockMode) {
46
        dao.lock(t, lockMode);
47
    }
48

    
49
    @Transactional(readOnly = true)
50
    public void refresh(T t, LockMode lockMode, List<String> propertyPaths) {
51
        dao.refresh(t, lockMode, propertyPaths);
52
    }
53

    
54
    @Transactional(readOnly = false)
55
    public void clear() {
56
        dao.clear();
57
    }
58

    
59
    @Transactional(readOnly = true)
60
    public int count(Class<? extends T> clazz) {
61
        return dao.count(clazz);
62
    }
63

    
64
    @Transactional(readOnly = false)
65
    public UUID delete(T persistentObject) {
66
        return dao.delete(persistentObject);
67
    }
68

    
69
    @Transactional(readOnly = true)
70
    public boolean exists(UUID uuid) {
71
        return dao.exists(uuid);
72
    }
73

    
74
    @Transactional(readOnly = true)
75
    public List<T> find(Set<UUID> uuidSet) {
76
        return dao.list(uuidSet, null, null, null, null);
77
    }
78

    
79
    @Transactional(readOnly = true)
80
    public List<T> findById(Set<Integer> idSet) {  //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
81
        return dao.listByIds(idSet, null, null, null, null);
82
    }
83

    
84
    @Transactional(readOnly = true)
85
    public T find(UUID uuid) {
86
        return dao.findByUuid(uuid);
87
    }
88

    
89
    @Transactional(readOnly = true)
90
    public T find(int id) {
91
        return dao.findById(id);
92
    }
93

    
94
    @Transactional(readOnly = true)
95
    public Session getSession() {
96
        return dao.getSession();
97
    }
98

    
99
    @Transactional(readOnly = true)
100
    public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
101
        return dao.group(clazz, limit, start, groups, propertyPaths);
102
    }
103

    
104
    @Transactional(readOnly = true)
105
    public  List<T> list(Class<? extends T> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths){
106
        return dao.list(type,limit, start, orderHints,propertyPaths);
107
    }
108

    
109
    @Transactional(readOnly = true)
110
    public T load(UUID uuid) {
111
        return dao.load(uuid);
112
    }
113

    
114
    @Transactional(readOnly = true)
115
    public T load(UUID uuid, List<String> propertyPaths){
116
        return dao.load(uuid, propertyPaths);
117
    }
118

    
119
    @Transactional(readOnly = false)
120
    public T merge(T newInstance) {
121
        return dao.merge(newInstance);
122
    }
123

    
124
    @Transactional(readOnly = true)
125
    public  Pager<T> page(Class<? extends T> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
126
        Integer numberOfResults = dao.count(type);
127
        List<T> results = new ArrayList<T>();
128
        pageNumber = pageNumber == null ? 0 : pageNumber;
129
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
130
            Integer start = pageSize == null ? 0 : pageSize * pageNumber;
131
            results = dao.list(type, pageSize, start, orderHints,propertyPaths);
132
        }
133
        return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
134
    }
135

    
136
    @Transactional(readOnly = true)
137
    public UUID refresh(T persistentObject) {
138
        return dao.refresh(persistentObject);
139
    }
140

    
141
    /**
142
     * FIXME Candidate for harmonization
143
     * is this method used, and if so, should it be exposed in the service layer?
144
     * it seems a bit incongruous that we use an ORM to hide the fact that there is a
145
     * database, then expose a method that talks about "rows" . . .
146
     */
147
    @Transactional(readOnly = true)
148
    public List<T> rows(String tableName, int limit, int start) {
149
        return dao.rows(tableName, limit, start);
150
    }
151

    
152
    @Transactional(readOnly = false)
153
    public Map<UUID, T> save(Collection<T> newInstances) {
154
        return dao.saveAll(newInstances);
155
    }
156

    
157
    @Transactional(readOnly = false)
158
    public UUID save(T newInstance) {
159
        return dao.save(newInstance);
160
    }
161

    
162
    @Transactional(readOnly = false)
163
    public UUID saveOrUpdate(T transientObject) {
164
        return dao.saveOrUpdate(transientObject);
165
    }
166

    
167
    @Transactional(readOnly = false)
168
    public Map<UUID, T> saveOrUpdate(Collection<T> transientInstances) {
169
        return dao.saveOrUpdateAll(transientInstances);
170
    }
171

    
172
    /* (non-Javadoc)
173
     * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
174
     */
175
    public void setApplicationContext(ApplicationContext appContext){
176
        this.appContext = appContext;
177
    }
178

    
179

    
180
    protected abstract void setDao(DAO dao);
181

    
182
    @Transactional(readOnly = false)
183
    public UUID update(T transientObject) {
184
        return dao.update(transientObject);
185
    }
186

    
187
    @Transactional(readOnly = true)
188
    public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
189
        return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
190
    }
191

    
192
}
(74-74/83)