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.asset;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.model.Group;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    import com.liferay.portal.service.GroupLocalServiceUtil;
022    import com.liferay.portal.service.UserLocalServiceUtil;
023    import com.liferay.portal.service.permission.UserPermissionUtil;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portlet.asset.model.AssetRenderer;
026    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
027    
028    /**
029     * @author Michael C. Han
030     */
031    public class UserAssetRendererFactory extends BaseAssetRendererFactory<User> {
032    
033            public static final String TYPE = "user";
034    
035            public UserAssetRendererFactory() {
036                    setSearchable(true);
037                    setSelectable(false);
038            }
039    
040            @Override
041            public AssetRenderer<User> getAssetRenderer(long classPK, int type)
042                    throws PortalException {
043    
044                    User user = UserLocalServiceUtil.getUserById(classPK);
045    
046                    UserAssetRenderer userAssetRenderer = new UserAssetRenderer(user);
047    
048                    userAssetRenderer.setAssetRendererType(type);
049    
050                    return userAssetRenderer;
051            }
052    
053            @Override
054            public AssetRenderer<User> getAssetRenderer(long groupId, String urlTitle)
055                    throws PortalException {
056    
057                    Group group = GroupLocalServiceUtil.getGroup(groupId);
058    
059                    User user = UserLocalServiceUtil.getUserByScreenName(
060                            group.getCompanyId(), urlTitle);
061    
062                    return new UserAssetRenderer(user);
063            }
064    
065            @Override
066            public String getClassName() {
067                    return User.class.getName();
068            }
069    
070            @Override
071            public String getIconCssClass() {
072                    return "icon-user";
073            }
074    
075            @Override
076            public String getType() {
077                    return TYPE;
078            }
079    
080            @Override
081            public boolean hasPermission(
082                            PermissionChecker permissionChecker, long classPK, String actionId)
083                    throws Exception {
084    
085                    return UserPermissionUtil.contains(
086                            permissionChecker, classPK, actionId);
087            }
088    
089            @Override
090            protected String getIconPath(ThemeDisplay themeDisplay) {
091                    return themeDisplay.getPathThemeImages() + "/common/user_icon.png";
092            }
093    
094    }