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