001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            @Override
041            public String getClassName() {
042                    return CLASS_NAME;
043            }
044    
045            @Override
046            public String getType(Locale locale) {
047                    return ResourceActionsUtil.getModelResource(locale, CLASS_NAME);
048            }
049    
050            @Override
051            public boolean isScopeable() {
052                    return false;
053            }
054    
055            @Override
056            public Object updateStatus(
057                            int status, Map<String, Serializable> workflowContext)
058                    throws PortalException, SystemException {
059    
060                    long userId = GetterUtil.getLong(
061                            (String)workflowContext.get(
062                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
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                            ServiceContext serviceContext = (ServiceContext)workflowContext.get(
071                                    WorkflowConstants.CONTEXT_SERVICE_CONTEXT);
072    
073                            UserLocalServiceUtil.completeUserRegistration(user, serviceContext);
074    
075                            serviceContext.setAttribute(
076                                    "passwordUnencrypted", user.getPasswordUnencrypted());
077                    }
078    
079                    return UserLocalServiceUtil.updateStatus(userId, status);
080            }
081    
082            @Override
083            protected String getIconPath(ThemeDisplay themeDisplay) {
084                    return themeDisplay.getPathThemeImages() + "/common/user_icon.png";
085            }
086    
087    }