2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.session
;
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
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
22 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.cdm
.persistence
.dto
.MergeResult
;
32 public class CdmEntitySessionManager
implements ICdmEntitySessionManager
{
33 private static final Logger logger
= Logger
.getLogger(CdmEntitySessionManager
.class);
35 private final Map
<ICdmEntitySessionEnabled
, ICdmEntitySession
> ownerSessionMap
=
36 new HashMap
<ICdmEntitySessionEnabled
, ICdmEntitySession
>();
38 private final List
<ICdmEntitySessionManagerObserver
> sessionObservers
= new ArrayList
<ICdmEntitySessionManagerObserver
>();
40 //private ICdmEntitySession activeSession;
42 private final InheritableThreadLocal
<ICdmEntitySession
> tlActiveSession
= new InheritableThreadLocal
<ICdmEntitySession
>();
44 private NullSession nullSession
;
47 public ICdmEntitySession
getNullSession() {
52 public ICdmEntitySession
newSession(ICdmEntitySessionEnabled sessionOwner
, boolean setAsActive
) {
53 ICdmEntitySession session
= ownerSessionMap
.get(sessionOwner
);
55 session
= new CdmEntitySession(sessionOwner
, this);
56 addToOwnerSessionMap(sessionOwner
, session
);
60 setActiveSession(session
);
67 public ICdmEntitySession
bindNullSession() {
69 if(nullSession
== null) {
70 nullSession
= new NullSession(null, this);
73 setActiveSession(nullSession
);
79 public ICdmEntitySession
getActiveSession() {
80 return tlActiveSession
.get();
84 private void setActiveSession(ICdmEntitySession activeSession
) {
85 this. tlActiveSession
.set(activeSession
);
91 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#getSessions()
94 public Collection
<ICdmEntitySession
> getSessions() {
95 return ownerSessionMap
.values();
100 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#bind(eu.etaxonomy.taxeditor.session.ISessionEventListener)
103 public void bind(ICdmEntitySessionEnabled sessionOwner
) {
104 if(sessionOwner
== null) {
105 setActiveSession(null);
108 ICdmEntitySession session
= ownerSessionMap
.get(sessionOwner
);
109 if(session
== null) {
110 throw new CdmClientSessionException("Trying to bind session which does not exist");
113 setActiveSession(session
);
118 public boolean contains(ICdmEntitySessionEnabled sessionOwner
) {
119 return ownerSessionMap
.containsKey(sessionOwner
);
124 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(T)
127 public <T
extends Object
> T
load(T obj
, boolean update
) {
128 if(tlActiveSession
.get() == null) {
131 return tlActiveSession
.get().load(obj
, update
);
137 public <T
extends CdmBase
> void update() {
138 if(tlActiveSession
.get() != null) {
139 tlActiveSession
.get().update();
144 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(T)
147 public <T
extends CdmBase
> T
load(T cdmBase
, boolean update
) {
148 if(tlActiveSession
.get() == null) {
151 return tlActiveSession
.get().load(cdmBase
, update
);
156 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(eu.etaxonomy.cdm.api.service.UpdateResult, boolean)
159 public UpdateResult
load(UpdateResult updateResult
, boolean update
) {
160 if(tlActiveSession
.get() == null) {
163 return tlActiveSession
.get().load(updateResult
, update
);
168 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(eu.etaxonomy.cdm.persistence.dto.MergeResult, boolean)
171 public MergeResult
load(MergeResult mergeResult
, boolean update
) {
172 if(tlActiveSession
.get() == null) {
175 return tlActiveSession
.get().load(mergeResult
, update
);
180 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#load(java.util.Collection)
183 public <T
extends CdmBase
> Collection
<T
> load(Collection
<T
> cdmBaseList
, boolean update
) {
184 if(tlActiveSession
.get() == null) {
187 return tlActiveSession
.get().load(cdmBaseList
, update
);
192 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#dispose(eu.etaxonomy.taxeditor.session.ISessionEventListener)
194 void remove(ICdmEntitySessionEnabled owner
) {
195 ICdmEntitySession session
= ownerSessionMap
.get(owner
);
196 if(session
== null) {
197 logger
.info("No Session connected to owner, nothing to do");
200 if(session
== tlActiveSession
.get()) {
201 setActiveSession(null);
203 ownerSessionMap
.remove(owner
);
209 public void dispose(ICdmEntitySessionEnabled owner
) {
210 ICdmEntitySession session
= ownerSessionMap
.get(owner
);
211 if(session
!= null) {
214 if(nullSession
!= null && nullSession
.getOwner() == owner
) {
222 public void disposeAll() {
223 Set
<ICdmEntitySessionEnabled
> owners
=
224 new HashSet
<ICdmEntitySessionEnabled
>(ownerSessionMap
.keySet());
225 for(ICdmEntitySessionEnabled owner
: owners
) {
226 ICdmEntitySession session
= ownerSessionMap
.get(owner
);
227 if(session
!= null) {
234 void addToOwnerSessionMap(ICdmEntitySessionEnabled owner
, ICdmEntitySession session
) {
235 ownerSessionMap
.put(owner
, session
);
240 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#addSessionListener()
243 public void addSessionObserver(ICdmEntitySessionManagerObserver sessionObserver
) {
244 sessionObservers
.add(sessionObserver
);
247 public void notifyObservers() {
248 for(ICdmEntitySessionManagerObserver sessionObserver
: sessionObservers
) {
249 sessionObserver
.changed();
254 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager#isRemoting()
257 public boolean isRemoting() {
258 // FIXME:Remoting stupid method to check whether we are in remoting