src/core/net/sf/basedb/util/extensions/ClientContext.java

Code
Comments
Other
Rev Date Author Line
4158 22 Feb 08 nicklas 1 /**
4198 28 Mar 08 nicklas 2   $Id:Context.java 4187 2008-03-20 11:15:25Z nicklas $
4158 22 Feb 08 nicklas 3
4158 22 Feb 08 nicklas 4   Copyright (C) Authors contributing to this file.
4158 22 Feb 08 nicklas 5
4158 22 Feb 08 nicklas 6   This file is part of BASE - BioArray Software Environment.
4158 22 Feb 08 nicklas 7   Available at http://base.thep.lu.se/
4158 22 Feb 08 nicklas 8
4158 22 Feb 08 nicklas 9   BASE is free software; you can redistribute it and/or
4158 22 Feb 08 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
4158 22 Feb 08 nicklas 12   of the License, or (at your option) any later version.
4158 22 Feb 08 nicklas 13
4158 22 Feb 08 nicklas 14   BASE is distributed in the hope that it will be useful,
4158 22 Feb 08 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4158 22 Feb 08 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4158 22 Feb 08 nicklas 17   GNU General Public License for more details.
4158 22 Feb 08 nicklas 18
4158 22 Feb 08 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   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 32   Keeps information about the current context in the client 
4207 04 Apr 08 nicklas 33   application that extensions can use to decide what actions to 
4207 04 Apr 08 nicklas 34   generate. It is, for example, possible to check the permissions
4168 04 Mar 08 nicklas 35   of logged in user and enable/disable certain actions
4168 04 Mar 08 nicklas 36   if a condition is not met. Or, the extension may inspect
4168 04 Mar 08 nicklas 37   the current item and decide if the extension can be used
4168 04 Mar 08 nicklas 38   on that item or not.
4168 04 Mar 08 nicklas 39   
4168 04 Mar 08 nicklas 40   <p>
4168 04 Mar 08 nicklas 41   The current context is passed to an {@link ActionFactory} twice, first
4168 04 Mar 08 nicklas 42   to check if an extension should be enabled or not
4207 04 Apr 08 nicklas 43   with the {@link ActionFactory#prepareContext(InvokationContext)}
4168 04 Mar 08 nicklas 44   method. This method also allows an extension to write
4168 04 Mar 08 nicklas 45   back information to the context. This may, for example,
4168 04 Mar 08 nicklas 46   include information about which Javascript scripts to import
4168 04 Mar 08 nicklas 47   or which stylesheets to load. The <code>prepareContext</code>
4168 04 Mar 08 nicklas 48   method is only called once for each use of an extension.
4158 22 Feb 08 nicklas 49
4168 04 Mar 08 nicklas 50   <p>
4168 04 Mar 08 nicklas 51   The second time is when calling the {@link 
4207 04 Apr 08 nicklas 52   ActionFactory#getActions(InvokationContext)} method. This method can
4168 04 Mar 08 nicklas 53   be called multiple times. For example, if an extension point
4168 04 Mar 08 nicklas 54   is in a list context, the <code>getActions</code> method may
4168 04 Mar 08 nicklas 55   be called one time for each item in the list. If so, the
4168 04 Mar 08 nicklas 56   {@link #getCurrentItem()} returns the currently active item.
4168 04 Mar 08 nicklas 57   
4168 04 Mar 08 nicklas 58   <p>
4168 04 Mar 08 nicklas 59   Client applications may subclass this object to provide more
4168 04 Mar 08 nicklas 60   information about the client environment. See, for example,
4168 04 Mar 08 nicklas 61   the {@link net.sf.basedb.clients.web.extensions.JspContext} object.
4168 04 Mar 08 nicklas 62
4158 22 Feb 08 nicklas 63   @author nicklas
4158 22 Feb 08 nicklas 64   @version 2.7
4198 28 Mar 08 nicklas 65   @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 78      Create a new context with no session or current item.
5599 31 Mar 11 nicklas 79     @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 87     Create a new context with a session and no DbControl. The session 
4237 18 Apr 08 nicklas 88     is final and can't be replaced.
4168 04 Mar 08 nicklas 89     @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 97     Create a new context with a session and item but no DbControl. The session is 
4237 18 Apr 08 nicklas 98     final and can't be replaced. Use {@link #setCurrentItem(Object)} to change 
4237 18 Apr 08 nicklas 99     the item.
4170 07 Mar 08 nicklas 100     
4168 04 Mar 08 nicklas 101     @param sc The current session
4168 04 Mar 08 nicklas 102     @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 110     Create a new context with a DbControl. The DbControl 
4237 18 Apr 08 nicklas 111     is final and can't be replaced.
4237 18 Apr 08 nicklas 112     @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 120     Create a new context with a DbControl and item. The DbControl is 
4237 18 Apr 08 nicklas 121     final and can't be replaced. Use {@link #setCurrentItem(Object)} to change 
4237 18 Apr 08 nicklas 122     the item.
4237 18 Apr 08 nicklas 123     
4237 18 Apr 08 nicklas 124     @param dc An open DbControl
4237 18 Apr 08 nicklas 125     @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 140     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 148     Get the current DbControl. This can be null if the
4237 18 Apr 08 nicklas 149     extension point doesn't provide an open DbControl.
4237 18 Apr 08 nicklas 150     An extension MUST NEVER call {@link DbControl#close()}
4237 18 Apr 08 nicklas 151     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 159     Get the currently active item. It can be any type of
4168 04 Mar 08 nicklas 160     object, but the extension point may declare (in it's 
4168 04 Mar 08 nicklas 161     documentation) that the current item is always of a 
4198 28 Mar 08 nicklas 162     certain class.
4168 04 Mar 08 nicklas 163     @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 172     Set the currently active item. 
4168 04 Mar 08 nicklas 173     @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 181     Get the currently active extension point.
8045 03 Jun 22 nicklas 182     @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 194     Get the currently active extension point.
8045 03 Jun 22 nicklas 195     @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 207     Get an attribute of the context.
4168 04 Mar 08 nicklas 208     @param name The name of the attribute
4168 04 Mar 08 nicklas 209     @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 218     Set an attribute of the context.
4168 04 Mar 08 nicklas 219     @param name The name of the attribute
4168 04 Mar 08 nicklas 220     @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 236     Link this context to the other context with respect to attributes.
8083 20 Oct 22 nicklas 237     Existing attributes in this context will be copied to the other context.
8083 20 Oct 22 nicklas 238     After this method call they will share the same internal storage so
8083 20 Oct 22 nicklas 239     that updates to one context is reflected in the other.
8083 20 Oct 22 nicklas 240     @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 250     Store all attributes that has been set on this context to
8083 20 Oct 22 nicklas 251     the given ItemContext instance. Since the ItemContext is
8083 20 Oct 22 nicklas 252     persistant for session it is possible to load the same 
8083 20 Oct 22 nicklas 253     attributes into another context later with {@link 
8083 20 Oct 22 nicklas 254     #loadAttributes(ItemContext, String)}
8083 20 Oct 22 nicklas 255     @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 266     Load attributes to this context from the given ItemContext
8083 20 Oct 22 nicklas 267     instance.
8083 20 Oct 22 nicklas 268     @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 }