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.Date;
026: import java.util.HashMap;
027:
028: import javax.portlet.ActionRequest;
029: import javax.portlet.ActionResponse;
030: import javax.portlet.PortletException;
031:
032: import org.w3c.dom.Element;
033:
034: import com.nabhinc.portal.api.PortalInformationStore;
035: import com.nabhinc.portal.api.PortalInformationStoreLocator;
036: import com.nabhinc.portal.model.UserPreferences;
037: import com.nabhinc.portal.spi.UserAdminServiceLocator;
038: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
039: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
040: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
041: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
042: import com.nabhinc.spi.EntityExistsException;
043: import com.nabhinc.spi.EntityUniqueException;
044: import com.nabhinc.spi.MissingRequiredAttributeException;
045: import com.nabhinc.spi.User;
046: import com.nabhinc.spi.UserAdminService;
047: import com.nabhinc.spi.UserImpl;
048: import com.nabhinc.util.StringUtil;
049: import com.nabhinc.util.i18n.DateTimeFormatUtil;
050:
051: /**
052: *
053: *
054: * @author Padmanabh Dabke
055: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
056: */
057: public class UserUpdater extends BaseRequestProcessor implements
058: ActionProcessor {
059: private String[] ucUserAttributes = new String[0];
060:
061: /**
062: * Caches controller portlet config
063: */
064: public void init(Element arg0, ControllerPortletConfig config)
065: throws PortletException {
066: super .init(arg0, config);
067: String attrStr = config.getParameter("userAttributes");
068: if (attrStr == null)
069: attrStr = "utitle, mname, suffix, address1, address2, city, state, country, zipcode, ophone, hphone, cphone, hemail, ofax, hfax, pager, website, sig, aim, yim, msnm, icq";
070: ucUserAttributes = StringUtil.split(attrStr, ",");
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 = new UserImpl();
083: user.setId(Integer.parseInt(request.getParameter("userid")));
084: user.setDisabled(request.getParameter("disabled") != null);
085: user.setFirstName(request.getParameter("fname"));
086: user.setLastName(request.getParameter("lname"));
087: user.setPrimaryEmail(request.getParameter("oemail"));
088: user.setUserName(request.getParameter("username"));
089:
090: HashMap attrMap = new HashMap();
091: for (int i = 0; i < ucUserAttributes.length; i++) {
092: String attrValue = request
093: .getParameter(ucUserAttributes[i]);
094: if (attrValue != null) {
095: attrMap.put(ucUserAttributes[i], attrValue);
096: }
097: }
098:
099: // Check if the birthdate is specified
100: String bdateStr = request.getParameter("bdate");
101: if (bdateStr != null && bdateStr.trim().length() > 0) {
102: try {
103: Date bDate = DateTimeFormatUtil.getDateFormat(
104: request.getLocale()).parse(bdateStr);
105: attrMap.put("bdate", bDate);
106: } catch (ParseException e) {
107: throw new PortletException("Invalid date format.");
108: }
109: }
110:
111: // Boolean attributes
112: /*
113: final String[] boolAttr = new String[] { "showname", "showemail" };
114: for (int i=0; i<boolAttr.length; i++) {
115: if (request.getParameter(boolAttr[i]) != null) {
116: attrMap.put(boolAttr[i], Boolean.TRUE);
117: }
118: }
119: */
120:
121: // Integer attributes
122: final String[] intAttr = new String[] { "gender" };
123: for (int i = 0; i < intAttr.length; i++) {
124: String intValue = request.getParameter(intAttr[i]);
125: if (intValue != null && intValue.trim().length() > 0) {
126: attrMap.put(intAttr[i], new Integer(intValue));
127: }
128: }
129:
130: user.setProfile(attrMap);
131:
132: String[] roles = request.getParameterValues("roles");
133: int[] roleIDs = new int[0];
134: if (roles != null) {
135: roleIDs = new int[roles.length];
136: for (int i = 0; i < roleIDs.length; i++) {
137: roleIDs[i] = Integer.parseInt(roles[i]);
138: }
139: }
140: try {
141: adminService.updateUser(user, roleIDs);
142: updateUserPreferences(user.getUserName(), request);
143: return "success";
144: } catch (RemoteException e) {
145: throw new PortletException(
146: "System exception in updating user information.", e);
147: } catch (EntityExistsException e) {
148: return "entity-exists";
149: } catch (MissingRequiredAttributeException e) {
150: return "missing-entity-attribute";
151: } catch (EntityUniqueException ex) {
152: return "unique-entity";
153: }
154: }
155:
156: private void updateUserPreferences(String userName,
157: ActionRequest request) throws RemoteException {
158: boolean showEmail = request.getParameter("showemail") != null;
159: boolean showName = request.getParameter("showname") != null;
160:
161: PortalInformationStore storeInfo = PortalInformationStoreLocator
162: .getPortalInformationStore();
163: UserPreferences userPrefs = storeInfo
164: .getUserPreferences(userName);
165: if (userPrefs == null)
166: userPrefs = new UserPreferences();
167: userPrefs.setName(userName);
168: userPrefs.setShowEmail(showEmail);
169: userPrefs.setShowName(showName);
170: storeInfo.saveUserPreferences(userPrefs);
171:
172: }
173:
174: }
|