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.wsrp;
023:
024: import static org.jboss.portal.common.p3p.P3PConstants.*;
025: import org.jboss.portal.portlet.spi.UserContext;
026: import org.jboss.portal.wsrp.core.Contact;
027: import org.jboss.portal.wsrp.core.EmployerInfo;
028: import org.jboss.portal.wsrp.core.Online;
029: import org.jboss.portal.wsrp.core.PersonName;
030: import org.jboss.portal.wsrp.core.Postal;
031: import org.jboss.portal.wsrp.core.Telecom;
032: import org.jboss.portal.wsrp.core.TelephoneNum;
033: import org.jboss.portal.wsrp.core.UserProfile;
034: import org.jboss.xb.binding.SimpleTypeBindings;
035:
036: import java.util.ArrayList;
037: import java.util.Calendar;
038: import java.util.Collections;
039: import java.util.HashMap;
040: import java.util.List;
041: import java.util.Locale;
042: import java.util.Map;
043:
044: /**
045: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
046: * @version $Revision: 9360 $
047: * @since 2.4 (May 8, 2006)
048: */
049: public class UserContextConverter {
050: private UserContextConverter() {
051: }
052:
053: public static UserContext createPortalUserContextFrom(
054: org.jboss.portal.wsrp.core.UserContext userContext,
055: String[] desiredLocales, String preferredLocale) {
056: return new WSRPMappedUserContext(userContext, desiredLocales,
057: preferredLocale);
058: }
059:
060: public static org.jboss.portal.wsrp.core.UserContext createWSRPUserContextFrom(
061: UserContext userContext, String userContextKey,
062: String[] userCategories) {
063: org.jboss.portal.wsrp.core.UserContext wsrpUserContext = WSRPTypeFactory
064: .createUserContext(userContextKey);
065: wsrpUserContext.setProfile(createUserProfileFrom(userContext));
066: wsrpUserContext.setUserCategories(userCategories);
067: return wsrpUserContext;
068: }
069:
070: private static UserProfile createUserProfileFrom(
071: UserContext userContext) {
072: Map userInfos = userContext.getInformations();
073:
074: if (userInfos == null || userInfos.isEmpty()) {
075: return null;
076: }
077:
078: PersonName name = createNameFrom(userInfos);
079:
080: Calendar bdate = null;
081: String bdateAsString = (String) userInfos.get(INFO_USER_BDATE);
082: if (bdateAsString != null) {
083: bdate = SimpleTypeBindings.unmarshalDateTime(bdateAsString);
084: }
085:
086: EmployerInfo employerInfo = new EmployerInfo((String) userInfos
087: .get(INFO_USER_EMPLOYER), (String) userInfos
088: .get(INFO_USER_DEPARTMENT), (String) userInfos
089: .get(INFO_USER_JOB_TITLE), null);
090:
091: return new UserProfile(name, bdate, (String) userInfos
092: .get(INFO_USER_GENDER), employerInfo,
093: createContactFrom(userInfos, false), createContactFrom(
094: userInfos, true), null);
095: }
096:
097: private static PersonName createNameFrom(Map userInfos) {
098: return new PersonName((String) userInfos
099: .get(INFO_USER_NAME_PREFIX), (String) userInfos
100: .get(INFO_USER_NAME_GIVEN), (String) userInfos
101: .get(INFO_USER_NAME_FAMILY), (String) userInfos
102: .get(INFO_USER_NAME_MIDDLE), (String) userInfos
103: .get(INFO_USER_NAME_SUFFIX), (String) userInfos
104: .get(INFO_USER_NAME_NICKNAME), null);
105: }
106:
107: private static Contact createContactFrom(Map infos,
108: boolean isBusiness) {
109: Online online = new Online(
110: (String) infos.get(getOnlineUserInfoKey(
111: OnlineInfo.EMAIL, isBusiness)), (String) infos
112: .get(getOnlineUserInfoKey(OnlineInfo.URI,
113: isBusiness)), null);
114:
115: Postal postal = new Postal(
116: (String) infos.get(getPostalUserInfoKey(
117: PostalInfo.NAME, isBusiness)), (String) infos
118: .get(getPostalUserInfoKey(PostalInfo.STREET,
119: isBusiness)), (String) infos
120: .get(getPostalUserInfoKey(PostalInfo.CITY,
121: isBusiness)), (String) infos
122: .get(getPostalUserInfoKey(PostalInfo.STATEPROV,
123: isBusiness)), (String) infos
124: .get(getPostalUserInfoKey(
125: PostalInfo.POSTALCODE, isBusiness)),
126: (String) infos.get(getPostalUserInfoKey(
127: PostalInfo.COUNTRY, isBusiness)),
128: (String) infos.get(getPostalUserInfoKey(
129: PostalInfo.ORGANIZATION, isBusiness)), null);
130:
131: Telecom telecom = new Telecom(createTelephoneNumFrom(infos,
132: TelecomType.TELEPHONE, isBusiness),
133: createTelephoneNumFrom(infos, TelecomType.FAX,
134: isBusiness), createTelephoneNumFrom(infos,
135: TelecomType.MOBILE, isBusiness),
136: createTelephoneNumFrom(infos, TelecomType.PAGER,
137: isBusiness), null);
138:
139: return new Contact(postal, telecom, online, null);
140: }
141:
142: private static TelephoneNum createTelephoneNumFrom(Map infos,
143: TelecomType type, boolean isBusiness) {
144: return new TelephoneNum((String) infos.get(getTelecomInfoKey(
145: type, TelecomInfo.INTCODE, isBusiness)), (String) infos
146: .get(getTelecomInfoKey(type, TelecomInfo.LOCCODE,
147: isBusiness)), (String) infos
148: .get(getTelecomInfoKey(type, TelecomInfo.NUMBER,
149: isBusiness)), (String) infos
150: .get(getTelecomInfoKey(type, TelecomInfo.EXT,
151: isBusiness)), (String) infos
152: .get(getTelecomInfoKey(type, TelecomInfo.COMMENT,
153: isBusiness)), null);
154: }
155:
156: /**
157: * Builds a Portal {@link org.jboss.portal.portlet.spi.UserContext} from a WSRP {@link
158: * org.jboss.portal.wsrp.core.UserContext}.
159: *
160: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
161: * @since 2.4 (May 8, 2006)
162: */
163: static class WSRPMappedUserContext implements UserContext {
164: private Map infos;
165: private String[] desiredLocales;
166: private Locale locale;
167: private String id;
168:
169: public WSRPMappedUserContext(
170: org.jboss.portal.wsrp.core.UserContext userContext,
171: String[] desiredLocales, String preferredLocale) {
172: this .desiredLocales = desiredLocales;
173: this .locale = WSRPUtils.getLocale(preferredLocale);
174: if (userContext != null) {
175: UserProfile profile = userContext.getProfile();
176: if (profile != null) {
177: infos = new HashMap();
178: Calendar bdate = profile.getBdate();
179: if (bdate != null) {
180: infos.put(INFO_USER_BDATE, SimpleTypeBindings
181: .marshalDateTime(bdate));
182: }
183:
184: infos.put(INFO_USER_GENDER, profile.getGender());
185:
186: PersonName name = profile.getName();
187: if (name != null) {
188: infos.put(INFO_USER_NAME_FAMILY, name
189: .getFamily());
190: infos
191: .put(INFO_USER_NAME_GIVEN, name
192: .getGiven());
193: infos.put(INFO_USER_NAME_MIDDLE, name
194: .getMiddle());
195: infos.put(INFO_USER_NAME_NICKNAME, name
196: .getNickname());
197: infos.put(INFO_USER_NAME_PREFIX, name
198: .getPrefix());
199: infos.put(INFO_USER_NAME_SUFFIX, name
200: .getSuffix());
201: }
202:
203: populateContactInfo(profile.getBusinessInfo(), true);
204:
205: populateContactInfo(profile.getHomeInfo(), false);
206:
207: EmployerInfo employerInfo = profile
208: .getEmployerInfo();
209: if (employerInfo != null) {
210: infos.put(INFO_USER_DEPARTMENT, employerInfo
211: .getDepartment());
212: infos.put(INFO_USER_EMPLOYER, employerInfo
213: .getEmployer());
214: infos.put(INFO_USER_JOB_TITLE, employerInfo
215: .getJobtitle());
216: }
217: }
218:
219: String key = userContext.getUserContextKey();
220: if (key == null) {
221: throw new IllegalArgumentException(
222: "Missing required userContextKey in UserContext!");
223: }
224: id = key;
225: } else {
226: infos = Collections.EMPTY_MAP;
227: }
228: }
229:
230: public String getId() {
231: return id;
232: }
233:
234: public Map getInformations() {
235: return infos;
236: }
237:
238: private void populateContactInfo(Contact contact,
239: boolean isBusiness) {
240: if (contact != null) {
241: Online online = contact.getOnline();
242: if (online != null) {
243: infos.put(getOnlineUserInfoKey(OnlineInfo.EMAIL,
244: isBusiness), online.getEmail());
245: infos.put(getOnlineUserInfoKey(OnlineInfo.URI,
246: isBusiness), online.getUri());
247: }
248:
249: Postal postal = contact.getPostal();
250: if (postal != null) {
251: infos.put(getPostalUserInfoKey(PostalInfo.NAME,
252: isBusiness), postal.getName());
253: infos.put(getPostalUserInfoKey(PostalInfo.STREET,
254: isBusiness), postal.getStreet());
255: infos.put(getPostalUserInfoKey(PostalInfo.CITY,
256: isBusiness), postal.getCity());
257: infos.put(getPostalUserInfoKey(
258: PostalInfo.STATEPROV, isBusiness), postal
259: .getStateprov());
260: infos.put(getPostalUserInfoKey(
261: PostalInfo.POSTALCODE, isBusiness), postal
262: .getPostalcode());
263: infos.put(getPostalUserInfoKey(PostalInfo.COUNTRY,
264: isBusiness), postal.getCountry());
265: infos.put(getPostalUserInfoKey(
266: PostalInfo.ORGANIZATION, isBusiness),
267: postal.getOrganization());
268: }
269:
270: Telecom telecom = contact.getTelecom();
271: if (telecom != null) {
272: populateTelephoneInfo(telecom.getFax(),
273: TelecomType.FAX, isBusiness);
274: populateTelephoneInfo(telecom.getMobile(),
275: TelecomType.MOBILE, isBusiness);
276: populateTelephoneInfo(telecom.getPager(),
277: TelecomType.PAGER, isBusiness);
278: populateTelephoneInfo(telecom.getTelephone(),
279: TelecomType.TELEPHONE, isBusiness);
280: }
281: }
282: }
283:
284: private void populateTelephoneInfo(TelephoneNum telephoneNum,
285: TelecomType type, boolean isBusiness) {
286: if (telephoneNum != null) {
287: infos.put(getTelecomInfoKey(type, TelecomInfo.INTCODE,
288: isBusiness), telephoneNum.getIntcode());
289: infos.put(getTelecomInfoKey(type, TelecomInfo.LOCCODE,
290: isBusiness), telephoneNum.getLoccode());
291: infos.put(getTelecomInfoKey(type, TelecomInfo.NUMBER,
292: isBusiness), telephoneNum.getNumber());
293: infos.put(getTelecomInfoKey(type, TelecomInfo.EXT,
294: isBusiness), telephoneNum.getExt());
295: infos.put(getTelecomInfoKey(type, TelecomInfo.COMMENT,
296: isBusiness), telephoneNum.getComment());
297: }
298: }
299:
300: public Locale getLocale() {
301: return locale;
302: }
303:
304: public List getLocales() {
305: int length = desiredLocales.length;
306: if (length > 0) {
307: List locales = new ArrayList(length);
308: for (int i = 0; i < length; i++) {
309: Locale locale = WSRPUtils
310: .getLocale(desiredLocales[i]);
311: locales.add(locale);
312: }
313: return locales;
314: } else {
315: return Collections.EMPTY_LIST;
316: }
317: }
318: }
319: }
|