Counting rows
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / ServiceBase.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.api.service;
11
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.UUID;
18
19 import org.apache.log4j.Logger;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.context.ApplicationContextAware;
22 import org.springframework.transaction.annotation.Transactional;
23
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
26
27
28 public abstract class ServiceBase<T extends CdmBase> implements IService<T>, ApplicationContextAware {
29 static Logger logger = Logger.getLogger(ServiceBase.class);
30
31 //flush after saving this number of objects
32 int flushAfterNo = 2000;
33 protected ApplicationContext appContext;
34 protected ICdmEntityDao<T> dao;
35
36 protected void setEntityDao(ICdmEntityDao<T> dao){
37 this.dao=dao;
38 }
39
40 /* (non-Javadoc)
41 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
42 */
43 public void setApplicationContext(ApplicationContext appContext){
44 this.appContext = appContext;
45 }
46
47 protected T getCdmObjectByUuid(UUID uuid){
48 return dao.findByUuid(uuid);
49 }
50
51
52 public int count(Class<T> clazz) {
53 return dao.count(clazz);
54 }
55
56 @Transactional(readOnly = false)
57 protected UUID saveCdmObject(T cdmObj){
58 if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
59 return dao.saveOrUpdate(cdmObj);
60 }
61
62 @Transactional(readOnly = false)
63 protected Map<UUID, T> saveCdmObjectAll(Collection<T> cdmObjCollection){
64 int types = cdmObjCollection.getClass().getTypeParameters().length;
65 if (types > 0){
66 if (logger.isDebugEnabled()){logger.debug("ClassType: + " + cdmObjCollection.getClass().getTypeParameters()[0]);}
67 }
68
69 Map<UUID, T> resultMap = new HashMap<UUID, T>();
70 Iterator<T> iterator = cdmObjCollection.iterator();
71 int i = 0;
72 while(iterator.hasNext()){
73 if ( ( (i % 5000) == 0) && (i > 0) ){logger.debug("Saved " + i + " objects" );}
74 T cdmObj = iterator.next();
75 UUID uuid = saveCdmObject(cdmObj);
76 if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
77 resultMap.put(uuid, cdmObj);
78 i++;
79 if ( (i % flushAfterNo) == 0){
80 try{
81 logger.debug("flush");
82 dao.flush();
83 }catch(Exception e){
84 logger.error("UUUIIIII");
85 e.printStackTrace();
86 }
87 }
88 }
89
90 if ( logger.isInfoEnabled() ){logger.info("Saved " + i + " objects" );}
91 return resultMap;
92 }
93
94 @Transactional(readOnly = false)
95 protected UUID removeCdmObject(T cdmObj){
96 if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
97 return dao.delete(cdmObj);
98 }
99
100 protected List<T> list(int limit, int start) {
101 return dao.list(limit, start);
102 }
103
104 }