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 UserCreator 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, gender, 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.setDisabled(request.getParameter("disabled") != null);
084: user.setFirstName(request.getParameter("fname"));
085: user.setLastName(request.getParameter("lname"));
086: user.setPrimaryEmail(request.getParameter("oemail"));
087: user.setUserName(request.getParameter("username"));
088:
089: HashMap attrMap = new HashMap();
090: for (int i = 0; i < ucUserAttributes.length; i++) {
091: String attrValue = request
092: .getParameter(ucUserAttributes[i]);
093: if (attrValue != null && attrValue.trim().length() > 0) {
094: attrMap.put(ucUserAttributes[i], attrValue);
095: }
096: }
097: // Check if the birthdate is specified
098: String bdateStr = request.getParameter("bdate");
099: if (bdateStr != null) {
100: try {
101: Date bDate = DateTimeFormatUtil.getDateFormat(
102: request.getLocale()).parse(bdateStr);
103: attrMap.put("bdate", bDate);
104: } catch (ParseException e) {
105: throw new PortletException("Invalid date format.");
106: }
107: }
108:
109: // Integer attributes
110: final String[] intAttr = new String[] { "gender" };
111: for (int i = 0; i < intAttr.length; i++) {
112: String intValue = request.getParameter(intAttr[i]);
113: if (intValue != null) {
114: attrMap.put(intAttr[i], Integer.getInteger(intValue));
115: }
116: }
117:
118: user.setProfile(attrMap);
119:
120: String[] roles = request.getParameterValues("roles");
121: int[] roleIDs = new int[0];
122: if (roles != null) {
123: roleIDs = new int[roles.length];
124: for (int i = 0; i < roleIDs.length; i++) {
125: roleIDs[i] = Integer.parseInt(roles[i]);
126: }
127: }
128: try {
129: adminService.createUser(user, request
130: .getParameter("password"), roleIDs);
131: createUserPreferences(user.getUserName(), request);
132: return "success";
133: } catch (RemoteException e) {
134: throw new PortletException(
135: "System exception in creating new user.", e);
136: } catch (EntityExistsException e) {
137: return "entity-exists";
138: } catch (MissingRequiredAttributeException e) {
139: return "missing-entity-attribute";
140: } catch (EntityUniqueException ex) {
141: return "unique-entity";
142: }
143: }
144:
145: private void createUserPreferences(String userName,
146: ActionRequest request) throws RemoteException {
147: boolean showEmail = request.getParameter("showemail") != null;
148: boolean showName = request.getParameter("showname") != null;
149:
150: PortalInformationStore storeInfo = PortalInformationStoreLocator
151: .getPortalInformationStore();
152: UserPreferences userPrefs = new UserPreferences();
153: userPrefs.setName(userName);
154: userPrefs.setShowEmail(showEmail);
155: userPrefs.setShowName(showName);
156: userPrefs.setTimezone(null);
157: userPrefs.setPreferredLocale(null);
158: storeInfo.saveUserPreferences(userPrefs);
159:
160: }
161:
162: }
|