Project

General

Profile

Download (3.46 KB) Statistics
| Branch: | Tag: | Revision:
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.annotation.Transactional;
25

    
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
28

    
29

    
30
public abstract class ServiceBase<T extends CdmBase> implements IService<T>, ApplicationContextAware {
31
	static Logger logger = Logger.getLogger(ServiceBase.class);
32
	
33
	//flush after saving this number of objects
34
	int flushAfterNo = 2000;
35
	protected ApplicationContext appContext;
36

    
37
	@Autowired
38
	@Qualifier("cdmDao")
39
	protected ICdmEntityDao<T> dao;
40
	
41
	protected void setEntityDao(ICdmEntityDao<T> dao){
42
		this.dao=dao;
43
	}
44
	
45
	/* (non-Javadoc)
46
	 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
47
	 */
48
	public void setApplicationContext(ApplicationContext appContext){
49
		this.appContext = appContext;
50
	}
51

    
52
	protected T getCdmObjectByUuid(UUID uuid){
53
		return dao.findByUuid(uuid);
54
	}
55
	
56

    
57
	public int count(Class<T> clazz) {
58
		return dao.count(clazz);
59
	}
60

    
61
	@Transactional(readOnly = false)
62
	protected UUID saveCdmObject(T cdmObj){
63
		if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
64
		return dao.saveOrUpdate(cdmObj);
65
	}
66

    
67
	@Transactional(readOnly = false)
68
	protected <S extends T> Map<UUID, S> saveCdmObjectAll(Collection<S> cdmObjCollection){
69
		int types = cdmObjCollection.getClass().getTypeParameters().length;
70
		if (types > 0){
71
			if (logger.isDebugEnabled()){logger.debug("ClassType: + " + cdmObjCollection.getClass().getTypeParameters()[0]);}
72
		}
73
		
74
		Map<UUID, S> resultMap = new HashMap<UUID, S>();
75
		Iterator<S> iterator = cdmObjCollection.iterator();
76
		int i = 0;
77
			while(iterator.hasNext()){
78
				if ( ( (i % 5000) == 0) && (i > 0)   ){logger.debug("Saved " + i + " objects" );}
79
				S cdmObj = iterator.next();
80
				UUID uuid = saveCdmObject(cdmObj);
81
				if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
82
				resultMap.put(uuid, cdmObj);
83
				i++;
84
				if ( (i % flushAfterNo) == 0){
85
					try{
86
									logger.debug("flush");
87
					dao.flush();
88
					}catch(Exception e){
89
						logger.error("UUUIIIII");
90
						e.printStackTrace();
91
					}
92
				}
93
			}
94

    
95
		if ( logger.isInfoEnabled() ){logger.info("Saved " + i + " objects" );}
96
		return resultMap;
97
	}
98
	
99
	@Transactional(readOnly = false)
100
	protected UUID removeCdmObject(T cdmObj){
101
		if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
102
		return dao.delete(cdmObj);
103
	}
104
	
105
	public List<T> list(int limit, int start) {
106
		return dao.list(limit, start);
107
	}
108

    
109
	public List<T> rows(String tableName, int limit, int start) {
110
		return dao.rows(tableName, limit, start);
111
	}
112
}
(22-22/24)