001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.directory.workflow;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.workflow.BaseWorkflowHandler;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.security.permission.ResourceActionsUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.UserLocalServiceUtil;
026    import com.liferay.portal.theme.ThemeDisplay;
027    
028    import java.io.Serializable;
029    
030    import java.util.Locale;
031    import java.util.Map;
032    
033    /**
034     * @author Michael C. Han
035     */
036    public class UserWorkflowHandler extends BaseWorkflowHandler {
037    
038            public static final String CLASS_NAME = User.class.getName();
039    
040            public String getClassName() {
041                    return CLASS_NAME;
042            }
043    
044            public String getType(Locale locale) {
045                    return ResourceActionsUtil.getModelResource(locale, CLASS_NAME);
046            }
047    
048            @Override
049            public boolean isScopeable() {
050                    return false;
051            }
052    
053            public Object updateStatus(
054                            int status, Map<String, Serializable> workflowContext)
055                    throws PortalException, SystemException {
056    
057                    long userId = GetterUtil.getLong(
058                            (String)workflowContext.get(
059                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
060    
061                    ServiceContext serviceContext = (ServiceContext)workflowContext.get(
062                            WorkflowConstants.CONTEXT_SERVICE_CONTEXT);
063    
064                    User user = UserLocalServiceUtil.getUser(userId);
065    
066                    if (((user.getStatus() == WorkflowConstants.STATUS_DRAFT) ||
067                             (user.getStatus() == WorkflowConstants.STATUS_PENDING)) &&
068                            (status == WorkflowConstants.STATUS_APPROVED)) {
069    
070                            UserLocalServiceUtil.completeUserRegistration(user, serviceContext);
071    
072                            serviceContext.setAttribute(
073                                    "passwordUnencrypted", user.getPasswordUnencrypted());
074                    }
075    
076                    return UserLocalServiceUtil.updateStatus(userId, status);
077            }
078    
079            @Override
080            protected String getIconPath(ThemeDisplay themeDisplay) {
081                    return themeDisplay.getPathThemeImages() + "/common/user_icon.png";
082            }
083    
084    }