KickJava   Java API By Example, From Geeks To Geeks.

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


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

28 package com.genimen.djeneric.repository;
29
30 import java.util.HashMap JavaDoc;
31
32 import com.genimen.djeneric.language.Messages;
33 import com.genimen.djeneric.repository.exceptions.DjenericException;
34
35 public abstract class DjUser
36 {
37   final static long NOT_YET_PERSISTED = -1;
38
39   /////////////////////////////////////////////////////////////////////
40
public abstract void delete(DjSession session) throws DjenericException;
41
42   public abstract void reload(DjSession session) throws DjenericException;
43
44   public abstract void persist(DjSession session) throws DjenericException;
45
46   public abstract void insert(DjSession session) throws DjenericException;
47
48   public abstract void removeAllContextAssociations(DjSession session) throws DjenericException;
49
50   public abstract void addView(DjSession session, DjModelView view) throws DjenericException;
51
52   public abstract void removeView(DjSession session, DjModelView view) throws DjenericException;
53
54   public abstract void removeAllViews(DjSession session) throws DjenericException;
55
56   public abstract DjModelView[] getViews() throws DjenericException;
57
58   public abstract DjUserContextAssociation[] getContextAssociations() throws DjenericException;
59
60   public abstract DjUserContextAssociation getContextAssociation(DjContext ctxt) throws DjenericException;
61
62   /////////////////////////////////////////////////////////////////////
63

64   long _id;
65   String JavaDoc _code = "";
66   String JavaDoc _name = "";
67   String JavaDoc _password = "";
68   boolean _administrator = false;
69   boolean _modeler = false;
70   private DjPersistenceManager _mgr;
71   HashMap JavaDoc _cachedCAs = new HashMap JavaDoc();
72
73   protected DjUser(DjPersistenceManager mgr)
74
75   {
76     _mgr = mgr;
77     _id = NOT_YET_PERSISTED;
78   }
79
80   protected DjUser(DjPersistenceManager mgr, long id, String JavaDoc code, String JavaDoc name, String JavaDoc password, boolean administrator,
81                    boolean modeler)
82   {
83     _mgr = mgr;
84     _id = id;
85     _code = code;
86     _name = name;
87     _password = password;
88     _administrator = administrator;
89     _modeler = modeler;
90   }
91
92   public boolean canModify(DjContext someContext) throws DjenericException
93   {
94     if(someContext == null && !_administrator) throw new DjenericException(Messages.getString("DjPersistenceManager.OnlyAdminCanSeeGlobal"));
95     
96     DjUserContextAssociation c = getCachedContextAssociation(someContext);
97     if (c != null) return c.isModify() || _administrator;
98
99     return _administrator;
100   }
101
102   public boolean canCreate(DjContext someContext) throws DjenericException
103   {
104     if(someContext == null && !_administrator) throw new DjenericException(Messages.getString("DjPersistenceManager.OnlyAdminCanSeeGlobal"));
105
106     DjUserContextAssociation c = getCachedContextAssociation(someContext);
107     if (c != null) return c.isCreate() || _administrator;
108
109     return _administrator;
110   }
111
112   public boolean canDelete(DjContext someContext) throws DjenericException
113   {
114     if(someContext == null && !_administrator) throw new DjenericException(Messages.getString("DjPersistenceManager.OnlyAdminCanSeeGlobal"));
115
116     DjUserContextAssociation c = getCachedContextAssociation(someContext);
117     if (c != null) return c.isDelete() || _administrator;
118
119     return _administrator;
120   }
121
122   public boolean canQuery(DjContext someContext) throws DjenericException
123   {
124     if(someContext == null && !_administrator) throw new DjenericException(Messages.getString("DjPersistenceManager.OnlyAdminCanSeeGlobal"));
125
126     DjUserContextAssociation c = getCachedContextAssociation(someContext);
127     if (c != null) return c.isQuery() || _administrator;
128
129     return _administrator;
130   }
131
132   protected DjUserContextAssociation getCachedContextAssociation(DjContext someContext) throws DjenericException
133   {
134     if(someContext == null) return null;
135     
136     DjUserContextAssociation c = (DjUserContextAssociation) _cachedCAs.get(someContext);
137     if (c == null)
138     {
139       c = getContextAssociation(someContext);
140       _cachedCAs.put(someContext, c);
141     }
142     return c;
143   }
144
145   public DjPersistenceManager getPersistenceManager()
146   {
147     return _mgr;
148   }
149
150   public long getId()
151   {
152     return _id;
153   }
154
155   protected void setId(long id)
156   {
157     _id = id;
158   }
159
160   public String JavaDoc getName()
161   {
162     return _name;
163   }
164
165   public void setName(String JavaDoc name)
166   {
167     _name = name;
168   }
169
170   public String JavaDoc getPassword()
171   {
172     if (_password == null) return "";
173     return _password;
174   }
175
176   public void setPassword(String JavaDoc pw)
177   {
178     _password = pw;
179   }
180
181   public String JavaDoc getCode()
182   {
183     return _code;
184   }
185
186   public void setCode(String JavaDoc code)
187   {
188     _code = code.toLowerCase();
189   }
190
191   public boolean isAdministrator()
192   {
193     return _administrator;
194   }
195
196   public void setAdministrator(boolean administrator)
197   {
198     _administrator = administrator;
199   }
200
201   public void setModeler(boolean modeler)
202   {
203     _modeler = modeler;
204   }
205
206   public boolean isModeler()
207   {
208     return _modeler;
209   }
210
211   public boolean equals(Object JavaDoc obj)
212   {
213     if (!(obj instanceof DjUser)) return false;
214     DjUser r = (DjUser) obj;
215
216     return getId() == r.getId();
217   }
218
219   public int hashCode()
220   {
221     return (int) getId();
222   }
223
224   public String JavaDoc toString()
225   {
226     return getName();
227   }
228
229 }
Popular Tags