1
|
/**
|
2
|
* Copyright (C) 2014 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
|
package eu.etaxonomy.taxeditor.session;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
|
25
|
|
26
|
/**
|
27
|
* @author cmathew
|
28
|
* @date 16 Oct 2014
|
29
|
*/
|
30
|
@Component
|
31
|
public class CdmEntitySessionManager implements ICdmEntitySessionManager {
|
32
|
private static final Logger logger = Logger.getLogger(CdmEntitySessionManager.class);
|
33
|
|
34
|
private final Map<ICdmEntitySessionEnabled, ICdmEntitySession> ownerSessionMap = new HashMap<>();
|
35
|
|
36
|
private final List<ICdmEntitySessionManagerObserver> sessionObservers = new ArrayList<>();
|
37
|
|
38
|
//private ICdmEntitySession activeSession;
|
39
|
|
40
|
private final InheritableThreadLocal<ICdmEntitySession> tlActiveSession = new InheritableThreadLocal<>();
|
41
|
|
42
|
private NullSession nullSession;
|
43
|
|
44
|
@Override
|
45
|
public ICdmEntitySession getNullSession() {
|
46
|
return nullSession;
|
47
|
}
|
48
|
|
49
|
@Override
|
50
|
public ICdmEntitySession newSession(ICdmEntitySessionEnabled sessionOwner, boolean setAsActive) {
|
51
|
ICdmEntitySession session = ownerSessionMap.get(sessionOwner);
|
52
|
if(session == null) {
|
53
|
session = new CdmEntitySession(sessionOwner, this);
|
54
|
addToOwnerSessionMap(sessionOwner, session);
|
55
|
}
|
56
|
|
57
|
if(setAsActive) {
|
58
|
setActiveSession(session);
|
59
|
}
|
60
|
|
61
|
return session;
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public ICdmEntitySession bindNullSession() {
|
66
|
|
67
|
if(nullSession == null) {
|
68
|
nullSession = new NullSession(null, this);
|
69
|
}
|
70
|
|
71
|
setActiveSession(nullSession);
|
72
|
|
73
|
return nullSession;
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
public ICdmEntitySession getActiveSession() {
|
78
|
return tlActiveSession.get();
|
79
|
}
|
80
|
|
81
|
private void setActiveSession(ICdmEntitySession activeSession) {
|
82
|
this. tlActiveSession.set(activeSession);
|
83
|
notifyObservers();
|
84
|
}
|
85
|
|
86
|
@Override
|
87
|
public Collection<ICdmEntitySession> getSessions() {
|
88
|
return ownerSessionMap.values();
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public void bind(ICdmEntitySessionEnabled sessionOwner) {
|
93
|
if(sessionOwner == null) {
|
94
|
setActiveSession(null);
|
95
|
return;
|
96
|
}
|
97
|
ICdmEntitySession session = ownerSessionMap.get(sessionOwner);
|
98
|
if(session == null) {
|
99
|
throw new CdmClientSessionException("Trying to bind session which does not exist");
|
100
|
}
|
101
|
|
102
|
setActiveSession(session);
|
103
|
}
|
104
|
|
105
|
@Override
|
106
|
public boolean contains(ICdmEntitySessionEnabled sessionOwner) {
|
107
|
return ownerSessionMap.containsKey(sessionOwner);
|
108
|
}
|
109
|
|
110
|
@Override
|
111
|
public <T extends Object> T load(T obj, boolean update) {
|
112
|
if(tlActiveSession.get() == null) {
|
113
|
return obj;
|
114
|
} else {
|
115
|
return tlActiveSession.get().load(obj, update);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public <T extends CdmBase> void update() {
|
121
|
if(tlActiveSession.get() != null) {
|
122
|
tlActiveSession.get().update();
|
123
|
}
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
public <T extends CdmBase> T load(T cdmBase, boolean update) {
|
128
|
if(tlActiveSession.get() == null) {
|
129
|
return cdmBase;
|
130
|
}
|
131
|
return tlActiveSession.get().load(cdmBase, update);
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
public UpdateResult load(UpdateResult updateResult, boolean update) {
|
136
|
if(tlActiveSession.get() == null) {
|
137
|
return updateResult;
|
138
|
}
|
139
|
return tlActiveSession.get().load(updateResult, update);
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public MergeResult load(MergeResult mergeResult, boolean update) {
|
144
|
if(tlActiveSession.get() == null) {
|
145
|
return mergeResult;
|
146
|
}
|
147
|
return tlActiveSession.get().load(mergeResult, update);
|
148
|
}
|
149
|
|
150
|
@Override
|
151
|
public <T extends CdmBase> Collection<T> load(Collection<T> cdmBaseList, boolean update) {
|
152
|
if(tlActiveSession.get() == null) {
|
153
|
return cdmBaseList;
|
154
|
}
|
155
|
return tlActiveSession.get().load(cdmBaseList, update);
|
156
|
}
|
157
|
|
158
|
void remove(ICdmEntitySessionEnabled owner) {
|
159
|
ICdmEntitySession session = ownerSessionMap.get(owner);
|
160
|
if(session == null) {
|
161
|
logger.info("No Session connected to owner, nothing to do");
|
162
|
return;
|
163
|
}
|
164
|
if(session == tlActiveSession.get()) {
|
165
|
setActiveSession(null);
|
166
|
}
|
167
|
ownerSessionMap.remove(owner);
|
168
|
notifyObservers();
|
169
|
|
170
|
}
|
171
|
|
172
|
@Override
|
173
|
public void dispose(ICdmEntitySessionEnabled owner) {
|
174
|
ICdmEntitySession session = ownerSessionMap.get(owner);
|
175
|
if(session != null) {
|
176
|
session.dispose();
|
177
|
}
|
178
|
if(nullSession != null && nullSession.getOwner() == owner) {
|
179
|
nullSession = null;
|
180
|
}
|
181
|
}
|
182
|
|
183
|
@Override
|
184
|
public void disposeAll() {
|
185
|
Set<ICdmEntitySessionEnabled> owners =
|
186
|
new HashSet<ICdmEntitySessionEnabled>(ownerSessionMap.keySet());
|
187
|
for(ICdmEntitySessionEnabled owner : owners) {
|
188
|
ICdmEntitySession session = ownerSessionMap.get(owner);
|
189
|
if(session != null) {
|
190
|
session.dispose();
|
191
|
}
|
192
|
}
|
193
|
}
|
194
|
|
195
|
void addToOwnerSessionMap(ICdmEntitySessionEnabled owner, ICdmEntitySession session) {
|
196
|
ownerSessionMap.put(owner, session);
|
197
|
notifyObservers();
|
198
|
}
|
199
|
|
200
|
@Override
|
201
|
public void addSessionObserver(ICdmEntitySessionManagerObserver sessionObserver) {
|
202
|
sessionObservers.add(sessionObserver);
|
203
|
}
|
204
|
|
205
|
public void notifyObservers() {
|
206
|
for(ICdmEntitySessionManagerObserver sessionObserver : sessionObservers) {
|
207
|
sessionObserver.changed();
|
208
|
}
|
209
|
}
|
210
|
|
211
|
@Override
|
212
|
public boolean isRemoting() {
|
213
|
// FIXME:Remoting stupid method to check whether we are in remoting
|
214
|
return true;
|
215
|
}
|
216
|
|
217
|
}
|
218
|
|