KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > user > RoleImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.commonimpl.user;
17
18 import java.util.Date JavaDoc;
19 import java.util.GregorianCalendar JavaDoc;
20
21 import org.outerj.daisy.repository.user.Role;
22 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
23 import org.outerj.daisy.repository.RepositoryException;
24 import org.outerx.daisy.x10.RoleDocument;
25
26 /**
27  * An administrative role object.
28  *
29  * <p>It is possible to change the role name before the object is persisted.
30  * After the save() method is called (i.e. persistence has happened), the
31  * rolename can no longer be changed!
32  */

33 public class RoleImpl implements Role {
34     private long id=-1;
35     private String JavaDoc name;
36     private String JavaDoc description;
37     private long lastModifier;
38     private Date JavaDoc lastModified;
39     private long updateCount = 0;
40     private AuthenticatedUser requestingUser;
41     private UserManagementStrategy userManagementStrategy;
42     private IntimateAccess intimateAccess = new IntimateAccess();
43     private boolean readOnly = false;
44     private static final String JavaDoc READ_ONLY_MESSAGE = "This Role object is read-only.";
45
46     /**
47      * @param userManagementStrategy
48      * @param roleName
49      */

50     public RoleImpl(UserManagementStrategy userManagementStrategy, String JavaDoc roleName, AuthenticatedUser requestingUser) {
51         this.userManagementStrategy = userManagementStrategy;
52         name = roleName;
53         this.requestingUser = requestingUser;
54     }
55
56     public String JavaDoc getName() {
57         return name;
58     }
59
60     public void setName(String JavaDoc roleName) {
61         if (readOnly)
62             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
63
64         name = roleName;
65     }
66
67     public String JavaDoc getDescription() {
68         return description;
69     }
70     
71     public void setDescription(String JavaDoc description) {
72         if (readOnly)
73             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
74
75         this.description = description;
76     }
77
78     public long getId() {
79         return id;
80     }
81
82     /**
83      * persists the state of this object to the data store
84      */

85
86     public void save() throws RepositoryException {
87         if (readOnly)
88             throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
89
90         userManagementStrategy.store(this);
91     }
92
93     /**
94      * return the xml representation of this Role
95      */

96
97     public RoleDocument getXml() {
98         RoleDocument roleDocument = RoleDocument.Factory.newInstance();
99         RoleDocument.Role roleXml = roleDocument.addNewRole();
100  
101         roleXml.setDescription(description);
102         roleXml.setName(name);
103         roleXml.setUpdateCount(updateCount);
104
105         if (id!=-1) {
106             roleXml.setId(id);
107             GregorianCalendar JavaDoc lastModifiedCalendar = new GregorianCalendar JavaDoc();
108             lastModifiedCalendar.setTime(lastModified);
109             
110             roleXml.setLastModified(lastModifiedCalendar);
111             roleXml.setLastModifier(lastModifier);
112         }
113         return roleDocument;
114     }
115
116     /**
117      * request intimate access to this object, only the strategy that created this object
118      * is allowed to actually <b>get</b> this intimate access.
119      * @param strategy
120      * @return
121      */

122     public RoleImpl.IntimateAccess getIntimateAccess(UserManagementStrategy strategy) {
123         if (this.userManagementStrategy == strategy)
124             return intimateAccess;
125         else
126             return null;
127     }
128
129     public Date JavaDoc getLastModified() {
130         return lastModified;
131     }
132
133     public long getLastModifier() {
134         return lastModifier;
135     }
136
137     public long getUpdateCount() {
138         return updateCount;
139     }
140
141     /**
142      * Disables all operations that can modify the state of this object.
143      */

144     public void makeReadOnly() {
145         this.readOnly = true;
146     }
147
148     public class IntimateAccess {
149         private IntimateAccess() {
150         }
151
152         public void setLastModified(Date JavaDoc lastModDate) {
153             if (readOnly)
154                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
155
156             lastModified = lastModDate;
157         }
158         public void setLastModifier(long lastMod) {
159             if (readOnly)
160                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
161
162             lastModifier = lastMod;
163         }
164         public AuthenticatedUser getCurrentUser() {
165             if (readOnly)
166                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
167
168             return requestingUser;
169         }
170
171         public void setId(long id) {
172             if (readOnly)
173                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
174
175             RoleImpl.this.id = id;
176         }
177
178         public void saved(long id, String JavaDoc roleName, String JavaDoc roleDescription, long updateCount) {
179             if (readOnly)
180                 throw new RuntimeException JavaDoc(READ_ONLY_MESSAGE);
181
182             RoleImpl.this.id = id;
183             name = roleName;
184             description = roleDescription;
185             RoleImpl.this.updateCount = updateCount;
186         }
187         
188     }
189 }
190
Popular Tags