001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.commonimpl.user;
017:
018: import java.util.Date;
019: import java.util.GregorianCalendar;
020:
021: import org.outerj.daisy.repository.user.Role;
022: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
023: import org.outerj.daisy.repository.RepositoryException;
024: import org.outerx.daisy.x10.RoleDocument;
025:
026: /**
027: * An administrative role object.
028: *
029: * <p>It is possible to change the role name before the object is persisted.
030: * After the save() method is called (i.e. persistence has happened), the
031: * rolename can no longer be changed!
032: */
033: public class RoleImpl implements Role {
034: private long id = -1;
035: private String name;
036: private String description;
037: private long lastModifier;
038: private Date lastModified;
039: private long updateCount = 0;
040: private AuthenticatedUser requestingUser;
041: private UserManagementStrategy userManagementStrategy;
042: private IntimateAccess intimateAccess = new IntimateAccess();
043: private boolean readOnly = false;
044: private static final String READ_ONLY_MESSAGE = "This Role object is read-only.";
045:
046: /**
047: * @param userManagementStrategy
048: * @param roleName
049: */
050: public RoleImpl(UserManagementStrategy userManagementStrategy,
051: String roleName, AuthenticatedUser requestingUser) {
052: this .userManagementStrategy = userManagementStrategy;
053: name = roleName;
054: this .requestingUser = requestingUser;
055: }
056:
057: public String getName() {
058: return name;
059: }
060:
061: public void setName(String roleName) {
062: if (readOnly)
063: throw new RuntimeException(READ_ONLY_MESSAGE);
064:
065: name = roleName;
066: }
067:
068: public String getDescription() {
069: return description;
070: }
071:
072: public void setDescription(String description) {
073: if (readOnly)
074: throw new RuntimeException(READ_ONLY_MESSAGE);
075:
076: this .description = description;
077: }
078:
079: public long getId() {
080: return id;
081: }
082:
083: /**
084: * persists the state of this object to the data store
085: */
086:
087: public void save() throws RepositoryException {
088: if (readOnly)
089: throw new RuntimeException(READ_ONLY_MESSAGE);
090:
091: userManagementStrategy.store(this );
092: }
093:
094: /**
095: * return the xml representation of this Role
096: */
097:
098: public RoleDocument getXml() {
099: RoleDocument roleDocument = RoleDocument.Factory.newInstance();
100: RoleDocument.Role roleXml = roleDocument.addNewRole();
101:
102: roleXml.setDescription(description);
103: roleXml.setName(name);
104: roleXml.setUpdateCount(updateCount);
105:
106: if (id != -1) {
107: roleXml.setId(id);
108: GregorianCalendar lastModifiedCalendar = new GregorianCalendar();
109: lastModifiedCalendar.setTime(lastModified);
110:
111: roleXml.setLastModified(lastModifiedCalendar);
112: roleXml.setLastModifier(lastModifier);
113: }
114: return roleDocument;
115: }
116:
117: /**
118: * request intimate access to this object, only the strategy that created this object
119: * is allowed to actually <b>get</b> this intimate access.
120: * @param strategy
121: * @return
122: */
123: public RoleImpl.IntimateAccess getIntimateAccess(
124: UserManagementStrategy strategy) {
125: if (this .userManagementStrategy == strategy)
126: return intimateAccess;
127: else
128: return null;
129: }
130:
131: public Date getLastModified() {
132: return lastModified;
133: }
134:
135: public long getLastModifier() {
136: return lastModifier;
137: }
138:
139: public long getUpdateCount() {
140: return updateCount;
141: }
142:
143: /**
144: * Disables all operations that can modify the state of this object.
145: */
146: public void makeReadOnly() {
147: this .readOnly = true;
148: }
149:
150: public class IntimateAccess {
151: private IntimateAccess() {
152: }
153:
154: public void setLastModified(Date lastModDate) {
155: if (readOnly)
156: throw new RuntimeException(READ_ONLY_MESSAGE);
157:
158: lastModified = lastModDate;
159: }
160:
161: public void setLastModifier(long lastMod) {
162: if (readOnly)
163: throw new RuntimeException(READ_ONLY_MESSAGE);
164:
165: lastModifier = lastMod;
166: }
167:
168: public AuthenticatedUser getCurrentUser() {
169: if (readOnly)
170: throw new RuntimeException(READ_ONLY_MESSAGE);
171:
172: return requestingUser;
173: }
174:
175: public void setId(long id) {
176: if (readOnly)
177: throw new RuntimeException(READ_ONLY_MESSAGE);
178:
179: RoleImpl.this .id = id;
180: }
181:
182: public void saved(long id, String roleName,
183: String roleDescription, long updateCount) {
184: if (readOnly)
185: throw new RuntimeException(READ_ONLY_MESSAGE);
186:
187: RoleImpl.this.id = id;
188: name = roleName;
189: description = roleDescription;
190: RoleImpl.this.updateCount = updateCount;
191: }
192:
193: }
194: }
|