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