KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > DjContextManager


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.repository;
31
32 import com.genimen.djeneric.language.Messages;
33 import com.genimen.djeneric.repository.exceptions.DjenericException;
34 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
35
36 /**
37  * A context manager is responsible for creating new users ({@link DjUser}), contexts ({@link DjContext})
38  * and assocations ({@link DjAssociation})
39  * between the two.
40  *
41  *@author Wido Riezebos
42  *@created 24 mei 2002
43  */

44 public abstract class DjContextManager
45 {
46   /**
47    * Creates a new Context for use with the persistce manager. You have to persist
48    * the context using the persist method before you can use it.
49    *
50    *@return The new context, not persisted yet
51    *@exception DjenericException Description of the Exception
52    */

53   public abstract DjContext createNewContext() throws DjenericException;
54
55   /**
56    * Returns an array of all contexts defined in the persistent store
57    *
58    *@return All contexts defined in the persistent store
59    *@exception DjenericException Description of the Exception
60    */

61   public abstract DjContext[] getContexts() throws DjenericException;
62
63   /**
64    * Creates a new association between a user and a context; effectively granting
65    * visibility of the context to the user.
66    *
67    *@return The new association
68    *@exception DjenericException Description of the Exception
69    */

70   public abstract DjUserContextAssociation createNewUserContextAssociation() throws DjenericException;
71
72   /**
73    * Creates a new user for use with the persistce manager. You have to persist
74    * the user using the persist method before you can use it.
75    *
76    *@return The new user, not persisted yet
77    *@exception DjenericException Description of the Exception
78    */

79   public abstract DjUser createNewUser() throws DjenericException;
80
81   /**
82    * Returns the user identified by the (unqiue) user code
83    *
84    *@param userCode The code identifying a user
85    *@return The user
86    *@exception DjenericException A LogonException is thrown if the user if not found
87    */

88   public abstract DjUser getUser(String JavaDoc userCode) throws DjenericException;
89
90   /**
91    * Returns an array of all users defined in the persistent store
92    *
93    *@return All users defined
94    *@exception DjenericException Description of the Exception
95    */

96   public abstract DjUser[] getUsers() throws DjenericException;
97
98   /////////////////////////////////////////////////////////////////////
99

100   DjPersistenceManager _mgr;
101   DjContext _currentContext = null;
102
103   /**
104    * Constructor for the DjContextManager object
105    *
106    *@param mgr The persistence manager this context manager is for
107    */

108   public DjContextManager(DjPersistenceManager mgr)
109
110   {
111     _mgr = mgr;
112   }
113
114   /**
115    * Returns the persistenceManager this context manager is for
116    *
117    *@return The persistenceManager
118    */

119   public DjPersistenceManager getPersistenceManager()
120   {
121     return _mgr;
122   }
123
124   /**
125    * Sets the current context; you can set the current context to null if
126    * you do not wish to restrict your view to a specific context.
127    *
128    *@param contextCode The unique code of a context
129    *@exception ObjectNotDefinedException Thrown if the context identified by the code is not found
130    *@exception DjenericException Description of the Exception
131    */

132   public void setCurrentContext(String JavaDoc contextCode) throws ObjectNotDefinedException, DjenericException
133   {
134     if (contextCode == null)
135     {
136       setCurrentContext((DjContext) null);
137       return;
138     }
139
140     DjContext[] ctxts = getContexts();
141     for (int i = 0; i < ctxts.length; i++)
142     {
143       if (ctxts[i].getCode().equals(contextCode))
144       {
145         setCurrentContext(ctxts[i]);
146         return;
147       }
148     }
149     throw new ObjectNotDefinedException(Messages.getString("DjContextManager.UnknownCode", contextCode));
150   }
151
152   /**
153    * Sets the current context; you can set the current context to null if
154    * you do not wish to restrict your view to a specific context.
155    *
156    *@param ctxt The context to be used as the current one
157    */

158   public void setCurrentContext(DjContext ctxt)
159   {
160     _currentContext = ctxt;
161   }
162
163   /**
164    * Returns the context that is currently in use, or null if none set
165    *
166    *@return The current context
167    */

168   public DjContext getCurrentContext()
169   {
170     return _currentContext;
171   }
172 }
Popular Tags