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.portlet.LiferayPortletRequest;
018    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.service.permission.UserPermissionUtil;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.asset.model.BaseAssetRenderer;
027    
028    import java.util.Locale;
029    
030    import javax.portlet.PortletRequest;
031    import javax.portlet.PortletResponse;
032    import javax.portlet.PortletURL;
033    
034    /**
035     * @author Michael C. Han
036     * @author Sergio Gonz??lez
037     */
038    public class UserAssetRenderer extends BaseAssetRenderer {
039    
040            public UserAssetRenderer(User user) {
041                    _user = user;
042            }
043    
044            @Override
045            public String getClassName() {
046                    return User.class.getName();
047            }
048    
049            @Override
050            public long getClassPK() {
051                    return _user.getPrimaryKey();
052            }
053    
054            @Override
055            public String getDiscussionPath() {
056                    return null;
057            }
058    
059            @Override
060            public long getGroupId() {
061                    return 0;
062            }
063    
064            @Override
065            public String getSummary(
066                    PortletRequest portletRequest, PortletResponse portletResponse) {
067    
068                    return _user.getComments();
069            }
070    
071            @Override
072            public String getTitle(Locale locale) {
073                    return _user.getFullName();
074            }
075    
076            @Override
077            public PortletURL getURLEdit(
078                            LiferayPortletRequest liferayPortletRequest,
079                            LiferayPortletResponse liferayPortletResponse)
080                    throws Exception {
081    
082                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
083                            getControlPanelPlid(liferayPortletRequest), PortletKeys.USERS_ADMIN,
084                            PortletRequest.RENDER_PHASE);
085    
086                    portletURL.setParameter("struts_action", "/users_admin/edit_user");
087                    portletURL.setParameter("p_u_i_d", String.valueOf(_user.getUserId()));
088    
089                    return portletURL;
090            }
091    
092            @Override
093            public String getUrlTitle() {
094                    return _user.getScreenName();
095            }
096    
097            @Override
098            public String getURLViewInContext(
099                    LiferayPortletRequest liferayPortletRequest,
100                    LiferayPortletResponse liferayPortletResponse,
101                    String noSuchEntryRedirect) {
102    
103                    ThemeDisplay themeDisplay =
104                            (ThemeDisplay)liferayPortletRequest.getAttribute(
105                                    WebKeys.THEME_DISPLAY);
106    
107                    try {
108                            return _user.getDisplayURL(themeDisplay);
109                    }
110                    catch (Exception e) {
111                    }
112    
113                    return noSuchEntryRedirect;
114            }
115    
116            @Override
117            public long getUserId() {
118                    return _user.getUserId();
119            }
120    
121            @Override
122            public String getUserName() {
123                    return _user.getFullName();
124            }
125    
126            @Override
127            public String getUuid() {
128                    return _user.getUuid();
129            }
130    
131            @Override
132            public boolean hasEditPermission(PermissionChecker permissionChecker) {
133                    return UserPermissionUtil.contains(
134                            permissionChecker, _user.getUserId(), ActionKeys.UPDATE);
135            }
136    
137            @Override
138            public boolean hasViewPermission(PermissionChecker permissionChecker) {
139                    return UserPermissionUtil.contains(
140                            permissionChecker, _user.getUserId(), ActionKeys.VIEW);
141            }
142    
143            @Override
144            public boolean isPrintable() {
145                    return false;
146            }
147    
148            @Override
149            public String render(
150                            PortletRequest portletRequest, PortletResponse portletResponse,
151                            String template)
152                    throws Exception {
153    
154                    if (template.equals(TEMPLATE_ABSTRACT) ||
155                            template.equals(TEMPLATE_FULL_CONTENT)) {
156    
157                            portletRequest.setAttribute(WebKeys.USER, _user);
158    
159                            return "/html/portlet/directory/asset/abstract.jsp";
160                    }
161                    else {
162                            return null;
163                    }
164            }
165    
166            @Override
167            protected String getIconPath(ThemeDisplay themeDisplay) {
168                    return themeDisplay.getPathThemeImages() + "/common/user_icon.png";
169            }
170    
171            private final User _user;
172    
173    }