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.model22.impl.user;
023:
024: import org.apache.log4j.Logger;
025: import org.jboss.portal.migration.model22.model.PreferencesGroup22;
026: import org.jboss.portal.migration.model22.model.PropertyMap;
027: import org.jboss.portal.migration.model22.model.Role;
028: import org.jboss.portal.migration.model22.model.User;
029: import org.jboss.portal.migration.model22.other.CoreConstants;
030: import org.jboss.portal.migration.model22.other.LocaleInfo;
031: import org.jboss.portal.migration.model22.other.PortletConstants;
032:
033: import java.lang.reflect.Field;
034: import java.util.Collections;
035: import java.util.Date;
036: import java.util.HashMap;
037: import java.util.HashSet;
038: import java.util.Iterator;
039: import java.util.Locale;
040: import java.util.Map;
041: import java.util.Set;
042:
043: /**
044: * User interface implementation.
045: *
046: * @author <a href="mailto:julien@jboss.org">Julien Viet </a>
047: * @author <a href="mailto:theute@jboss.org">Thomas Heute </a>
048: * @version $Revision: 8784 $
049: */
050: public class User22Impl implements User {
051:
052: private static final Logger log = Logger
053: .getLogger(User22Impl.class);
054:
055: static final Map ACCESSORS = buildAccessors();
056:
057: private static Map buildAccessors() {
058: Map map = new HashMap();
059:
060: // Map attributes defined by the JSR 168 spec
061: map.put(PortletConstants.INFO_USER_NAME_NICKNAME,
062: new PropertyAccessor(
063: PortletConstants.INFO_USER_NAME_NICKNAME,
064: "userName", false));
065: map
066: .put(
067: PortletConstants.INFO_USER_BUSINESS_INFO_ONLINE_EMAIL,
068: new PropertyAccessor(
069: PortletConstants.INFO_USER_BUSINESS_INFO_ONLINE_EMAIL,
070: "realEmail", true));
071: map.put(PortletConstants.INFO_USER_NAME_GIVEN,
072: new PropertyAccessor(
073: PortletConstants.INFO_USER_NAME_GIVEN,
074: "givenName", true));
075: map.put(PortletConstants.INFO_USER_NAME_FAMILY,
076: new PropertyAccessor(
077: PortletConstants.INFO_USER_NAME_FAMILY,
078: "familyName", true));
079:
080: // Map attributes of JBoss Portal
081: // Need to define the format
082: // map.put(CoreConstants.INFO_USER_REGISTRATION_DATE, new PropertyAccessor(PortletConstants.INFO_USER_NAME_NICKNAME, "registrationDate", false));
083:
084: return Collections.unmodifiableMap(map);
085: }
086:
087: public Long key;
088: public String userName;
089: public String givenName;
090: public String familyName;
091: public String realEmail;
092: public String fakeEmail;
093: public boolean viewRealEmail;
094: public String password;
095: public boolean enabled;
096: public Map dynamic;
097: public PropertyMap properties;
098: public Set roles;
099: public Date registrationDate;
100: public Set roleNames;
101:
102: /**
103: *
104: */
105: public User22Impl() {
106: this .key = null;
107: this .userName = null;
108: this .dynamic = null;
109: this .properties = new PropertyMapImpl(this );
110: this .roles = null;
111: this .registrationDate = null;
112: this .enabled = false;
113: }
114:
115: /**
116: *
117: */
118: public User22Impl(String userName) {
119: this .key = null;
120: this .userName = userName;
121: this .dynamic = new HashMap();
122: this .properties = new PropertyMapImpl(this );
123: this .roles = new HashSet();
124: this .registrationDate = new Date();
125: this .enabled = false;
126: }
127:
128: /** Called by hibernate. */
129: public Long getKey() {
130: return key;
131: }
132:
133: /** Called by hibernate. */
134: protected void setKey(Long key) {
135: this .key = key;
136: }
137:
138: /** Called by hibernate. */
139: protected void setUserName(String userName) {
140: this .userName = userName;
141: }
142:
143: /** Called by Hibernate. */
144: protected Map getDynamic() {
145: return dynamic;
146: }
147:
148: /** Called by Hibernate. */
149: protected void setDynamic(Map dynamic) {
150: this .dynamic = dynamic;
151: }
152:
153: // User implementation **********************************************************************************************
154:
155: /**
156: *
157: */
158: public Object getId() {
159: return key;
160: }
161:
162: /**
163: *
164: */
165: public String getUserName() {
166: return userName;
167: }
168:
169: /**
170: *
171: */
172: public String getGivenName() {
173: return givenName;
174: }
175:
176: public void setGivenName(String givenName) {
177: this .givenName = givenName;
178: }
179:
180: /**
181: *
182: */
183: public String getFamilyName() {
184: return familyName;
185: }
186:
187: public void setFamilyName(String familyName) {
188: this .familyName = familyName;
189: }
190:
191: /**
192: *
193: */
194: public String getPassword() {
195: return password;
196: }
197:
198: public void setPassword(String password) {
199: this .password = password;
200: }
201:
202: /**
203: *
204: */
205: public String getRealEmail() {
206: return realEmail;
207: }
208:
209: /**
210: *
211: */
212: public void setRealEmail(String realEmail) {
213: this .realEmail = realEmail;
214: }
215:
216: /**
217: *
218: */
219: public String getFakeEmail() {
220: return fakeEmail;
221: }
222:
223: /**
224: *
225: */
226: public void setFakeEmail(String fakeEmail) {
227: this .fakeEmail = fakeEmail;
228: }
229:
230: /**
231: *
232: */
233: public Date getRegistrationDate() {
234: return registrationDate;
235: }
236:
237: /**
238: *
239: */
240: public void setRegistrationDate(Date registrationDate) {
241: this .registrationDate = registrationDate;
242: }
243:
244: /**
245: *
246: */
247: public boolean getViewRealEmail() {
248: return viewRealEmail;
249: }
250:
251: /**
252: *
253: */
254: public void setViewRealEmail(boolean viewRealEmail) {
255: this .viewRealEmail = viewRealEmail;
256: }
257:
258: /**
259: *
260: */
261: public boolean getEnabled() {
262: return enabled;
263: }
264:
265: /**
266: *
267: */
268: public void setEnabled(boolean enable) {
269: this .enabled = enable;
270: }
271:
272: /**
273: *
274: */
275: public PropertyMap getProperties() {
276: return properties;
277: }
278:
279: /**
280: *
281: */
282: public Set getRoles() {
283: return roles;
284: }
285:
286: /**
287: *
288: */
289: public void setRoles(Set roles) {
290: this .roles = roles;
291: this .roleNames = null;
292: }
293:
294: /**
295: *
296: */
297: public Date getLastVisitDate() {
298: Date date = null;
299: String dateAsString = (String) getDynamic().get(
300: CoreConstants.INFO_USER_LAST_LOGIN_DATE);
301: if (dateAsString != null) {
302: try {
303: long dateAsLong = Long.parseLong(dateAsString);
304: date = new Date(dateAsLong);
305: } catch (NumberFormatException e) {
306: log.error("Bad date format " + dateAsString
307: + " try to remove it", e);
308: getDynamic().remove(
309: CoreConstants.INFO_USER_LAST_LOGIN_DATE);
310: }
311: }
312: return date;
313: }
314:
315: /**
316: *
317: */
318: public void setLastVisitDate(Date date) {
319: if (date == null) {
320: getDynamic()
321: .remove(CoreConstants.INFO_USER_LAST_LOGIN_DATE);
322: } else {
323: long dateAsLong = date.getTime();
324: String dateAsString = Long.toString(dateAsLong);
325: getDynamic().put(CoreConstants.INFO_USER_LAST_LOGIN_DATE,
326: dateAsString);
327: }
328: }
329:
330: /**
331: *
332: */
333: public String getSignature() {
334: return (String) getDynamic().get(
335: CoreConstants.INFO_USER_SIGNATURE);
336: }
337:
338: /**
339: *
340: */
341: public void setSignature(String signature) {
342: getDynamic().put(CoreConstants.INFO_USER_SIGNATURE, signature);
343: }
344:
345: /**
346: *
347: */
348: public Locale getPreferredLocale() {
349: Locale locale = null;
350: String localeAsString = (String) getDynamic().get(
351: CoreConstants.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(CoreConstants.INFO_USER_LOCALE);
365: } else {
366: String localeAsString = locale.toString();
367: getDynamic().put(CoreConstants.INFO_USER_LOCALE,
368: localeAsString);
369: }
370: }
371:
372: /**
373: *
374: */
375: public Set getRoleNames() {
376: if (roleNames == null) {
377: Iterator it = roles.iterator();
378: roleNames = new HashSet();
379: while (it.hasNext()) {
380: roleNames.add(((Role) it.next()).getName());
381: }
382: }
383: return roleNames;
384: }
385:
386: /**
387: *
388: */
389: public String toString() {
390: return "User[" + key + "," + userName + "]";
391: }
392:
393: public PreferencesGroup22 getPreferencesGroup() {
394: return null;
395: }
396:
397: /** An accessor that maps a user field to a property name. */
398: static class PropertyAccessor {
399: private final String propertyName;
400: private final boolean writable;
401: private final Field field;
402:
403: public PropertyAccessor(String propertyName, String fieldName,
404: boolean writable) {
405: try {
406: this .propertyName = propertyName;
407: this .writable = writable;
408: this .field = User22Impl.class
409: .getDeclaredField(fieldName);
410: } catch (NoSuchFieldException e) {
411: e.printStackTrace();
412: throw new Error(e);
413: }
414: }
415:
416: public String getPropertyName() {
417: return propertyName;
418: }
419:
420: public boolean isWritable() {
421: return writable;
422: }
423:
424: public void set(Object instance, String value)
425: throws IllegalArgumentException {
426: try {
427: field.set(instance, value);
428: } catch (IllegalAccessException e) {
429: throw new Error(e);
430: }
431: }
432:
433: public String get(Object instance) {
434: try {
435: return (String) field.get(instance);
436: } catch (IllegalAccessException e) {
437: throw new Error(e);
438: }
439: }
440:
441: public String toString() {
442: return "PropertyAccessor[" + propertyName + "," + writable
443: + "," + field + "]";
444: }
445: }
446: }
|