1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.io.common;
|
5
|
|
6
|
import java.util.HashMap;
|
7
|
import java.util.HashSet;
|
8
|
import java.util.Map;
|
9
|
import java.util.Collection;
|
10
|
import java.util.Set;
|
11
|
import java.util.UUID;
|
12
|
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
import eu.etaxonomy.cdm.api.service.IService;
|
17
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
18
|
|
19
|
/**
|
20
|
* @author a.mueller
|
21
|
*
|
22
|
*/
|
23
|
public class MapWrapper<T extends CdmBase> {
|
24
|
private static Logger logger = Logger.getLogger(MapWrapper.class);
|
25
|
|
26
|
private Map internalMap;
|
27
|
private IService<CdmBase> service = null;
|
28
|
|
29
|
public MapWrapper(IService<CdmBase> service){
|
30
|
makeNewMap(service);
|
31
|
}
|
32
|
|
33
|
public void put(Object id, T cdmBase){
|
34
|
if (service != null){
|
35
|
throw new RuntimeException();
|
36
|
}else{
|
37
|
internalMap.put(id, cdmBase);
|
38
|
}
|
39
|
}
|
40
|
|
41
|
public void put(Object id, UUID uuid){
|
42
|
if (service == null){
|
43
|
throw new RuntimeException();
|
44
|
}else{
|
45
|
//TODO
|
46
|
//service.save(cdmBase);
|
47
|
internalMap.put(id, uuid);
|
48
|
}
|
49
|
}
|
50
|
|
51
|
public T get(Object id){
|
52
|
T result;
|
53
|
if (service == null){
|
54
|
result = (T)internalMap.get(id);
|
55
|
}else{
|
56
|
result = getObjectFromService(id);
|
57
|
}
|
58
|
return result;
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Returns all values that are either stored in the wrapper or the the database.
|
63
|
* If <code>service</code> is null then only the elements stored in the wrapper are returned.
|
64
|
* @return
|
65
|
*/
|
66
|
public Set<T> getAllValues(){
|
67
|
Set<T> result = new HashSet<T>();
|
68
|
if (service == null){
|
69
|
result.addAll(internalMap.values());
|
70
|
}else{
|
71
|
result.addAll(internalMap.values());
|
72
|
logger.warn("getAll not yet implemented !!");
|
73
|
//TODO Set<T> persitentAll = service.getAll();
|
74
|
//result.addAll(persistentALl);
|
75
|
}
|
76
|
return result;
|
77
|
}
|
78
|
|
79
|
public boolean containsId(Object id){
|
80
|
return internalMap.containsKey(id);
|
81
|
}
|
82
|
|
83
|
public Collection<T> objects(){
|
84
|
//TODO from service
|
85
|
return (Collection<T>)internalMap.values();
|
86
|
}
|
87
|
|
88
|
private T getObjectFromService(Object id){
|
89
|
if (service == null){
|
90
|
throw new RuntimeException("no service defined");
|
91
|
}else{
|
92
|
T result = null;
|
93
|
UUID uuid = (UUID)internalMap.get(id);
|
94
|
if (uuid == null){
|
95
|
result = null;
|
96
|
}else{
|
97
|
//logger.warn(uuid);
|
98
|
//TODO
|
99
|
//result = (T)service.getObjectUuid(uuid); //.getCdmObjectByUuid(uuid);// taxonService.getTaxonByUuid(taxonUuid);
|
100
|
}
|
101
|
return result;
|
102
|
}
|
103
|
}
|
104
|
|
105
|
public boolean makeEmpty(){
|
106
|
return makeNewMap(service);
|
107
|
}
|
108
|
|
109
|
public boolean makeNewMap(IService<CdmBase> service){
|
110
|
if (service == null){
|
111
|
internalMap = new HashMap<Integer, CdmBase>();
|
112
|
}else{
|
113
|
this.service = service;
|
114
|
internalMap = new HashMap<Integer, UUID>();
|
115
|
}
|
116
|
return true;
|
117
|
}
|
118
|
}
|