001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration.model20.impl.user;
023:
024: import org.apache.log4j.Logger;
025: import org.jboss.portal.migration.model20.impl.preferences.MappedPreferenceSet;
026: import org.jboss.portal.migration.model20.impl.preferences.MappedPreferenceStore;
027: import org.jboss.portal.migration.model20.model.PreferenceStore;
028: import org.jboss.portal.migration.model20.model.PropertyMap;
029: import org.jboss.portal.migration.model20.model.Role20;
030: import org.jboss.portal.migration.model20.model.User20;
031: import org.jboss.portal.migration.model20.other.CoreConstants;
032:
033: import java.util.Date;
034: import java.util.HashMap;
035: import java.util.HashSet;
036: import java.util.Iterator;
037: import java.util.Map;
038: import java.util.Set;
039:
040: /**
041: * User interface implementation.
042: *
043: * @author <a href="mailto:julien@jboss.org">Julien Viet </a>
044: * @author <a href="mailto:theute@jboss.org">Thomas Heute </a>
045: * @version $Revision: 8784 $
046: * @hibernate.class table="jbp_users"
047: */
048: public class User20Impl implements User20 {
049:
050: private static final Logger log = Logger
051: .getLogger(User20Impl.class);
052:
053: //private Integer id;
054: public Integer id;
055:
056: public String userName;
057:
058: public String givenName;
059:
060: public String familyName;
061:
062: public String realEmail;
063:
064: public String fakeEmail;
065:
066: public boolean viewRealEmail;
067:
068: public String password;
069:
070: public boolean enabled = false;
071:
072: //private UserPrefSet20 preferenceSet20;
073: public UserPrefSet20 preferenceSet20;
074:
075: //private Map dynamic;
076: public Map dynamic;
077:
078: private PropertyMap properties;
079:
080: //private Set roles;
081: public Set roles;
082:
083: //private Date registrationDate;
084: public Date registrationDate;
085:
086: private Set roleNames;
087:
088: /** The preference set store. */
089: private final MappedPreferenceStore store = new MappedPreferenceStore() {
090: protected MappedPreferenceSet getRoot() {
091: if (preferenceSet20 == null) {
092: if (log.isDebugEnabled()) {
093: log.debug("No root - create a new one");
094: }
095: preferenceSet20 = new UserPrefSet20("root");
096: }
097: return preferenceSet20;
098: }
099: };
100:
101: /** Called by hibernate. */
102: public User20Impl() {
103: this .id = null;
104: this .userName = null;
105: this .dynamic = new HashMap();
106: this .properties = new PropertyMapImpl(this );
107: this .roles = new HashSet();
108: this .registrationDate = new Date();
109: }
110:
111: public User20Impl(String userName) {
112: this .id = null;
113: this .userName = userName;
114: this .dynamic = new HashMap();
115: this .properties = new PropertyMapImpl(this );
116: this .roles = new HashSet();
117: this .registrationDate = new Date();
118: }
119:
120: /** Called by hibernate. */
121: private void setID(Integer id) {
122: this .id = id;
123: }
124:
125: /** Called by hibernate. */
126: private void setUserName(String userName) {
127: this .userName = userName;
128: }
129:
130: public void setGivenName(String givenName) {
131: this .givenName = givenName;
132: }
133:
134: public void setFamilyName(String familyName) {
135: this .familyName = familyName;
136: }
137:
138: /**
139: * @hibernate.many-to-one class="org.jboss.portal.core.impl.user.UserPrefSet" column="jbp_root_pref_set_id"
140: * cascade="all"
141: */
142: private UserPrefSet20 getRootPreferenceSet() {
143: // unique = true
144: return preferenceSet20;
145: }
146:
147: private void setRootPreferenceSet(UserPrefSet20 preferenceSet20) {
148: this .preferenceSet20 = preferenceSet20;
149: }
150:
151: /**
152: * @hibernate.map table="jbp_user_prop" cascade="all"
153: * @hibernate.collection-key column="jbp_uid"
154: * @hibernate.collection-index column="jbp_name" type="string"
155: * @hibernate.collection-element column="jbp_value" type="string"
156: */
157: public Map getDynamic() {
158: return dynamic;
159: }
160:
161: /** Called by Hibernate. */
162: private void setDynamic(Map dynamic) {
163: this .dynamic = dynamic;
164: }
165:
166: /** Called by Hibernate. */
167: public void setRoles(Set roles) {
168: this .roles = roles;
169: this .roleNames = null;
170: }
171:
172: public Set getRoleNames() {
173: if (roleNames == null) {
174: Iterator it = roles.iterator();
175: roleNames = new HashSet();
176: while (it.hasNext()) {
177: roleNames.add(((Role20) it.next()).getName());
178: }
179: }
180: return roleNames;
181: }
182:
183: // User implementation
184: // **********************************************************************************************
185:
186: /** @hibernate.id column="jbp_uid" generator-class="native" */
187: public Integer getID() {
188: return id;
189: }
190:
191: /** @hibernate.property column="jbp_uname" unique="true" update="false" */
192: public String getUserName() {
193: return userName;
194: }
195:
196: /** @hibernate.property column="jbp_givenname" unique="false" update="true" */
197: public String getGivenName() {
198: return givenName;
199: }
200:
201: /** @hibernate.property column="jbp_familyname" unique="false" update="true" */
202: public String getFamilyName() {
203: return familyName;
204: }
205:
206: public void setPassword(String password) {
207: this .password = password;
208: }
209:
210: /** @hibernate.property column="jbp_password" unique="false" update="true" */
211: public String getPassword() {
212: return password;
213: }
214:
215: /** @hibernate.property column="jbp_realemail" unique="false" update="true" */
216: public String getRealEmail() {
217: return realEmail;
218: }
219:
220: public void setRealEmail(String realEmail) {
221: this .realEmail = realEmail;
222: }
223:
224: /** @hibernate.property column="jbp_fakeemail" unique="false" update="true" */
225: public String getFakeEmail() {
226: return fakeEmail;
227: }
228:
229: public void setFakeEmail(String fakeEmail) {
230: this .fakeEmail = fakeEmail;
231: }
232:
233: /** @hibernate.property column="jbp_regdate" unique="false" update="false" */
234: public Date getRegistrationDate() {
235: return registrationDate;
236: }
237:
238: public void setRegistrationDate(Date registrationDate) {
239: this .registrationDate = registrationDate;
240: }
241:
242: /** @hibernate.property column="jbp_viewrealemail" unique="false" update="true" */
243: public boolean getViewRealEmail() {
244: return viewRealEmail;
245: }
246:
247: public void setViewRealEmail(boolean viewRealEmail) {
248: this .viewRealEmail = viewRealEmail;
249: }
250:
251: public void setEnabled(boolean enable) {
252: this .enabled = enable;
253: }
254:
255: /** @hibernate.property column="jbp_enabled" unique="false" update="true" */
256: public boolean getEnabled() {
257: return enabled;
258: }
259:
260: public PropertyMap getProperties() {
261: return properties;
262: }
263:
264: public PreferenceStore getPreferenceStore() {
265: return store;
266: }
267:
268: /**
269: * @hibernate.set inverse="false" table="jbp_role_membership" lazy="false" cascade="none"
270: * @hibernate.collection-key column="jbp_uid"
271: * @hibernate.collection-many-to-many column="jbp_rid" class="org.jboss.portal.core.impl.role.RoleImpl"
272: * outer-join="true"
273: */
274: public Set getRoles() {
275: return roles;
276: }
277:
278: public String toString() {
279: return "User[" + id + "," + userName + "]";
280: }
281:
282: public Date getLastVisitDate() {
283: Date date = null;
284: String dateAsString = (String) getDynamic().get(
285: CoreConstants.INFO_USER_LAST_LOGIN_DATE);
286: if (dateAsString != null) {
287: try {
288: long dateAsLong = Long.parseLong(dateAsString);
289: date = new Date(dateAsLong);
290: } catch (NumberFormatException e) {
291: log.error("Bad date format " + dateAsString
292: + " try to remove it", e);
293: getDynamic().remove(
294: CoreConstants.INFO_USER_LAST_LOGIN_DATE);
295: }
296: }
297: return date;
298: }
299:
300: public void setLastVisitDate(Date date) {
301: if (date == null) {
302: getDynamic()
303: .remove(CoreConstants.INFO_USER_LAST_LOGIN_DATE);
304: } else {
305: long dateAsLong = date.getTime();
306: String dateAsString = Long.toString(dateAsLong);
307: getDynamic().put(CoreConstants.INFO_USER_LAST_LOGIN_DATE,
308: dateAsString);
309: }
310: }
311:
312: public String getSignature() {
313: return (String) getDynamic().get(
314: CoreConstants.INFO_USER_SIGNATURE);
315: }
316: }
|