📄 allocatableimpl.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.entities.domain.internal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import org.rapla.components.util.iterator.IteratorChain;
import org.rapla.components.util.iterator.NestedIterator;
import org.rapla.entities.EntityNotFoundException;
import org.rapla.entities.RaplaType;
import org.rapla.entities.User;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Permission;
import org.rapla.entities.dynamictype.Classification;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.entities.dynamictype.internal.ClassificationImpl;
import org.rapla.entities.storage.DynamicTypeDependant;
import org.rapla.entities.storage.RefEntity;
import org.rapla.entities.storage.EntityResolver;
import org.rapla.entities.storage.Mementable;
import org.rapla.entities.storage.internal.SimpleEntity;
public class AllocatableImpl extends SimpleEntity implements Allocatable,Mementable,java.io.Serializable, DynamicTypeDependant {
// Don't forget to increase the serialVersionUID when you change the fields
private static final long serialVersionUID = 1;
private ClassificationImpl classification;
private boolean holdBackConflicts;
private boolean _isPerson;
private ArrayList permissions = new ArrayList();
transient private boolean permissionArrayUpToDate = false;
transient private Permission[] permissionArray;
public AllocatableImpl() {
_isPerson = false;
}
public void resolveEntities( EntityResolver resolver) throws EntityNotFoundException {
super.resolveEntities( resolver);
classification.resolveEntities( resolver);
for (Iterator it = permissions.iterator();it.hasNext();)
{
((PermissionImpl) it.next()).resolveEntities( resolver);
}
}
public void setReadOnly(boolean enable) {
super.setReadOnly( enable );
classification.setReadOnly( enable );
Iterator it = permissions.iterator();
while (it.hasNext()) {
((PermissionImpl) it.next()).setReadOnly(enable);
}
}
public RaplaType getRaplaType() {
return TYPE;
}
// Implementation of interface classifiable
public Classification getClassification() { return classification; }
public void setClassification(Classification classification) {
this.classification = (ClassificationImpl) classification;
}
public void setHoldBackConflicts(boolean enable) {
holdBackConflicts = enable;
}
public boolean isHoldBackConflicts() {
return holdBackConflicts;
}
public String getName(Locale locale) {
Classification c = getClassification();
if (c == null)
return "";
return c.getName(locale);
}
public void setPerson(boolean isPerson) {
checkWritable();
_isPerson = isPerson;
}
public boolean isPerson() {
return _isPerson;
}
private boolean hasAccess( User user, int accessLevel ) {
Permission[] permissions = getPermissions();
if ( user == null || user.isAdmin() )
return true;
for ( int i = 0; i < permissions.length; i++ ) {
Permission p = permissions[i];
if ( p.affectsUser(user)
&& (p.getAccessLevel() >= accessLevel ))
{
return true;
}
}
return false;
}
public boolean canCreateConflicts( User user ) {
return hasAccess( user, Permission.ALLOCATE_CONFLICTS);
}
public boolean canModify(User user) {
return hasAccess( user, Permission.ADMIN);
}
public boolean canRead(User user) {
return hasAccess( user, Permission.READ );
}
public boolean canAllocate( User user, Date start, Date end, Date today ) {
if (user == null)
return false;
Permission[] permissions = getPermissions();
if ( user.isAdmin() )
return true;
for ( int i = 0; i < permissions.length; i++ ) {
Permission p = permissions[i];
if ( p.affectsUser(user)
&& p.getAccessLevel() >= Permission.ALLOCATE
&& (p.getAccessLevel() == Permission.ADMIN || p.covers( start, end, today ) ))
{
return true;
}
}
return false;
}
public void addPermission(Permission permission) {
checkWritable();
permissionArrayUpToDate = false;
permissions.add(permission);
}
public boolean removePermission(Permission permission) {
checkWritable();
permissionArrayUpToDate = false;
return permissions.remove(permission);
}
public Permission newPermission() {
return new PermissionImpl();
}
public Permission[] getPermissions() {
updatePermissionArray();
return permissionArray;
}
private void updatePermissionArray() {
if ( permissionArrayUpToDate )
return;
permissionArray = (Permission[])permissions.toArray(Permission.PERMISSION_ARRAY);
permissionArrayUpToDate = true;
}
public Iterator getReferences() {
return new IteratorChain
(
classification.getReferences()
,new NestedIterator( permissions.iterator() ) {
public Iterator getNestedIterator(Object obj) {
return ((PermissionImpl)obj).getReferences();
}
}
);
}
public boolean needsChange(DynamicType type) {
return classification.needsChange( type );
}
public void commitChange(DynamicType type) {
classification.commitChange( type );
}
public boolean isRefering(RefEntity object) {
if (super.isRefering(object))
return true;
if (classification.isRefering(object))
return true;
Permission[] permissions = getPermissions();
for ( int i = 0; i < permissions.length; i++ ) {
if ( ((PermissionImpl) permissions[i]).isRefering( object ) )
return true;
}
return false;
}
static private void copy(AllocatableImpl source,AllocatableImpl dest) {
dest.permissionArrayUpToDate = false;
dest.classification = (ClassificationImpl) source.classification.clone();
dest.permissions.clear();
Iterator it = source.permissions.iterator();
while ( it.hasNext() ) {
dest.permissions.add(((PermissionImpl)it.next()).clone());
}
dest.holdBackConflicts = source.holdBackConflicts;
dest._isPerson = source._isPerson;
}
public void copy(Object obj) {
super.copy((AllocatableImpl)obj);
copy((AllocatableImpl)obj,this);
}
public Object deepClone() {
AllocatableImpl clone = new AllocatableImpl();
super.deepClone(clone);
copy(this,clone);
return clone;
}
public Object clone() {
AllocatableImpl clone = new AllocatableImpl();
super.clone(clone);
copy(this,clone);
return clone;
}
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(" [");
buf.append(super.toString());
buf.append(",");
buf.append(super.getVersion());
buf.append("] ");
if ( getClassification() != null) {
buf.append (getClassification().toString()) ;
}
return buf.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -