falg to turn off preliminar natural langage representation improvement
[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 @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.findByUuid(uuidSet);
77 }
78
79 @Transactional(readOnly = true)
80 public T find(UUID uuid) {
81 return dao.findByUuid(uuid);
82 }
83
84 @Transactional(readOnly = true)
85 public Session getSession() {
86 return dao.getSession();
87 }
88
89 @Transactional(readOnly = true)
90 public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
91 return dao.group(clazz, limit, start, groups, propertyPaths);
92 }
93
94 @Transactional(readOnly = true)
95 public List<T> list(Class<? extends T> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths){
96 return dao.list(type,limit, start, orderHints,propertyPaths);
97 }
98
99 @Transactional(readOnly = true)
100 public T load(UUID uuid) {
101 return dao.load(uuid);
102 }
103
104 @Transactional(readOnly = true)
105 public T load(UUID uuid, List<String> propertyPaths){
106 return dao.load(uuid, propertyPaths);
107 }
108
109 @Transactional(readOnly = false)
110 public UUID merge(T newInstance) {
111 return dao.merge(newInstance);
112 }
113
114 @Transactional(readOnly = true)
115 public Pager<T> page(Class<? extends T> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
116 Integer numberOfResults = dao.count(type);
117 List<T> results = new ArrayList<T>();
118 pageNumber = pageNumber == null ? 0 : pageNumber;
119 if(numberOfResults > 0) { // no point checking again
120 Integer start = pageSize == null ? 0 : pageSize * pageNumber;
121 results = dao.list(type, pageSize, start, orderHints,propertyPaths);
122 }
123 return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
124 }
125
126 @Transactional(readOnly = true)
127 public UUID refresh(T persistentObject) {
128 return dao.refresh(persistentObject);
129 }
130
131 /**
132 * FIXME Candidate for harmonization
133 * is this method used, and if so, should it be exposed in the service layer?
134 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
135 * database, then expose a method that talks about "rows" . . .
136 */
137 @Transactional(readOnly = true)
138 public List<T> rows(String tableName, int limit, int start) {
139 return dao.rows(tableName, limit, start);
140 }
141
142 @Transactional(readOnly = false)
143 public Map<UUID, T> save(Collection<T> newInstances) {
144 return dao.saveAll(newInstances);
145 }
146
147 @Transactional(readOnly = false)
148 public UUID save(T newInstance) {
149 return dao.save(newInstance);
150 }
151
152 @Transactional(readOnly = false)
153 public UUID saveOrUpdate(T transientObject) {
154 return dao.saveOrUpdate(transientObject);
155 }
156
157 @Transactional(readOnly = false)
158 public Map<UUID, T> saveOrUpdateAll(Collection<T> transientInstances) {
159 return dao.saveOrUpdateAll(transientInstances);
160 }
161
162 /* (non-Javadoc)
163 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
164 */
165 public void setApplicationContext(ApplicationContext appContext){
166 this.appContext = appContext;
167 }
168
169
170 protected abstract void setDao(DAO dao);
171
172 @Transactional(readOnly = false)
173 public UUID update(T transientObject) {
174 return dao.update(transientObject);
175 }
176
177 @Transactional(readOnly = true)
178 public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
179 return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
180 }
181 }