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.model24.identity;
023:
024: import org.apache.log4j.Logger;
025: import org.jboss.portal.migration.helper.Tools;
026:
027: import java.lang.reflect.Field;
028: import java.text.DateFormat;
029: import java.text.ParseException;
030: import java.text.SimpleDateFormat;
031: import java.util.Collections;
032: import java.util.Date;
033: import java.util.HashMap;
034: import java.util.HashSet;
035: import java.util.Iterator;
036: import java.util.Locale;
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: */
047: public class User24Impl implements User {
048:
049: private static final Logger log = Logger
050: .getLogger(User24Impl.class);
051:
052: /* static final Map ACCESSORS = User24Impl.buildAccessors();*/
053:
054: /* */
055: private static Map buildAccessors() {
056: Map map = new HashMap();
057:
058: // Map attributes defined by the JSR 168 spec
059: map.put(P3PConstants.INFO_USER_NAME_NICKNAME,
060: new StringPropertyAccessor(
061: P3PConstants.INFO_USER_NAME_NICKNAME,
062: "userName", false, false));
063: map
064: .put(
065: P3PConstants.INFO_USER_BUSINESS_INFO_ONLINE_EMAIL,
066: new StringPropertyAccessor(
067: P3PConstants.INFO_USER_BUSINESS_INFO_ONLINE_EMAIL,
068: "realEmail", true, true));
069: map.put(P3PConstants.INFO_USER_NAME_GIVEN,
070: new StringPropertyAccessor(
071: P3PConstants.INFO_USER_NAME_GIVEN, "givenName",
072: true, true));
073: map.put(P3PConstants.INFO_USER_NAME_FAMILY,
074: new StringPropertyAccessor(
075: P3PConstants.INFO_USER_NAME_FAMILY,
076: "familyName", true, true));
077:
078: // Map attributes specific to JBoss Portal
079: map.put(User.INFO_USER_EMAIL_FAKE, new StringPropertyAccessor(
080: User.INFO_USER_EMAIL_FAKE, "fakeEmail", true, true));
081: map.put(User.INFO_USER_REGISTRATION_DATE,
082: new DatePropertyAccessor(
083: User.INFO_USER_REGISTRATION_DATE,
084: "registrationDate", false, false));
085: map.put(User.INFO_USER_VIEW_EMAIL_VIEW_REAL,
086: new BooleanPropertyAccessor(
087: User.INFO_USER_VIEW_EMAIL_VIEW_REAL,
088: "viewRealEmail", true, false));
089:
090: //
091: return Collections.unmodifiableMap(map);
092: }
093:
094: /** Non mapped. */
095: public Long key;
096: public boolean enabled;
097: public String password;
098:
099: /** P3P mapped. */
100: public String userName;
101: public String givenName;
102: public String familyName;
103: public String realEmail;
104:
105: /** Extension mapped. */
106: public String fakeEmail;
107: public boolean viewRealEmail;
108: public Date registrationDate;
109:
110: /** Other fields. */
111: public Map dynamic;
112: public PropertyMap properties;
113: public Set roleNames;
114: public Set roles;
115:
116: /**
117: *
118:
119: public User24Impl()
120: {
121: this.key = null;
122: this.userName = null;
123: this.dynamic = null;
124: this.properties = new PropertyMapImpl(this);
125: this.roles = null;
126: this.registrationDate = null;
127: this.enabled = false;
128: } */
129:
130: /**
131: *
132:
133: public User24Impl(String userName)
134: {
135: this.key = null;
136: this.userName = userName;
137: this.dynamic = new HashMap();
138: this.properties = new PropertyMapImpl(this);
139: this.roles = new HashSet();
140: this.registrationDate = new Date();
141: this.enabled = false;
142: }*/
143:
144: /** Called by hibernate. */
145: public Long getKey() {
146: return key;
147: }
148:
149: /** Called by hibernate. */
150: protected void setKey(Long key) {
151: this .key = key;
152: }
153:
154: /** Called by hibernate. */
155: protected void setUserName(String userName) {
156: this .userName = userName;
157: }
158:
159: /** Called by Hibernate. */
160: protected Map getDynamic() {
161: return dynamic;
162: }
163:
164: /** Called by Hibernate. */
165: protected void setDynamic(Map dynamic) {
166: this .dynamic = dynamic;
167: }
168:
169: // User implementation **********************************************************************************************
170:
171: /**
172: *
173: */
174: public Object getId() {
175: return key;
176: }
177:
178: /**
179: *
180: */
181: public String getUserName() {
182: return userName;
183: }
184:
185: /**
186: *
187: */
188: public String getGivenName() {
189: return givenName;
190: }
191:
192: public void setGivenName(String givenName) {
193: this .givenName = givenName;
194: }
195:
196: /**
197: *
198: */
199: public String getFamilyName() {
200: return familyName;
201: }
202:
203: public void setFamilyName(String familyName) {
204: this .familyName = familyName;
205: }
206:
207: public void updatePassword(String password) {
208: this .password = Tools.md5AsHexString(password);
209: }
210:
211: /**
212: *
213: */
214: public String getRealEmail() {
215: return realEmail;
216: }
217:
218: /**
219: *
220: */
221: public void setRealEmail(String realEmail) {
222: this .realEmail = realEmail;
223: }
224:
225: /**
226: *
227: */
228: public String getFakeEmail() {
229: return fakeEmail;
230: }
231:
232: /**
233: *
234: */
235: public void setFakeEmail(String fakeEmail) {
236: this .fakeEmail = fakeEmail;
237: }
238:
239: /**
240: *
241: */
242: public Date getRegistrationDate() {
243: return registrationDate;
244: }
245:
246: /**
247: *
248: */
249: public void setRegistrationDate(Date registrationDate) {
250: this .registrationDate = registrationDate;
251: }
252:
253: /**
254: *
255: */
256: public boolean getViewRealEmail() {
257: return viewRealEmail;
258: }
259:
260: /**
261: *
262: */
263: public void setViewRealEmail(boolean viewRealEmail) {
264: this .viewRealEmail = viewRealEmail;
265: }
266:
267: /**
268: *
269: */
270: public boolean getEnabled() {
271: return enabled;
272: }
273:
274: /**
275: *
276: */
277: public void setEnabled(boolean enable) {
278: this .enabled = enable;
279: }
280:
281: /**
282: *
283: */
284: public PropertyMap getProperties() {
285: return properties;
286: }
287:
288: public String getPassword() {
289: return password;
290: }
291:
292: public void setPassword(String password) {
293: this .password = password;
294: }
295:
296: /** Returns the roles related to this user. */
297: public Set getRoles() {
298: return roles;
299: }
300:
301: /** Update the roles. */
302: public void setRoles(Set roles) {
303: this .roles = roles;
304: this .roleNames = null;
305: }
306:
307: // Dynamic attributes
308:
309: /** Implemented as a dynamic attribute (see #INFO_USER_LAST_LOGIN_DATE). */
310: public Date getLastVisitDate() {
311: Date date = null;
312: String dateAsString = (String) getDynamic().get(
313: User.INFO_USER_LAST_LOGIN_DATE);
314: if (dateAsString != null) {
315: try {
316: long dateAsLong = Long.parseLong(dateAsString);
317: date = new Date(dateAsLong);
318: } catch (NumberFormatException e) {
319: User24Impl.log.error("Bad date format " + dateAsString
320: + " try to remove it", e);
321: getDynamic().remove(User.INFO_USER_LAST_LOGIN_DATE);
322: }
323: }
324: return date;
325: }
326:
327: public void setLastVisitDate(Date date) {
328: if (date == null) {
329: getDynamic().remove(User.INFO_USER_LAST_LOGIN_DATE);
330: } else {
331: long dateAsLong = date.getTime();
332: String dateAsString = Long.toString(dateAsLong);
333: getDynamic().put(User.INFO_USER_LAST_LOGIN_DATE,
334: dateAsString);
335: }
336: }
337:
338: /** Implemented as a dynamic attribute (see #INFO_USER_SIGNATURE). */
339: public String getSignature() {
340: return (String) getDynamic().get(User.INFO_USER_SIGNATURE);
341: }
342:
343: public void setSignature(String signature) {
344: getDynamic().put(User.INFO_USER_SIGNATURE, signature);
345: }
346:
347: /** Implemented as a dynamic attribute (see #INFO_USER_LOCALE). */
348: public Locale getPreferredLocale() {
349: Locale locale = null;
350: String localeAsString = (String) getDynamic().get(
351: User.INFO_USER_LOCALE);
352: if (localeAsString != null) {
353: LocaleInfo info = LocaleInfo
354: .decodeLocaleInfo(localeAsString);
355: if (info != null) {
356: locale = info.getLocale();
357: }
358: }
359: return locale;
360: }
361:
362: public void setPreferredLocale(Locale locale) {
363: if (locale == null) {
364: getDynamic().remove(User.INFO_USER_LOCALE);
365: } else {
366: String localeAsString = locale.toString();
367: getDynamic().put(User.INFO_USER_LOCALE, localeAsString);
368: }
369: }
370:
371: public boolean validatePassword(String password) {
372: if (password != null) {
373: String hashedPassword = Tools.md5AsHexString(password);
374: return hashedPassword.equals(this .password);
375: }
376: return false;
377: }
378:
379: /** Return all the role names of the user. */
380: public Set getRoleNames() {
381: if (roleNames == null) {
382: Iterator it = roles.iterator();
383: roleNames = new HashSet();
384: while (it.hasNext()) {
385: roleNames.add(((Role) it.next()).getName());
386: }
387: }
388: return roleNames;
389: }
390:
391: /**
392: *
393: */
394: public String toString() {
395: return "User[" + key + "," + userName + "]";
396: }
397:
398: /** An accessor that maps a user field to a property name. */
399: static abstract class PropertyAccessor {
400:
401: protected final String propertyName;
402: protected final Field field;
403: protected final boolean writable;
404: protected final boolean nullable;
405:
406: public PropertyAccessor(String propertyName, String fieldName,
407: boolean writable, boolean nullable) {
408: try {
409: this .propertyName = propertyName;
410: this .writable = writable;
411: this .field = User24Impl.class
412: .getDeclaredField(fieldName);
413: this .nullable = nullable;
414: } catch (NoSuchFieldException e) {
415: throw new Error(e);
416: }
417: }
418:
419: public String getPropertyName() {
420: return propertyName;
421: }
422:
423: public boolean isNullable() {
424: return nullable;
425: }
426:
427: public boolean isWritable() {
428: return writable;
429: }
430:
431: /**
432: * @param instance the user instance
433: * @param string the value
434: * @throws IllegalArgumentException if the string cannot be converted to an object
435: */
436: public void set(Object instance, String string)
437: throws IllegalArgumentException {
438: try {
439: if (string == null) {
440: field.set(instance, null);
441: } else {
442: Object object = toObject(string);
443: field.set(instance, object);
444: }
445: } catch (IllegalAccessException e) {
446: throw new Error(e);
447: }
448: }
449:
450: /**
451: * @param instance the user instance
452: * @return the converted value
453: * @throws IllegalArgumentException if the object cannot be converted to a string
454: */
455: public String get(Object instance)
456: throws IllegalArgumentException {
457: try {
458: Object object = field.get(instance);
459: if (object == null) {
460: return null;
461: } else {
462: return toString(object);
463: }
464: } catch (IllegalAccessException e) {
465: throw new Error(e);
466: }
467: }
468:
469: /**
470: * Perform the to object conversion.
471: *
472: * @param value the value to convert
473: * @return the converted value
474: * @throws IllegalArgumentException if the string cannot be converted to an object
475: */
476: protected abstract Object toObject(String value)
477: throws IllegalArgumentException;
478:
479: /**
480: * Perform the to strong conversion.
481: *
482: * @param value the value to convert
483: * @return the converted value
484: * @throws IllegalArgumentException if the object cannot be converted to a string
485: */
486: protected abstract String toString(Object value);
487:
488: public String toString() {
489: return "PropertyAccessor[" + propertyName + "," + field
490: + "]";
491: }
492: }
493:
494: static class StringPropertyAccessor extends PropertyAccessor {
495: public StringPropertyAccessor(String propertyName,
496: String fieldName, boolean writable, boolean nullable) {
497: super (propertyName, fieldName, writable, nullable);
498: }
499:
500: protected Object toObject(String value) {
501: return value;
502: }
503:
504: protected String toString(Object value) {
505: return (String) value;
506: }
507: }
508:
509: static class BooleanPropertyAccessor extends PropertyAccessor {
510: public BooleanPropertyAccessor(String propertyName,
511: String fieldName, boolean writable, boolean nullable) {
512: super (propertyName, fieldName, writable, nullable);
513: }
514:
515: protected Object toObject(String value)
516: throws IllegalArgumentException {
517: if ("true".equalsIgnoreCase(value)) {
518: return Boolean.TRUE;
519: } else if ("false".equalsIgnoreCase(value)) {
520: return Boolean.FALSE;
521: } else {
522: throw new IllegalArgumentException(
523: "The value "
524: + value
525: + " cannot be converted to boolean for accessor "
526: + toString());
527: }
528: }
529:
530: protected String toString(Object value) {
531: return value.toString();
532: }
533: }
534:
535: static class DatePropertyAccessor extends PropertyAccessor {
536: private static final ThreadLocal formatLocal = new ThreadLocal() {
537: protected Object initialValue() {
538: return new SimpleDateFormat();
539: }
540: };
541:
542: public DatePropertyAccessor(String propertyName,
543: String fieldName, boolean writable, boolean nullable) {
544: super (propertyName, fieldName, writable, nullable);
545: }
546:
547: protected Object toObject(String value)
548: throws IllegalArgumentException {
549: try {
550: DateFormat format = (DateFormat) DatePropertyAccessor.formatLocal
551: .get();
552: Date date = format.parse(value);
553: return date;
554: } catch (ParseException e) {
555: throw new IllegalArgumentException();
556: }
557: }
558:
559: protected String toString(Object value) {
560: Date date = (Date) value;
561: DateFormat format = (DateFormat) DatePropertyAccessor.formatLocal
562: .get();
563: return format.format(date);
564: }
565: }
566: }
|