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            @Override
039            public String getClassName() {
040                    return User.class.getName();
041            }
042    
043            @Override
044            public String getType(Locale locale) {
045                    return ResourceActionsUtil.getModelResource(locale, getClassName());
046            }
047    
048            @Override
049            public boolean isScopeable() {
050                    return false;
051            }
052    
053            @Override
054            public Object updateStatus(
055                            int status, Map<String, Serializable> workflowContext)
056                    throws PortalException, SystemException {
057    
058                    long userId = GetterUtil.getLong(
059                            (String)workflowContext.get(
060                                    WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
061    
062                    User user = UserLocalServiceUtil.getUser(userId);
063    
064                    ServiceContext serviceContext = (ServiceContext)workflowContext.get(
065                            WorkflowConstants.CONTEXT_SERVICE_CONTEXT);
066    
067                    if (((user.getStatus() == WorkflowConstants.STATUS_DRAFT) ||
068                             (user.getStatus() == WorkflowConstants.STATUS_PENDING)) &&
069                            (status == WorkflowConstants.STATUS_APPROVED)) {
070    
071                            UserLocalServiceUtil.completeUserRegistration(user, serviceContext);
072                    }
073    
074                    return UserLocalServiceUtil.updateStatus(
075                            userId, status, serviceContext);
076            }
077    
078            @Override
079            protected String getIconPath(ThemeDisplay themeDisplay) {
080                    return themeDisplay.getPathThemeImages() + "/common/user_icon.png";
081            }
082    
083    }