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.text.ParseException;
025: import java.util.Map;
026:
027: import javax.portlet.ActionRequest;
028: import javax.portlet.ActionResponse;
029: import javax.portlet.PortletException;
030: import javax.servlet.http.HttpServletRequest;
031:
032: import com.nabhinc.portal.spi.UserAdminServiceLocator;
033: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
034: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
035: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
036: import com.nabhinc.spi.EntityExistsException;
037: import com.nabhinc.spi.EntityUniqueException;
038: import com.nabhinc.spi.MissingRequiredAttributeException;
039: import com.nabhinc.spi.NoSuchEntityException;
040: import com.nabhinc.spi.User;
041: import com.nabhinc.spi.UserAdminService;
042: import com.nabhinc.util.StringUtil;
043: import com.nabhinc.util.i18n.DateTimeFormatUtil;
044:
045: /**
046: *
047: *
048: * @author Padmanabh Dabke
049: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
050: */
051: public class UserProfileUpdater extends BaseRequestProcessor implements
052: ActionProcessor {
053:
054: public String process(ActionRequest request,
055: ActionResponse response, ActionConfig actionConfig)
056: throws PortletException, IOException {
057: UserAdminService adminService = UserAdminServiceLocator
058: .getUserAdminService();
059: if (adminService == null) {
060: throw new PortletException(
061: "Cannot create user since user admin service is not running");
062: }
063: String userName = request.getRemoteUser();
064: if (userName == null) {
065: return "unlogged-user";
066: }
067:
068: try {
069:
070: User user = adminService.getUser(userName);
071: String fname = request.getParameter("fname");
072: String lname = request.getParameter("lname");
073: String email = request.getParameter("oemail");
074:
075: user.setFirstName(fname);
076: user.setLastName(lname);
077: user.setPrimaryEmail(email);
078: Map profile = user.getProfile();
079:
080: for (int i = 0; i < UserAdminService.STRING_USER_ATTRIBUTES.length; i++) {
081: String paramValue = request
082: .getParameter(UserAdminService.STRING_USER_ATTRIBUTES[i]);
083: if (StringUtil.isNotNullOrEmpty(paramValue))
084: profile.put(
085: UserAdminService.STRING_USER_ATTRIBUTES[i],
086: paramValue);
087: else
088: profile
089: .remove(UserAdminService.STRING_USER_ATTRIBUTES[i]);
090: }
091:
092: for (int i = 0; i < UserAdminService.INTEGER_USER_ATTRIBUTES.length; i++) {
093: String paramValue = request
094: .getParameter(UserAdminService.INTEGER_USER_ATTRIBUTES[i]);
095: if (StringUtil.isNotNullOrEmpty(paramValue))
096: profile
097: .put(
098: UserAdminService.INTEGER_USER_ATTRIBUTES[i],
099: Integer.valueOf(paramValue));
100: else
101: profile
102: .remove(UserAdminService.INTEGER_USER_ATTRIBUTES[i]);
103: }
104:
105: for (int i = 0; i < UserAdminService.DATE_USER_ATTRIBUTES.length; i++) {
106: String paramValue = request
107: .getParameter(UserAdminService.DATE_USER_ATTRIBUTES[i]);
108: if (StringUtil.isNotNullOrEmpty(paramValue)) {
109: profile.put(
110: UserAdminService.DATE_USER_ATTRIBUTES[i],
111: DateTimeFormatUtil.getDateFormat(
112: request.getLocale()).parse(
113: paramValue));
114: } else
115: profile
116: .remove(UserAdminService.DATE_USER_ATTRIBUTES[i]);
117: }
118:
119: adminService.updateUserProfile(user);
120: return "success";
121: } catch (RemoteException e) {
122: throw new PortletException(
123: "System exception in updating user information.", e);
124: } catch (EntityExistsException e) {
125: return "entity-exists";
126: } catch (MissingRequiredAttributeException e) {
127: return "missing-entity-attribute";
128: } catch (EntityUniqueException ex) {
129: return "unique-entity";
130: } catch (NoSuchEntityException ex) {
131: return "no-such-entity";
132: } catch (ParseException ex) {
133: return "invalid-date-format";
134: }
135: }
136:
137: @SuppressWarnings("unchecked")
138: private void setStringParam(String paramName,
139: HttpServletRequest request, Map profile) {
140: String paramValue = request.getParameter(paramName);
141: if (StringUtil.isNotNullOrEmpty(paramValue))
142: profile.put(paramName, paramValue);
143: }
144:
145: @SuppressWarnings("unchecked")
146: private void setIntegerParam(String paramName,
147: HttpServletRequest request, Map profile) {
148: String paramValue = request.getParameter(paramName);
149: if (StringUtil.isNotNullOrEmpty(paramValue))
150: profile.put(paramName, Integer.valueOf(paramValue));
151: else
152: profile.remove(paramName);
153: }
154:
155: }
|