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