001: /*
002: * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portlet.useradmin;
021:
022: import java.io.IOException;
023: import java.rmi.RemoteException;
024: import java.sql.Timestamp;
025: import java.util.Date;
026: import java.util.Iterator;
027: import java.util.Map;
028:
029: import javax.portlet.ActionRequest;
030: import javax.portlet.ActionResponse;
031: import javax.portlet.PortletException;
032:
033: import org.w3c.dom.Element;
034:
035: import com.nabhinc.portal.api.PortalInformationStore;
036: import com.nabhinc.portal.api.PortalInformationStoreLocator;
037: import com.nabhinc.portal.model.UserPreferences;
038: import com.nabhinc.portal.spi.RoleAdminServiceLocator;
039: import com.nabhinc.portal.spi.UserAdminServiceLocator;
040: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
041: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
042: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
043: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
044: import com.nabhinc.spi.NoSuchEntityException;
045: import com.nabhinc.spi.User;
046: import com.nabhinc.spi.UserAdminService;
047: import com.nabhinc.util.StringUtil;
048: import com.nabhinc.util.i18n.DateTimeFormatUtil;
049:
050: /**
051: *
052: *
053: * @author Padmanabh Dabke
054: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
055: */
056: public class UserInitializer extends BaseRequestProcessor implements
057: ActionProcessor {
058: private String[] ucUserAttributes = new String[0];
059:
060: /**
061: * Caches controller portlet config
062: */
063: public void init(Element arg0, ControllerPortletConfig config)
064: throws PortletException {
065: super .init(arg0, config);
066: String attrStr = config.getParameter("userAttributes");
067: if (attrStr == null)
068: attrStr = "utitle, mname, suffix, address1, address2, city, state, country, zipcode, ophone, hphone, cphone, hemail, ofax, hfax, pager, website, gender, bdate, sig, aim, yim, msnm, icq, showemail, showname";
069: ucUserAttributes = StringUtil.split(attrStr, ",");
070:
071: }
072:
073: public String process(ActionRequest request,
074: ActionResponse response, ActionConfig actionConfig)
075: throws PortletException, IOException {
076: UserAdminService adminService = UserAdminServiceLocator
077: .getUserAdminService();
078: if (adminService == null) {
079: throw new PortletException(
080: "Cannot create user since user admin service is not running");
081: }
082: User user;
083: int userID = Integer.parseInt(request.getParameter("userid"));
084: try {
085: user = adminService.getUser(userID);
086: } catch (NumberFormatException e) {
087: throw new IllegalArgumentException("Invalid user ID: "
088: + request.getParameter("userid"));
089: } catch (RemoteException e) {
090: throw new PortletException(
091: "Failed to retrieve user information.", e);
092: } catch (NoSuchEntityException e) {
093: throw new PortletException("Invalid user id.", e);
094: }
095: if (user.isDisabled())
096: response.setRenderParameter("disabled", "true");
097: response.setRenderParameter("fname", user.getFirstName());
098: response.setRenderParameter("lname", user.getLastName());
099: response.setRenderParameter("oemail", user.getPrimaryEmail());
100: response.setRenderParameter("username", user.getUserName());
101:
102: Iterator iter = user.getProfile().entrySet().iterator();
103: while (iter.hasNext()) {
104: Map.Entry entry = (Map.Entry) iter.next();
105: String paramName = (String) entry.getKey();
106: Object paramValueObj = entry.getValue();
107: String paramValue = null;
108: if (paramValueObj instanceof Timestamp) {
109: DateTimeFormatUtil.getDateTimeFormat(
110: request.getLocale()).format(
111: new java.util.Date(((Timestamp) paramValueObj)
112: .getTime()));
113: } else if (paramValueObj instanceof Date) {
114: paramValue = DateTimeFormatUtil.getDateFormat(
115: request.getLocale()).format(
116: (Date) paramValueObj);
117: } else {
118: paramValue = paramValueObj.toString();
119: }
120: response.setRenderParameter(paramName, paramValue);
121: }
122:
123: PortalInformationStore storeInfo = PortalInformationStoreLocator
124: .getPortalInformationStore();
125: UserPreferences userPref = storeInfo.getUserPreferences(user
126: .getUserName());
127: if (userPref != null) {
128: if (userPref.isShowEmail())
129: response.setRenderParameter("showemail", "true");
130: if (userPref.isShowName())
131: response.setRenderParameter("showname", "true");
132: }
133: // Retrieve user roles
134: try {
135: int[] roleIDs = RoleAdminServiceLocator
136: .getRoleAdminService().getUserRoleIDs(userID);
137: if (roleIDs != null && roleIDs.length != 0) {
138: String[] roleIDStr = new String[roleIDs.length];
139: for (int i = 0; i < roleIDStr.length; i++) {
140: roleIDStr[i] = Integer.toString(roleIDs[i]);
141: }
142: response.setRenderParameter("roles", roleIDStr);
143: }
144: } catch (RemoteException e) {
145: throw new PortletException(
146: "Failed to retrieve user role information.", e);
147: }
148: return "success";
149: }
150: }
|