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.core.identity.ui.common;
023:
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.ResourceBundle;
031: import java.util.Set;
032:
033: import javax.faces.context.FacesContext;
034:
035: import org.jboss.logging.Logger;
036: import org.jboss.portal.core.identity.services.IdentityConstants;
037: import org.jboss.portal.core.identity.services.metadata.UIComponentConfiguration;
038: import org.jboss.portal.core.identity.ui.IdentityUIUser;
039: import org.jboss.portal.identity.IdentityException;
040: import org.jboss.portal.identity.NoSuchUserException;
041: import org.jboss.portal.identity.User;
042: import org.jboss.portal.identity.UserModule;
043: import org.jboss.portal.identity.UserProfileModule;
044:
045: /**
046: * @author <a href="mailto:emuckenh@redhat.com">Emanuel Muckenhuber</a>
047: * @version $Revision$
048: */
049: public class IdentityUserBean {
050:
051: /** The identity user module */
052: private UserModule userModule;
053:
054: /** The user profile module */
055: private UserProfileModule userProfileModule;
056:
057: /** The core-identity meta data service */
058: private MetaDataServiceBean metaDataService;
059:
060: /** the logger */
061: private static final Logger log = Logger
062: .getLogger(IdentityUserBean.class);
063:
064: public UserModule getUserModule() {
065: return userModule;
066: }
067:
068: public void setUserModule(UserModule userModule) {
069: this .userModule = userModule;
070: }
071:
072: public UserProfileModule getUserProfileModule() {
073: return userProfileModule;
074: }
075:
076: public void setUserProfileModule(UserProfileModule userProfileModule) {
077: this .userProfileModule = userProfileModule;
078: }
079:
080: public MetaDataServiceBean getMetaDataService() {
081: return metaDataService;
082: }
083:
084: public void setMetaDataService(MetaDataServiceBean metaDataService) {
085: this .metaDataService = metaDataService;
086: }
087:
088: /*
089: * converts the dynamic attribute Map to a map concerning the identity service - UserProfileModule
090: */
091: public Map getProfileMap(Map attributeMap) {
092: Map profileMap = new HashMap();
093: Set attributeSet = attributeMap.keySet();
094: Iterator i = attributeSet.iterator();
095:
096: while (i.hasNext()) {
097: String key = (String) i.next();
098: Object value = attributeMap.get(key);
099: UIComponentConfiguration uiComponent = (UIComponentConfiguration) this .metaDataService
100: .getValue(key).getObject();
101: if (uiComponent != null) {
102: profileMap.put(uiComponent.getPropertyRef(), value);
103: }
104: }
105: return profileMap;
106: }
107:
108: public void updateProfile(User user, Map attributeMap) {
109: Map profileMap = this .getProfileMap(attributeMap);
110: Iterator i = profileMap.keySet().iterator();
111:
112: while (i.hasNext()) {
113: String key = (String) i.next();
114: Object value = profileMap.get(key);
115: try {
116: this .userProfileModule.setProperty(user, key, value);
117: } catch (Exception e) {
118: log.error("updateProfile failed", e);
119: }
120: }
121: }
122:
123: public User findUserByUserName(String username)
124: throws IllegalArgumentException, NoSuchUserException,
125: IdentityException {
126: return userModule.findUserByUserName(username);
127: }
128:
129: public List findUsersFilteredByUserName(String filter, int offset,
130: int limit) throws IllegalArgumentException,
131: IdentityException {
132: Set users = new HashSet();
133: List list = new ArrayList();
134:
135: users = userModule.findUsersFilteredByUserName(filter, offset,
136: limit);
137:
138: Iterator i = users.iterator();
139:
140: while (i.hasNext()) {
141: User u = (User) i.next();
142: list.add(new IdentityUIUser(u.getUserName()));
143: }
144: return list;
145: }
146:
147: public void updatePassword(String username, String password)
148: throws IllegalArgumentException, IdentityException,
149: IdentityException {
150: User user = userModule.findUserByUserName(username);
151: user.updatePassword(password);
152: }
153:
154: public Object getLocalizedValue(String propertyName, Object value) {
155: UIComponentConfiguration uiComponent = (UIComponentConfiguration) this .metaDataService
156: .getValue(propertyName).getObject();
157: if (uiComponent.getValues() != null && value instanceof String
158: && uiComponent.getValues().size() > 0) {
159: try {
160: ResourceBundle bundle = ResourceBundle.getBundle(
161: "conf.bundles.Identity", FacesContext
162: .getCurrentInstance().getViewRoot()
163: .getLocale());
164: return bundle
165: .getString(IdentityConstants.DYNAMIC_VALUE_PREFIX
166: + ((String) value).toUpperCase());
167: } catch (Exception e) {
168: return value;
169: }
170: }
171: return value;
172: }
173:
174: public Class getPropertyType(String propertyName)
175: throws IdentityException, ClassNotFoundException {
176: UIComponentConfiguration uiComponent = (UIComponentConfiguration) this .metaDataService
177: .getValue(propertyName).getObject();
178: return uiComponent.getPropertyClass();
179: }
180:
181: public Object getUserProperty(String username, String propertyName)
182: throws IllegalArgumentException, NoSuchUserException,
183: IdentityException {
184: User user = this .findUserByUserName(username);
185: UIComponentConfiguration uiComponent = (UIComponentConfiguration) this.metaDataService
186: .getValue(propertyName).getObject();
187: return this.userProfileModule.getProperty(user, uiComponent
188: .getPropertyRef());
189: }
190: }
|