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