delete warnings in cdmlib-services
[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.beans.factory.annotation.Autowired;
21 import org.springframework.beans.factory.annotation.Qualifier;
22 import org.springframework.context.ApplicationContext;
23 import org.springframework.context.ApplicationContextAware;
24 import org.springframework.transaction.TransactionStatus;
25 import org.springframework.transaction.annotation.Transactional;
26
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
29
30
31 public abstract class ServiceBase<T extends CdmBase> implements IService<T>, ApplicationContextAware {
32 private static final Logger logger = Logger.getLogger(ServiceBase.class);
33
34 //flush after saving this number of objects
35 int flushAfterNo = 2000;
36 protected ApplicationContext appContext;
37
38 @Autowired
39 @Qualifier("cdmDao")
40 protected ICdmEntityDao<T> dao;
41
42 protected void setEntityDao(ICdmEntityDao<T> dao){
43 this.dao=dao;
44 }
45
46 /* (non-Javadoc)
47 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
48 */
49 public void setApplicationContext(ApplicationContext appContext){
50 this.appContext = appContext;
51 }
52
53 protected T getCdmObjectByUuid(UUID uuid){
54 return dao.findByUuid(uuid);
55 }
56
57
58 public int count(Class<T> clazz) {
59 return dao.count(clazz);
60 }
61
62 @Transactional(readOnly = false)
63 protected UUID saveCdmObject(T cdmObj){
64 if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
65 return dao.saveOrUpdate(cdmObj);
66 }
67
68 @Transactional(readOnly = false)
69 protected UUID saveCdmObject(T cdmObj, TransactionStatus txStatus){
70 // TODO: Implement with considering txStatus
71 if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
72 return dao.saveOrUpdate(cdmObj);
73 }
74
75 @Transactional(readOnly = false)
76 protected <S extends T> Map<UUID, S> saveCdmObjectAll(Collection<S> cdmObjCollection){
77 int types = cdmObjCollection.getClass().getTypeParameters().length;
78 if (types > 0){
79 if (logger.isDebugEnabled()){logger.debug("ClassType: + " + cdmObjCollection.getClass().getTypeParameters()[0]);}
80 }
81
82 Map<UUID, S> resultMap = new HashMap<UUID, S>();
83 Iterator<S> iterator = cdmObjCollection.iterator();
84 int i = 0;
85 while(iterator.hasNext()){
86 if ( ( (i % 5000) == 0) && (i > 0) ){logger.debug("Saved " + i + " objects" );}
87 S cdmObj = iterator.next();
88 UUID uuid = saveCdmObject(cdmObj);
89 // if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
90 resultMap.put(uuid, cdmObj);
91 i++;
92 if ( (i % flushAfterNo) == 0){
93 try{
94 logger.debug("flush");
95 dao.flush();
96 }catch(Exception e){
97 logger.error("UUUIIIII");
98 e.printStackTrace();
99 }
100 }
101 }
102
103 if ( logger.isInfoEnabled() ){logger.info("Saved " + i + " objects" );}
104 return resultMap;
105 }
106
107 @Transactional(readOnly = false)
108 protected UUID removeCdmObject(T cdmObj){
109 if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
110 return dao.delete(cdmObj);
111 }
112
113 public List<T> list(int limit, int start) {
114 return dao.list(limit, start);
115 }
116
117 public List<T> rows(String tableName, int limit, int start) {
118 return dao.rows(tableName, limit, start);
119 }
120 }