additional service methods for new REST service
[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.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.UUID;
20
21 import org.apache.log4j.Logger;
22 import org.springframework.beans.factory.annotation.Qualifier;
23 import org.springframework.context.ApplicationContext;
24 import org.springframework.context.ApplicationContextAware;
25 import org.springframework.transaction.TransactionStatus;
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.model.reference.ReferenceBase;
32 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
33 import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
34 import eu.etaxonomy.cdm.persistence.query.OrderHint;
35
36 public abstract class ServiceBase<T extends CdmBase, DAO extends ICdmEntityDao<T>> implements IService<T>, ApplicationContextAware {
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 @Qualifier("baseDao")
44 protected DAO dao;
45
46 protected abstract void setDao(DAO dao);
47
48 /* (non-Javadoc)
49 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
50 */
51 public void setApplicationContext(ApplicationContext appContext){
52 this.appContext = appContext;
53 }
54
55 public T getCdmObjectByUuid(UUID uuid) {
56 return dao.findByUuid(uuid);
57 }
58
59 @Transactional(readOnly = true)
60 public <TYPE extends T> int count(Class<TYPE> clazz) {
61 return dao.count(clazz);
62 }
63
64 @Transactional(readOnly = true)
65 public int count() {
66 return dao.count();
67 }
68
69 /**
70 * FIXME harmonise with saveOrUpdate
71 * @param cdmObj
72 * @return
73 */
74 @Transactional(readOnly = false)
75 protected UUID saveCdmObject(T cdmObj){
76 if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
77 return dao.saveOrUpdate(cdmObj);
78 }
79
80
81 /**
82 * FIXME harmonise with saveOrUpdate
83 * @param cdmObj
84 * @return
85 */
86 @Transactional(readOnly = false)
87 protected UUID saveCdmObject(T cdmObj, TransactionStatus txStatus){
88 // TODO: Implement with considering txStatus
89 if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
90 return dao.saveOrUpdate(cdmObj);
91 }
92
93 /**
94 * FIXME harmonise with saveAll
95 * @param <S>
96 * @param cdmObjCollection
97 * @return
98 */
99 @Transactional(readOnly = false)
100 protected <S extends T> Map<UUID, S> saveCdmObjectAll(Collection<? extends S> cdmObjCollection){
101 int types = cdmObjCollection.getClass().getTypeParameters().length;
102 if (types > 0){
103 if (logger.isDebugEnabled()){logger.debug("ClassType: + " + cdmObjCollection.getClass().getTypeParameters()[0]);}
104 }
105
106 Map<UUID, S> resultMap = new HashMap<UUID, S>();
107 Iterator<? extends S> iterator = cdmObjCollection.iterator();
108 int i = 0;
109 while(iterator.hasNext()){
110 if ( ( (i % 5000) == 0) && (i > 0) ){logger.debug("Saved " + i + " objects" );}
111 S cdmObj = iterator.next();
112 UUID uuid = saveCdmObject(cdmObj);
113 // if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
114 resultMap.put(uuid, cdmObj);
115 i++;
116 if ( (i % flushAfterNo) == 0){
117 try{
118 logger.debug("flush");
119 dao.flush();
120 }catch(Exception e){
121 logger.error("UUUIIIII");
122 e.printStackTrace();
123 }
124 }
125 }
126
127 if ( logger.isInfoEnabled() ){logger.info("Saved " + i + " objects" );}
128 return resultMap;
129 }
130
131 @Transactional(readOnly = false)
132 public UUID delete(T persistentObject) {
133 return dao.delete(persistentObject);
134 }
135
136 @Transactional(readOnly = true)
137 public boolean exists(UUID uuid) {
138 return dao.exists(uuid);
139 }
140
141 @Transactional(readOnly = true)
142 public T findByUuid(UUID uuid) {
143 return dao.findByUuid(uuid);
144 }
145
146 @Transactional(readOnly = true)
147 public T load(UUID uuid) {
148 return dao.load(uuid);
149 }
150
151 @Transactional(readOnly = true)
152 public T load(UUID uuid, List<String> propertyPaths){
153 return dao.load(uuid, propertyPaths);
154 }
155
156
157 @Transactional(readOnly = true)
158 public <TYPE extends T> List<TYPE> list(Class<TYPE> type, int limit,int start) {
159 return dao.list(type, limit, start);
160 }
161
162 @Transactional(readOnly = true)
163 public Pager<T> list(Integer pageSize, Integer pageNumber){
164 return list(pageSize, pageNumber, null);
165 }
166
167 @Transactional (readOnly = true)
168 public Pager<T> list(Integer pageSize, Integer pageNumber, List<OrderHint> orderHints){
169 Integer numberOfResults = dao.count();
170 List<T> results = new ArrayList<T>();
171 if(numberOfResults > 0) { // no point checking again
172 Integer start = pageSize == null ? null : pageSize * (pageNumber - 1);
173 results = dao.list(pageSize, start, orderHints);
174 }
175 return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
176 }
177
178 @Transactional(readOnly = false)
179 public UUID save(T newInstance) {
180 return dao.save(newInstance);
181 }
182
183 @Transactional(readOnly = false)
184 public Map<UUID, T> saveAll(Collection<T> newInstances) {
185 return dao.saveAll(newInstances);
186 }
187
188 @Transactional(readOnly = false)
189 public UUID saveOrUpdate(T transientObject) {
190 return dao.saveOrUpdate(transientObject);
191 }
192
193 @Transactional(readOnly = false)
194 public UUID update(T transientObject) {
195 return dao.update(transientObject);
196 }
197
198 @Transactional(readOnly = true)
199 public UUID refresh(T persistentObject) {
200 return dao.refresh(persistentObject);
201 }
202
203 /**
204 * FIXME harmonise with delete()
205 * @param cdmObj
206 * @return
207 */
208 @Transactional(readOnly = false)
209 protected UUID removeCdmObject(T cdmObj){
210 if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
211 return dao.delete(cdmObj);
212 }
213
214 @Transactional(readOnly = true)
215 public List<T> list(int limit, int start) {
216 return dao.list(limit, start);
217 }
218
219 @Transactional(readOnly = true)
220 public List<T> rows(String tableName, int limit, int start) {
221 return dao.rows(tableName, limit, start);
222 }
223 }