001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.entities.domain.internal;
014:
015: import java.util.ArrayList;
016: import java.util.Date;
017: import java.util.Iterator;
018: import java.util.Locale;
019:
020: import org.rapla.components.util.iterator.IteratorChain;
021: import org.rapla.components.util.iterator.NestedIterator;
022: import org.rapla.entities.EntityNotFoundException;
023: import org.rapla.entities.RaplaType;
024: import org.rapla.entities.User;
025: import org.rapla.entities.domain.Allocatable;
026: import org.rapla.entities.domain.Permission;
027: import org.rapla.entities.dynamictype.Classification;
028: import org.rapla.entities.dynamictype.DynamicType;
029: import org.rapla.entities.dynamictype.internal.ClassificationImpl;
030: import org.rapla.entities.storage.DynamicTypeDependant;
031: import org.rapla.entities.storage.RefEntity;
032: import org.rapla.entities.storage.EntityResolver;
033: import org.rapla.entities.storage.Mementable;
034: import org.rapla.entities.storage.internal.SimpleEntity;
035:
036: public class AllocatableImpl extends SimpleEntity implements
037: Allocatable, Mementable, java.io.Serializable,
038: DynamicTypeDependant {
039: // Don't forget to increase the serialVersionUID when you change the fields
040: private static final long serialVersionUID = 1;
041:
042: private ClassificationImpl classification;
043: private boolean holdBackConflicts;
044: private boolean _isPerson;
045: private ArrayList permissions = new ArrayList();
046:
047: transient private boolean permissionArrayUpToDate = false;
048: transient private Permission[] permissionArray;
049:
050: public AllocatableImpl() {
051: _isPerson = false;
052: }
053:
054: public void resolveEntities(EntityResolver resolver)
055: throws EntityNotFoundException {
056: super .resolveEntities(resolver);
057: classification.resolveEntities(resolver);
058: for (Iterator it = permissions.iterator(); it.hasNext();) {
059: ((PermissionImpl) it.next()).resolveEntities(resolver);
060: }
061: }
062:
063: public void setReadOnly(boolean enable) {
064: super .setReadOnly(enable);
065: classification.setReadOnly(enable);
066: Iterator it = permissions.iterator();
067: while (it.hasNext()) {
068: ((PermissionImpl) it.next()).setReadOnly(enable);
069: }
070: }
071:
072: public RaplaType getRaplaType() {
073: return TYPE;
074: }
075:
076: // Implementation of interface classifiable
077: public Classification getClassification() {
078: return classification;
079: }
080:
081: public void setClassification(Classification classification) {
082: this .classification = (ClassificationImpl) classification;
083: }
084:
085: public void setHoldBackConflicts(boolean enable) {
086: holdBackConflicts = enable;
087: }
088:
089: public boolean isHoldBackConflicts() {
090: return holdBackConflicts;
091: }
092:
093: public String getName(Locale locale) {
094: Classification c = getClassification();
095: if (c == null)
096: return "";
097: return c.getName(locale);
098: }
099:
100: public void setPerson(boolean isPerson) {
101: checkWritable();
102: _isPerson = isPerson;
103: }
104:
105: public boolean isPerson() {
106: return _isPerson;
107: }
108:
109: private boolean hasAccess(User user, int accessLevel) {
110: Permission[] permissions = getPermissions();
111: if (user == null || user.isAdmin())
112: return true;
113:
114: for (int i = 0; i < permissions.length; i++) {
115: Permission p = permissions[i];
116: if (p.affectsUser(user)
117: && (p.getAccessLevel() >= accessLevel)) {
118: return true;
119: }
120: }
121: return false;
122: }
123:
124: public boolean canCreateConflicts(User user) {
125: return hasAccess(user, Permission.ALLOCATE_CONFLICTS);
126: }
127:
128: public boolean canModify(User user) {
129: return hasAccess(user, Permission.ADMIN);
130: }
131:
132: public boolean canRead(User user) {
133: return hasAccess(user, Permission.READ);
134: }
135:
136: public boolean canAllocate(User user, Date start, Date end,
137: Date today) {
138: if (user == null)
139: return false;
140:
141: Permission[] permissions = getPermissions();
142: if (user.isAdmin())
143: return true;
144:
145: for (int i = 0; i < permissions.length; i++) {
146: Permission p = permissions[i];
147: if (p.affectsUser(user)
148: && p.getAccessLevel() >= Permission.ALLOCATE
149: && (p.getAccessLevel() == Permission.ADMIN || p
150: .covers(start, end, today))) {
151: return true;
152: }
153: }
154: return false;
155: }
156:
157: public void addPermission(Permission permission) {
158: checkWritable();
159: permissionArrayUpToDate = false;
160: permissions.add(permission);
161: }
162:
163: public boolean removePermission(Permission permission) {
164: checkWritable();
165: permissionArrayUpToDate = false;
166: return permissions.remove(permission);
167: }
168:
169: public Permission newPermission() {
170: return new PermissionImpl();
171: }
172:
173: public Permission[] getPermissions() {
174: updatePermissionArray();
175: return permissionArray;
176: }
177:
178: private void updatePermissionArray() {
179: if (permissionArrayUpToDate)
180: return;
181:
182: permissionArray = (Permission[]) permissions
183: .toArray(Permission.PERMISSION_ARRAY);
184: permissionArrayUpToDate = true;
185: }
186:
187: public Iterator getReferences() {
188: return new IteratorChain(classification.getReferences(),
189: new NestedIterator(permissions.iterator()) {
190: public Iterator getNestedIterator(Object obj) {
191: return ((PermissionImpl) obj).getReferences();
192: }
193: });
194: }
195:
196: public boolean needsChange(DynamicType type) {
197: return classification.needsChange(type);
198: }
199:
200: public void commitChange(DynamicType type) {
201: classification.commitChange(type);
202: }
203:
204: public boolean isRefering(RefEntity object) {
205: if (super .isRefering(object))
206: return true;
207: if (classification.isRefering(object))
208: return true;
209: Permission[] permissions = getPermissions();
210: for (int i = 0; i < permissions.length; i++) {
211: if (((PermissionImpl) permissions[i]).isRefering(object))
212: return true;
213: }
214: return false;
215: }
216:
217: static private void copy(AllocatableImpl source,
218: AllocatableImpl dest) {
219: dest.permissionArrayUpToDate = false;
220: dest.classification = (ClassificationImpl) source.classification
221: .clone();
222:
223: dest.permissions.clear();
224: Iterator it = source.permissions.iterator();
225: while (it.hasNext()) {
226: dest.permissions.add(((PermissionImpl) it.next()).clone());
227: }
228:
229: dest.holdBackConflicts = source.holdBackConflicts;
230: dest._isPerson = source._isPerson;
231: }
232:
233: public void copy(Object obj) {
234: super .copy((AllocatableImpl) obj);
235: copy((AllocatableImpl) obj, this );
236: }
237:
238: public Object deepClone() {
239: AllocatableImpl clone = new AllocatableImpl();
240: super .deepClone(clone);
241: copy(this , clone);
242: return clone;
243: }
244:
245: public Object clone() {
246: AllocatableImpl clone = new AllocatableImpl();
247: super .clone(clone);
248: copy(this , clone);
249: return clone;
250: }
251:
252: public String toString() {
253: StringBuffer buf = new StringBuffer();
254: buf.append(" [");
255: buf.append(super .toString());
256: buf.append(",");
257: buf.append(super .getVersion());
258: buf.append("] ");
259: if (getClassification() != null) {
260: buf.append(getClassification().toString());
261: }
262: return buf.toString();
263: }
264:
265: }
|