001    /**
002     * Copyright (c) 2000-present 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.util.GetterUtil;
019    import com.liferay.portal.kernel.workflow.BaseWorkflowHandler;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.security.permission.ResourceActionsUtil;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    
027    import java.io.Serializable;
028    
029    import java.util.Locale;
030    import java.util.Map;
031    
032    /**
033     * @author Michael C. Han
034     */
035    public class UserWorkflowHandler extends BaseWorkflowHandler<User> {
036    
037            @Override
038            public String getClassName() {
039                    return User.class.getName();
040            }
041    
042            @Override
043            public String getType(Locale locale) {
044                    return ResourceActionsUtil.getModelResource(locale, getClassName());
045            }
046    
047            @Override
048            public boolean isScopeable() {
049                    return false;
050            }
051    
052            @Override
053            public User updateStatus(
054                            int status, Map<String, Serializable> workflowContext)
055                    throws PortalException {
056    
057                    long userId = GetterUtil.getLong(
058                            (String)workflowContext.get(
059                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
060    
061                    User user = UserLocalServiceUtil.getUser(userId);
062    
063                    ServiceContext serviceContext = (ServiceContext)workflowContext.get(
064                            WorkflowConstants.CONTEXT_SERVICE_CONTEXT);
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    
073                    return UserLocalServiceUtil.updateStatus(
074                            userId, status, serviceContext);
075            }
076    
077            @Override
078            protected String getIconPath(ThemeDisplay themeDisplay) {
079                    return themeDisplay.getPathThemeImages() + "/common/user_icon.png";
080            }
081    
082    }