4158 |
22 Feb 08 |
nicklas |
1 |
/** |
4198 |
28 Mar 08 |
nicklas |
$Id:Context.java 4187 2008-03-20 11:15:25Z nicklas $ |
4158 |
22 Feb 08 |
nicklas |
3 |
|
4158 |
22 Feb 08 |
nicklas |
Copyright (C) Authors contributing to this file. |
4158 |
22 Feb 08 |
nicklas |
5 |
|
4158 |
22 Feb 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4158 |
22 Feb 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4158 |
22 Feb 08 |
nicklas |
8 |
|
4158 |
22 Feb 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4158 |
22 Feb 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
4158 |
22 Feb 08 |
nicklas |
of the License, or (at your option) any later version. |
4158 |
22 Feb 08 |
nicklas |
13 |
|
4158 |
22 Feb 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4158 |
22 Feb 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4158 |
22 Feb 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4158 |
22 Feb 08 |
nicklas |
GNU General Public License for more details. |
4158 |
22 Feb 08 |
nicklas |
18 |
|
4158 |
22 Feb 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4158 |
22 Feb 08 |
nicklas |
21 |
*/ |
4158 |
22 Feb 08 |
nicklas |
22 |
package net.sf.basedb.util.extensions; |
4158 |
22 Feb 08 |
nicklas |
23 |
|
4168 |
04 Mar 08 |
nicklas |
24 |
import java.util.HashMap; |
4168 |
04 Mar 08 |
nicklas |
25 |
import java.util.Map; |
4168 |
04 Mar 08 |
nicklas |
26 |
|
4237 |
18 Apr 08 |
nicklas |
27 |
import net.sf.basedb.core.DbControl; |
8083 |
20 Oct 22 |
nicklas |
28 |
import net.sf.basedb.core.ItemContext; |
4158 |
22 Feb 08 |
nicklas |
29 |
import net.sf.basedb.core.SessionControl; |
4158 |
22 Feb 08 |
nicklas |
30 |
|
4158 |
22 Feb 08 |
nicklas |
31 |
/** |
4207 |
04 Apr 08 |
nicklas |
Keeps information about the current context in the client |
4207 |
04 Apr 08 |
nicklas |
application that extensions can use to decide what actions to |
4207 |
04 Apr 08 |
nicklas |
generate. It is, for example, possible to check the permissions |
4168 |
04 Mar 08 |
nicklas |
of logged in user and enable/disable certain actions |
4168 |
04 Mar 08 |
nicklas |
if a condition is not met. Or, the extension may inspect |
4168 |
04 Mar 08 |
nicklas |
the current item and decide if the extension can be used |
4168 |
04 Mar 08 |
nicklas |
on that item or not. |
4168 |
04 Mar 08 |
nicklas |
39 |
|
4168 |
04 Mar 08 |
nicklas |
<p> |
4168 |
04 Mar 08 |
nicklas |
The current context is passed to an {@link ActionFactory} twice, first |
4168 |
04 Mar 08 |
nicklas |
to check if an extension should be enabled or not |
4207 |
04 Apr 08 |
nicklas |
with the {@link ActionFactory#prepareContext(InvokationContext)} |
4168 |
04 Mar 08 |
nicklas |
method. This method also allows an extension to write |
4168 |
04 Mar 08 |
nicklas |
back information to the context. This may, for example, |
4168 |
04 Mar 08 |
nicklas |
include information about which Javascript scripts to import |
4168 |
04 Mar 08 |
nicklas |
or which stylesheets to load. The <code>prepareContext</code> |
4168 |
04 Mar 08 |
nicklas |
method is only called once for each use of an extension. |
4158 |
22 Feb 08 |
nicklas |
49 |
|
4168 |
04 Mar 08 |
nicklas |
<p> |
4168 |
04 Mar 08 |
nicklas |
The second time is when calling the {@link |
4207 |
04 Apr 08 |
nicklas |
ActionFactory#getActions(InvokationContext)} method. This method can |
4168 |
04 Mar 08 |
nicklas |
be called multiple times. For example, if an extension point |
4168 |
04 Mar 08 |
nicklas |
is in a list context, the <code>getActions</code> method may |
4168 |
04 Mar 08 |
nicklas |
be called one time for each item in the list. If so, the |
4168 |
04 Mar 08 |
nicklas |
{@link #getCurrentItem()} returns the currently active item. |
4168 |
04 Mar 08 |
nicklas |
57 |
|
4168 |
04 Mar 08 |
nicklas |
<p> |
4168 |
04 Mar 08 |
nicklas |
Client applications may subclass this object to provide more |
4168 |
04 Mar 08 |
nicklas |
information about the client environment. See, for example, |
4168 |
04 Mar 08 |
nicklas |
the {@link net.sf.basedb.clients.web.extensions.JspContext} object. |
4168 |
04 Mar 08 |
nicklas |
62 |
|
4158 |
22 Feb 08 |
nicklas |
@author nicklas |
4158 |
22 Feb 08 |
nicklas |
@version 2.7 |
4198 |
28 Mar 08 |
nicklas |
@base.modified $Date:2008-03-20 12:15:25 +0100 (Thu, 20 Mar 2008) $ |
4168 |
04 Mar 08 |
nicklas |
66 |
*/ |
4207 |
04 Apr 08 |
nicklas |
67 |
public class ClientContext |
4158 |
22 Feb 08 |
nicklas |
68 |
{ |
4158 |
22 Feb 08 |
nicklas |
69 |
|
4158 |
22 Feb 08 |
nicklas |
70 |
private final SessionControl sc; |
4237 |
18 Apr 08 |
nicklas |
71 |
private final DbControl dc; |
8045 |
03 Jun 22 |
nicklas |
72 |
private ExtensionPoint<?> currentXtPoint; |
8045 |
03 Jun 22 |
nicklas |
73 |
private Extension<?> currentXt; |
4158 |
22 Feb 08 |
nicklas |
74 |
private Object item; |
4168 |
04 Mar 08 |
nicklas |
75 |
private Map<String, Object> attributes; |
4158 |
22 Feb 08 |
nicklas |
76 |
|
4168 |
04 Mar 08 |
nicklas |
77 |
/** |
5599 |
31 Mar 11 |
nicklas |
Create a new context with no session or current item. |
5599 |
31 Mar 11 |
nicklas |
@since 3.0 |
5599 |
31 Mar 11 |
nicklas |
80 |
*/ |
5599 |
31 Mar 11 |
nicklas |
81 |
public ClientContext() |
5599 |
31 Mar 11 |
nicklas |
82 |
{ |
5599 |
31 Mar 11 |
nicklas |
83 |
this(null, null, null); |
5599 |
31 Mar 11 |
nicklas |
84 |
} |
5599 |
31 Mar 11 |
nicklas |
85 |
|
5599 |
31 Mar 11 |
nicklas |
86 |
/** |
4237 |
18 Apr 08 |
nicklas |
Create a new context with a session and no DbControl. The session |
4237 |
18 Apr 08 |
nicklas |
is final and can't be replaced. |
4168 |
04 Mar 08 |
nicklas |
@param sc The current session |
4168 |
04 Mar 08 |
nicklas |
90 |
*/ |
4207 |
04 Apr 08 |
nicklas |
91 |
public ClientContext(SessionControl sc) |
4158 |
22 Feb 08 |
nicklas |
92 |
{ |
4237 |
18 Apr 08 |
nicklas |
93 |
this(sc, null, null); |
4158 |
22 Feb 08 |
nicklas |
94 |
} |
4158 |
22 Feb 08 |
nicklas |
95 |
|
4168 |
04 Mar 08 |
nicklas |
96 |
/** |
4237 |
18 Apr 08 |
nicklas |
Create a new context with a session and item but no DbControl. The session is |
4237 |
18 Apr 08 |
nicklas |
final and can't be replaced. Use {@link #setCurrentItem(Object)} to change |
4237 |
18 Apr 08 |
nicklas |
the item. |
4170 |
07 Mar 08 |
nicklas |
100 |
|
4168 |
04 Mar 08 |
nicklas |
@param sc The current session |
4168 |
04 Mar 08 |
nicklas |
@param item The current item |
4168 |
04 Mar 08 |
nicklas |
103 |
*/ |
4207 |
04 Apr 08 |
nicklas |
104 |
public ClientContext(SessionControl sc, Object item) |
4158 |
22 Feb 08 |
nicklas |
105 |
{ |
4237 |
18 Apr 08 |
nicklas |
106 |
this(sc, null, item); |
4237 |
18 Apr 08 |
nicklas |
107 |
} |
4237 |
18 Apr 08 |
nicklas |
108 |
|
4237 |
18 Apr 08 |
nicklas |
109 |
/** |
4237 |
18 Apr 08 |
nicklas |
Create a new context with a DbControl. The DbControl |
4237 |
18 Apr 08 |
nicklas |
is final and can't be replaced. |
4237 |
18 Apr 08 |
nicklas |
@param dc An open DbControl |
4237 |
18 Apr 08 |
nicklas |
113 |
*/ |
4237 |
18 Apr 08 |
nicklas |
114 |
public ClientContext(DbControl dc) |
4237 |
18 Apr 08 |
nicklas |
115 |
{ |
5614 |
18 Apr 11 |
nicklas |
116 |
this(dc == null ? null : dc.getSessionControl(), dc, null); |
4237 |
18 Apr 08 |
nicklas |
117 |
} |
4237 |
18 Apr 08 |
nicklas |
118 |
|
4237 |
18 Apr 08 |
nicklas |
119 |
/** |
4237 |
18 Apr 08 |
nicklas |
Create a new context with a DbControl and item. The DbControl is |
4237 |
18 Apr 08 |
nicklas |
final and can't be replaced. Use {@link #setCurrentItem(Object)} to change |
4237 |
18 Apr 08 |
nicklas |
the item. |
4237 |
18 Apr 08 |
nicklas |
123 |
|
4237 |
18 Apr 08 |
nicklas |
@param dc An open DbControl |
4237 |
18 Apr 08 |
nicklas |
@param item The current item |
4237 |
18 Apr 08 |
nicklas |
126 |
*/ |
4237 |
18 Apr 08 |
nicklas |
127 |
public ClientContext(DbControl dc, Object item) |
4237 |
18 Apr 08 |
nicklas |
128 |
{ |
5614 |
18 Apr 11 |
nicklas |
129 |
this(dc == null ? null : dc.getSessionControl(), dc, item); |
4237 |
18 Apr 08 |
nicklas |
130 |
} |
4237 |
18 Apr 08 |
nicklas |
131 |
|
4237 |
18 Apr 08 |
nicklas |
132 |
protected ClientContext(SessionControl sc, DbControl dc, Object item) |
4237 |
18 Apr 08 |
nicklas |
133 |
{ |
4237 |
18 Apr 08 |
nicklas |
134 |
this.dc = dc; |
4168 |
04 Mar 08 |
nicklas |
135 |
this.sc = sc; |
4158 |
22 Feb 08 |
nicklas |
136 |
this.item = item; |
4158 |
22 Feb 08 |
nicklas |
137 |
} |
4237 |
18 Apr 08 |
nicklas |
138 |
|
4168 |
04 Mar 08 |
nicklas |
139 |
/** |
4168 |
04 Mar 08 |
nicklas |
Get the current session control. |
4168 |
04 Mar 08 |
nicklas |
141 |
*/ |
4168 |
04 Mar 08 |
nicklas |
142 |
public SessionControl getSessionControl() |
4158 |
22 Feb 08 |
nicklas |
143 |
{ |
4168 |
04 Mar 08 |
nicklas |
144 |
return sc; |
4158 |
22 Feb 08 |
nicklas |
145 |
} |
4168 |
04 Mar 08 |
nicklas |
146 |
|
4168 |
04 Mar 08 |
nicklas |
147 |
/** |
4237 |
18 Apr 08 |
nicklas |
Get the current DbControl. This can be null if the |
4237 |
18 Apr 08 |
nicklas |
extension point doesn't provide an open DbControl. |
4237 |
18 Apr 08 |
nicklas |
An extension MUST NEVER call {@link DbControl#close()} |
4237 |
18 Apr 08 |
nicklas |
or {@link DbControl#commit()}. |
4237 |
18 Apr 08 |
nicklas |
152 |
*/ |
4237 |
18 Apr 08 |
nicklas |
153 |
public DbControl getDbControl() |
4237 |
18 Apr 08 |
nicklas |
154 |
{ |
4237 |
18 Apr 08 |
nicklas |
155 |
return dc; |
4237 |
18 Apr 08 |
nicklas |
156 |
} |
4237 |
18 Apr 08 |
nicklas |
157 |
|
4237 |
18 Apr 08 |
nicklas |
158 |
/** |
4168 |
04 Mar 08 |
nicklas |
Get the currently active item. It can be any type of |
4168 |
04 Mar 08 |
nicklas |
object, but the extension point may declare (in it's |
4168 |
04 Mar 08 |
nicklas |
documentation) that the current item is always of a |
4198 |
28 Mar 08 |
nicklas |
certain class. |
4168 |
04 Mar 08 |
nicklas |
@return The current item, or null |
4168 |
04 Mar 08 |
nicklas |
164 |
*/ |
7895 |
18 Jan 21 |
nicklas |
165 |
@SuppressWarnings("unchecked") |
7895 |
18 Jan 21 |
nicklas |
166 |
public <T> T getCurrentItem() |
4158 |
22 Feb 08 |
nicklas |
167 |
{ |
7895 |
18 Jan 21 |
nicklas |
168 |
return (T)item; |
4158 |
22 Feb 08 |
nicklas |
169 |
} |
4158 |
22 Feb 08 |
nicklas |
170 |
|
4168 |
04 Mar 08 |
nicklas |
171 |
/** |
4168 |
04 Mar 08 |
nicklas |
Set the currently active item. |
4168 |
04 Mar 08 |
nicklas |
@param item The item that is the current item |
4168 |
04 Mar 08 |
nicklas |
174 |
*/ |
4168 |
04 Mar 08 |
nicklas |
175 |
public void setCurrentItem(Object item) |
4158 |
22 Feb 08 |
nicklas |
176 |
{ |
4168 |
04 Mar 08 |
nicklas |
177 |
this.item = item; |
4158 |
22 Feb 08 |
nicklas |
178 |
} |
4158 |
22 Feb 08 |
nicklas |
179 |
|
4168 |
04 Mar 08 |
nicklas |
180 |
/** |
8045 |
03 Jun 22 |
nicklas |
Get the currently active extension point. |
8045 |
03 Jun 22 |
nicklas |
@since 3.19.3 |
8045 |
03 Jun 22 |
nicklas |
183 |
*/ |
8045 |
03 Jun 22 |
nicklas |
184 |
public ExtensionPoint<?> getCurrentExtensionPoint() |
8045 |
03 Jun 22 |
nicklas |
185 |
{ |
8045 |
03 Jun 22 |
nicklas |
186 |
return currentXtPoint; |
8045 |
03 Jun 22 |
nicklas |
187 |
} |
8045 |
03 Jun 22 |
nicklas |
188 |
protected void setCurrentExtensionPoint(ExtensionPoint<?> xtPoint) |
8045 |
03 Jun 22 |
nicklas |
189 |
{ |
8045 |
03 Jun 22 |
nicklas |
190 |
this.currentXtPoint = xtPoint; |
8045 |
03 Jun 22 |
nicklas |
191 |
} |
8045 |
03 Jun 22 |
nicklas |
192 |
|
8045 |
03 Jun 22 |
nicklas |
193 |
/** |
8045 |
03 Jun 22 |
nicklas |
Get the currently active extension point. |
8045 |
03 Jun 22 |
nicklas |
@since 3.19.3 |
8045 |
03 Jun 22 |
nicklas |
196 |
*/ |
8045 |
03 Jun 22 |
nicklas |
197 |
public Extension<?> getCurrentExtension() |
8045 |
03 Jun 22 |
nicklas |
198 |
{ |
8045 |
03 Jun 22 |
nicklas |
199 |
return currentXt; |
8045 |
03 Jun 22 |
nicklas |
200 |
} |
8045 |
03 Jun 22 |
nicklas |
201 |
protected void setCurrentExtension(Extension<?> xt) |
8045 |
03 Jun 22 |
nicklas |
202 |
{ |
8045 |
03 Jun 22 |
nicklas |
203 |
this.currentXt = xt; |
8045 |
03 Jun 22 |
nicklas |
204 |
} |
8045 |
03 Jun 22 |
nicklas |
205 |
|
8045 |
03 Jun 22 |
nicklas |
206 |
/** |
4168 |
04 Mar 08 |
nicklas |
Get an attribute of the context. |
4168 |
04 Mar 08 |
nicklas |
@param name The name of the attribute |
4168 |
04 Mar 08 |
nicklas |
@return The value, or null |
4168 |
04 Mar 08 |
nicklas |
210 |
*/ |
7895 |
18 Jan 21 |
nicklas |
211 |
@SuppressWarnings("unchecked") |
7895 |
18 Jan 21 |
nicklas |
212 |
public <T> T getAttribute(String name) |
4168 |
04 Mar 08 |
nicklas |
213 |
{ |
7895 |
18 Jan 21 |
nicklas |
214 |
return attributes == null ? null : (T)attributes.get(name); |
4168 |
04 Mar 08 |
nicklas |
215 |
} |
4158 |
22 Feb 08 |
nicklas |
216 |
|
4168 |
04 Mar 08 |
nicklas |
217 |
/** |
4168 |
04 Mar 08 |
nicklas |
Set an attribute of the context. |
4168 |
04 Mar 08 |
nicklas |
@param name The name of the attribute |
4168 |
04 Mar 08 |
nicklas |
@param value The value, if null the attribute is removed |
4168 |
04 Mar 08 |
nicklas |
221 |
*/ |
4168 |
04 Mar 08 |
nicklas |
222 |
public void setAttribute(String name, Object value) |
4168 |
04 Mar 08 |
nicklas |
223 |
{ |
4168 |
04 Mar 08 |
nicklas |
224 |
if (attributes == null) attributes = new HashMap<String, Object>(); |
4168 |
04 Mar 08 |
nicklas |
225 |
if (value != null) |
4168 |
04 Mar 08 |
nicklas |
226 |
{ |
4168 |
04 Mar 08 |
nicklas |
227 |
attributes.put(name, value); |
4168 |
04 Mar 08 |
nicklas |
228 |
} |
4168 |
04 Mar 08 |
nicklas |
229 |
else |
4168 |
04 Mar 08 |
nicklas |
230 |
{ |
4168 |
04 Mar 08 |
nicklas |
231 |
attributes.remove(name); |
4168 |
04 Mar 08 |
nicklas |
232 |
} |
4168 |
04 Mar 08 |
nicklas |
233 |
} |
4168 |
04 Mar 08 |
nicklas |
234 |
|
8083 |
20 Oct 22 |
nicklas |
235 |
/** |
8083 |
20 Oct 22 |
nicklas |
Link this context to the other context with respect to attributes. |
8083 |
20 Oct 22 |
nicklas |
Existing attributes in this context will be copied to the other context. |
8083 |
20 Oct 22 |
nicklas |
After this method call they will share the same internal storage so |
8083 |
20 Oct 22 |
nicklas |
that updates to one context is reflected in the other. |
8083 |
20 Oct 22 |
nicklas |
@since 3.19.4 |
8083 |
20 Oct 22 |
nicklas |
241 |
*/ |
8083 |
20 Oct 22 |
nicklas |
242 |
public void linkAttributes(ClientContext other) |
8083 |
20 Oct 22 |
nicklas |
243 |
{ |
8083 |
20 Oct 22 |
nicklas |
244 |
if (other.attributes == null) other.attributes = new HashMap<>(); |
8083 |
20 Oct 22 |
nicklas |
245 |
if (attributes != null) other.attributes.putAll(attributes); |
8083 |
20 Oct 22 |
nicklas |
246 |
attributes = other.attributes; |
8083 |
20 Oct 22 |
nicklas |
247 |
} |
8083 |
20 Oct 22 |
nicklas |
248 |
|
8083 |
20 Oct 22 |
nicklas |
249 |
/** |
8083 |
20 Oct 22 |
nicklas |
Store all attributes that has been set on this context to |
8083 |
20 Oct 22 |
nicklas |
the given ItemContext instance. Since the ItemContext is |
8083 |
20 Oct 22 |
nicklas |
persistant for session it is possible to load the same |
8083 |
20 Oct 22 |
nicklas |
attributes into another context later with {@link |
8083 |
20 Oct 22 |
nicklas |
#loadAttributes(ItemContext, String)} |
8083 |
20 Oct 22 |
nicklas |
@since 3.19.4 |
8083 |
20 Oct 22 |
nicklas |
256 |
*/ |
8083 |
20 Oct 22 |
nicklas |
257 |
public void storeAttributes(ItemContext cc, String key) |
8083 |
20 Oct 22 |
nicklas |
258 |
{ |
8083 |
20 Oct 22 |
nicklas |
259 |
if (attributes != null && attributes.size() > 0) |
8083 |
20 Oct 22 |
nicklas |
260 |
{ |
8083 |
20 Oct 22 |
nicklas |
261 |
cc.setObject(key, attributes); |
8083 |
20 Oct 22 |
nicklas |
262 |
} |
8083 |
20 Oct 22 |
nicklas |
263 |
} |
8083 |
20 Oct 22 |
nicklas |
264 |
|
8083 |
20 Oct 22 |
nicklas |
265 |
/** |
8083 |
20 Oct 22 |
nicklas |
Load attributes to this context from the given ItemContext |
8083 |
20 Oct 22 |
nicklas |
instance. |
8083 |
20 Oct 22 |
nicklas |
@since 3.19.4 |
8083 |
20 Oct 22 |
nicklas |
269 |
*/ |
8083 |
20 Oct 22 |
nicklas |
270 |
public void loadAttributes(ItemContext cc, String key) |
8083 |
20 Oct 22 |
nicklas |
271 |
{ |
8083 |
20 Oct 22 |
nicklas |
272 |
Map<String, Object> tmp = cc.getObject(key); |
8083 |
20 Oct 22 |
nicklas |
273 |
if (tmp != null && tmp.size() > 0) |
8083 |
20 Oct 22 |
nicklas |
274 |
{ |
8083 |
20 Oct 22 |
nicklas |
275 |
if (attributes != null) |
8083 |
20 Oct 22 |
nicklas |
276 |
{ |
8083 |
20 Oct 22 |
nicklas |
277 |
attributes.putAll(tmp); |
8083 |
20 Oct 22 |
nicklas |
278 |
} |
8083 |
20 Oct 22 |
nicklas |
279 |
else |
8083 |
20 Oct 22 |
nicklas |
280 |
{ |
8083 |
20 Oct 22 |
nicklas |
281 |
attributes = tmp; |
8083 |
20 Oct 22 |
nicklas |
282 |
} |
8083 |
20 Oct 22 |
nicklas |
283 |
} |
8083 |
20 Oct 22 |
nicklas |
284 |
} |
8083 |
20 Oct 22 |
nicklas |
285 |
|
4158 |
22 Feb 08 |
nicklas |
286 |
} |