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.internal;
014:
015: import java.util.ArrayList;
016: import java.util.Collection;
017: import java.util.Iterator;
018: import java.util.Locale;
019:
020: import org.rapla.entities.Category;
021: import org.rapla.entities.RaplaType;
022: import org.rapla.entities.User;
023: import org.rapla.entities.storage.Mementable;
024: import org.rapla.entities.storage.RefEntity;
025: import org.rapla.entities.storage.internal.SimpleEntity;
026:
027: public class UserImpl extends SimpleEntity implements User, Mementable,
028: java.io.Serializable {
029: // Don't forget to increase the serialVersionUID when you change the fields
030: private static final long serialVersionUID = 1;
031:
032: private String username = "";
033: private String email = "";
034: private String name = "";
035: private boolean bAdmin = false;
036:
037: transient private boolean groupArrayUpToDate = false;
038: // The resolved references
039: transient private Category[] groups;
040:
041: final public RaplaType getRaplaType() {
042: return TYPE;
043: }
044:
045: public boolean isAdmin() {
046: return bAdmin;
047: }
048:
049: public String getName() {
050: return name;
051: }
052:
053: public String getEmail() {
054: return email;
055: }
056:
057: public String getUsername() {
058: return username;
059: }
060:
061: public String toString() {
062: return getUsername();
063: }
064:
065: public void setName(String name) {
066: checkWritable();
067: this .name = name;
068: }
069:
070: public void setEmail(String email) {
071: checkWritable();
072: this .email = email;
073: }
074:
075: public void setUsername(String username) {
076: checkWritable();
077: this .username = username;
078: }
079:
080: public void setAdmin(boolean bAdmin) {
081: checkWritable();
082: this .bAdmin = bAdmin;
083: }
084:
085: public String getName(Locale locale) {
086: return getUsername();
087: }
088:
089: public void addGroup(Category group) {
090: checkWritable();
091: if (getReferenceHandler().isRefering((RefEntity) group))
092: return;
093: groupArrayUpToDate = false;
094: getReferenceHandler().add(group);
095: }
096:
097: public boolean removeGroup(Category group) {
098: checkWritable();
099: if (!getReferenceHandler().isRefering((RefEntity) group))
100: return false;
101: groupArrayUpToDate = false;
102: return getReferenceHandler().remove(group);
103: }
104:
105: public Category[] getGroups() {
106: updateGroupArray();
107: return groups;
108: }
109:
110: public boolean belongsTo(Category group) {
111: return getReferenceHandler().isRefering((RefEntity) group);
112: }
113:
114: private void updateGroupArray() {
115: if (groupArrayUpToDate)
116: return;
117: Collection groupList = new ArrayList();
118: Iterator it = super .getReferences();
119: while (it.hasNext()) {
120: RefEntity o = (RefEntity) it.next();
121: if (o.getRaplaType().equals(Category.TYPE)) {
122: groupList.add(o);
123: }
124: }
125: groups = (Category[]) groupList
126: .toArray(Category.CATEGORY_ARRAY);
127: groupArrayUpToDate = true;
128: }
129:
130: static private void copy(UserImpl source, UserImpl dest) {
131: dest.groupArrayUpToDate = false;
132:
133: dest.username = source.username;
134: dest.name = source.name;
135: dest.email = source.email;
136: dest.bAdmin = source.bAdmin;
137: }
138:
139: public void copy(Object obj) {
140: super .copy((UserImpl) obj);
141: copy((UserImpl) obj, this );
142: }
143:
144: public Object deepClone() {
145: UserImpl clone = new UserImpl();
146: super .deepClone(clone);
147: copy(this , clone);
148: return clone;
149: }
150:
151: public Object clone() {
152: UserImpl clone = new UserImpl();
153: super.clone(clone);
154: copy(this, clone);
155: return clone;
156: }
157:
158: }
|