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